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); |
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 view->RemoveObserver(this); |
| 113 } |
| 114 |
99 void HelloBackAck() { | 115 void HelloBackAck() { |
100 printf("HelloBack() ack'ed\n"); | 116 printf("HelloBack() ack'ed\n"); |
101 } | 117 } |
102 | 118 |
| 119 void OpenNewWindow() { |
| 120 mojo::ViewManagerInitServicePtr init_svc; |
| 121 app_->ConnectToService("mojo:mojo_view_manager", &init_svc); |
| 122 mojo::ServiceProviderPtr sp; |
| 123 init_svc->Embed("mojo:mojo_wm_flow_app", sp.Pass(), |
| 124 base::Bind(&ConnectCallback)); |
| 125 } |
| 126 |
103 int embed_count_; | 127 int embed_count_; |
104 mojo::ViewManagerClientFactory view_manager_client_factory_; | 128 mojo::ViewManagerClientFactory view_manager_client_factory_; |
105 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; | 129 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; |
106 EmbeddeePtr embeddee_; | 130 EmbeddeePtr embeddee_; |
| 131 mojo::ApplicationImpl* app_; |
107 | 132 |
108 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); | 133 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); |
109 }; | 134 }; |
110 | 135 |
111 } // namespace examples | 136 } // namespace examples |
112 | 137 |
113 namespace mojo { | 138 namespace mojo { |
114 | 139 |
115 // static | 140 // static |
116 ApplicationDelegate* ApplicationDelegate::Create() { | 141 ApplicationDelegate* ApplicationDelegate::Create() { |
117 return new examples::WMFlowApp; | 142 return new examples::WMFlowApp; |
118 } | 143 } |
119 | 144 |
120 } // namespace | 145 } // namespace |
OLD | NEW |