Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(152)

Side by Side Diff: content/browser/webui/web_ui_mojo_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698