OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
darin (slow to review)
2014/03/21 00:04:52
nit: no "(c)"
sky
2014/03/21 03:19:16
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/run_loop.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "content/browser/webui/web_ui_controller_factory_registry.h" | |
12 #include "content/common/mojo/mojo_channel_init.h" | |
13 #include "content/public/browser/browser_context.h" | |
14 #include "content/public/browser/render_view_host.h" | |
15 #include "content/public/browser/web_contents.h" | |
16 #include "content/public/browser/web_ui_controller.h" | |
17 #include "content/public/browser/web_ui_data_source.h" | |
18 #include "content/public/common/content_paths.h" | |
19 #include "content/public/common/content_switches.h" | |
20 #include "content/public/common/url_utils.h" | |
21 #include "content/test/content_browser_test.h" | |
22 #include "content/test/content_browser_test_utils.h" | |
23 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h" | |
24 #include "grit/content_resources.h" | |
25 #include "mojo/public/bindings/js/defaults.h" | |
26 #include "mojo/public/bindings/remote_ptr.h" | |
27 | |
28 namespace content { | |
29 namespace { | |
30 | |
31 bool got_message = false; | |
32 | |
33 // The bindings for the page are generated from a .mojom file. This code looks | |
34 // up the generated file from disk and returns it. | |
35 bool GetResource(const std::string& id, | |
36 const WebUIDataSource::GotDataCallback& callback) { | |
37 // These are handled by the WebUIDataSource that AddMojoDataSource() creates. | |
38 if (id == mojo::kCodecModuleName || id == mojo::kConnectorModuleName) | |
39 return false; | |
40 | |
41 // TODO: this is a hack. | |
darin (slow to review)
2014/03/21 00:04:52
TODO(sky) ? :)
sky
2014/03/21 03:19:16
I ended up removing the comment. This seems like t
| |
42 std::string binding_path = "gen/" + id + ".js"; | |
43 #if defined(OS_WIN) | |
44 std::string tmp; | |
45 base::ReplaceChars(binding_path, "//", "\\", &tmp); | |
46 binding_path.swap(tmp); | |
47 #endif | |
48 base::FilePath file_path; | |
49 PathService::Get(CHILD_PROCESS_EXE, &file_path); | |
50 file_path = file_path.DirName().AppendASCII(binding_path); | |
51 std::string contents; | |
52 CHECK(base::ReadFileToString(file_path, &contents, std::string::npos)); | |
53 base::RefCountedString* ref_contents = new base::RefCountedString; | |
54 ref_contents->data() = contents; | |
55 callback.Run(ref_contents); | |
56 return true; | |
57 } | |
58 | |
59 // BrowserTarget implementation that quits a RunLoop when BrowserTarget::Test() | |
60 // is called. | |
61 class BrowserTargetImpl : public mojo::BrowserTarget { | |
62 public: | |
63 BrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, | |
64 base::RunLoop* run_loop) | |
65 : client_(handle.Pass(), this), | |
66 run_loop_(run_loop) { | |
67 client_->Test(); | |
68 } | |
69 virtual ~BrowserTargetImpl() {} | |
70 | |
71 void Test() { | |
72 got_message = true; | |
73 run_loop_->Quit(); | |
74 } | |
75 | |
76 private: | |
77 mojo::RemotePtr<mojo::RendererTarget> client_; | |
78 | |
79 base::RunLoop* run_loop_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); | |
82 }; | |
83 | |
84 // WebUIController that sets up mojo bindings. Additionally it creates the | |
85 // BrowserTarget implementation at the right time. | |
86 class TestWebUIController : public WebUIController { | |
87 public: | |
88 explicit TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | |
89 : WebUIController(web_ui), | |
90 run_loop_(run_loop) { | |
91 content::WebUIDataSource* data_source = | |
92 WebUIDataSource::AddMojoDataSource( | |
93 web_ui->GetWebContents()->GetBrowserContext()); | |
94 data_source->SetRequestFilter(base::Bind(&GetResource)); | |
95 } | |
96 | |
97 virtual void RenderViewCreated(RenderViewHost* render_view_host) { | |
98 mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe; | |
99 browser_target_.reset( | |
100 new BrowserTargetImpl(pipe.handle_to_peer.Pass(), run_loop_)); | |
101 render_view_host->SetBrowserWebUIHandle( | |
102 MakeScopedHandle(mojo::Handle(pipe.handle_to_self.release().value()))); | |
103 } | |
104 | |
105 private: | |
106 base::RunLoop* run_loop_; | |
107 | |
108 scoped_ptr<BrowserTargetImpl> browser_target_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); | |
111 }; | |
112 | |
113 // WebUIControllerFactory that creates TestWebUIController. | |
114 class TestWebUIControllerFactory : public WebUIControllerFactory { | |
115 public: | |
116 TestWebUIControllerFactory() : run_loop_(NULL) {} | |
117 | |
118 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | |
119 | |
120 virtual WebUIController* CreateWebUIControllerForURL( | |
121 WebUI* web_ui, const GURL& url) const OVERRIDE { | |
122 return new TestWebUIController(web_ui, run_loop_); | |
123 } | |
124 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | |
125 const GURL& url) const OVERRIDE { | |
126 return reinterpret_cast<WebUI::TypeID>(1); | |
127 } | |
128 virtual bool UseWebUIForURL(BrowserContext* browser_context, | |
129 const GURL& url) const OVERRIDE { | |
130 return true; | |
131 } | |
132 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, | |
133 const GURL& url) const OVERRIDE { | |
134 return true; | |
135 } | |
136 | |
137 private: | |
138 base::RunLoop* run_loop_; | |
139 | |
140 DISALLOW_COPY_AND_ASSIGN(TestWebUIControllerFactory); | |
141 }; | |
142 | |
143 class WebUIMojoTest : public ContentBrowserTest { | |
144 public: | |
145 WebUIMojoTest() { | |
146 MojoChannelInit::InitMojo(); | |
147 WebUIControllerFactory::RegisterFactory(&factory_); | |
148 } | |
149 | |
150 virtual ~WebUIMojoTest() { | |
151 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_); | |
152 } | |
153 | |
154 TestWebUIControllerFactory* factory() { return &factory_; } | |
155 | |
156 private: | |
157 TestWebUIControllerFactory factory_; | |
158 | |
159 DISALLOW_COPY_AND_ASSIGN(WebUIMojoTest); | |
160 }; | |
161 | |
162 // TODO(sky): this is temporarily disabled as it depends upon generating files | |
163 // (the js bindings). Once we move to isolates it can be enabled. | |
164 | |
165 // Loads a webui page that contains mojo bindings and verifies a message makes | |
166 // it from the browser to the page and back. | |
167 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, DISABLED_EndToEnd) { | |
168 got_message = false; | |
169 ASSERT_TRUE(test_server()->Start()); | |
170 base::RunLoop run_loop; | |
171 factory()->set_run_loop(&run_loop); | |
172 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html")); | |
173 NavigateToURL(shell(), test_url); | |
174 // RunLoop is quit when message received from page. | |
175 run_loop.Run(); | |
176 EXPECT_TRUE(got_message); | |
177 } | |
178 | |
179 } // namespace | |
180 } // namespace content | |
OLD | NEW |