| 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 // 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 std::string binding_path = "gen/" + id + ".js"; | |
| 42 #if defined(OS_WIN) | |
| 43 std::string tmp; | |
| 44 base::ReplaceChars(binding_path, "//", "\\", &tmp); | |
| 45 binding_path.swap(tmp); | |
| 46 #endif | |
| 47 base::FilePath file_path; | |
| 48 PathService::Get(CHILD_PROCESS_EXE, &file_path); | |
| 49 file_path = file_path.DirName().AppendASCII(binding_path); | |
| 50 std::string contents; | |
| 51 CHECK(base::ReadFileToString(file_path, &contents, std::string::npos)); | |
| 52 base::RefCountedString* ref_contents = new base::RefCountedString; | |
| 53 ref_contents->data() = contents; | |
| 54 callback.Run(ref_contents); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 // BrowserTarget implementation that quits a RunLoop when BrowserTarget::Test() | |
| 59 // is called. | |
| 60 class BrowserTargetImpl : public mojo::BrowserTarget { | |
| 61 public: | |
| 62 BrowserTargetImpl(mojo::ScopedRendererTargetHandle handle, | |
| 63 base::RunLoop* run_loop) | |
| 64 : client_(handle.Pass(), this), | |
| 65 run_loop_(run_loop) { | |
| 66 client_->Test(); | |
| 67 } | |
| 68 virtual ~BrowserTargetImpl() {} | |
| 69 | |
| 70 // mojo::BrowserTarget overrides: | |
| 71 virtual void Test() OVERRIDE { | |
| 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 // WebUIController overrides: | |
| 98 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { | |
| 99 mojo::InterfacePipe<mojo::BrowserTarget, mojo::RendererTarget> pipe; | |
| 100 browser_target_.reset( | |
| 101 new BrowserTargetImpl(pipe.handle_to_peer.Pass(), run_loop_)); | |
| 102 render_view_host->SetWebUIHandle( | |
| 103 mojo::ScopedMessagePipeHandle(pipe.handle_to_self.release())); | |
| 104 } | |
| 105 | |
| 106 private: | |
| 107 base::RunLoop* run_loop_; | |
| 108 | |
| 109 scoped_ptr<BrowserTargetImpl> browser_target_; | |
| 110 | |
| 111 DISALLOW_COPY_AND_ASSIGN(TestWebUIController); | |
| 112 }; | |
| 113 | |
| 114 // WebUIControllerFactory that creates TestWebUIController. | |
| 115 class TestWebUIControllerFactory : public WebUIControllerFactory { | |
| 116 public: | |
| 117 TestWebUIControllerFactory() : run_loop_(NULL) {} | |
| 118 | |
| 119 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | |
| 120 | |
| 121 virtual WebUIController* CreateWebUIControllerForURL( | |
| 122 WebUI* web_ui, const GURL& url) const OVERRIDE { | |
| 123 return new TestWebUIController(web_ui, run_loop_); | |
| 124 } | |
| 125 virtual WebUI::TypeID GetWebUIType(BrowserContext* browser_context, | |
| 126 const GURL& url) const OVERRIDE { | |
| 127 return reinterpret_cast<WebUI::TypeID>(1); | |
| 128 } | |
| 129 virtual bool UseWebUIForURL(BrowserContext* browser_context, | |
| 130 const GURL& url) const OVERRIDE { | |
| 131 return true; | |
| 132 } | |
| 133 virtual bool UseWebUIBindingsForURL(BrowserContext* browser_context, | |
| 134 const GURL& url) const OVERRIDE { | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 private: | |
| 139 base::RunLoop* run_loop_; | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(TestWebUIControllerFactory); | |
| 142 }; | |
| 143 | |
| 144 class WebUIMojoTest : public ContentBrowserTest { | |
| 145 public: | |
| 146 WebUIMojoTest() { | |
| 147 MojoChannelInit::InitMojo(); | |
| 148 WebUIControllerFactory::RegisterFactory(&factory_); | |
| 149 } | |
| 150 | |
| 151 virtual ~WebUIMojoTest() { | |
| 152 WebUIControllerFactory::UnregisterFactoryForTesting(&factory_); | |
| 153 } | |
| 154 | |
| 155 TestWebUIControllerFactory* factory() { return &factory_; } | |
| 156 | |
| 157 private: | |
| 158 TestWebUIControllerFactory factory_; | |
| 159 | |
| 160 DISALLOW_COPY_AND_ASSIGN(WebUIMojoTest); | |
| 161 }; | |
| 162 | |
| 163 // TODO(sky): this is temporarily disabled as it depends upon generating files | |
| 164 // (the js bindings). Once we move to isolates it can be enabled. | |
| 165 | |
| 166 // Loads a webui page that contains mojo bindings and verifies a message makes | |
| 167 // it from the browser to the page and back. | |
| 168 IN_PROC_BROWSER_TEST_F(WebUIMojoTest, EndToEnd) { | |
| 169 got_message = false; | |
| 170 ASSERT_TRUE(test_server()->Start()); | |
| 171 base::RunLoop run_loop; | |
| 172 factory()->set_run_loop(&run_loop); | |
| 173 GURL test_url(test_server()->GetURL("files/web_ui_mojo.html")); | |
| 174 NavigateToURL(shell(), test_url); | |
| 175 // RunLoop is quit when message received from page. | |
| 176 run_loop.Run(); | |
| 177 EXPECT_TRUE(got_message); | |
| 178 } | |
| 179 | |
| 180 } // namespace | |
| 181 } // namespace content | |
| OLD | NEW |