1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74

content / public / test / commit_message_delayer.cc [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/public/test/commit_message_delayer.h"

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "content/common/navigation_client.mojom.h"
#include "content/test/did_commit_navigation_interceptor.h"
#include "url/gurl.h"

namespace content {

class CommitMessageDelayer::Impl : public DidCommitNavigationInterceptor {
 public:
  Impl(WebContents* web_contents,
       const GURL& deferred_url,
       DidCommitCallback deferred_action)
      : DidCommitNavigationInterceptor(web_contents),
        deferred_url_(deferred_url),
        deferred_action_(std::move(deferred_action)) {}

  ~Impl() override = default;

  Impl(const Impl&) = delete;
  Impl& operator=(const Impl&) = delete;

  void Wait() {
    CHECK(deferred_action_)
        << "The deferred action was already run before calling Wait().";
    run_loop_ = std::make_unique<base::RunLoop>();
    run_loop_->Run();
    run_loop_.reset();
  }

 private:
  // DidCommitNavigationInterceptor override:
  bool WillProcessDidCommitNavigation(
      RenderFrameHost* render_frame_host,
      NavigationRequest* navigation_request,
      mojom::DidCommitProvisionalLoadParamsPtr* params,
      mojom::DidCommitProvisionalLoadInterfaceParamsPtr* interface_params)
      override {
    if ((**params).url == deferred_url_) {
      std::move(deferred_action_).Run(render_frame_host);
      if (run_loop_)
        run_loop_->Quit();
    }
    return true;
  }

  std::unique_ptr<base::RunLoop> run_loop_;

  const GURL deferred_url_;
  DidCommitCallback deferred_action_;
};

CommitMessageDelayer::CommitMessageDelayer(WebContents* web_contents,
                                           const GURL& deferred_url,
                                           DidCommitCallback deferred_action) {
  impl_ = std::make_unique<Impl>(web_contents, deferred_url,
                                 std::move(deferred_action));
}

CommitMessageDelayer::~CommitMessageDelayer() = default;

void CommitMessageDelayer::Wait() {
  impl_->Wait();
}

}  // namespace content