Chromium Code Reviews| 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/bind.h" | 5 #include "base/bind.h" |
| 6 #include "mojo/examples/wm_flow/app/embedder.mojom.h" | 6 #include "mojo/examples/wm_flow/app/embedder.mojom.h" |
| 7 #include "mojo/examples/wm_flow/embedded/embeddee.mojom.h" | 7 #include "mojo/examples/wm_flow/embedded/embeddee.mojom.h" |
| 8 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
| 9 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "mojo/public/cpp/application/connect.h" | 11 #include "mojo/public/cpp/application/connect.h" |
| 12 #include "mojo/public/cpp/application/interface_factory_impl.h" | 12 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 13 #include "mojo/public/cpp/application/service_provider_impl.h" | 13 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 14 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 14 #include "mojo/public/interfaces/application/service_provider.mojom.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/interfaces/view_manager/view_manager.mojom.h" | 20 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" |
| 20 | 21 |
| 21 namespace examples { | 22 namespace examples { |
| 22 namespace { | 23 namespace { |
| 23 void ConnectCallback(bool success) {} | 24 void ConnectCallback(bool success) {} |
| 24 | 25 |
| 25 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW }; | 26 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW }; |
| 26 | 27 |
| 27 class EmbedderImpl : public mojo::InterfaceImpl<Embedder> { | 28 class EmbedderImpl : public mojo::InterfaceImpl<Embedder> { |
| 28 public: | 29 public: |
| 29 EmbedderImpl() {} | 30 EmbedderImpl() {} |
| 30 virtual ~EmbedderImpl() {} | 31 virtual ~EmbedderImpl() {} |
| 31 | 32 |
| 32 private: | 33 private: |
| 33 // Overridden from Embedder: | 34 // Overridden from Embedder: |
| 34 virtual void HelloWorld(const mojo::Callback<void()>& callback) OVERRIDE { | 35 virtual void HelloWorld(const mojo::Callback<void()>& callback) OVERRIDE { |
| 35 callback.Run(); | 36 callback.Run(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl); | 39 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl); |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 } // namespace | 42 } // namespace |
| 42 | 43 |
| 43 // This app starts its life via Connect() rather than by being embed, so it does | 44 // This app starts its life via Connect() rather than by being embed, so it does |
| 44 // not start with a connection to the ViewManager service. It has to obtain a | 45 // not start with a connection to the ViewManager service. It has to obtain a |
| 45 // connection by connecting to the ViewManagerInit service and asking to be | 46 // connection by connecting to the ViewManagerInit service and asking to be |
| 46 // embed without a view context. | 47 // embed without a view context. |
| 47 class WMFlowApp : public mojo::ApplicationDelegate, | 48 class WMFlowApp : public mojo::ApplicationDelegate, |
| 48 public mojo::ViewManagerDelegate { | 49 public mojo::ViewManagerDelegate, |
| 50 public mojo::ViewObserver { | |
| 49 public: | 51 public: |
| 50 WMFlowApp() | 52 WMFlowApp() |
| 51 : embed_count_(0), | 53 : embed_count_(0), |
| 52 view_manager_client_factory_(this) {} | 54 view_manager_client_factory_(this), |
| 55 app_(NULL) {} | |
| 53 virtual ~WMFlowApp() {} | 56 virtual ~WMFlowApp() {} |
| 54 | 57 |
| 55 private: | 58 private: |
| 56 // Overridden from Application: | 59 // Overridden from Application: |
| 57 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { | 60 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { |
| 58 mojo::ServiceProviderPtr sp; | 61 app_ = app; |
| 59 mojo::ViewManagerInitServicePtr init_svc; | 62 OpenNewWindow(); |
| 60 app->ConnectToService("mojo:mojo_view_manager", &init_svc); | 63 OpenNewWindow(); |
| 61 init_svc->Embed("mojo:mojo_wm_flow_app", sp.Pass(), | 64 OpenNewWindow(); |
| 62 base::Bind(&ConnectCallback)); | |
| 63 } | 65 } |
| 64 virtual bool ConfigureIncomingConnection( | 66 virtual bool ConfigureIncomingConnection( |
| 65 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { | 67 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { |
| 66 connection->AddService(&view_manager_client_factory_); | 68 connection->AddService(&view_manager_client_factory_); |
| 67 return true; | 69 return true; |
| 68 } | 70 } |
| 69 | 71 |
| 70 void OnConnect(bool success) {} | 72 void OnConnect(bool success) {} |
| 71 | 73 |
| 72 // Overridden from mojo::ViewManagerDelegate: | 74 // Overridden from mojo::ViewManagerDelegate: |
| 73 virtual void OnEmbed( | 75 virtual void OnEmbed( |
| 74 mojo::ViewManager* view_manager, | 76 mojo::ViewManager* view_manager, |
| 75 mojo::View* root, | 77 mojo::View* root, |
| 76 mojo::ServiceProviderImpl* exported_services, | 78 mojo::ServiceProviderImpl* exported_services, |
| 77 scoped_ptr<mojo::ServiceProvider> imported_services) MOJO_OVERRIDE { | 79 scoped_ptr<mojo::ServiceProvider> imported_services) MOJO_OVERRIDE { |
| 80 root->AddObserver(this); | |
|
sky
2014/08/15 23:09:36
Remove this some where?
| |
| 78 root->SetColor(kColors[embed_count_++ % arraysize(kColors)]); | 81 root->SetColor(kColors[embed_count_++ % arraysize(kColors)]); |
| 79 | 82 |
| 80 mojo::View* embed = mojo::View::Create(view_manager); | 83 mojo::View* embed = mojo::View::Create(view_manager); |
| 81 root->AddChild(embed); | 84 root->AddChild(embed); |
| 82 gfx::Rect bounds = root->bounds(); | 85 gfx::Rect bounds = gfx::Rect(root->bounds().size()); |
| 83 bounds.Inset(25, 25); | 86 bounds.Inset(25, 25); |
| 84 embed->SetBounds(bounds); | 87 embed->SetBounds(bounds); |
| 85 | 88 |
| 86 scoped_ptr<mojo::ServiceProviderImpl> registry( | 89 scoped_ptr<mojo::ServiceProviderImpl> registry( |
| 87 new mojo::ServiceProviderImpl); | 90 new mojo::ServiceProviderImpl); |
| 88 // Expose some services to the embeddee... | 91 // Expose some services to the embeddee... |
| 89 registry->AddService(&embedder_factory_); | 92 registry->AddService(&embedder_factory_); |
| 90 scoped_ptr<mojo::ServiceProvider> imported = | 93 scoped_ptr<mojo::ServiceProvider> imported = |
| 91 embed->Embed("mojo:mojo_wm_flow_embedded", registry.Pass()); | 94 embed->Embed("mojo:mojo_wm_flow_embedded", registry.Pass()); |
| 92 mojo::ConnectToService(imported.get(), &embeddee_); | 95 mojo::ConnectToService(imported.get(), &embeddee_); |
| 93 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck, | 96 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck, |
| 94 base::Unretained(this))); | 97 base::Unretained(this))); |
| 95 } | 98 } |
| 96 virtual void OnViewManagerDisconnected( | 99 virtual void OnViewManagerDisconnected( |
| 97 mojo::ViewManager* view_manager) MOJO_OVERRIDE {} | 100 mojo::ViewManager* view_manager) MOJO_OVERRIDE {} |
| 98 | 101 |
| 102 // Overridden from mojo::ViewObserver: | |
| 103 virtual void OnViewInputEvent(mojo::View* view, | |
| 104 const mojo::EventPtr& event) MOJO_OVERRIDE { | |
| 105 if (event->action == mojo::EVENT_TYPE_MOUSE_RELEASED && | |
| 106 event->flags & mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | |
| 107 OpenNewWindow(); | |
| 108 } | |
| 109 } | |
| 110 virtual void OnViewDestroyed(mojo::View* view) MOJO_OVERRIDE { | |
| 111 --embed_count_; | |
| 112 } | |
| 113 | |
| 99 void HelloBackAck() { | 114 void HelloBackAck() { |
| 100 printf("HelloBack() ack'ed\n"); | 115 printf("HelloBack() ack'ed\n"); |
| 101 } | 116 } |
| 102 | 117 |
| 118 void OpenNewWindow() { | |
| 119 mojo::ViewManagerInitServicePtr init_svc; | |
| 120 app_->ConnectToService("mojo:mojo_view_manager", &init_svc); | |
| 121 mojo::ServiceProviderPtr sp; | |
| 122 init_svc->Embed("mojo:mojo_wm_flow_app", sp.Pass(), | |
| 123 base::Bind(&ConnectCallback)); | |
| 124 } | |
| 125 | |
| 103 int embed_count_; | 126 int embed_count_; |
| 104 mojo::ViewManagerClientFactory view_manager_client_factory_; | 127 mojo::ViewManagerClientFactory view_manager_client_factory_; |
| 105 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; | 128 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; |
| 106 EmbeddeePtr embeddee_; | 129 EmbeddeePtr embeddee_; |
| 130 mojo::ApplicationImpl* app_; | |
| 107 | 131 |
| 108 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); | 132 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); |
| 109 }; | 133 }; |
| 110 | 134 |
| 111 } // namespace examples | 135 } // namespace examples |
| 112 | 136 |
| 113 namespace mojo { | 137 namespace mojo { |
| 114 | 138 |
| 115 // static | 139 // static |
| 116 ApplicationDelegate* ApplicationDelegate::Create() { | 140 ApplicationDelegate* ApplicationDelegate::Create() { |
| 117 return new examples::WMFlowApp; | 141 return new examples::WMFlowApp; |
| 118 } | 142 } |
| 119 | 143 |
| 120 } // namespace | 144 } // namespace |
| OLD | NEW |