OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <limits> | 5 #include <limits> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
11 #include "base/macros.h" | 10 #include "base/macros.h" |
12 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
13 #include "base/path_service.h" | 12 #include "base/path_service.h" |
14 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
15 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
16 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
17 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 16 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
18 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
(...skipping 12 matching lines...) Expand all Loading... |
31 #include "content/shell/browser/shell.h" | 30 #include "content/shell/browser/shell.h" |
32 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h" | 31 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h" |
33 #include "mojo/public/cpp/bindings/binding.h" | 32 #include "mojo/public/cpp/bindings/binding.h" |
34 #include "mojo/public/cpp/bindings/interface_request.h" | 33 #include "mojo/public/cpp/bindings/interface_request.h" |
35 #include "net/test/embedded_test_server/embedded_test_server.h" | 34 #include "net/test/embedded_test_server/embedded_test_server.h" |
36 #include "services/shell/public/cpp/interface_registry.h" | 35 #include "services/shell/public/cpp/interface_registry.h" |
37 | 36 |
38 namespace content { | 37 namespace content { |
39 namespace { | 38 namespace { |
40 | 39 |
| 40 bool g_got_message = false; |
| 41 |
41 base::FilePath GetFilePathForJSResource(const std::string& path) { | 42 base::FilePath GetFilePathForJSResource(const std::string& path) { |
42 base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; | 43 base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; |
43 | 44 |
44 std::string binding_path = "gen/" + path + ".js"; | 45 std::string binding_path = "gen/" + path + ".js"; |
45 #if defined(OS_WIN) | 46 #if defined(OS_WIN) |
46 base::ReplaceChars(binding_path, "//", "\\", &binding_path); | 47 base::ReplaceChars(binding_path, "//", "\\", &binding_path); |
47 #endif | 48 #endif |
48 base::FilePath exe_dir; | 49 base::FilePath exe_dir; |
49 PathService::Get(base::DIR_EXE, &exe_dir); | 50 PathService::Get(base::DIR_EXE, &exe_dir); |
50 return exe_dir.AppendASCII(binding_path); | 51 return exe_dir.AppendASCII(binding_path); |
51 } | 52 } |
52 | 53 |
53 bool got_message = false; | |
54 | |
55 // The bindings for the page are generated from a .mojom file. This code looks | 54 // The bindings for the page are generated from a .mojom file. This code looks |
56 // up the generated file from disk and returns it. | 55 // up the generated file from disk and returns it. |
57 bool GetResource(const std::string& id, | 56 bool GetResource(const std::string& id, |
58 const WebUIDataSource::GotDataCallback& callback) { | 57 const WebUIDataSource::GotDataCallback& callback) { |
59 base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; | 58 base::ThreadRestrictions::ScopedAllowIO allow_io_from_test_callbacks; |
60 | 59 |
61 if (id.find(".mojom") != std::string::npos) { | 60 std::string contents; |
62 std::string contents; | 61 if (base::EndsWith(id, ".mojom", base::CompareCase::SENSITIVE)) { |
63 CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents)) | 62 CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents)) |
64 << id; | 63 << id; |
65 base::RefCountedString* ref_contents = new base::RefCountedString; | 64 } else { |
66 ref_contents->data() = contents; | 65 base::FilePath path; |
67 callback.Run(ref_contents); | 66 CHECK(base::PathService::Get(content::DIR_TEST_DATA, &path)); |
68 return true; | 67 path = path.AppendASCII(id.substr(0, id.find("?"))); |
| 68 CHECK(base::ReadFileToString(path, &contents)) << path.value(); |
69 } | 69 } |
70 | 70 |
71 base::FilePath path; | |
72 CHECK(base::PathService::Get(content::DIR_TEST_DATA, &path)); | |
73 path = path.AppendASCII(id.substr(0, id.find("?"))); | |
74 std::string contents; | |
75 CHECK(base::ReadFileToString(path, &contents)) << path.value(); | |
76 base::RefCountedString* ref_contents = new base::RefCountedString; | 71 base::RefCountedString* ref_contents = new base::RefCountedString; |
77 ref_contents->data() = contents; | 72 ref_contents->data() = contents; |
78 callback.Run(ref_contents); | 73 callback.Run(ref_contents); |
79 return true; | 74 return true; |
80 } | 75 } |
81 | 76 |
82 class BrowserTargetImpl : public mojom::BrowserTarget { | 77 class BrowserTargetImpl : public mojom::BrowserTarget { |
83 public: | 78 public: |
84 BrowserTargetImpl(base::RunLoop* run_loop, | 79 BrowserTargetImpl(base::RunLoop* run_loop, |
85 mojo::InterfaceRequest<mojom::BrowserTarget> request) | 80 mojo::InterfaceRequest<mojom::BrowserTarget> request) |
86 : run_loop_(run_loop), binding_(this, std::move(request)) {} | 81 : run_loop_(run_loop), binding_(this, std::move(request)) {} |
87 | 82 |
88 ~BrowserTargetImpl() override {} | 83 ~BrowserTargetImpl() override {} |
89 | 84 |
90 // mojom::BrowserTarget overrides: | 85 // mojom::BrowserTarget overrides: |
91 void Start(const StartCallback& closure) override { | 86 void Start(const StartCallback& closure) override { |
92 closure.Run(); | 87 closure.Run(); |
93 } | 88 } |
94 void Stop() override { | 89 void Stop() override { |
95 got_message = true; | 90 g_got_message = true; |
96 run_loop_->Quit(); | 91 run_loop_->Quit(); |
97 } | 92 } |
98 | 93 |
99 protected: | 94 protected: |
100 base::RunLoop* run_loop_; | 95 base::RunLoop* const run_loop_; |
101 | 96 |
102 private: | 97 private: |
103 mojo::Binding<mojom::BrowserTarget> binding_; | 98 mojo::Binding<mojom::BrowserTarget> binding_; |
104 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); | 99 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); |
105 }; | 100 }; |
106 | 101 |
107 // WebUIController that sets up mojo bindings. | 102 // WebUIController that sets up mojo bindings. |
108 class TestWebUIController : public WebUIController { | 103 class TestWebUIController : public WebUIController { |
109 public: | 104 public: |
110 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | 105 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
111 : WebUIController(web_ui), run_loop_(run_loop) { | 106 : WebUIController(web_ui), run_loop_(run_loop) { |
112 content::WebUIDataSource* data_source = | 107 WebUIDataSource* data_source = WebUIDataSource::Create("mojo-web-ui"); |
113 WebUIDataSource::Create("mojo-web-ui"); | |
114 data_source->SetRequestFilter(base::Bind(&GetResource)); | 108 data_source->SetRequestFilter(base::Bind(&GetResource)); |
115 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), | 109 WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), |
116 data_source); | 110 data_source); |
117 } | 111 } |
118 | 112 |
119 protected: | 113 protected: |
120 base::RunLoop* run_loop_; | 114 base::RunLoop* const run_loop_; |
121 std::unique_ptr<BrowserTargetImpl> browser_target_; | 115 std::unique_ptr<BrowserTargetImpl> browser_target_; |
122 | 116 |
123 private: | 117 private: |
124 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); | 118 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); |
125 }; | 119 }; |
126 | 120 |
127 // TestWebUIController that additionally creates the ping test BrowserTarget | 121 // TestWebUIController that additionally creates the ping test BrowserTarget |
128 // implementation at the right time. | 122 // implementation at the right time. |
129 class PingTestWebUIController : public TestWebUIController { | 123 class PingTestWebUIController : public TestWebUIController { |
130 public: | 124 public: |
131 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | 125 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
132 : TestWebUIController(web_ui, run_loop) { | 126 : TestWebUIController(web_ui, run_loop) {} |
133 } | 127 ~PingTestWebUIController() override {} |
134 ~PingTestWebUIController() override {} | |
135 | 128 |
136 // WebUIController overrides: | 129 // WebUIController overrides: |
137 void RenderViewCreated(RenderViewHost* render_view_host) override { | 130 void RenderViewCreated(RenderViewHost* render_view_host) override { |
138 render_view_host->GetMainFrame()->GetInterfaceRegistry()->AddInterface( | 131 render_view_host->GetMainFrame()->GetInterfaceRegistry()->AddInterface( |
139 base::Bind(&PingTestWebUIController::CreateHandler, | 132 base::Bind(&PingTestWebUIController::CreateHandler, |
140 base::Unretained(this))); | 133 base::Unretained(this))); |
141 } | 134 } |
142 | 135 |
143 void CreateHandler(mojo::InterfaceRequest<mojom::BrowserTarget> request) { | 136 void CreateHandler(mojo::InterfaceRequest<mojom::BrowserTarget> request) { |
144 browser_target_.reset(new BrowserTargetImpl(run_loop_, std::move(request))); | 137 browser_target_ = |
| 138 base::MakeUnique<BrowserTargetImpl>(run_loop_, std::move(request)); |
145 } | 139 } |
146 | 140 |
147 private: | 141 private: |
148 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); | 142 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); |
149 }; | 143 }; |
150 | 144 |
151 // WebUIControllerFactory that creates TestWebUIController. | 145 // WebUIControllerFactory that creates TestWebUIController. |
152 class TestWebUIControllerFactory : public WebUIControllerFactory { | 146 class TestWebUIControllerFactory : public WebUIControllerFactory { |
153 public: | 147 public: |
154 TestWebUIControllerFactory() : run_loop_(NULL) {} | 148 TestWebUIControllerFactory() : run_loop_(nullptr) {} |
155 | 149 |
156 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 150 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
157 | 151 |
158 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui, | 152 WebUIController* CreateWebUIControllerForURL(WebUI* web_ui, |
159 const GURL& url) const override { | 153 const GURL& url) const override { |
160 if (url.query() == "ping") | 154 if (url.query() == "ping") |
161 return new PingTestWebUIController(web_ui, run_loop_); | 155 return new PingTestWebUIController(web_ui, run_loop_); |
162 return new TestWebUIController(web_ui, run_loop_); | 156 return new TestWebUIController(web_ui, run_loop_); |
163 } | 157 } |
164 WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | 158 WebUI::TypeID GetWebUIType(BrowserContext* browser_context, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return false; | 205 return false; |
212 } | 206 } |
213 | 207 |
214 // Loads a webui page that contains mojo bindings and verifies a message makes | 208 // Loads a webui page that contains mojo bindings and verifies a message makes |
215 // it from the browser to the page and back. | 209 // it from the browser to the page and back. |
216 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { | 210 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { |
217 if (!IsGeneratedResourceAvailable( | 211 if (!IsGeneratedResourceAvailable( |
218 "content/test/data/web_ui_test_mojo_bindings.mojom")) | 212 "content/test/data/web_ui_test_mojo_bindings.mojom")) |
219 return; | 213 return; |
220 | 214 |
221 got_message = false; | 215 g_got_message = false; |
222 ASSERT_TRUE(embedded_test_server()->Start()); | 216 ASSERT_TRUE(embedded_test_server()->Start()); |
223 base::RunLoop run_loop; | 217 base::RunLoop run_loop; |
224 factory()->set_run_loop(&run_loop); | 218 factory()->set_run_loop(&run_loop); |
225 GURL test_url("chrome://mojo-web-ui/web_ui_mojo.html?ping"); | 219 GURL test_url("chrome://mojo-web-ui/web_ui_mojo.html?ping"); |
226 NavigateToURL(shell(), test_url); | 220 NavigateToURL(shell(), test_url); |
227 // RunLoop is quit when message received from page. | 221 // RunLoop is quit when message received from page. |
228 run_loop.Run(); | 222 run_loop.Run(); |
229 EXPECT_TRUE(got_message); | 223 EXPECT_TRUE(g_got_message); |
230 | 224 |
231 // Check that a second render frame in the same renderer process works | 225 // Check that a second render frame in the same renderer process works |
232 // correctly. | 226 // correctly. |
233 Shell* other_shell = CreateBrowser(); | 227 Shell* other_shell = CreateBrowser(); |
234 got_message = false; | 228 g_got_message = false; |
235 base::RunLoop other_run_loop; | 229 base::RunLoop other_run_loop; |
236 factory()->set_run_loop(&other_run_loop); | 230 factory()->set_run_loop(&other_run_loop); |
237 NavigateToURL(other_shell, test_url); | 231 NavigateToURL(other_shell, test_url); |
238 // RunLoop is quit when message received from page. | 232 // RunLoop is quit when message received from page. |
239 other_run_loop.Run(); | 233 other_run_loop.Run(); |
240 EXPECT_TRUE(got_message); | 234 EXPECT_TRUE(g_got_message); |
241 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), | 235 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), |
242 other_shell->web_contents()->GetRenderProcessHost()); | 236 other_shell->web_contents()->GetRenderProcessHost()); |
243 } | 237 } |
244 | 238 |
245 } // namespace | 239 } // namespace |
246 } // namespace content | 240 } // namespace content |
OLD | NEW |