| 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/bindings/interface_impl.h" | 15 #include "mojo/public/cpp/bindings/interface_impl.h" |
| 16 #include "mojo/services/public/cpp/view_manager/node.h" | 16 #include "mojo/services/public/cpp/view_manager/node.h" |
| 17 #include "mojo/services/public/cpp/view_manager/node_observer.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view.h" | 18 #include "mojo/services/public/cpp/view_manager/view.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 20 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
| 21 #include "mojo/views/native_widget_view_manager.h" | 22 #include "mojo/views/native_widget_view_manager.h" |
| 22 #include "mojo/views/views_init.h" | 23 #include "mojo/views/views_init.h" |
| 23 #include "skia/ext/platform_canvas.h" | 24 #include "skia/ext/platform_canvas.h" |
| 24 #include "skia/ext/refptr.h" | 25 #include "skia/ext/refptr.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 26 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #include "third_party/skia/include/core/SkCanvas.h" | 27 #include "third_party/skia/include/core/SkCanvas.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 navigation::NavigationDetailsPtr navigation_details, | 196 navigation::NavigationDetailsPtr navigation_details, |
| 196 navigation::ResponseDetailsPtr response_details) OVERRIDE; | 197 navigation::ResponseDetailsPtr response_details) OVERRIDE; |
| 197 | 198 |
| 198 MediaViewer* viewer_; | 199 MediaViewer* viewer_; |
| 199 | 200 |
| 200 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 201 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| 201 }; | 202 }; |
| 202 | 203 |
| 203 class MediaViewer : public ApplicationDelegate, | 204 class MediaViewer : public ApplicationDelegate, |
| 204 public view_manager::ViewManagerDelegate, | 205 public view_manager::ViewManagerDelegate, |
| 205 public ControlPanel::Delegate { | 206 public ControlPanel::Delegate, |
| 207 public view_manager::NodeObserver { |
| 206 public: | 208 public: |
| 207 MediaViewer() : app_(NULL), | 209 MediaViewer() : app_(NULL), |
| 208 view_manager_(NULL), | 210 view_manager_(NULL), |
| 211 root_node_(NULL), |
| 209 control_node_(NULL), | 212 control_node_(NULL), |
| 210 content_node_(NULL), | 213 content_node_(NULL), |
| 211 control_panel_(this) { | 214 control_panel_(this) { |
| 212 handler_map_["image/png"] = "mojo:mojo_png_viewer"; | 215 handler_map_["image/png"] = "mojo:mojo_png_viewer"; |
| 213 } | 216 } |
| 214 | 217 |
| 215 virtual ~MediaViewer() {} | 218 virtual ~MediaViewer() { |
| 219 if (root_node_) |
| 220 root_node_->RemoveObserver(this); |
| 221 } |
| 216 | 222 |
| 217 void Navigate( | 223 void Navigate( |
| 218 uint32_t node_id, | 224 uint32_t node_id, |
| 219 navigation::NavigationDetailsPtr navigation_details, | 225 navigation::NavigationDetailsPtr navigation_details, |
| 220 navigation::ResponseDetailsPtr response_details) { | 226 navigation::ResponseDetailsPtr response_details) { |
| 221 // TODO(yzshen): This shouldn't be needed once FIFO is ready. | 227 // TODO(yzshen): This shouldn't be needed once FIFO is ready. |
| 222 if (!view_manager_) { | 228 if (!view_manager_) { |
| 223 pending_navigate_request_.reset(new PendingNavigateRequest); | 229 pending_navigate_request_.reset(new PendingNavigateRequest); |
| 224 pending_navigate_request_->node_id = node_id; | 230 pending_navigate_request_->node_id = node_id; |
| 225 pending_navigate_request_->navigation_details = navigation_details.Pass(); | 231 pending_navigate_request_->navigation_details = navigation_details.Pass(); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 views_init_.reset(new ViewsInit); | 269 views_init_.reset(new ViewsInit); |
| 264 } | 270 } |
| 265 | 271 |
| 266 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 272 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 267 OVERRIDE { | 273 OVERRIDE { |
| 268 connection->AddService<NavigatorImpl>(this); | 274 connection->AddService<NavigatorImpl>(this); |
| 269 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); | 275 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); |
| 270 return true; | 276 return true; |
| 271 } | 277 } |
| 272 | 278 |
| 279 void LayoutNodes() { |
| 280 view_manager::Node* root = content_node_->parent(); |
| 281 gfx::Rect control_bounds(root->bounds().width(), 28); |
| 282 control_node_->SetBounds(control_bounds); |
| 283 gfx::Rect content_bounds(0, control_bounds.height(), root->bounds().width(), |
| 284 root->bounds().height() - control_bounds.height()); |
| 285 content_node_->SetBounds(content_bounds); |
| 286 } |
| 287 |
| 273 // Overridden from view_manager::ViewManagerDelegate: | 288 // Overridden from view_manager::ViewManagerDelegate: |
| 274 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 289 virtual void OnRootAdded(view_manager::ViewManager* view_manager, |
| 275 view_manager::Node* root) OVERRIDE { | 290 view_manager::Node* root) OVERRIDE { |
| 291 root_node_ = root; |
| 276 view_manager_ = view_manager; | 292 view_manager_ = view_manager; |
| 277 | 293 |
| 278 control_node_ = view_manager::Node::Create(view_manager_); | 294 control_node_ = view_manager::Node::Create(view_manager_); |
| 279 root->AddChild(control_node_); | 295 root_node_->AddChild(control_node_); |
| 280 gfx::Rect control_bounds(root->bounds().width(), 28); | 296 |
| 281 control_node_->SetBounds(control_bounds); | 297 content_node_ = view_manager::Node::Create(view_manager_); |
| 298 root_node_->AddChild(content_node_); |
| 299 |
| 282 control_node_->SetActiveView(view_manager::View::Create(view_manager_)); | 300 control_node_->SetActiveView(view_manager::View::Create(view_manager_)); |
| 283 | |
| 284 control_panel_.Initialize(control_node_); | 301 control_panel_.Initialize(control_node_); |
| 285 | 302 |
| 286 content_node_ = view_manager::Node::Create(view_manager_); | 303 LayoutNodes(); |
| 287 root->AddChild(content_node_); | 304 root_node_->AddObserver(this); |
| 288 gfx::Rect content_bounds(0, control_bounds.height(), root->bounds().width(), | |
| 289 root->bounds().height() - control_bounds.height()); | |
| 290 content_node_->SetBounds(content_bounds); | |
| 291 | 305 |
| 292 if (pending_navigate_request_) { | 306 if (pending_navigate_request_) { |
| 293 scoped_ptr<PendingNavigateRequest> request( | 307 scoped_ptr<PendingNavigateRequest> request( |
| 294 pending_navigate_request_.release()); | 308 pending_navigate_request_.release()); |
| 295 | 309 |
| 296 Navigate(request->node_id, request->navigation_details.Pass(), | 310 Navigate(request->node_id, request->navigation_details.Pass(), |
| 297 request->response_details.Pass()); | 311 request->response_details.Pass()); |
| 298 } | 312 } |
| 299 } | 313 } |
| 300 virtual void OnViewManagerDisconnected( | 314 virtual void OnViewManagerDisconnected( |
| (...skipping 13 matching lines...) Expand all Loading... |
| 314 zoomable_media_->ZoomToActualSize(); | 328 zoomable_media_->ZoomToActualSize(); |
| 315 break; | 329 break; |
| 316 case ControlPanel::CONTROL_ZOOM_OUT: | 330 case ControlPanel::CONTROL_ZOOM_OUT: |
| 317 zoomable_media_->ZoomOut(); | 331 zoomable_media_->ZoomOut(); |
| 318 break; | 332 break; |
| 319 default: | 333 default: |
| 320 NOTIMPLEMENTED(); | 334 NOTIMPLEMENTED(); |
| 321 } | 335 } |
| 322 } | 336 } |
| 323 | 337 |
| 338 // Overridden from view_manager::NodeObserver |
| 339 virtual void OnNodeBoundsChanged(view_manager::Node* node, |
| 340 const gfx::Rect& old_bounds, |
| 341 const gfx::Rect& new_bounds) OVERRIDE { |
| 342 LayoutNodes(); |
| 343 } |
| 344 |
| 345 // Overridden from NodeObserver: |
| 346 virtual void OnNodeDestroying(view_manager::Node* node) OVERRIDE { |
| 347 DCHECK_EQ(node, root_node_); |
| 348 node->RemoveObserver(this); |
| 349 } |
| 350 |
| 324 std::string GetHandlerForContentType(const std::string& content_type) { | 351 std::string GetHandlerForContentType(const std::string& content_type) { |
| 325 HandlerMap::const_iterator it = handler_map_.find(content_type); | 352 HandlerMap::const_iterator it = handler_map_.find(content_type); |
| 326 return it != handler_map_.end() ? it->second : std::string(); | 353 return it != handler_map_.end() ? it->second : std::string(); |
| 327 } | 354 } |
| 328 | 355 |
| 329 ApplicationImpl* app_; | 356 ApplicationImpl* app_; |
| 330 scoped_ptr<ViewsInit> views_init_; | 357 scoped_ptr<ViewsInit> views_init_; |
| 331 view_manager::ViewManager* view_manager_; | 358 view_manager::ViewManager* view_manager_; |
| 359 view_manager::Node* root_node_; |
| 332 view_manager::Node* control_node_; | 360 view_manager::Node* control_node_; |
| 333 view_manager::Node* content_node_; | 361 view_manager::Node* content_node_; |
| 334 ControlPanel control_panel_; | 362 ControlPanel control_panel_; |
| 335 ZoomableMediaPtr zoomable_media_; | 363 ZoomableMediaPtr zoomable_media_; |
| 336 HandlerMap handler_map_; | 364 HandlerMap handler_map_; |
| 337 scoped_ptr<PendingNavigateRequest> pending_navigate_request_; | 365 scoped_ptr<PendingNavigateRequest> pending_navigate_request_; |
| 338 | 366 |
| 339 DISALLOW_COPY_AND_ASSIGN(MediaViewer); | 367 DISALLOW_COPY_AND_ASSIGN(MediaViewer); |
| 340 }; | 368 }; |
| 341 | 369 |
| 342 void NavigatorImpl::Navigate( | 370 void NavigatorImpl::Navigate( |
| 343 uint32_t node_id, | 371 uint32_t node_id, |
| 344 navigation::NavigationDetailsPtr navigation_details, | 372 navigation::NavigationDetailsPtr navigation_details, |
| 345 navigation::ResponseDetailsPtr response_details) { | 373 navigation::ResponseDetailsPtr response_details) { |
| 346 viewer_->Navigate(node_id, navigation_details.Pass(), | 374 viewer_->Navigate(node_id, navigation_details.Pass(), |
| 347 response_details.Pass()); | 375 response_details.Pass()); |
| 348 } | 376 } |
| 349 | 377 |
| 350 } // namespace examples | 378 } // namespace examples |
| 351 | 379 |
| 352 // static | 380 // static |
| 353 ApplicationDelegate* ApplicationDelegate::Create() { | 381 ApplicationDelegate* ApplicationDelegate::Create() { |
| 354 return new examples::MediaViewer; | 382 return new examples::MediaViewer; |
| 355 } | 383 } |
| 356 | 384 |
| 357 } // namespace mojo | 385 } // namespace mojo |
| OLD | NEW |