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

Side by Side Diff: mojo/examples/embedded_app/embedded_app.cc

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix network_service_loader 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 | Annotate | Revision Log
« no previous file with comments | « mojo/examples/browser/browser.cc ('k') | mojo/examples/keyboard/keyboard.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "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_with_context.h"
13 #include "mojo/services/public/cpp/view_manager/node.h" 14 #include "mojo/services/public/cpp/view_manager/node.h"
14 #include "mojo/services/public/cpp/view_manager/node_observer.h" 15 #include "mojo/services/public/cpp/view_manager/node_observer.h"
15 #include "mojo/services/public/cpp/view_manager/view.h" 16 #include "mojo/services/public/cpp/view_manager/view.h"
16 #include "mojo/services/public/cpp/view_manager/view_manager.h" 17 #include "mojo/services/public/cpp/view_manager/view_manager.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
18 #include "mojo/services/public/cpp/view_manager/view_observer.h" 20 #include "mojo/services/public/cpp/view_manager/view_observer.h"
19 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" 21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
20 #include "ui/events/event_constants.h" 22 #include "ui/events/event_constants.h"
21 #include "url/gurl.h" 23 #include "url/gurl.h"
22 #include "url/url_util.h" 24 #include "url/url_util.h"
23 25
24 using mojo::view_manager::Node; 26 using mojo::view_manager::Node;
25 using mojo::view_manager::NodeObserver; 27 using mojo::view_manager::NodeObserver;
26 using mojo::view_manager::View; 28 using mojo::view_manager::View;
27 using mojo::view_manager::ViewManager; 29 using mojo::view_manager::ViewManager;
28 using mojo::view_manager::ViewManagerDelegate; 30 using mojo::view_manager::ViewManagerDelegate;
31 using mojo::view_manager::ViewManagerClientFactory;
29 using mojo::view_manager::ViewObserver; 32 using mojo::view_manager::ViewObserver;
30 33
31 namespace mojo { 34 namespace mojo {
32 namespace examples { 35 namespace examples {
36 class EmbeddedApp;
37
38 class Navigator : public InterfaceImpl<navigation::Navigator> {
39 public:
40 explicit Navigator(EmbeddedApp* app) : app_(app) {}
41
42 private:
43 virtual void Navigate(
44 uint32 node_id,
45 navigation::NavigationDetailsPtr navigation_details,
46 navigation::ResponseDetailsPtr response_details) OVERRIDE;
47
48 EmbeddedApp* app_;
49 DISALLOW_COPY_AND_ASSIGN(Navigator);
50 };
33 51
34 class EmbeddedApp : public ApplicationDelegate, 52 class EmbeddedApp : public ApplicationDelegate,
35 public ViewManagerDelegate, 53 public ViewManagerDelegate,
36 public ViewObserver, 54 public ViewObserver,
37 public NodeObserver { 55 public NodeObserver,
56 public InterfaceFactoryWithContext<Navigator, EmbeddedApp> {
38 public: 57 public:
39 EmbeddedApp() : view_manager_(NULL) { 58 EmbeddedApp()
59 : InterfaceFactoryWithContext(this),
60 view_manager_(NULL),
61 view_manager_client_factory_(this) {
40 url::AddStandardScheme("mojo"); 62 url::AddStandardScheme("mojo");
41 } 63 }
42 virtual ~EmbeddedApp() {} 64 virtual ~EmbeddedApp() {}
43 65
44 void SetNodeColor(uint32 node_id, SkColor color) { 66 void SetNodeColor(uint32 node_id, SkColor color) {
45 pending_node_colors_[node_id] = color; 67 pending_node_colors_[node_id] = color;
46 ProcessPendingNodeColor(node_id); 68 ProcessPendingNodeColor(node_id);
47 } 69 }
48 70
49 private: 71 private:
50 class Navigator : public InterfaceImpl<navigation::Navigator> {
51 public:
52 Navigator(ApplicationConnection* connection,
53 EmbeddedApp* app) : app_(app) {}
54 private:
55 virtual void Navigate(
56 uint32 node_id,
57 navigation::NavigationDetailsPtr navigation_details,
58 navigation::ResponseDetailsPtr response_details) OVERRIDE {
59 GURL url(navigation_details->url.To<std::string>());
60 if (!url.is_valid()) {
61 LOG(ERROR) << "URL is invalid.";
62 return;
63 }
64 // TODO(aa): Verify new URL is same origin as current origin.
65 SkColor color = 0x00;
66 if (!base::HexStringToUInt(url.path().substr(1), &color)) {
67 LOG(ERROR) << "Invalid URL, path not convertible to integer";
68 return;
69 }
70 app_->SetNodeColor(node_id, color);
71 }
72 EmbeddedApp* app_;
73 DISALLOW_COPY_AND_ASSIGN(Navigator);
74 };
75 72
76 // Overridden from ApplicationDelegate: 73 // Overridden from ApplicationDelegate:
77 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE { 74 virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
78 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like 75 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like
79 // embedder should be able to specify the SP embeddee receives, then 76 // embedder should be able to specify the SP embeddee receives, then
80 // communication can be anonymous. 77 // communication can be anonymous.
81 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_); 78 app->ConnectToService("mojo:mojo_window_manager", &navigator_host_);
82 } 79 }
83 80
84 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) 81 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
85 MOJO_OVERRIDE { 82 MOJO_OVERRIDE {
86 ViewManager::ConfigureIncomingConnection(connection, this); 83 connection->AddService(&view_manager_client_factory_);
87 connection->AddService<Navigator>(this); 84 connection->AddService(this);
88 return true; 85 return true;
89 } 86 }
90 87
91 // Overridden from ViewManagerDelegate: 88 // Overridden from ViewManagerDelegate:
92 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { 89 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
93 View* view = View::Create(view_manager); 90 View* view = View::Create(view_manager);
94 view->AddObserver(this); 91 view->AddObserver(this);
95 root->SetActiveView(view); 92 root->SetActiveView(view);
96 root->AddObserver(this); 93 root->AddObserver(this);
97 94
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 if (color == pending_node_colors_.end()) 137 if (color == pending_node_colors_.end())
141 return; 138 return;
142 139
143 root->second->active_view()->SetColor(color->second); 140 root->second->active_view()->SetColor(color->second);
144 pending_node_colors_.erase(color); 141 pending_node_colors_.erase(color);
145 } 142 }
146 143
147 view_manager::ViewManager* view_manager_; 144 view_manager::ViewManager* view_manager_;
148 navigation::NavigatorHostPtr navigator_host_; 145 navigation::NavigatorHostPtr navigator_host_;
149 std::map<Node*, View*> views_to_reap_; 146 std::map<Node*, View*> views_to_reap_;
147 ViewManagerClientFactory view_manager_client_factory_;
150 148
151 typedef std::map<view_manager::Id, Node*> RootMap; 149 typedef std::map<view_manager::Id, Node*> RootMap;
152 RootMap roots_; 150 RootMap roots_;
153 151
154 // We can receive navigations for nodes we don't have yet. 152 // We can receive navigations for nodes we don't have yet.
155 typedef std::map<uint32, SkColor> PendingNodeColors; 153 typedef std::map<uint32, SkColor> PendingNodeColors;
156 PendingNodeColors pending_node_colors_; 154 PendingNodeColors pending_node_colors_;
157 155
158 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); 156 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp);
159 }; 157 };
160 158
159 void Navigator::Navigate(uint32 node_id,
160 navigation::NavigationDetailsPtr navigation_details,
161 navigation::ResponseDetailsPtr response_details) {
162 GURL url(navigation_details->url.To<std::string>());
163 if (!url.is_valid()) {
164 LOG(ERROR) << "URL is invalid.";
165 return;
166 }
167 // TODO(aa): Verify new URL is same origin as current origin.
168 SkColor color = 0x00;
169 if (!base::HexStringToUInt(url.path().substr(1), &color)) {
170 LOG(ERROR) << "Invalid URL, path not convertible to integer";
171 return;
172 }
173 app_->SetNodeColor(node_id, color);
174 }
175
161 } // namespace examples 176 } // namespace examples
162 177
163 // static 178 // static
164 ApplicationDelegate* ApplicationDelegate::Create() { 179 ApplicationDelegate* ApplicationDelegate::Create() {
165 return new examples::EmbeddedApp; 180 return new examples::EmbeddedApp;
166 } 181 }
167 182
168 } // namespace mojo 183 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/examples/browser/browser.cc ('k') | mojo/examples/keyboard/keyboard.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698