OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mandoline/ui/browser/browser.h" | 5 #include "mandoline/ui/browser/browser.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "components/view_manager/public/cpp/view.h" | 9 #include "components/view_manager/public/cpp/view.h" |
10 #include "components/view_manager/public/cpp/view_manager_init.h" | 10 #include "components/view_manager/public/cpp/view_manager_init.h" |
11 #include "mandoline/tab/frame.h" | 11 #include "mandoline/tab/frame.h" |
12 #include "mandoline/tab/frame_services.h" | 12 #include "mandoline/tab/frame_connection.h" |
13 #include "mandoline/tab/frame_tree.h" | 13 #include "mandoline/tab/frame_tree.h" |
14 #include "mandoline/ui/browser/browser_ui.h" | 14 #include "mandoline/ui/browser/browser_ui.h" |
15 #include "mandoline/ui/browser/merged_service_provider.h" | |
16 #include "mojo/application/public/cpp/application_runner.h" | 15 #include "mojo/application/public/cpp/application_runner.h" |
17 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
18 #include "mojo/converters/geometry/geometry_type_converters.h" | 17 #include "mojo/converters/geometry/geometry_type_converters.h" |
19 #include "third_party/mojo/src/mojo/public/c/system/main.h" | 18 #include "third_party/mojo/src/mojo/public/c/system/main.h" |
20 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
21 | 20 |
22 namespace mandoline { | 21 namespace mandoline { |
23 namespace { | 22 namespace { |
24 | 23 |
25 gfx::Size GetInitialViewportSize() { | 24 gfx::Size GetInitialViewportSize() { |
26 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) |
27 // Resize to match the Nexus 5 aspect ratio: | 26 // Resize to match the Nexus 5 aspect ratio: |
28 return gfx::Size(320, 640); | 27 return gfx::Size(320, 640); |
29 #else | 28 #else |
30 return gfx::Size(1280, 800); | 29 return gfx::Size(1280, 800); |
31 #endif | 30 #endif |
32 } | 31 } |
33 | 32 |
34 } // namespace | 33 } // namespace |
35 | 34 |
36 Browser::Browser() | 35 Browser::Browser() |
37 : root_(nullptr), | 36 : root_(nullptr), |
38 content_(nullptr), | 37 content_(nullptr), |
39 omnibox_(nullptr), | 38 omnibox_(nullptr), |
40 navigator_host_(this), | 39 navigator_host_(this), |
41 app_(nullptr) { | 40 app_(nullptr) { |
42 exposed_services_impl_.AddService<mojo::NavigatorHost>(this); | |
43 } | 41 } |
44 | 42 |
45 Browser::~Browser() { | 43 Browser::~Browser() { |
46 // Destruct ui_ manually while |this| is alive and reset the pointer first. | 44 // Destruct ui_ manually while |this| is alive and reset the pointer first. |
47 // This is to avoid a double delete when OnViewManagerDestroyed gets | 45 // This is to avoid a double delete when OnViewManagerDestroyed gets |
48 // called. | 46 // called. |
49 delete ui_.release(); | 47 delete ui_.release(); |
50 } | 48 } |
51 | 49 |
52 void Browser::ReplaceContentWithRequest(mojo::URLRequestPtr request) { | 50 void Browser::ReplaceContentWithRequest(mojo::URLRequestPtr request) { |
53 Embed(request.Pass(), nullptr, nullptr); | 51 Embed(request.Pass()); |
54 } | 52 } |
55 | 53 |
56 void Browser::Initialize(mojo::ApplicationImpl* app) { | 54 void Browser::Initialize(mojo::ApplicationImpl* app) { |
57 app_ = app; | 55 app_ = app; |
58 view_manager_init_.reset(new mojo::ViewManagerInit(app, this, this)); | 56 view_manager_init_.reset(new mojo::ViewManagerInit(app, this, this)); |
59 | 57 |
60 ui_.reset(BrowserUI::Create(this, app)); | 58 ui_.reset(BrowserUI::Create(this, app)); |
61 | 59 |
62 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 60 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
63 base::CommandLine::StringVector args = command_line->GetArgs(); | 61 base::CommandLine::StringVector args = command_line->GetArgs(); |
(...skipping 13 matching lines...) Expand all Loading... |
77 // TODO: register embed interface here. | 75 // TODO: register embed interface here. |
78 return true; | 76 return true; |
79 } | 77 } |
80 | 78 |
81 bool Browser::ConfigureOutgoingConnection( | 79 bool Browser::ConfigureOutgoingConnection( |
82 mojo::ApplicationConnection* connection) { | 80 mojo::ApplicationConnection* connection) { |
83 connection->AddService<ViewEmbedder>(this); | 81 connection->AddService<ViewEmbedder>(this); |
84 return true; | 82 return true; |
85 } | 83 } |
86 | 84 |
87 void Browser::OnEmbed( | 85 void Browser::OnEmbed(mojo::View* root) { |
88 mojo::View* root, | |
89 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
90 mojo::ServiceProviderPtr exposed_services) { | |
91 // Browser does not support being embedded more than once. | 86 // Browser does not support being embedded more than once. |
92 CHECK(!root_); | 87 CHECK(!root_); |
93 | 88 |
94 // Make it so we get OnWillEmbed() for any Embed()s done by other apps we | 89 // Make it so we get OnWillEmbed() for any Embed()s done by other apps we |
95 // Embed(). | 90 // Embed(). |
96 root->view_manager()->SetEmbedRoot(); | 91 root->view_manager()->SetEmbedRoot(); |
97 | 92 |
98 // TODO(beng): still unhappy with the fact that both this class & the UI class | 93 // TODO(beng): still unhappy with the fact that both this class & the UI class |
99 // know so much about these views. Figure out how to shift more to | 94 // know so much about these views. Figure out how to shift more to |
100 // the UI class. | 95 // the UI class. |
101 root_ = root; | 96 root_ = root; |
102 content_ = root->view_manager()->CreateView(); | 97 content_ = root->view_manager()->CreateView(); |
103 ui_->Init(root_); | 98 ui_->Init(root_); |
104 | 99 |
105 view_manager_init_->view_manager_root()->SetViewportSize( | 100 view_manager_init_->view_manager_root()->SetViewportSize( |
106 mojo::Size::From(GetInitialViewportSize())); | 101 mojo::Size::From(GetInitialViewportSize())); |
107 | 102 |
108 root_->AddChild(content_); | 103 root_->AddChild(content_); |
109 content_->SetVisible(true); | 104 content_->SetVisible(true); |
110 | 105 |
111 view_manager_init_->view_manager_root()->AddAccelerator( | 106 view_manager_init_->view_manager_root()->AddAccelerator( |
112 mojo::KEYBOARD_CODE_BROWSER_BACK, mojo::EVENT_FLAGS_NONE); | 107 mojo::KEYBOARD_CODE_BROWSER_BACK, mojo::EVENT_FLAGS_NONE); |
113 | 108 |
114 // Now that we're ready, either load a pending url or the default url. | 109 // Now that we're ready, either load a pending url or the default url. |
115 if (pending_request_) { | 110 if (pending_request_) { |
116 Embed(pending_request_.Pass(), services.Pass(), exposed_services.Pass()); | 111 Embed(pending_request_.Pass()); |
117 } else if (!default_url_.empty()) { | 112 } else if (!default_url_.empty()) { |
118 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 113 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
119 request->url = mojo::String::From(default_url_); | 114 request->url = mojo::String::From(default_url_); |
120 Embed(request.Pass(), services.Pass(), exposed_services.Pass()); | 115 Embed(request.Pass()); |
121 } | 116 } |
122 } | 117 } |
123 | 118 |
124 bool Browser::OnWillEmbed( | 119 void Browser::OnEmbedForDescendant(mojo::View* view, |
125 mojo::View* view, | 120 mojo::URLRequestPtr request, |
126 mojo::InterfaceRequest<mojo::ServiceProvider>* services, | 121 mojo::ViewManagerClientPtr* client) { |
127 mojo::ServiceProviderPtr* exposed_services) { | |
128 // TODO(sky): move this to Frame/FrameTree. | 122 // TODO(sky): move this to Frame/FrameTree. |
129 Frame* frame = Frame::FindFirstFrameAncestor(view); | 123 Frame* frame = Frame::FindFirstFrameAncestor(view); |
130 if (!frame) { | 124 if (!frame) { |
131 // TODO(sky): add requestor url so that we can return false if it's not | 125 // TODO(sky): add requestor url so that we can return false if it's not |
132 // an app we expect. | 126 // an app we expect. |
133 return true; | 127 mojo::ApplicationConnection* connection = |
| 128 app_->ConnectToApplication(request.Pass()); |
| 129 connection->ConnectToService(client); |
| 130 return; |
134 } | 131 } |
135 | 132 |
136 Frame* parent = frame; | 133 Frame* parent = frame; |
137 if (frame->view() == view) { | 134 if (frame->view() == view) { |
138 parent = frame->parent(); | 135 parent = frame->parent(); |
139 // This is a reembed. | 136 // This is a reembed. |
140 delete frame; | 137 delete frame; |
141 frame = nullptr; | 138 frame = nullptr; |
142 } | 139 } |
143 | 140 |
144 scoped_ptr<FrameServices> frame_services(new FrameServices); | 141 scoped_ptr<FrameConnection> frame_connection(new FrameConnection); |
145 frame_services->Init(services, exposed_services); | 142 frame_connection->Init(app_, request.Pass(), client); |
146 FrameTreeClient* frame_tree_client = frame_services->frame_tree_client(); | 143 frame_tree_->CreateAndAddFrame(view, parent, |
147 frame_tree_->CreateAndAddFrame(view, parent, frame_tree_client, | 144 frame_connection->frame_tree_client(), |
148 frame_services.Pass()); | 145 frame_connection.Pass()); |
149 return true; | |
150 } | 146 } |
151 | 147 |
152 void Browser::OnViewManagerDestroyed(mojo::ViewManager* view_manager) { | 148 void Browser::OnViewManagerDestroyed(mojo::ViewManager* view_manager) { |
153 ui_.reset(); | 149 ui_.reset(); |
154 root_ = nullptr; | 150 root_ = nullptr; |
155 app_->Terminate(); | 151 app_->Terminate(); |
156 } | 152 } |
157 | 153 |
158 void Browser::OnAccelerator(mojo::EventPtr event) { | 154 void Browser::OnAccelerator(mojo::EventPtr event) { |
159 DCHECK_EQ(mojo::KEYBOARD_CODE_BROWSER_BACK, | 155 DCHECK_EQ(mojo::KEYBOARD_CODE_BROWSER_BACK, |
160 event->key_data->windows_key_code); | 156 event->key_data->windows_key_code); |
161 navigator_host_.RequestNavigateHistory(-1); | 157 navigator_host_.RequestNavigateHistory(-1); |
162 } | 158 } |
163 | 159 |
164 void Browser::OpenURL(const mojo::String& url) { | 160 void Browser::OpenURL(const mojo::String& url) { |
165 omnibox_->SetVisible(false); | 161 omnibox_->SetVisible(false); |
166 mojo::URLRequestPtr request(mojo::URLRequest::New()); | 162 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
167 request->url = mojo::String::From(url); | 163 request->url = mojo::String::From(url); |
168 ReplaceContentWithRequest(request.Pass()); | 164 ReplaceContentWithRequest(request.Pass()); |
169 } | 165 } |
170 | 166 |
171 void Browser::Embed(mojo::URLRequestPtr request, | 167 void Browser::Embed(mojo::URLRequestPtr request) { |
172 mojo::InterfaceRequest<mojo::ServiceProvider> services, | 168 const std::string string_url = request->url.To<std::string>(); |
173 mojo::ServiceProviderPtr exposed_services) { | |
174 std::string string_url = request->url.To<std::string>(); | |
175 if (string_url == "mojo:omnibox") { | 169 if (string_url == "mojo:omnibox") { |
176 ShowOmnibox(request.Pass(), services.Pass(), exposed_services.Pass()); | 170 ShowOmnibox(request.Pass()); |
177 return; | 171 return; |
178 } | 172 } |
179 | 173 |
180 // We can get Embed calls before we've actually been | 174 // We can get Embed calls before we've actually been |
181 // embedded into the root view and content_ is created. | 175 // embedded into the root view and content_ is created. |
182 // Just save the last url, we'll embed it when we're ready. | 176 // Just save the last url, we'll embed it when we're ready. |
183 if (!content_) { | 177 if (!content_) { |
184 pending_request_ = request.Pass(); | 178 pending_request_ = request.Pass(); |
185 return; | 179 return; |
186 } | 180 } |
187 | 181 |
188 GURL gurl(string_url); | 182 GURL gurl(string_url); |
189 bool changed = current_url_ != gurl; | 183 bool changed = current_url_ != gurl; |
190 current_url_ = gurl; | 184 current_url_ = gurl; |
191 if (changed) | 185 if (changed) |
192 ui_->OnURLChanged(); | 186 ui_->OnURLChanged(); |
193 | 187 |
194 merged_service_provider_.reset( | 188 scoped_ptr<FrameConnection> frame_connection(new FrameConnection); |
195 new MergedServiceProvider(exposed_services.Pass(), this)); | 189 mojo::ViewManagerClientPtr view_manager_client; |
196 scoped_ptr<FrameServices> frame_services(new FrameServices); | 190 frame_connection->Init(app_, request.Pass(), &view_manager_client); |
197 // TODO(sky): FrameServices and MergedServiceProvider need to be combined. | 191 frame_connection->application_connection()->AddService<mojo::NavigatorHost>( |
198 // TODO(sky): FrameServices needs to man in the middle services. | 192 this); |
199 frame_services->Init(&services, nullptr); | 193 frame_tree_.reset(new FrameTree(content_, nullptr, |
200 FrameTreeClient* frame_tree_client = frame_services->frame_tree_client(); | 194 frame_connection->frame_tree_client(), |
201 frame_tree_.reset(new FrameTree(content_, nullptr, frame_tree_client, | 195 frame_connection.Pass())); |
202 frame_services.Pass())); | 196 content_->Embed(view_manager_client.Pass()); |
203 content_->Embed(request.Pass(), services.Pass(), | |
204 merged_service_provider_->GetServiceProviderPtr().Pass()); | |
205 | 197 |
206 navigator_host_.RecordNavigation(gurl.spec()); | 198 navigator_host_.RecordNavigation(gurl.spec()); |
207 } | 199 } |
208 | 200 |
209 void Browser::Create(mojo::ApplicationConnection* connection, | 201 void Browser::Create(mojo::ApplicationConnection* connection, |
210 mojo::InterfaceRequest<mojo::NavigatorHost> request) { | 202 mojo::InterfaceRequest<mojo::NavigatorHost> request) { |
211 navigator_host_.Bind(request.Pass()); | 203 navigator_host_.Bind(request.Pass()); |
212 } | 204 } |
213 | 205 |
214 void Browser::Create(mojo::ApplicationConnection* connection, | 206 void Browser::Create(mojo::ApplicationConnection* connection, |
215 mojo::InterfaceRequest<ViewEmbedder> request) { | 207 mojo::InterfaceRequest<ViewEmbedder> request) { |
216 view_embedder_bindings_.AddBinding(this, request.Pass()); | 208 view_embedder_bindings_.AddBinding(this, request.Pass()); |
217 } | 209 } |
218 | 210 |
219 void Browser::ShowOmnibox( | 211 void Browser::ShowOmnibox(mojo::URLRequestPtr request) { |
220 mojo::URLRequestPtr request, | |
221 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
222 mojo::ServiceProviderPtr exposed_services) { | |
223 if (!omnibox_) { | 212 if (!omnibox_) { |
224 omnibox_ = root_->view_manager()->CreateView(); | 213 omnibox_ = root_->view_manager()->CreateView(); |
225 root_->AddChild(omnibox_); | 214 root_->AddChild(omnibox_); |
226 omnibox_->SetVisible(true); | 215 omnibox_->SetVisible(true); |
227 omnibox_->SetBounds(root_->bounds()); | 216 omnibox_->SetBounds(root_->bounds()); |
228 } | 217 } |
229 omnibox_->Embed(request.Pass(), services.Pass(), exposed_services.Pass()); | 218 mojo::ViewManagerClientPtr view_manager_client; |
| 219 mojo::ApplicationConnection* connection = |
| 220 app_->ConnectToApplication(request.Pass()); |
| 221 connection->ConnectToService(&view_manager_client); |
| 222 omnibox_->Embed(view_manager_client.Pass()); |
230 } | 223 } |
231 | 224 |
232 } // namespace mandoline | 225 } // namespace mandoline |
OLD | NEW |