OLD | NEW |
(Empty) | |
| 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 |
| 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/constants.h" |
| 26 #include "mojo/public/bindings/remote_ptr.h" |
| 27 |
| 28 namespace content { |
| 29 namespace { |
| 30 |
| 31 bool got_message = false; |
| 32 |
| 33 // Returns the path to the mojom js bindings file. |
| 34 base::FilePath GetFilePathForJSResource(const std::string& path) { |
| 35 std::string binding_path = "gen/" + path + ".js"; |
| 36 #if defined(OS_WIN) |
| 37 std::string tmp; |
| 38 base::ReplaceChars(binding_path, "//", "\\", &tmp); |
| 39 binding_path.swap(tmp); |
| 40 #endif |
| 41 base::FilePath file_path; |
| 42 PathService::Get(CHILD_PROCESS_EXE, &file_path); |
| 43 return file_path.DirName().AppendASCII(binding_path); |
| 44 } |
| 45 |
| 46 // The bindings for the page are generated from a .mojom file. This code looks |
| 47 // up the generated file from disk and returns it. |
| 48 bool GetResource(const std::string& id, |
| 49 const WebUIDataSource::GotDataCallback& callback) { |
| 50 // These are handled by the WebUIDataSource that AddMojoDataSource() creates. |
| 51 if (id == mojo::kCodecModuleName || id == mojo::kConnectorModuleName) |
| 52 return false; |
| 53 |
| 54 std::string contents; |
| 55 CHECK(base::ReadFileToString(GetFilePathForJSResource(id), &contents, |
| 56 std::string::npos)); |
| 57 base::RefCountedString* ref_contents = new base::RefCountedString; |
| 58 ref_contents->data() = contents; |
| 59 callback.Run(ref_contents); |
| 60 return true; |
| 61 } |
| 62 |
| 63 // BrowserTarget implementation that quits a RunLoop when BrowserTarget::Test() |
| 64 // is called. |
| 65 class BrowserTargetImpl : public mojo::BrowserTarget { |
| 66 public: |
| 67 BrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, |
| 68 base::RunLoop* run_loop) |
| 69 : client_(handle.Pass(), this), |
| 70 run_loop_(run_loop) { |
| 71 client_->Test(); |
| 72 } |
| 73 virtual ~BrowserTargetImpl() {} |
| 74 |
| 75 // mojo::BrowserTarget overrides: |
| 76 virtual void Test() OVERRIDE { |
| 77 got_message = true; |
| 78 run_loop_->Quit(); |
| 79 } |
| 80 |
| 81 private: |
| 82 mojo::RemotePtr<mojo::RendererTarget> client_; |
| 83 |
| 84 base::RunLoop* run_loop_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); |
| 87 }; |
| 88 |
| 89 // WebUIController that sets up mojo bindings. Additionally it creates the |
| 90 // BrowserTarget implementation at the right time. |
| 91 class TestWebUIController : public WebUIController { |
| 92 public: |
| 93 explicit TestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
| 94 : WebUIController(web_ui), |
| 95 run_loop_(run_loop) { |
| 96 content::WebUIDataSource* data_source = |
| 97 WebUIDataSource::AddMojoDataSource( |
| 98 web_ui->GetWebContents()->GetBrowserContext()); |
| 99 data_source->SetRequestFilter(base::Bind(&GetResource)); |
| 100 } |
| 101 |
| 102 // WebUIController overrides: |
| 103 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { |
| 104 mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe; |
| 105 browser_target_.reset( |
| 106 new BrowserTargetImpl(pipe.handle_to_peer.Pass(), run_loop_)); |
| 107 render_view_host->SetWebUIHandle( |
| 108 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release())); |
| 109 } |
| 110 |
| 111 private: |
| 112 base::RunLoop* run_loop_; |
| 113 |
| 114 scoped_ptr<BrowserTargetImpl> browser_target_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); |
| 117 }; |
| 118 |
| 119 // WebUIControllerFactory that creates TestWebUIController. |
| 120 class TestWebUIControllerFactory : public WebUIControllerFactory { |
| 121 public: |
| 122 TestWebUIControllerFactory() : run_loop_(NULL) {} |
| 123 |
| 124 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 125 |
| 126 virtual WebUIController* CreateWebUIControllerForURL( |
| 127 WebUI* web_ui, const GURL& url) const OVERRIDE { |
| 128 return new TestWebUIController(web_ui, run_loop_); |
| 129 } |
| 130 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, |
| 131 const GURL& url) const OVERRIDE { |
| 132 return reinterpret_cast<WebUI::TypeID>(1); |
| 133 } |
| 134 virtual bool UseWebUIForURL(BrowserContext* browser_context, |
| 135 const GURL& url) const OVERRIDE { |
| 136 return true; |
| 137 } |
| 138 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, |
| 139 const GURL& url) const OVERRIDE { |
| 140 return true; |
| 141 } |
| 142 |
| 143 private: |
| 144 base::RunLoop* run_loop_; |
| 145 |
| 146 DISALLOW_COPY_AND_ASSIGN(TestWebUIControllerFactory); |
| 147 }; |
| 148 |
| 149 class WebUIMojoTest : public ContentBrowserTest { |
| 150 public: |
| 151 WebUIMojoTest() { |
| 152 MojoChannelInit::InitMojo(); |
| 153 WebUIControllerFactory::RegisterFactory(&factory_); |
| 154 } |
| 155 |
| 156 virtual ~WebUIMojoTest() { |
| 157 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_); |
| 158 } |
| 159 |
| 160 TestWebUIControllerFactory* factory() { return &factory_; } |
| 161 |
| 162 private: |
| 163 TestWebUIControllerFactory factory_; |
| 164 |
| 165 DISALLOW_COPY_AND_ASSIGN(WebUIMojoTest); |
| 166 }; |
| 167 |
| 168 // Loads a webui page that contains mojo bindings and verifies a message makes |
| 169 // it from the browser to the page and back. |
| 170 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEnd) { |
| 171 // Currently there is no way to have a generated file included in the isolate |
| 172 // files. If the bindings file doesn't exist assume we're on such a bot and |
| 173 // pass. |
| 174 // TODO(sky): remove this conditional when isolates support copying from gen. |
| 175 const base::FilePath test_file_path( |
| 176 GetFilePathForJSResource( |
| 177 "content/test/data/web_ui_test_mojo_bindings.mojom")); |
| 178 if (!base::PathExists(test_file_path)) { |
| 179 LOG(WARNING) << " mojom binding file doesn't exist, assuming on isolate"; |
| 180 return; |
| 181 } |
| 182 |
| 183 got_message = false; |
| 184 ASSERT_TRUE(test_server()->Start()); |
| 185 base::RunLoop run_loop; |
| 186 factory()->set_run_loop(&run_loop); |
| 187 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html")); |
| 188 NavigateToURL(shell(), test_url); |
| 189 // RunLoop is quit when message received from page. |
| 190 run_loop.Run(); |
| 191 EXPECT_TRUE(got_message); |
| 192 } |
| 193 |
| 194 } // namespace |
| 195 } // namespace content |
OLD | NEW |