blob: 20872b5f526a136df802568bb5af8f7d4a3e915b [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) {
Alex Moshchukaeb20fe32019-09-25 17:40:0169 EXPECT_TRUE(NavigateToURL(shell(), GURL("data:text/html,ho hum")));
rockot96c63e32017-03-20 23:23:0370
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;
Ken Rockot3b799c62019-04-25 17:38:1080 auto* binding_set = WebContentsBindingSet::GetForWebContents<
81 mojom::BrowserAssociatedInterfaceTestDriver>(web_contents);
82
83 TestInterfaceBinder test_binder(run_loop.QuitClosure());
84 binding_set->SetBinder(&test_binder);
rockot96c63e32017-03-20 23:23:0385
86 // Simulate an inbound request for the test interface. This should get routed
87 // to the overriding binder and allow the test to complete.
88 mojom::BrowserAssociatedInterfaceTestDriverAssociatedPtr override_client;
rockot8bbad222017-03-22 04:34:0589 static_cast<WebContentsImpl*>(web_contents)
90 ->OnAssociatedInterfaceRequest(
91 web_contents->GetMainFrame(),
92 mojom::BrowserAssociatedInterfaceTestDriver::Name_,
Balazs Engedyf0b497ed2017-11-04 00:50:2193 mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
94 .PassHandle());
rockot96c63e32017-03-20 23:23:0395 run_loop.Run();
Ken Rockot3b799c62019-04-25 17:38:1096
97 binding_set->SetBinder(nullptr);
rockot96c63e32017-03-20 23:23:0398}
99
Ken Rockot59f7dd72017-05-31 16:00:00100IN_PROC_BROWSER_TEST_F(WebContentsBindingSetBrowserTest, CloseOnFrameDeletion) {
101 EXPECT_TRUE(embedded_test_server()->Start());
102 EXPECT_TRUE(NavigateToURL(
103 shell(), embedded_test_server()->GetURL(kTestHost1, "/hello.html")));
104
105 // Simulate an inbound request on the navigated main frame.
106 auto* web_contents = shell()->web_contents();
107 TestFrameInterfaceBinder binder(web_contents);
108 mojom::WebContentsFrameBindingSetTestAssociatedPtr override_client;
109 static_cast<WebContentsImpl*>(web_contents)
110 ->OnAssociatedInterfaceRequest(
111 web_contents->GetMainFrame(),
112 mojom::WebContentsFrameBindingSetTest::Name_,
Balazs Engedyf0b497ed2017-11-04 00:50:21113 mojo::MakeRequestAssociatedWithDedicatedPipe(&override_client)
114 .PassHandle());
Ken Rockot59f7dd72017-05-31 16:00:00115
116 base::RunLoop run_loop;
117 override_client.set_connection_error_handler(run_loop.QuitClosure());
118
119 // Now navigate the WebContents elsewhere, eventually tearing down the old
120 // main frame.
121 RenderFrameDeletedObserver deleted_observer(web_contents->GetMainFrame());
122 EXPECT_TRUE(NavigateToURL(
123 shell(), embedded_test_server()->GetURL(kTestHost2, "/title2.html")));
124 deleted_observer.WaitUntilDeleted();
125
126 // Verify that this message never reaches the binding for the old frame. If it
127 // does, the impl will hit a DCHECK. The RunLoop terminates when the client is
128 // disconnected.
tzike2aca992017-09-05 08:50:54129 override_client->Ping(base::BindOnce([] {}));
Ken Rockot59f7dd72017-05-31 16:00:00130 run_loop.Run();
131}
132
rockot96c63e32017-03-20 23:23:03133} // namespace content