| 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 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/path_service.h" | |
| 11 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 12 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 13 #include "content/browser/webui/web_ui_controller_factory_registry.h" | 12 #include "content/browser/webui/web_ui_controller_factory_registry.h" |
| 14 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 14 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_process_host.h" | 15 #include "content/public/browser/render_process_host.h" |
| 17 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/browser/web_ui_controller.h" | 18 #include "content/public/browser/web_ui_controller.h" |
| 20 #include "content/public/browser/web_ui_data_source.h" | 19 #include "content/public/browser/web_ui_data_source.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 46 if (id == mojo::kBindingsModuleName || | 45 if (id == mojo::kBindingsModuleName || |
| 47 id == mojo::kBufferModuleName || | 46 id == mojo::kBufferModuleName || |
| 48 id == mojo::kCodecModuleName || | 47 id == mojo::kCodecModuleName || |
| 49 id == mojo::kConnectionModuleName || | 48 id == mojo::kConnectionModuleName || |
| 50 id == mojo::kConnectorModuleName || | 49 id == mojo::kConnectorModuleName || |
| 51 id == mojo::kUnicodeModuleName || | 50 id == mojo::kUnicodeModuleName || |
| 52 id == mojo::kRouterModuleName || | 51 id == mojo::kRouterModuleName || |
| 53 id == mojo::kValidatorModuleName) | 52 id == mojo::kValidatorModuleName) |
| 54 return false; | 53 return false; |
| 55 | 54 |
| 56 if (id.find(".mojom") != std::string::npos) { | |
| 57 std::string contents; | |
| 58 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id), | |
| 59 &contents, std::string::npos)) | |
| 60 << id; | |
| 61 base::RefCountedString* ref_contents = new base::RefCountedString; | |
| 62 ref_contents->data() = contents; | |
| 63 callback.Run(ref_contents); | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 base::FilePath path; | |
| 68 DCHECK(base::PathService::Get(content::DIR_TEST_DATA, &path)); | |
| 69 path = path.AppendASCII(id.substr(0, id.find("?"))); | |
| 70 std::string contents; | 55 std::string contents; |
| 71 CHECK(base::ReadFileToString(path, &contents, std::string::npos)) << id; | 56 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id), |
| 57 &contents, |
| 58 std::string::npos)) << id; |
| 72 base::RefCountedString* ref_contents = new base::RefCountedString; | 59 base::RefCountedString* ref_contents = new base::RefCountedString; |
| 73 ref_contents->data() = contents; | 60 ref_contents->data() = contents; |
| 74 callback.Run(ref_contents); | 61 callback.Run(ref_contents); |
| 75 return true; | 62 return true; |
| 76 } | 63 } |
| 77 | 64 |
| 78 class BrowserTargetImpl : public BrowserTarget { | 65 class BrowserTargetImpl : public BrowserTarget { |
| 79 public: | 66 public: |
| 80 BrowserTargetImpl(base::RunLoop* run_loop, | 67 BrowserTargetImpl(base::RunLoop* run_loop, |
| 81 mojo::InterfaceRequest<BrowserTarget> request) | 68 mojo::InterfaceRequest<BrowserTarget> request) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 96 base::RunLoop* run_loop_; | 83 base::RunLoop* run_loop_; |
| 97 | 84 |
| 98 private: | 85 private: |
| 99 mojo::Binding<BrowserTarget> binding_; | 86 mojo::Binding<BrowserTarget> binding_; |
| 100 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); | 87 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); |
| 101 }; | 88 }; |
| 102 | 89 |
| 103 // WebUIController that sets up mojo bindings. | 90 // WebUIController that sets up mojo bindings. |
| 104 class TestWebUIController : public WebUIController { | 91 class TestWebUIController : public WebUIController { |
| 105 public: | 92 public: |
| 106 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | 93 TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
| 107 : WebUIController(web_ui), run_loop_(run_loop) { | 94 : WebUIController(web_ui), |
| 95 run_loop_(run_loop) { |
| 108 content::WebUIDataSource* data_source = | 96 content::WebUIDataSource* data_source = |
| 109 WebUIDataSource::Create("mojo-web-ui"); | 97 WebUIDataSource::AddMojoDataSource( |
| 110 data_source->AddMojoResources(); | 98 web_ui->GetWebContents()->GetBrowserContext()); |
| 111 data_source->SetRequestFilter(base::Bind(&GetResource)); | 99 data_source->SetRequestFilter(base::Bind(&GetResource)); |
| 112 content::WebUIDataSource::Add(web_ui->GetWebContents()->GetBrowserContext(), | |
| 113 data_source); | |
| 114 } | 100 } |
| 115 | 101 |
| 116 protected: | 102 protected: |
| 117 base::RunLoop* run_loop_; | 103 base::RunLoop* run_loop_; |
| 118 scoped_ptr<BrowserTargetImpl> browser_target_; | 104 scoped_ptr<BrowserTargetImpl> browser_target_; |
| 119 | 105 |
| 120 private: | 106 private: |
| 121 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); | 107 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); |
| 122 }; | 108 }; |
| 123 | 109 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // it from the browser to the page and back. | 198 // it from the browser to the page and back. |
| 213 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { | 199 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEndPing) { |
| 214 if (!IsGeneratedResourceAvailable( | 200 if (!IsGeneratedResourceAvailable( |
| 215 "content/test/data/web_ui_test_mojo_bindings.mojom")) | 201 "content/test/data/web_ui_test_mojo_bindings.mojom")) |
| 216 return; | 202 return; |
| 217 | 203 |
| 218 got_message = false; | 204 got_message = false; |
| 219 ASSERT_TRUE(embedded_test_server()->Start()); | 205 ASSERT_TRUE(embedded_test_server()->Start()); |
| 220 base::RunLoop run_loop; | 206 base::RunLoop run_loop; |
| 221 factory()->set_run_loop(&run_loop); | 207 factory()->set_run_loop(&run_loop); |
| 222 GURL test_url("chrome://mojo-web-ui/web_ui_mojo.html?ping"); | 208 GURL test_url(embedded_test_server()->GetURL("/web_ui_mojo.html?ping")); |
| 223 NavigateToURL(shell(), test_url); | 209 NavigateToURL(shell(), test_url); |
| 224 // RunLoop is quit when message received from page. | 210 // RunLoop is quit when message received from page. |
| 225 run_loop.Run(); | 211 run_loop.Run(); |
| 226 EXPECT_TRUE(got_message); | 212 EXPECT_TRUE(got_message); |
| 227 | 213 |
| 228 // Check that a second render frame in the same renderer process works | 214 // Check that a second render frame in the same renderer process works |
| 229 // correctly. | 215 // correctly. |
| 230 Shell* other_shell = CreateBrowser(); | 216 Shell* other_shell = CreateBrowser(); |
| 231 got_message = false; | 217 got_message = false; |
| 232 base::RunLoop other_run_loop; | 218 base::RunLoop other_run_loop; |
| 233 factory()->set_run_loop(&other_run_loop); | 219 factory()->set_run_loop(&other_run_loop); |
| 234 NavigateToURL(other_shell, test_url); | 220 NavigateToURL(other_shell, test_url); |
| 235 // RunLoop is quit when message received from page. | 221 // RunLoop is quit when message received from page. |
| 236 other_run_loop.Run(); | 222 other_run_loop.Run(); |
| 237 EXPECT_TRUE(got_message); | 223 EXPECT_TRUE(got_message); |
| 238 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), | 224 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), |
| 239 other_shell->web_contents()->GetRenderProcessHost()); | 225 other_shell->web_contents()->GetRenderProcessHost()); |
| 240 } | 226 } |
| 241 | 227 |
| 242 // Loads a webui page that connects to a test Mojo application via the browser's | 228 // Loads a webui page that connects to a test Mojo application via the browser's |
| 243 // Mojo shell interface. | 229 // Mojo shell interface. |
| 244 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, ConnectToApplication) { | 230 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, ConnectToApplication) { |
| 245 if (!IsGeneratedResourceAvailable( | 231 if (!IsGeneratedResourceAvailable( |
| 246 "content/public/test/test_mojo_service.mojom")) | 232 "content/public/test/test_mojo_service.mojom")) |
| 247 return; | 233 return; |
| 248 | 234 |
| 249 ASSERT_TRUE(embedded_test_server()->Start()); | 235 ASSERT_TRUE(embedded_test_server()->Start()); |
| 250 NavigateToURL(shell(), | 236 NavigateToURL(shell(), |
| 251 GURL("chrome://mojo-web-ui/web_ui_mojo_shell_test.html")); | 237 embedded_test_server()->GetURL("/web_ui_mojo_shell_test.html")); |
| 252 | 238 |
| 253 DOMMessageQueue message_queue; | 239 DOMMessageQueue message_queue; |
| 254 std::string message; | 240 std::string message; |
| 255 ASSERT_TRUE(message_queue.WaitForMessage(&message)); | 241 ASSERT_TRUE(message_queue.WaitForMessage(&message)); |
| 256 EXPECT_EQ("true", message); | 242 EXPECT_EQ("true", message); |
| 257 } | 243 } |
| 258 | 244 |
| 259 } // namespace | 245 } // namespace |
| 260 } // namespace content | 246 } // namespace content |
| OLD | NEW |