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

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

Issue 302573002: Add a Javascript wrapper around ServiceRegistry and expose it to WebUI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@render-view-shell
Patch Set: fix compile Created 6 years, 5 months 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/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
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,
41 const WebUIDataSource::GotDataCallback& callback) { 43 const WebUIDataSource::GotDataCallback& callback) {
42 // These are handled by the WebUIDataSource that AddMojoDataSource() creates. 44 // These are handled by the WebUIDataSource that AddMojoDataSource() creates.
43 if (id == mojo::kCodecModuleName || 45 if (id == mojo::kCodecModuleName ||
44 id == mojo::kConnectionModuleName || 46 id == mojo::kConnectionModuleName ||
45 id == mojo::kConnectorModuleName || 47 id == mojo::kConnectorModuleName ||
46 id == mojo::kUnicodeModuleName || 48 id == mojo::kUnicodeModuleName ||
47 id == mojo::kRouterModuleName) 49 id == mojo::kRouterModuleName)
48 return false; 50 return false;
49 51
50 std::string contents; 52 std::string contents;
51 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id), 53 CHECK(base::ReadFileToString(mojo::test::GetFilePathForJSResource(id),
52 &contents, 54 &contents,
53 std::string::npos)) << id; 55 std::string::npos)) << id;
54 base::RefCountedString* ref_contents = new base::RefCountedString; 56 base::RefCountedString* ref_contents = new base::RefCountedString;
55 ref_contents->data() = contents; 57 ref_contents->data() = contents;
56 callback.Run(ref_contents); 58 callback.Run(ref_contents);
57 return true; 59 return true;
58 } 60 }
59 61
60 class BrowserTargetImpl : public BrowserTarget { 62 class BrowserTargetImpl : public mojo::InterfaceImpl<BrowserTarget> {
61 public: 63 public:
62 BrowserTargetImpl(mojo::ScopedMessagePipeHandle handle, 64 explicit BrowserTargetImpl(base::RunLoop* run_loop) : run_loop_(run_loop) {}
63 base::RunLoop* run_loop)
64 : run_loop_(run_loop) {
65 renderer_.Bind(handle.Pass());
66 renderer_.set_client(this);
67 }
68 65
69 virtual ~BrowserTargetImpl() {} 66 virtual ~BrowserTargetImpl() {}
70 67
71 // BrowserTarget overrides: 68 // mojo::InterfaceImpl<BrowserTarget> overrides:
72 virtual void PingResponse() OVERRIDE { 69 virtual void PingResponse() OVERRIDE {
73 NOTREACHED(); 70 NOTREACHED();
74 } 71 }
72 virtual void OnConnectionError() OVERRIDE {}
darin (slow to review) 2014/07/29 16:22:05 hmm, why bother overriding this? the default imple
Sam McNally 2014/07/30 00:18:08 Done.
75 73
76 protected: 74 protected:
77 RendererTargetPtr renderer_;
78 base::RunLoop* run_loop_; 75 base::RunLoop* run_loop_;
79 76
80 private: 77 private:
81 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl); 78 DISALLOW_COPY_AND_ASSIGN(BrowserTargetImpl);
82 }; 79 };
83 80
84 class PingBrowserTargetImpl : public BrowserTargetImpl { 81 class PingBrowserTargetImpl : public BrowserTargetImpl {
85 public: 82 public:
86 PingBrowserTargetImpl(mojo::ScopedMessagePipeHandle handle, 83 explicit PingBrowserTargetImpl(base::RunLoop* run_loop)
87 base::RunLoop* run_loop) 84 : BrowserTargetImpl(run_loop) {}
88 : BrowserTargetImpl(handle.Pass(), run_loop) {
89 renderer_->Ping();
90 }
91 85
92 virtual ~PingBrowserTargetImpl() {} 86 virtual ~PingBrowserTargetImpl() {}
93 87
94 // BrowserTarget overrides: 88 // mojo::InterfaceImpl<BrowserTarget> overrides:
89 virtual void OnConnectionEstablished() OVERRIDE {
90 client()->Ping();
91 }
92
95 // Quit the RunLoop when called. 93 // Quit the RunLoop when called.
96 virtual void PingResponse() OVERRIDE { 94 virtual void PingResponse() OVERRIDE {
97 got_message = true; 95 got_message = true;
98 run_loop_->Quit(); 96 run_loop_->Quit();
99 } 97 }
100 98
101 private: 99 private:
102 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl); 100 DISALLOW_COPY_AND_ASSIGN(PingBrowserTargetImpl);
103 }; 101 };
104 102
(...skipping 21 matching lines...) Expand all
126 // implementation at the right time. 124 // implementation at the right time.
127 class PingTestWebUIController : public TestWebUIController { 125 class PingTestWebUIController : public TestWebUIController {
128 public: 126 public:
129 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop) 127 PingTestWebUIController(WebUI* web_ui, base::RunLoop* run_loop)
130 : TestWebUIController(web_ui, run_loop) { 128 : TestWebUIController(web_ui, run_loop) {
131 } 129 }
132 virtual ~PingTestWebUIController() {} 130 virtual ~PingTestWebUIController() {}
133 131
134 // WebUIController overrides: 132 // WebUIController overrides:
135 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE { 133 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE {
136 render_view_host->GetMainFrame()->GetServiceRegistry()->AddService( 134 render_view_host->GetMainFrame()->GetServiceRegistry()->
137 "webui_controller", 135 AddService<BrowserTarget>(base::Bind(
138 base::Bind(&PingTestWebUIController::CreateHandler, 136 &PingTestWebUIController::CreateHandler, base::Unretained(this)));
139 base::Unretained(this)));
140 } 137 }
141 138
142 void CreateHandler(mojo::ScopedMessagePipeHandle handle) { 139 void CreateHandler(mojo::InterfaceRequest<BrowserTarget> request) {
143 browser_target_.reset(new PingBrowserTargetImpl(handle.Pass(), run_loop_)); 140 browser_target_.reset(
141 mojo::BindToRequest(new PingBrowserTargetImpl(run_loop_), &request));
darin (slow to review) 2014/07/29 16:22:05 did you want to use WeakBindToRequest here? BindTo
Sam McNally 2014/07/30 00:18:08 Done.
144 } 142 }
145 143
146 private: 144 private:
147 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController); 145 DISALLOW_COPY_AND_ASSIGN(PingTestWebUIController);
148 }; 146 };
149 147
150 // WebUIControllerFactory that creates TestWebUIController. 148 // WebUIControllerFactory that creates TestWebUIController.
151 class TestWebUIControllerFactory : public WebUIControllerFactory { 149 class TestWebUIControllerFactory : public WebUIControllerFactory {
152 public: 150 public:
153 TestWebUIControllerFactory() : run_loop_(NULL) {} 151 TestWebUIControllerFactory() : run_loop_(NULL) {}
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 NavigateToURL(other_shell, test_url); 229 NavigateToURL(other_shell, test_url);
232 // RunLoop is quit when message received from page. 230 // RunLoop is quit when message received from page.
233 other_run_loop.Run(); 231 other_run_loop.Run();
234 EXPECT_TRUE(got_message); 232 EXPECT_TRUE(got_message);
235 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(), 233 EXPECT_EQ(shell()->web_contents()->GetRenderProcessHost(),
236 other_shell->web_contents()->GetRenderProcessHost()); 234 other_shell->web_contents()->GetRenderProcessHost());
237 } 235 }
238 236
239 } // namespace 237 } // namespace
240 } // namespace content 238 } // namespace content
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/omnibox/omnibox_ui.cc ('k') | content/common/mojo/service_registry_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698