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