| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| 11 #include "mojo/public/cpp/application/application_delegate.h" | 11 #include "mojo/public/cpp/application/application_delegate.h" |
| 12 #include "mojo/public/cpp/application/application_impl.h" | 12 #include "mojo/public/cpp/application/application_impl.h" |
| 13 #include "mojo/public/cpp/application/interface_factory_impl.h" | 13 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 14 #include "mojo/services/public/cpp/view_manager/node.h" | 14 #include "mojo/services/public/cpp/view_manager/view.h" |
| 15 #include "mojo/services/public/cpp/view_manager/node_observer.h" | |
| 16 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 15 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
| 19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
| 20 #include "ui/events/event_constants.h" | 20 #include "ui/events/event_constants.h" |
| 21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 22 #include "url/url_util.h" | 22 #include "url/url_util.h" |
| 23 | 23 |
| 24 namespace mojo { | 24 namespace mojo { |
| 25 namespace examples { | 25 namespace examples { |
| 26 class EmbeddedApp; | 26 class EmbeddedApp; |
| 27 | 27 |
| 28 class NavigatorImpl : public InterfaceImpl<Navigator> { | 28 class NavigatorImpl : public InterfaceImpl<Navigator> { |
| 29 public: | 29 public: |
| 30 explicit NavigatorImpl(EmbeddedApp* app) : app_(app) {} | 30 explicit NavigatorImpl(EmbeddedApp* app) : app_(app) {} |
| 31 | 31 |
| 32 private: | 32 private: |
| 33 virtual void Navigate( | 33 virtual void Navigate( |
| 34 uint32 node_id, | 34 uint32 view_id, |
| 35 NavigationDetailsPtr navigation_details, | 35 NavigationDetailsPtr navigation_details, |
| 36 ResponseDetailsPtr response_details) OVERRIDE; | 36 ResponseDetailsPtr response_details) OVERRIDE; |
| 37 | 37 |
| 38 EmbeddedApp* app_; | 38 EmbeddedApp* app_; |
| 39 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | 39 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 class EmbeddedApp | 42 class EmbeddedApp |
| 43 : public ApplicationDelegate, | 43 : public ApplicationDelegate, |
| 44 public ViewManagerDelegate, | 44 public ViewManagerDelegate, |
| 45 public NodeObserver { | 45 public ViewObserver { |
| 46 public: | 46 public: |
| 47 EmbeddedApp() | 47 EmbeddedApp() |
| 48 : navigator_factory_(this), | 48 : navigator_factory_(this), |
| 49 view_manager_(NULL), | 49 view_manager_(NULL), |
| 50 view_manager_client_factory_(this) { | 50 view_manager_client_factory_(this) { |
| 51 url::AddStandardScheme("mojo"); | 51 url::AddStandardScheme("mojo"); |
| 52 } | 52 } |
| 53 virtual ~EmbeddedApp() {} | 53 virtual ~EmbeddedApp() {} |
| 54 | 54 |
| 55 void SetNodeColor(uint32 node_id, SkColor color) { | 55 void SetViewColor(uint32 view_id, SkColor color) { |
| 56 pending_node_colors_[node_id] = color; | 56 pending_view_colors_[view_id] = color; |
| 57 ProcessPendingNodeColor(node_id); | 57 ProcessPendingViewColor(view_id); |
| 58 } | 58 } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 | 61 |
| 62 // Overridden from ApplicationDelegate: | 62 // Overridden from ApplicationDelegate: |
| 63 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { | 63 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { |
| 64 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like | 64 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like |
| 65 // embedder should be able to specify the SP embeddee receives, then | 65 // embedder should be able to specify the SP embeddee receives, then |
| 66 // communication can be anonymous. | 66 // communication can be anonymous. |
| 67 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); | 67 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); |
| 68 } | 68 } |
| 69 | 69 |
| 70 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 70 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
| 71 MOJO_OVERRIDE { | 71 MOJO_OVERRIDE { |
| 72 connection->AddService(&view_manager_client_factory_); | 72 connection->AddService(&view_manager_client_factory_); |
| 73 connection->AddService(&navigator_factory_); | 73 connection->AddService(&navigator_factory_); |
| 74 return true; | 74 return true; |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Overridden from ViewManagerDelegate: | 77 // Overridden from ViewManagerDelegate: |
| 78 virtual void OnEmbed(ViewManager* view_manager, | 78 virtual void OnEmbed(ViewManager* view_manager, |
| 79 Node* root, | 79 View* root, |
| 80 ServiceProviderImpl* exported_services, | 80 ServiceProviderImpl* exported_services, |
| 81 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 81 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
| 82 root->AddObserver(this); | 82 root->AddObserver(this); |
| 83 roots_[root->id()] = root; | 83 roots_[root->id()] = root; |
| 84 ProcessPendingNodeColor(root->id()); | 84 ProcessPendingViewColor(root->id()); |
| 85 } | 85 } |
| 86 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 86 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
| 87 base::MessageLoop::current()->Quit(); | 87 base::MessageLoop::current()->Quit(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 // Overridden from NodeObserver: | 90 // Overridden from ViewObserver: |
| 91 virtual void OnNodeDestroyed(Node* node) OVERRIDE { | 91 virtual void OnViewDestroyed(View* view) OVERRIDE { |
| 92 DCHECK(roots_.find(node->id()) != roots_.end()); | 92 DCHECK(roots_.find(view->id()) != roots_.end()); |
| 93 roots_.erase(node->id()); | 93 roots_.erase(view->id()); |
| 94 } | 94 } |
| 95 virtual void OnNodeInputEvent(Node* node, const EventPtr& event) OVERRIDE { | 95 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
| 96 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { | 96 if (event->action == EVENT_TYPE_MOUSE_RELEASED) { |
| 97 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 97 if (event->flags & EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
| 98 NavigationDetailsPtr nav_details(NavigationDetails::New()); | 98 NavigationDetailsPtr nav_details(NavigationDetails::New()); |
| 99 nav_details->request->url = | 99 nav_details->request->url = |
| 100 "http://www.aaronboodman.com/z_dropbox/test.html"; | 100 "http://www.aaronboodman.com/z_dropbox/test.html"; |
| 101 navigator_host_->RequestNavigate(node->id(), TARGET_SOURCE_NODE, | 101 navigator_host_->RequestNavigate(view->id(), TARGET_SOURCE_NODE, |
| 102 nav_details.Pass()); | 102 nav_details.Pass()); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 void ProcessPendingNodeColor(uint32 node_id) { | 107 void ProcessPendingViewColor(uint32 view_id) { |
| 108 RootMap::iterator root = roots_.find(node_id); | 108 RootMap::iterator root = roots_.find(view_id); |
| 109 if (root == roots_.end()) | 109 if (root == roots_.end()) |
| 110 return; | 110 return; |
| 111 | 111 |
| 112 PendingNodeColors::iterator color = pending_node_colors_.find(node_id); | 112 PendingViewColors::iterator color = pending_view_colors_.find(view_id); |
| 113 if (color == pending_node_colors_.end()) | 113 if (color == pending_view_colors_.end()) |
| 114 return; | 114 return; |
| 115 | 115 |
| 116 root->second->SetColor(color->second); | 116 root->second->SetColor(color->second); |
| 117 pending_node_colors_.erase(color); | 117 pending_view_colors_.erase(color); |
| 118 } | 118 } |
| 119 | 119 |
| 120 InterfaceFactoryImplWithContext<NavigatorImpl, EmbeddedApp> | 120 InterfaceFactoryImplWithContext<NavigatorImpl, EmbeddedApp> |
| 121 navigator_factory_; | 121 navigator_factory_; |
| 122 | 122 |
| 123 ViewManager* view_manager_; | 123 ViewManager* view_manager_; |
| 124 NavigatorHostPtr navigator_host_; | 124 NavigatorHostPtr navigator_host_; |
| 125 ViewManagerClientFactory view_manager_client_factory_; | 125 ViewManagerClientFactory view_manager_client_factory_; |
| 126 | 126 |
| 127 typedef std::map<Id, Node*> RootMap; | 127 typedef std::map<Id, View*> RootMap; |
| 128 RootMap roots_; | 128 RootMap roots_; |
| 129 | 129 |
| 130 // We can receive navigations for nodes we don't have yet. | 130 // We can receive navigations for views we don't have yet. |
| 131 typedef std::map<uint32, SkColor> PendingNodeColors; | 131 typedef std::map<uint32, SkColor> PendingViewColors; |
| 132 PendingNodeColors pending_node_colors_; | 132 PendingViewColors pending_view_colors_; |
| 133 | 133 |
| 134 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 134 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
| 135 }; | 135 }; |
| 136 | 136 |
| 137 void NavigatorImpl::Navigate(uint32 node_id, | 137 void NavigatorImpl::Navigate(uint32 view_id, |
| 138 NavigationDetailsPtr navigation_details, | 138 NavigationDetailsPtr navigation_details, |
| 139 ResponseDetailsPtr response_details) { | 139 ResponseDetailsPtr response_details) { |
| 140 GURL url(navigation_details->request->url.To<std::string>()); | 140 GURL url(navigation_details->request->url.To<std::string>()); |
| 141 if (!url.is_valid()) { | 141 if (!url.is_valid()) { |
| 142 LOG(ERROR) << "URL is invalid."; | 142 LOG(ERROR) << "URL is invalid."; |
| 143 return; | 143 return; |
| 144 } | 144 } |
| 145 // TODO(aa): Verify new URL is same origin as current origin. | 145 // TODO(aa): Verify new URL is same origin as current origin. |
| 146 SkColor color = 0x00; | 146 SkColor color = 0x00; |
| 147 if (!base::HexStringToUInt(url.path().substr(1), &color)) { | 147 if (!base::HexStringToUInt(url.path().substr(1), &color)) { |
| 148 LOG(ERROR) << "Invalid URL, path not convertible to integer"; | 148 LOG(ERROR) << "Invalid URL, path not convertible to integer"; |
| 149 return; | 149 return; |
| 150 } | 150 } |
| 151 app_->SetNodeColor(node_id, color); | 151 app_->SetViewColor(view_id, color); |
| 152 } | 152 } |
| 153 | 153 |
| 154 } // namespace examples | 154 } // namespace examples |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 ApplicationDelegate* ApplicationDelegate::Create() { | 157 ApplicationDelegate* ApplicationDelegate::Create() { |
| 158 return new examples::EmbeddedApp; | 158 return new examples::EmbeddedApp; |
| 159 } | 159 } |
| 160 | 160 |
| 161 } // namespace mojo | 161 } // namespace mojo |
| OLD | NEW |