summaryrefslogtreecommitdiff
path: root/chromium/content/browser/web_contents_binding_set_browsertest.cc
blob: 20872b5f526a136df802568bb5af8f7d4a3e915b (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/bind.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/web_contents_binding_set.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_binding_set_test_binder.h"
#include "content/shell/browser/shell.h"
#include "content/test/test_browser_associated_interfaces.mojom.h"
#include "net/dns/mock_host_resolver.h"

namespace content {

namespace {

const char kTestHost1[] = "foo.com";
const char kTestHost2[] = "bar.com";

class WebContentsBindingSetBrowserTest : public ContentBrowserTest {
 public:
  void SetUpOnMainThread() override {
    host_resolver()->AddRule(kTestHost1, "127.0.0.1");
    host_resolver()->AddRule(kTestHost2, "127.0.0.1");
  }
};

class TestInterfaceBinder : public WebContentsBindingSetTestBinder<
                                mojom::BrowserAssociatedInterfaceTestDriver> {
 public:
  explicit TestInterfaceBinder(const base::Closure& bind_callback)
      : bind_callback_(bind_callback) {}
  ~TestInterfaceBinder() override {}

  void BindRequest(RenderFrameHost* frame_host,
                   mojom::BrowserAssociatedInterfaceTestDriverAssociatedRequest
                       request) override {
    bind_callback_.Run();
  }

 private:
  const base::Closure bind_callback_;

  DISALLOW_COPY_AND_ASSIGN(TestInterfaceBinder);
};

class TestFrameInterfaceBinder : public mojom::WebContentsFrameBindingSetTest {
 public:
  explicit TestFrameInterfaceBinder(WebContents* web_contents)
      : bindings_(web_contents, this) {}
  ~TestFrameInterfaceBinder() override {}

 private:
  // mojom::WebContentsFrameBindingSetTest:
  void Ping(PingCallback callback) override { NOTREACHED(); }

  WebContentsFrameBindingSet<mojom::WebContentsFrameBindingSetTest> bindings_;
};

}  // namespace

IN_PROC_BROWSER_TEST_F(WebContentsBindingSetBrowserTest, OverrideForTesting) {
  EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,ho hum")));

  // Ensure that we can add a WebContentsFrameBindingSet and then override its
  // request handler.
  auto* web_contents = shell()->web_contents();
  WebContentsFrameBindingSet<mojom::BrowserAssociatedInterfaceTestDriver>
      frame_bindings(web_contents, nullptr);

  // Now override the binder for this interface. It quits |run_loop| whenever
  // an incoming interface request is received.
  base::RunLoop run_loop;
  auto* binding_set = WebContentsBindingSet::GetForWebContents<
      mojom::BrowserAssociatedInterfaceTestDriver>(web_contents);

  TestInterfaceBinder test_binder(run_loop.QuitClosure());
  binding_set->SetBinder(&test_binder);

  // Simulate an inbound request for the test interface. This should get routed
  // to the overriding binder and allow the test to complete.
  mojom::BrowserAssociatedInterfaceTestDriverAssociatedPtr override_client;
  static_cast<WebContentsImpl*>(web_contents)
      ->OnAssociatedInterfaceRequest(
          web_contents->GetMainFrame(),
          mojom::BrowserAssociatedInterfaceTestDriver::Name_,
          mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
              .PassHandle());
  run_loop.Run();

  binding_set->SetBinder(nullptr);
}

IN_PROC_BROWSER_TEST_F(WebContentsBindingSetBrowserTest, CloseOnFrameDeletion) {
  EXPECT_TRUE(embedded_test_server()->Start());
  EXPECT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL(kTestHost1, "/hello.html")));

  // Simulate an inbound request on the navigated main frame.
  auto* web_contents = shell()->web_contents();
  TestFrameInterfaceBinder binder(web_contents);
  mojom::WebContentsFrameBindingSetTestAssociatedPtr override_client;
  static_cast<WebContentsImpl*>(web_contents)
      ->OnAssociatedInterfaceRequest(
          web_contents->GetMainFrame(),
          mojom::WebContentsFrameBindingSetTest::Name_,
          mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
              .PassHandle());

  base::RunLoop run_loop;
  override_client.set_connection_error_handler(run_loop.QuitClosure());

  // Now navigate the WebContents elsewhere, eventually tearing down the old
  // main frame.
  RenderFrameDeletedObserver deleted_observer(web_contents->GetMainFrame());
  EXPECT_TRUE(NavigateToURL(
      shell(), embedded_test_server()->GetURL(kTestHost2, "/title2.html")));
  deleted_observer.WaitUntilDeleted();

  // Verify that this message never reaches the binding for the old frame. If it
  // does, the impl will hit a DCHECK. The RunLoop terminates when the client is
  // disconnected.
  override_client->Ping(base::BindOnce([] {}));
  run_loop.Run();
}

}  // namespace content