blob: 50b293fdb345f10ae65325cf5a0ac74e315fb987 [file] [log] [blame]
rockot96c63e32017-03-20 23:23:031// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Sebastien Marchandf8cbfab2019-01-25 16:02:305#include "base/bind.h"
rockot96c63e32017-03-20 23:23:036#include "base/macros.h"
Gabriel Charette078e3662017-08-28 22:59:047#include "base/run_loop.h"
rockot96c63e32017-03-20 23:23:038#include "content/browser/web_contents/web_contents_impl.h"
9#include "content/public/browser/web_contents_binding_set.h"
10#include "content/public/test/browser_test_utils.h"
11#include "content/public/test/content_browser_test.h"
12#include "content/public/test/content_browser_test_utils.h"
Ken Rockot59f7dd72017-05-31 16:00:0013#include "content/public/test/test_utils.h"
rockot96c63e32017-03-20 23:23:0314#include "content/public/test/web_contents_binding_set_test_binder.h"
15#include "content/shell/browser/shell.h"
16#include "content/test/test_browser_associated_interfaces.mojom.h"
Ken Rockot59f7dd72017-05-31 16:00:0017#include "net/dns/mock_host_resolver.h"
rockot96c63e32017-03-20 23:23:0318
19namespace content {
20
rockot96c63e32017-03-20 23:23:0321namespace {
22
Ken Rockot59f7dd72017-05-31 16:00:0023const char kTestHost1[] = "foo.com";
24const char kTestHost2[] = "bar.com";
25
26class WebContentsBindingSetBrowserTest : public ContentBrowserTest {
27 public:
28 void SetUpOnMainThread() override {
29 host_resolver()->AddRule(kTestHost1, "127.0.0.1");
30 host_resolver()->AddRule(kTestHost2, "127.0.0.1");
31 }
32};
33
rockot96c63e32017-03-20 23:23:0334class TestInterfaceBinder : public WebContentsBindingSetTestBinder<
35 mojom::BrowserAssociatedInterfaceTestDriver> {
36 public:
37 explicit TestInterfaceBinder(const base::Closure& bind_callback)
38 : bind_callback_(bind_callback) {}
39 ~TestInterfaceBinder() override {}
40
41 void BindRequest(RenderFrameHost* frame_host,
42 mojom::BrowserAssociatedInterfaceTestDriverAssociatedRequest
43 request) override {
44 bind_callback_.Run();
45 }
46
47 private:
48 const base::Closure bind_callback_;
49
50 DISALLOW_COPY_AND_ASSIGN(TestInterfaceBinder);
51};
52
Ken Rockot59f7dd72017-05-31 16:00:0053class TestFrameInterfaceBinder : public mojom::WebContentsFrameBindingSetTest {
54 public:
55 explicit TestFrameInterfaceBinder(WebContents* web_contents)
56 : bindings_(web_contents, this) {}
57 ~TestFrameInterfaceBinder() override {}
58
59 private:
60 // mojom::WebContentsFrameBindingSetTest:
61 void Ping(PingCallback callback) override { NOTREACHED(); }
62
63 WebContentsFrameBindingSet<mojom::WebContentsFrameBindingSetTest> bindings_;
64};
65
rockot96c63e32017-03-20 23:23:0366} // namespace
67
68IN_PROC_BROWSER_TEST_F(WebContentsBindingSetBrowserTest, OverrideForTesting) {
69 NavigateToURL(shell(), GURL("data:text/html,ho hum"));
70
71 // Ensure that we can add a WebContentsFrameBindingSet and then override its
72 // request handler.
rockot8bbad222017-03-22 04:34:0573 auto* web_contents = shell()->web_contents();
rockot96c63e32017-03-20 23:23:0374 WebContentsFrameBindingSet<mojom::BrowserAssociatedInterfaceTestDriver>
75 frame_bindings(web_contents, nullptr);
76
77 // Now override the binder for this interface. It quits |run_loop| whenever
78 // an incoming interface request is received.
79 base::RunLoop run_loop;
rockot8bbad222017-03-22 04:34:0580 WebContentsBindingSet::GetForWebContents<
81 mojom::BrowserAssociatedInterfaceTestDriver>(web_contents)
82 ->SetBinderForTesting(
Jeremy Roman04f27c372017-10-27 15:20:5583 std::make_unique<TestInterfaceBinder>(run_loop.QuitClosure()));
rockot96c63e32017-03-20 23:23:0384
85 // Simulate an inbound request for the test interface. This should get routed
86 // to the overriding binder and allow the test to complete.
87 mojom::BrowserAssociatedInterfaceTestDriverAssociatedPtr override_client;
rockot8bbad222017-03-22 04:34:0588 static_cast<WebContentsImpl*>(web_contents)
89 ->OnAssociatedInterfaceRequest(
90 web_contents->GetMainFrame(),
91 mojom::BrowserAssociatedInterfaceTestDriver::Name_,
Balazs Engedyf0b497ed2017-11-04 00:50:2192 mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
93 .PassHandle());
rockot96c63e32017-03-20 23:23:0394 run_loop.Run();
95}
96
Ken Rockot59f7dd72017-05-31 16:00:0097IN_PROC_BROWSER_TEST_F(WebContentsBindingSetBrowserTest, CloseOnFrameDeletion) {
98 EXPECT_TRUE(embedded_test_server()->Start());
99 EXPECT_TRUE(NavigateToURL(
100 shell(), embedded_test_server()->GetURL(kTestHost1, "/hello.html")));
101
102 // Simulate an inbound request on the navigated main frame.
103 auto* web_contents = shell()->web_contents();
104 TestFrameInterfaceBinder binder(web_contents);
105 mojom::WebContentsFrameBindingSetTestAssociatedPtr override_client;
106 static_cast<WebContentsImpl*>(web_contents)
107 ->OnAssociatedInterfaceRequest(
108 web_contents->GetMainFrame(),
109 mojom::WebContentsFrameBindingSetTest::Name_,
Balazs Engedyf0b497ed2017-11-04 00:50:21110 mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
111 .PassHandle());
Ken Rockot59f7dd72017-05-31 16:00:00112
113 base::RunLoop run_loop;
114 override_client.set_connection_error_handler(run_loop.QuitClosure());
115
116 // Now navigate the WebContents elsewhere, eventually tearing down the old
117 // main frame.
118 RenderFrameDeletedObserver deleted_observer(web_contents->GetMainFrame());
119 EXPECT_TRUE(NavigateToURL(
120 shell(), embedded_test_server()->GetURL(kTestHost2, "/title2.html")));
121 deleted_observer.WaitUntilDeleted();
122
123 // Verify that this message never reaches the binding for the old frame. If it
124 // does, the impl will hit a DCHECK. The RunLoop terminates when the client is
125 // disconnected.
tzike2aca992017-09-05 08:50:54126 override_client->Ping(base::BindOnce([] {}));
Ken Rockot59f7dd72017-05-31 16:00:00127 run_loop.Run();
128}
129
rockot96c63e32017-03-20 23:23:03130} // namespace content