| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "content/public/common/content_paths.h" | 21 #include "content/public/common/content_paths.h" |
| 22 #include "content/public/common/content_switches.h" | 22 #include "content/public/common/content_switches.h" |
| 23 #include "content/public/common/service_registry.h" | 23 #include "content/public/common/service_registry.h" |
| 24 #include "content/public/common/url_utils.h" | 24 #include "content/public/common/url_utils.h" |
| 25 #include "content/public/test/content_browser_test.h" | 25 #include "content/public/test/content_browser_test.h" |
| 26 #include "content/public/test/content_browser_test_utils.h" | 26 #include "content/public/test/content_browser_test_utils.h" |
| 27 #include "content/shell/browser/shell.h" | 27 #include "content/shell/browser/shell.h" |
| 28 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h" | 28 #include "content/test/data/web_ui_test_mojo_bindings.mojom.h" |
| 29 #include "grit/content_resources.h" | 29 #include "grit/content_resources.h" |
| 30 #include "mojo/common/test/test_utils.h" | 30 #include "mojo/common/test/test_utils.h" |
| 31 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 32 #include "mojo/public/cpp/bindings/interface_request.h" |
| 31 #include "mojo/public/js/bindings/constants.h" | 33 #include "mojo/public/js/bindings/constants.h" |
| 32 | 34 |
| 33 namespace content { | 35 namespace content { |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 bool got_message = false; | 38 bool got_message = false; |
| 37 | 39 |
| 38 // The bindings for the page are generated from a .mojom file. This code looks | 40 // The bindings for the page are generated from a .mojom file. This code looks |
| 39 // up the generated file from disk and returns it. | 41 // up the generated file from disk and returns it. |
| 40 bool GetResource(const std::string& id, | 42 bool GetResource(const std::string& id, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 51 std::string contents; | 53 std::string contents; |
| 52 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id), | 54 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id), |
| 53 &contents, | 55 &contents, |
| 54 std::string::npos)) << id; | 56 std::string::npos)) << id; |
| 55 base::RefCountedString* ref_contents = new base::RefCountedString; | 57 base::RefCountedString* ref_contents = new base::RefCountedString; |
| 56 ref_contents->data() = contents; | 58 ref_contents->data() = contents; |
| 57 callback.Run(ref_contents); | 59 callback.Run(ref_contents); |
| 58 return true; | 60 return true; |
| 59 } | 61 } |
| 60 | 62 |
| 61 class BrowserTargetImpl : public BrowserTarget { | 63 class BrowserTargetImpl : public mojo::InterfaceImpl<BrowserTarget> { |
| 62 public: | 64 public: |
| 63 BrowserTargetImpl(mojo::ScopedMessagePipeHandle handle, | 65 explicit BrowserTargetImpl(base::RunLoop* run_loop) : run_loop_(run_loop) {} |
| 64 base::RunLoop* run_loop) | |
| 65 : run_loop_(run_loop) { | |
| 66 renderer_.Bind(handle.Pass()); | |
| 67 renderer_.set_client(this); | |
| 68 } | |
| 69 | 66 |
| 70 virtual ~BrowserTargetImpl() {} | 67 virtual ~BrowserTargetImpl() {} |
| 71 | 68 |
| 72 // BrowserTarget overrides: | 69 // mojo::InterfaceImpl<BrowserTarget> overrides: |
| 73 virtual void PingResponse() OVERRIDE { | 70 virtual void PingResponse() OVERRIDE { |
| 74 NOTREACHED(); | 71 NOTREACHED(); |
| 75 } | 72 } |
| 76 | 73 |
| 77 protected: | 74 protected: |
| 78 RendererTargetPtr renderer_; | |
| 79 base::RunLoop* run_loop_; | 75 base::RunLoop* run_loop_; |
| 80 | 76 |
| 81 private: | 77 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); | 78 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); |
| 83 }; | 79 }; |
| 84 | 80 |
| 85 class PingBrowserTargetImpl : public BrowserTargetImpl { | 81 class PingBrowserTargetImpl : public BrowserTargetImpl { |
| 86 public: | 82 public: |
| 87 PingBrowserTargetImpl(mojo::ScopedMessagePipeHandle handle, | 83 explicit PingBrowserTargetImpl(base::RunLoop* run_loop) |
| 88 base::RunLoop* run_loop) | 84 : BrowserTargetImpl(run_loop) {} |
| 89 : BrowserTargetImpl(handle.Pass(), run_loop) { | |
| 90 renderer_->Ping(); | |
| 91 } | |
| 92 | 85 |
| 93 virtual ~PingBrowserTargetImpl() {} | 86 virtual ~PingBrowserTargetImpl() {} |
| 94 | 87 |
| 95 // BrowserTarget overrides: | 88 // mojo::InterfaceImpl<BrowserTarget> overrides: |
| 89 virtual void OnConnectionEstablished() OVERRIDE { |
| 90 client()->Ping(); |
| 91 } |
| 92 |
| 96 // Quit the RunLoop when called. | 93 // Quit the RunLoop when called. |
| 97 virtual void PingResponse() OVERRIDE { | 94 virtual void PingResponse() OVERRIDE { |
| 98 got_message = true; | 95 got_message = true; |
| 99 run_loop_->Quit(); | 96 run_loop_->Quit(); |
| 100 } | 97 } |
| 101 | 98 |
| 102 private: | 99 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl); | 100 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl); |
| 104 }; | 101 }; |
| 105 | 102 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 127 // implementation at the right time. | 124 // implementation at the right time. |
| 128 class PingTestWebUIController : public TestWebUIController { | 125 class PingTestWebUIController : public TestWebUIController { |
| 129 public: | 126 public: |
| 130 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) | 127 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) |
| 131 : TestWebUIController(web_ui, run_loop) { | 128 : TestWebUIController(web_ui, run_loop) { |
| 132 } | 129 } |
| 133 virtual ~PingTestWebUIController() {} | 130 virtual ~PingTestWebUIController() {} |
| 134 | 131 |
| 135 // WebUIController overrides: | 132 // WebUIController overrides: |
| 136 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { | 133 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { |
| 137 render_view_host->GetMainFrame()->GetServiceRegistry()->AddService( | 134 render_view_host->GetMainFrame()->GetServiceRegistry()-> |
| 138 "webui_controller", | 135 AddService<BrowserTarget>(base::Bind( |
| 139 base::Bind(&PingTestWebUIController::CreateHandler, | 136 &PingTestWebUIController::CreateHandler, base::Unretained(this))); |
| 140 base::Unretained(this))); | |
| 141 } | 137 } |
| 142 | 138 |
| 143 void CreateHandler(mojo::ScopedMessagePipeHandle handle) { | 139 void CreateHandler(mojo::InterfaceRequest<BrowserTarget> request) { |
| 144 browser_target_.reset(new PingBrowserTargetImpl(handle.Pass(), run_loop_)); | 140 browser_target_.reset(mojo::WeakBindToRequest( |
| 141 new PingBrowserTargetImpl(run_loop_), &request)); |
| 145 } | 142 } |
| 146 | 143 |
| 147 private: | 144 private: |
| 148 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); | 145 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); |
| 149 }; | 146 }; |
| 150 | 147 |
| 151 // WebUIControllerFactory that creates TestWebUIController. | 148 // WebUIControllerFactory that creates TestWebUIController. |
| 152 class TestWebUIControllerFactory : public WebUIControllerFactory { | 149 class TestWebUIControllerFactory : public WebUIControllerFactory { |
| 153 public: | 150 public: |
| 154 TestWebUIControllerFactory() : run_loop_(NULL) {} | 151 TestWebUIControllerFactory() : run_loop_(NULL) {} |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 NavigateToURL(other_shell, test_url); | 229 NavigateToURL(other_shell, test_url); |
| 233 // RunLoop is quit when message received from page. | 230 // RunLoop is quit when message received from page. |
| 234 other_run_loop.Run(); | 231 other_run_loop.Run(); |
| 235 EXPECT_TRUE(got_message); | 232 EXPECT_TRUE(got_message); |
| 236 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), | 233 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), |
| 237 other_shell->web_contents()->GetRenderProcessHost()); | 234 other_shell->web_contents()->GetRenderProcessHost()); |
| 238 } | 235 } |
| 239 | 236 |
| 240 } // namespace | 237 } // namespace |
| 241 } // namespace content | 238 } // namespace content |
| OLD | NEW |