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/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "mojo/examples/window_manager/window_manager.mojom.h" | 9 #include "mojo/examples/window_manager/window_manager.mojom.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/interface_factory_with_context.h" |
12 #include "mojo/services/public/cpp/view_manager/node.h" | 13 #include "mojo/services/public/cpp/view_manager/node.h" |
13 #include "mojo/services/public/cpp/view_manager/node_observer.h" | 14 #include "mojo/services/public/cpp/view_manager/node_observer.h" |
14 #include "mojo/services/public/cpp/view_manager/view.h" | 15 #include "mojo/services/public/cpp/view_manager/view.h" |
15 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 16 #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_delegate.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
17 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 19 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 20 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
19 #include "ui/events/event_constants.h" | 21 #include "ui/events/event_constants.h" |
20 #include "url/gurl.h" | 22 #include "url/gurl.h" |
21 | 23 |
22 using mojo::view_manager::Node; | 24 using mojo::view_manager::Node; |
23 using mojo::view_manager::NodeObserver; | 25 using mojo::view_manager::NodeObserver; |
24 using mojo::view_manager::View; | 26 using mojo::view_manager::View; |
25 using mojo::view_manager::ViewManager; | 27 using mojo::view_manager::ViewManager; |
| 28 using mojo::view_manager::ViewManagerClientFactory; |
26 using mojo::view_manager::ViewManagerDelegate; | 29 using mojo::view_manager::ViewManagerDelegate; |
27 using mojo::view_manager::ViewObserver; | 30 using mojo::view_manager::ViewObserver; |
28 | 31 |
29 namespace mojo { | 32 namespace mojo { |
30 namespace examples { | 33 namespace examples { |
31 | 34 |
32 namespace { | 35 namespace { |
33 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; | 36 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; |
34 } | 37 } |
35 | 38 |
| 39 class NestingApp; |
| 40 |
| 41 class Navigator : public InterfaceImpl<navigation::Navigator> { |
| 42 public: |
| 43 explicit Navigator(NestingApp* app) : app_(app) {} |
| 44 |
| 45 private: |
| 46 virtual void Navigate( |
| 47 uint32 node_id, |
| 48 navigation::NavigationDetailsPtr navigation_details, |
| 49 navigation::ResponseDetailsPtr response_details) OVERRIDE; |
| 50 |
| 51 NestingApp* app_; |
| 52 DISALLOW_COPY_AND_ASSIGN(Navigator); |
| 53 }; |
| 54 |
36 // An app that embeds another app. | 55 // An app that embeds another app. |
37 // TODO(davemoore): Is this the right name? | 56 // TODO(davemoore): Is this the right name? |
38 class NestingApp : public ApplicationDelegate, | 57 class NestingApp : public ApplicationDelegate, |
39 public ViewManagerDelegate, | 58 public ViewManagerDelegate, |
40 public ViewObserver, | 59 public ViewObserver, |
41 public NodeObserver { | 60 public NodeObserver, |
| 61 public InterfaceFactoryWithContext<Navigator, NestingApp> { |
42 public: | 62 public: |
43 NestingApp() : nested_(NULL) {} | 63 NestingApp() |
| 64 : InterfaceFactoryWithContext(this), |
| 65 nested_(NULL), |
| 66 view_manager_client_factory_(this) {} |
44 virtual ~NestingApp() {} | 67 virtual ~NestingApp() {} |
45 | 68 |
| 69 void set_color(const std::string& color) { color_ = color; } |
| 70 |
| 71 void NavigateChild() { |
| 72 if (!color_.empty() && nested_) { |
| 73 navigation::NavigationDetailsPtr details( |
| 74 navigation::NavigationDetails::New()); |
| 75 details->url = |
| 76 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str()); |
| 77 navigation::ResponseDetailsPtr response_details( |
| 78 navigation::ResponseDetails::New()); |
| 79 navigator_->Navigate( |
| 80 nested_->id(), details.Pass(), response_details.Pass()); |
| 81 } |
| 82 } |
| 83 |
46 private: | 84 private: |
47 class Navigator : public InterfaceImpl<navigation::Navigator> { | |
48 public: | |
49 explicit Navigator(ApplicationConnection* connection, | |
50 NestingApp* app) : app_(app) {} | |
51 private: | |
52 virtual void Navigate( | |
53 uint32 node_id, | |
54 navigation::NavigationDetailsPtr navigation_details, | |
55 navigation::ResponseDetailsPtr response_details) OVERRIDE { | |
56 GURL url(navigation_details->url.To<std::string>()); | |
57 if (!url.is_valid()) { | |
58 LOG(ERROR) << "URL is invalid."; | |
59 return; | |
60 } | |
61 app_->color_ = url.path().substr(1); | |
62 app_->NavigateChild(); | |
63 } | |
64 NestingApp* app_; | |
65 DISALLOW_COPY_AND_ASSIGN(Navigator); | |
66 }; | |
67 | |
68 // Overridden from ApplicationImpl: | 85 // Overridden from ApplicationImpl: |
69 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 86 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
70 MOJO_OVERRIDE { | 87 MOJO_OVERRIDE { |
71 ViewManager::ConfigureIncomingConnection(connection, this); | 88 connection->AddServiceFactory(&view_manager_client_factory_); |
72 connection->ConnectToService(&window_manager_); | 89 connection->ConnectToService(&window_manager_); |
73 connection->AddService<Navigator>(this); | 90 connection->AddServiceFactory(this); |
74 // TODO(davemoore): Is this ok? | 91 // TODO(davemoore): Is this ok? |
75 if (!navigator_.get()) { | 92 if (!navigator_.get()) { |
76 connection->ConnectToApplication( | 93 connection->ConnectToApplication( |
77 kEmbeddedAppURL)->ConnectToService(&navigator_); | 94 kEmbeddedAppURL)->ConnectToService(&navigator_); |
78 } | 95 } |
79 return true; | 96 return true; |
80 } | 97 } |
81 | 98 |
82 // Overridden from ViewManagerDelegate: | 99 // Overridden from ViewManagerDelegate: |
83 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { | 100 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
(...skipping 20 matching lines...) Expand all Loading... |
104 if (event->action == ui::ET_MOUSE_RELEASED) | 121 if (event->action == ui::ET_MOUSE_RELEASED) |
105 window_manager_->CloseWindow(view->node()->id()); | 122 window_manager_->CloseWindow(view->node()->id()); |
106 } | 123 } |
107 | 124 |
108 // Overridden from NodeObserver: | 125 // Overridden from NodeObserver: |
109 virtual void OnNodeDestroyed(Node* node) OVERRIDE { | 126 virtual void OnNodeDestroyed(Node* node) OVERRIDE { |
110 // TODO(beng): reap views & child nodes. | 127 // TODO(beng): reap views & child nodes. |
111 nested_ = NULL; | 128 nested_ = NULL; |
112 } | 129 } |
113 | 130 |
114 void NavigateChild() { | |
115 if (!color_.empty() && nested_) { | |
116 navigation::NavigationDetailsPtr details( | |
117 navigation::NavigationDetails::New()); | |
118 details->url = | |
119 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str()); | |
120 navigation::ResponseDetailsPtr response_details( | |
121 navigation::ResponseDetails::New()); | |
122 navigator_->Navigate(nested_->id(), | |
123 details.Pass(), | |
124 response_details.Pass()); | |
125 } | |
126 } | |
127 | |
128 std::string color_; | 131 std::string color_; |
129 Node* nested_; | 132 Node* nested_; |
130 navigation::NavigatorPtr navigator_; | 133 navigation::NavigatorPtr navigator_; |
131 IWindowManagerPtr window_manager_; | 134 IWindowManagerPtr window_manager_; |
| 135 ViewManagerClientFactory view_manager_client_factory_; |
132 | 136 |
133 DISALLOW_COPY_AND_ASSIGN(NestingApp); | 137 DISALLOW_COPY_AND_ASSIGN(NestingApp); |
134 }; | 138 }; |
135 | 139 |
| 140 void Navigator::Navigate(uint32 node_id, |
| 141 navigation::NavigationDetailsPtr navigation_details, |
| 142 navigation::ResponseDetailsPtr response_details) { |
| 143 GURL url(navigation_details->url.To<std::string>()); |
| 144 if (!url.is_valid()) { |
| 145 LOG(ERROR) << "URL is invalid."; |
| 146 return; |
| 147 } |
| 148 app_->set_color(url.path().substr(1)); |
| 149 app_->NavigateChild(); |
| 150 } |
| 151 |
136 } // namespace examples | 152 } // namespace examples |
137 | 153 |
138 // static | 154 // static |
139 ApplicationDelegate* ApplicationDelegate::Create() { | 155 ApplicationDelegate* ApplicationDelegate::Create() { |
140 return new examples::NestingApp; | 156 return new examples::NestingApp; |
141 } | 157 } |
142 | 158 |
143 } // namespace mojo | 159 } // namespace mojo |
OLD | NEW |