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 <map> | 5 #include <map> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "mojo/examples/media_viewer/media_viewer.mojom.h" | 11 #include "mojo/examples/media_viewer/media_viewer.mojom.h" |
12 #include "mojo/public/cpp/application/application_connection.h" | 12 #include "mojo/public/cpp/application/application_connection.h" |
13 #include "mojo/public/cpp/application/application_delegate.h" | 13 #include "mojo/public/cpp/application/application_delegate.h" |
14 #include "mojo/public/cpp/application/application_impl.h" | 14 #include "mojo/public/cpp/application/application_impl.h" |
15 #include "mojo/public/cpp/application/interface_factory_impl.h" | 15 #include "mojo/public/cpp/application/interface_factory_impl.h" |
16 #include "mojo/public/cpp/bindings/interface_impl.h" | 16 #include "mojo/public/cpp/bindings/interface_impl.h" |
17 #include "mojo/services/public/cpp/view_manager/node.h" | 17 #include "mojo/services/public/cpp/view_manager/view.h" |
18 #include "mojo/services/public/cpp/view_manager/node_observer.h" | |
19 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
20 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
21 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 21 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
22 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 22 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
23 #include "mojo/views/native_widget_view_manager.h" | 23 #include "mojo/views/native_widget_view_manager.h" |
24 #include "mojo/views/views_init.h" | 24 #include "mojo/views/views_init.h" |
25 #include "skia/ext/platform_canvas.h" | 25 #include "skia/ext/platform_canvas.h" |
26 #include "skia/ext/refptr.h" | 26 #include "skia/ext/refptr.h" |
27 #include "third_party/skia/include/core/SkBitmap.h" | 27 #include "third_party/skia/include/core/SkBitmap.h" |
28 #include "third_party/skia/include/core/SkCanvas.h" | 28 #include "third_party/skia/include/core/SkCanvas.h" |
29 #include "third_party/skia/include/core/SkColor.h" | 29 #include "third_party/skia/include/core/SkColor.h" |
30 #include "third_party/skia/include/core/SkPaint.h" | 30 #include "third_party/skia/include/core/SkPaint.h" |
31 #include "third_party/skia/include/core/SkRect.h" | 31 #include "third_party/skia/include/core/SkRect.h" |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 public: | 129 public: |
130 virtual ~Delegate() {} | 130 virtual ~Delegate() {} |
131 | 131 |
132 virtual void ButtonPressed(ControlType type) = 0; | 132 virtual void ButtonPressed(ControlType type) = 0; |
133 }; | 133 }; |
134 | 134 |
135 ControlPanel(Delegate* delegate) : delegate_(delegate), buttons_() {} | 135 ControlPanel(Delegate* delegate) : delegate_(delegate), buttons_() {} |
136 | 136 |
137 virtual ~ControlPanel() {} | 137 virtual ~ControlPanel() {} |
138 | 138 |
139 void Initialize(Node* node) { | 139 void Initialize(View* view) { |
140 const char* kNames[] = { "Zoom In", "Actual Size", "Zoom Out" }; | 140 const char* kNames[] = { "Zoom In", "Actual Size", "Zoom Out" }; |
141 | 141 |
142 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 142 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
143 | 143 |
144 widget_delegate->GetContentsView()->SetLayoutManager( | 144 widget_delegate->GetContentsView()->SetLayoutManager( |
145 new views::BoxLayout(views::BoxLayout::kHorizontal, 5, 2, 5)); | 145 new views::BoxLayout(views::BoxLayout::kHorizontal, 5, 2, 5)); |
146 | 146 |
147 widget_delegate->GetContentsView()->set_background( | 147 widget_delegate->GetContentsView()->set_background( |
148 views::Background::CreateSolidBackground(SK_ColorLTGRAY)); | 148 views::Background::CreateSolidBackground(SK_ColorLTGRAY)); |
149 | 149 |
150 for (int type = 0; type < CONTROL_COUNT; ++type) { | 150 for (int type = 0; type < CONTROL_COUNT; ++type) { |
151 views::Button* button = new views::LabelButton( | 151 views::Button* button = new views::LabelButton( |
152 this, base::ASCIIToUTF16(kNames[type])); | 152 this, base::ASCIIToUTF16(kNames[type])); |
153 button->SetBorder(scoped_ptr<views::Border>(new CustomButtonBorder)); | 153 button->SetBorder(scoped_ptr<views::Border>(new CustomButtonBorder)); |
154 buttons_[type] = button; | 154 buttons_[type] = button; |
155 widget_delegate->GetContentsView()->AddChildView(button); | 155 widget_delegate->GetContentsView()->AddChildView(button); |
156 } | 156 } |
157 | 157 |
158 views::Widget* widget = new views::Widget; | 158 views::Widget* widget = new views::Widget; |
159 views::Widget::InitParams params( | 159 views::Widget::InitParams params( |
160 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 160 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
161 params.native_widget = new NativeWidgetViewManager(widget, node); | 161 params.native_widget = new NativeWidgetViewManager(widget, view); |
162 params.delegate = widget_delegate; | 162 params.delegate = widget_delegate; |
163 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); | 163 params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height()); |
164 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW; | 164 params.opacity = views::Widget::InitParams::OPAQUE_WINDOW; |
165 widget->Init(params); | 165 widget->Init(params); |
166 widget->Show(); | 166 widget->Show(); |
167 } | 167 } |
168 | 168 |
169 private: | 169 private: |
170 // Overridden from views::ButtonListener: | 170 // Overridden from views::ButtonListener: |
171 virtual void ButtonPressed(views::Button* sender, | 171 virtual void ButtonPressed(views::Button* sender, |
172 const ui::Event& event) OVERRIDE { | 172 const ui::Event& event) OVERRIDE { |
173 for (int i = 0; i < CONTROL_COUNT; ++i) { | 173 for (int i = 0; i < CONTROL_COUNT; ++i) { |
(...skipping 11 matching lines...) Expand all Loading... |
185 }; | 185 }; |
186 | 186 |
187 class NavigatorImpl : public InterfaceImpl<Navigator> { | 187 class NavigatorImpl : public InterfaceImpl<Navigator> { |
188 public: | 188 public: |
189 explicit NavigatorImpl(MediaViewer* viewer) : viewer_(viewer) {} | 189 explicit NavigatorImpl(MediaViewer* viewer) : viewer_(viewer) {} |
190 virtual ~NavigatorImpl() {} | 190 virtual ~NavigatorImpl() {} |
191 | 191 |
192 private: | 192 private: |
193 // Overridden from Navigator: | 193 // Overridden from Navigator: |
194 virtual void Navigate( | 194 virtual void Navigate( |
195 uint32_t node_id, | 195 uint32_t view_id, |
196 NavigationDetailsPtr navigation_details, | 196 NavigationDetailsPtr navigation_details, |
197 ResponseDetailsPtr response_details) OVERRIDE; | 197 ResponseDetailsPtr response_details) OVERRIDE; |
198 | 198 |
199 MediaViewer* viewer_; | 199 MediaViewer* viewer_; |
200 | 200 |
201 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 201 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
202 }; | 202 }; |
203 | 203 |
204 class MediaViewer | 204 class MediaViewer |
205 : public ApplicationDelegate, | 205 : public ApplicationDelegate, |
206 public ViewManagerDelegate, | 206 public ViewManagerDelegate, |
207 public ControlPanel::Delegate, | 207 public ControlPanel::Delegate, |
208 public NodeObserver { | 208 public ViewObserver { |
209 public: | 209 public: |
210 MediaViewer() | 210 MediaViewer() |
211 : navigator_factory_(this), | 211 : navigator_factory_(this), |
212 view_manager_client_factory_(this), | 212 view_manager_client_factory_(this), |
213 app_(NULL), | 213 app_(NULL), |
214 view_manager_(NULL), | 214 view_manager_(NULL), |
215 root_node_(NULL), | 215 root_view_(NULL), |
216 control_node_(NULL), | 216 control_view_(NULL), |
217 content_node_(NULL), | 217 content_view_(NULL), |
218 control_panel_(this) { | 218 control_panel_(this) { |
219 handler_map_["image/png"] = "mojo:mojo_png_viewer"; | 219 handler_map_["image/png"] = "mojo:mojo_png_viewer"; |
220 } | 220 } |
221 | 221 |
222 virtual ~MediaViewer() { | 222 virtual ~MediaViewer() { |
223 if (root_node_) | 223 if (root_view_) |
224 root_node_->RemoveObserver(this); | 224 root_view_->RemoveObserver(this); |
225 } | 225 } |
226 | 226 |
227 void Navigate( | 227 void Navigate( |
228 uint32_t node_id, | 228 uint32_t view_id, |
229 NavigationDetailsPtr navigation_details, | 229 NavigationDetailsPtr navigation_details, |
230 ResponseDetailsPtr response_details) { | 230 ResponseDetailsPtr response_details) { |
231 // TODO(yzshen): This shouldn't be needed once FIFO is ready. | 231 // TODO(yzshen): This shouldn't be needed once FIFO is ready. |
232 if (!view_manager_) { | 232 if (!view_manager_) { |
233 pending_navigate_request_.reset(new PendingNavigateRequest); | 233 pending_navigate_request_.reset(new PendingNavigateRequest); |
234 pending_navigate_request_->node_id = node_id; | 234 pending_navigate_request_->view_id = view_id; |
235 pending_navigate_request_->navigation_details = navigation_details.Pass(); | 235 pending_navigate_request_->navigation_details = navigation_details.Pass(); |
236 pending_navigate_request_->response_details = response_details.Pass(); | 236 pending_navigate_request_->response_details = response_details.Pass(); |
237 | 237 |
238 return; | 238 return; |
239 } | 239 } |
240 | 240 |
241 std::string handler = GetHandlerForContentType( | 241 std::string handler = GetHandlerForContentType( |
242 response_details->response->mime_type); | 242 response_details->response->mime_type); |
243 if (handler.empty()) | 243 if (handler.empty()) |
244 return; | 244 return; |
245 | 245 |
246 content_node_->Embed(handler); | 246 content_view_->Embed(handler); |
247 | 247 |
248 if (navigation_details) { | 248 if (navigation_details) { |
249 NavigatorPtr navigator; | 249 NavigatorPtr navigator; |
250 app_->ConnectToService(handler, &navigator); | 250 app_->ConnectToService(handler, &navigator); |
251 navigator->Navigate(content_node_->id(), navigation_details.Pass(), | 251 navigator->Navigate(content_view_->id(), navigation_details.Pass(), |
252 response_details.Pass()); | 252 response_details.Pass()); |
253 } | 253 } |
254 | 254 |
255 // TODO(yzshen): determine the set of controls to show based on what | 255 // TODO(yzshen): determine the set of controls to show based on what |
256 // interfaces the embedded app provides. | 256 // interfaces the embedded app provides. |
257 app_->ConnectToService(handler, &zoomable_media_); | 257 app_->ConnectToService(handler, &zoomable_media_); |
258 } | 258 } |
259 | 259 |
260 private: | 260 private: |
261 typedef std::map<std::string, std::string> HandlerMap; | 261 typedef std::map<std::string, std::string> HandlerMap; |
262 | 262 |
263 struct PendingNavigateRequest { | 263 struct PendingNavigateRequest { |
264 uint32_t node_id; | 264 uint32_t view_id; |
265 NavigationDetailsPtr navigation_details; | 265 NavigationDetailsPtr navigation_details; |
266 ResponseDetailsPtr response_details; | 266 ResponseDetailsPtr response_details; |
267 }; | 267 }; |
268 | 268 |
269 | 269 |
270 // Overridden from ApplicationDelegate: | 270 // Overridden from ApplicationDelegate: |
271 virtual void Initialize(ApplicationImpl* app) OVERRIDE { | 271 virtual void Initialize(ApplicationImpl* app) OVERRIDE { |
272 app_ = app; | 272 app_ = app; |
273 views_init_.reset(new ViewsInit); | 273 views_init_.reset(new ViewsInit); |
274 } | 274 } |
275 | 275 |
276 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 276 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
277 OVERRIDE { | 277 OVERRIDE { |
278 connection->AddService(&navigator_factory_); | 278 connection->AddService(&navigator_factory_); |
279 connection->AddService(&view_manager_client_factory_); | 279 connection->AddService(&view_manager_client_factory_); |
280 return true; | 280 return true; |
281 } | 281 } |
282 | 282 |
283 void LayoutNodes() { | 283 void LayoutViews() { |
284 Node* root = content_node_->parent(); | 284 View* root = content_view_->parent(); |
285 gfx::Rect control_bounds(root->bounds().width(), 28); | 285 gfx::Rect control_bounds(root->bounds().width(), 28); |
286 control_node_->SetBounds(control_bounds); | 286 control_view_->SetBounds(control_bounds); |
287 gfx::Rect content_bounds(0, control_bounds.height(), root->bounds().width(), | 287 gfx::Rect content_bounds(0, control_bounds.height(), root->bounds().width(), |
288 root->bounds().height() - control_bounds.height()); | 288 root->bounds().height() - control_bounds.height()); |
289 content_node_->SetBounds(content_bounds); | 289 content_view_->SetBounds(content_bounds); |
290 } | 290 } |
291 | 291 |
292 // Overridden from ViewManagerDelegate: | 292 // Overridden from ViewManagerDelegate: |
293 virtual void OnEmbed(ViewManager* view_manager, | 293 virtual void OnEmbed(ViewManager* view_manager, |
294 Node* root, | 294 View* root, |
295 ServiceProviderImpl* exported_services, | 295 ServiceProviderImpl* exported_services, |
296 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 296 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
297 root_node_ = root; | 297 root_view_ = root; |
298 view_manager_ = view_manager; | 298 view_manager_ = view_manager; |
299 | 299 |
300 control_node_ = Node::Create(view_manager_); | 300 control_view_ = View::Create(view_manager_); |
301 root_node_->AddChild(control_node_); | 301 root_view_->AddChild(control_view_); |
302 | 302 |
303 content_node_ = Node::Create(view_manager_); | 303 content_view_ = View::Create(view_manager_); |
304 root_node_->AddChild(content_node_); | 304 root_view_->AddChild(content_view_); |
305 | 305 |
306 control_panel_.Initialize(control_node_); | 306 control_panel_.Initialize(control_view_); |
307 | 307 |
308 LayoutNodes(); | 308 LayoutViews(); |
309 root_node_->AddObserver(this); | 309 root_view_->AddObserver(this); |
310 | 310 |
311 if (pending_navigate_request_) { | 311 if (pending_navigate_request_) { |
312 scoped_ptr<PendingNavigateRequest> request( | 312 scoped_ptr<PendingNavigateRequest> request( |
313 pending_navigate_request_.release()); | 313 pending_navigate_request_.release()); |
314 | 314 |
315 Navigate(request->node_id, request->navigation_details.Pass(), | 315 Navigate(request->view_id, request->navigation_details.Pass(), |
316 request->response_details.Pass()); | 316 request->response_details.Pass()); |
317 } | 317 } |
318 } | 318 } |
319 virtual void OnViewManagerDisconnected( | 319 virtual void OnViewManagerDisconnected( |
320 ViewManager* view_manager) OVERRIDE { | 320 ViewManager* view_manager) OVERRIDE { |
321 DCHECK_EQ(view_manager_, view_manager); | 321 DCHECK_EQ(view_manager_, view_manager); |
322 view_manager_ = NULL; | 322 view_manager_ = NULL; |
323 base::MessageLoop::current()->Quit(); | 323 base::MessageLoop::current()->Quit(); |
324 } | 324 } |
325 | 325 |
326 // Overridden from ControlPanel::Delegate: | 326 // Overridden from ControlPanel::Delegate: |
327 virtual void ButtonPressed(ControlPanel::ControlType type) OVERRIDE { | 327 virtual void ButtonPressed(ControlPanel::ControlType type) OVERRIDE { |
328 switch (type) { | 328 switch (type) { |
329 case ControlPanel::CONTROL_ZOOM_IN: | 329 case ControlPanel::CONTROL_ZOOM_IN: |
330 zoomable_media_->ZoomIn(); | 330 zoomable_media_->ZoomIn(); |
331 break; | 331 break; |
332 case ControlPanel::CONTROL_ACTUAL_SIZE: | 332 case ControlPanel::CONTROL_ACTUAL_SIZE: |
333 zoomable_media_->ZoomToActualSize(); | 333 zoomable_media_->ZoomToActualSize(); |
334 break; | 334 break; |
335 case ControlPanel::CONTROL_ZOOM_OUT: | 335 case ControlPanel::CONTROL_ZOOM_OUT: |
336 zoomable_media_->ZoomOut(); | 336 zoomable_media_->ZoomOut(); |
337 break; | 337 break; |
338 default: | 338 default: |
339 NOTIMPLEMENTED(); | 339 NOTIMPLEMENTED(); |
340 } | 340 } |
341 } | 341 } |
342 | 342 |
343 // NodeObserver: | 343 // ViewObserver: |
344 virtual void OnNodeBoundsChanged(Node* node, | 344 virtual void OnViewBoundsChanged(View* view, |
345 const gfx::Rect& old_bounds, | 345 const gfx::Rect& old_bounds, |
346 const gfx::Rect& new_bounds) OVERRIDE { | 346 const gfx::Rect& new_bounds) OVERRIDE { |
347 LayoutNodes(); | 347 LayoutViews(); |
348 } | 348 } |
349 virtual void OnNodeDestroyed(Node* node) OVERRIDE { | 349 virtual void OnViewDestroyed(View* view) OVERRIDE { |
350 DCHECK_EQ(node, root_node_); | 350 DCHECK_EQ(view, root_view_); |
351 node->RemoveObserver(this); | 351 view->RemoveObserver(this); |
352 root_node_ = NULL; | 352 root_view_ = NULL; |
353 } | 353 } |
354 | 354 |
355 std::string GetHandlerForContentType(const std::string& content_type) { | 355 std::string GetHandlerForContentType(const std::string& content_type) { |
356 HandlerMap::const_iterator it = handler_map_.find(content_type); | 356 HandlerMap::const_iterator it = handler_map_.find(content_type); |
357 return it != handler_map_.end() ? it->second : std::string(); | 357 return it != handler_map_.end() ? it->second : std::string(); |
358 } | 358 } |
359 | 359 |
360 InterfaceFactoryImplWithContext<NavigatorImpl, MediaViewer> | 360 InterfaceFactoryImplWithContext<NavigatorImpl, MediaViewer> |
361 navigator_factory_; | 361 navigator_factory_; |
362 ViewManagerClientFactory view_manager_client_factory_; | 362 ViewManagerClientFactory view_manager_client_factory_; |
363 | 363 |
364 ApplicationImpl* app_; | 364 ApplicationImpl* app_; |
365 scoped_ptr<ViewsInit> views_init_; | 365 scoped_ptr<ViewsInit> views_init_; |
366 ViewManager* view_manager_; | 366 ViewManager* view_manager_; |
367 Node* root_node_; | 367 View* root_view_; |
368 Node* control_node_; | 368 View* control_view_; |
369 Node* content_node_; | 369 View* content_view_; |
370 ControlPanel control_panel_; | 370 ControlPanel control_panel_; |
371 ZoomableMediaPtr zoomable_media_; | 371 ZoomableMediaPtr zoomable_media_; |
372 HandlerMap handler_map_; | 372 HandlerMap handler_map_; |
373 scoped_ptr<PendingNavigateRequest> pending_navigate_request_; | 373 scoped_ptr<PendingNavigateRequest> pending_navigate_request_; |
374 | 374 |
375 DISALLOW_COPY_AND_ASSIGN(MediaViewer); | 375 DISALLOW_COPY_AND_ASSIGN(MediaViewer); |
376 }; | 376 }; |
377 | 377 |
378 void NavigatorImpl::Navigate( | 378 void NavigatorImpl::Navigate( |
379 uint32_t node_id, | 379 uint32_t view_id, |
380 NavigationDetailsPtr navigation_details, | 380 NavigationDetailsPtr navigation_details, |
381 ResponseDetailsPtr response_details) { | 381 ResponseDetailsPtr response_details) { |
382 viewer_->Navigate(node_id, navigation_details.Pass(), | 382 viewer_->Navigate(view_id, navigation_details.Pass(), |
383 response_details.Pass()); | 383 response_details.Pass()); |
384 } | 384 } |
385 | 385 |
386 } // namespace examples | 386 } // namespace examples |
387 | 387 |
388 // static | 388 // static |
389 ApplicationDelegate* ApplicationDelegate::Create() { | 389 ApplicationDelegate* ApplicationDelegate::Create() { |
390 return new examples::MediaViewer; | 390 return new examples::MediaViewer; |
391 } | 391 } |
392 | 392 |
393 } // namespace mojo | 393 } // namespace mojo |
OLD | NEW |