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/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
11 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
13 #include "mojo/public/cpp/application/application_runner_chromium.h" | 13 #include "mojo/public/cpp/application/application_runner_chromium.h" |
14 #include "mojo/public/cpp/application/interface_factory_impl.h" | 14 #include "mojo/public/cpp/application/interface_factory_impl.h" |
15 #include "mojo/services/public/cpp/view_manager/view.h" | 15 #include "mojo/services/public/cpp/view_manager/view.h" |
16 #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" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
19 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 19 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
20 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | |
21 #include "ui/events/event_constants.h" | 20 #include "ui/events/event_constants.h" |
22 #include "url/gurl.h" | 21 #include "url/gurl.h" |
23 | 22 |
24 namespace mojo { | 23 namespace mojo { |
25 namespace examples { | 24 namespace examples { |
26 | 25 |
27 namespace { | 26 namespace { |
28 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; | 27 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app"; |
29 } | 28 } |
30 | 29 |
31 class NestingApp; | 30 class NestingApp; |
32 | 31 |
33 class NavigatorImpl : public InterfaceImpl<Navigator> { | |
34 public: | |
35 explicit NavigatorImpl(NestingApp* app) : app_(app) {} | |
36 | |
37 private: | |
38 virtual void Navigate( | |
39 uint32 node_id, | |
40 NavigationDetailsPtr navigation_details, | |
41 ResponseDetailsPtr response_details) OVERRIDE; | |
42 | |
43 NestingApp* app_; | |
44 DISALLOW_COPY_AND_ASSIGN(NavigatorImpl); | |
45 }; | |
46 | |
47 // An app that embeds another app. | 32 // An app that embeds another app. |
48 // TODO(davemoore): Is this the right name? | 33 // TODO(davemoore): Is this the right name? |
49 class NestingApp | 34 class NestingApp |
50 : public ApplicationDelegate, | 35 : public ApplicationDelegate, |
51 public ViewManagerDelegate, | 36 public ViewManagerDelegate, |
52 public ViewObserver { | 37 public ViewObserver { |
53 public: | 38 public: |
54 NestingApp() | 39 NestingApp() |
55 : navigator_factory_(this), | 40 : view_manager_client_factory_(this), |
56 view_manager_client_factory_(this), | |
57 nested_(NULL) {} | 41 nested_(NULL) {} |
58 virtual ~NestingApp() {} | 42 virtual ~NestingApp() {} |
59 | 43 |
60 void set_color(const std::string& color) { color_ = color; } | |
61 | |
62 void NavigateChild() { | |
63 if (!color_.empty() && nested_) { | |
64 NavigationDetailsPtr details(NavigationDetails::New()); | |
65 details->request->url = | |
66 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str()); | |
67 ResponseDetailsPtr response_details(ResponseDetails::New()); | |
68 navigator_->Navigate( | |
69 nested_->id(), details.Pass(), response_details.Pass()); | |
70 } | |
71 } | |
72 | |
73 private: | 44 private: |
74 // Overridden from ApplicationImpl: | 45 // Overridden from ApplicationImpl: |
75 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 46 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
76 MOJO_OVERRIDE { | 47 MOJO_OVERRIDE { |
77 connection->ConnectToService(&window_manager_); | 48 connection->ConnectToService(&window_manager_); |
78 connection->AddService(&view_manager_client_factory_); | 49 connection->AddService(&view_manager_client_factory_); |
79 connection->AddService(&navigator_factory_); | |
80 // TODO(davemoore): Is this ok? | |
81 if (!navigator_) { | |
82 connection->ConnectToApplication( | |
83 kEmbeddedAppURL)->ConnectToService(&navigator_); | |
84 } | |
85 return true; | 50 return true; |
86 } | 51 } |
87 | 52 |
88 // Overridden from ViewManagerDelegate: | 53 // Overridden from ViewManagerDelegate: |
89 virtual void OnEmbed(ViewManager* view_manager, | 54 virtual void OnEmbed(ViewManager* view_manager, |
90 View* root, | 55 View* root, |
91 ServiceProviderImpl* exported_services, | 56 ServiceProviderImpl* exported_services, |
92 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { | 57 scoped_ptr<ServiceProvider> imported_services) OVERRIDE { |
93 root->AddObserver(this); | 58 root->AddObserver(this); |
94 root->SetColor(SK_ColorCYAN); | 59 root->SetColor(SK_ColorCYAN); |
95 | 60 |
96 nested_ = View::Create(view_manager); | 61 nested_ = View::Create(view_manager); |
97 root->AddChild(nested_); | 62 root->AddChild(nested_); |
98 nested_->SetBounds(gfx::Rect(20, 20, 50, 50)); | 63 nested_->SetBounds(gfx::Rect(20, 20, 50, 50)); |
99 nested_->Embed(kEmbeddedAppURL); | 64 nested_->Embed(kEmbeddedAppURL); |
100 | |
101 NavigateChild(); | |
102 } | 65 } |
103 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { | 66 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE { |
104 base::MessageLoop::current()->Quit(); | 67 base::MessageLoop::current()->Quit(); |
105 } | 68 } |
106 | 69 |
107 // Overridden from ViewObserver: | 70 // Overridden from ViewObserver: |
108 virtual void OnViewDestroyed(View* view) OVERRIDE { | 71 virtual void OnViewDestroyed(View* view) OVERRIDE { |
109 // TODO(beng): reap views & child Views. | 72 // TODO(beng): reap views & child Views. |
110 nested_ = NULL; | 73 nested_ = NULL; |
111 } | 74 } |
112 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { | 75 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE { |
113 if (event->action == EVENT_TYPE_MOUSE_RELEASED) | 76 if (event->action == EVENT_TYPE_MOUSE_RELEASED) |
114 window_manager_->CloseWindow(view->id()); | 77 window_manager_->CloseWindow(view->id()); |
115 } | 78 } |
116 | 79 |
117 InterfaceFactoryImplWithContext<NavigatorImpl, NestingApp> navigator_factory_; | |
118 ViewManagerClientFactory view_manager_client_factory_; | 80 ViewManagerClientFactory view_manager_client_factory_; |
119 | 81 |
120 std::string color_; | |
121 View* nested_; | 82 View* nested_; |
122 NavigatorPtr navigator_; | |
123 IWindowManagerPtr window_manager_; | 83 IWindowManagerPtr window_manager_; |
124 | 84 |
125 DISALLOW_COPY_AND_ASSIGN(NestingApp); | 85 DISALLOW_COPY_AND_ASSIGN(NestingApp); |
126 }; | 86 }; |
127 | 87 |
128 void NavigatorImpl::Navigate(uint32 node_id, | |
129 NavigationDetailsPtr navigation_details, | |
130 ResponseDetailsPtr response_details) { | |
131 GURL url(navigation_details->request->url.To<std::string>()); | |
132 if (!url.is_valid()) { | |
133 LOG(ERROR) << "URL is invalid."; | |
134 return; | |
135 } | |
136 app_->set_color(url.path().substr(1)); | |
137 app_->NavigateChild(); | |
138 } | |
139 | |
140 } // namespace examples | 88 } // namespace examples |
141 } // namespace mojo | 89 } // namespace mojo |
142 | 90 |
143 MojoResult MojoMain(MojoHandle shell_handle) { | 91 MojoResult MojoMain(MojoHandle shell_handle) { |
144 mojo::ApplicationRunnerChromium runner(new mojo::examples::NestingApp); | 92 mojo::ApplicationRunnerChromium runner(new mojo::examples::NestingApp); |
145 return runner.Run(shell_handle); | 93 return runner.Run(shell_handle); |
146 } | 94 } |
OLD | NEW |