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