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" |
| 7 #include "mojo/examples/wm_flow/embedded/embeddee.mojom.h" |
6 #include "mojo/public/cpp/application/application_connection.h" | 8 #include "mojo/public/cpp/application/application_connection.h" |
7 #include "mojo/public/cpp/application/application_delegate.h" | 9 #include "mojo/public/cpp/application/application_delegate.h" |
8 #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" |
| 12 #include "mojo/public/cpp/application/interface_factory_impl.h" |
| 13 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 14 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
9 #include "mojo/services/public/cpp/view_manager/node.h" | 15 #include "mojo/services/public/cpp/view_manager/node.h" |
10 #include "mojo/services/public/cpp/view_manager/view.h" | 16 #include "mojo/services/public/cpp/view_manager/view.h" |
11 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
12 #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" |
13 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
14 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" | 20 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" |
15 | 21 |
16 namespace examples { | 22 namespace examples { |
17 namespace { | 23 namespace { |
18 void ConnectCallback(bool success) {} | 24 void ConnectCallback(bool success) {} |
19 | 25 |
20 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW }; | 26 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW }; |
21 | 27 |
| 28 class EmbedderImpl : public mojo::InterfaceImpl<Embedder> { |
| 29 public: |
| 30 EmbedderImpl() {} |
| 31 virtual ~EmbedderImpl() {} |
| 32 |
| 33 private: |
| 34 // Overridden from Embedder: |
| 35 virtual void HelloWorld(const mojo::Callback<void()>& callback) OVERRIDE { |
| 36 callback.Run(); |
| 37 } |
| 38 |
| 39 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl); |
| 40 }; |
| 41 |
22 } // namespace | 42 } // namespace |
23 | 43 |
24 // 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 |
25 // 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 |
26 // connection by connecting to the ViewManagerInit service and asking to be | 46 // connection by connecting to the ViewManagerInit service and asking to be |
27 // embed without a node context. | 47 // embed without a node context. |
28 class WMFlowApp : public mojo::ApplicationDelegate, | 48 class WMFlowApp : public mojo::ApplicationDelegate, |
29 public mojo::ViewManagerDelegate { | 49 public mojo::ViewManagerDelegate { |
30 public: | 50 public: |
31 WMFlowApp() : embed_count_(0), view_manager_client_factory_(this) {} | 51 WMFlowApp() |
| 52 : embed_count_(0), |
| 53 view_manager_client_factory_(this) {} |
32 virtual ~WMFlowApp() {} | 54 virtual ~WMFlowApp() {} |
33 | 55 |
34 private: | 56 private: |
35 // Overridden from Application: | 57 // Overridden from Application: |
36 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { | 58 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { |
37 mojo::ViewManagerInitServicePtr init_svc; | 59 mojo::ViewManagerInitServicePtr init_svc; |
| 60 mojo::ServiceProviderPtr sp; |
38 app->ConnectToService("mojo:mojo_view_manager", &init_svc); | 61 app->ConnectToService("mojo:mojo_view_manager", &init_svc); |
39 init_svc->Embed("mojo:mojo_wm_flow_app", base::Bind(&ConnectCallback)); | 62 init_svc->Embed("mojo:mojo_wm_flow_app", sp.Pass(), |
40 init_svc->Embed("mojo:mojo_wm_flow_app", base::Bind(&ConnectCallback)); | 63 base::Bind(&ConnectCallback)); |
41 init_svc->Embed("mojo:mojo_wm_flow_app", base::Bind(&ConnectCallback)); | |
42 } | 64 } |
43 virtual bool ConfigureIncomingConnection( | 65 virtual bool ConfigureIncomingConnection( |
44 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { | 66 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { |
45 connection->AddService(&view_manager_client_factory_); | 67 connection->AddService(&view_manager_client_factory_); |
46 return true; | 68 return true; |
47 } | 69 } |
48 | 70 |
49 void OnConnect(bool success) {} | 71 void OnConnect(bool success) {} |
50 | 72 |
51 // Overridden from mojo::ViewManagerDelegate: | 73 // Overridden from mojo::ViewManagerDelegate: |
52 virtual void OnEmbed(mojo::ViewManager* view_manager, | 74 virtual void OnEmbed( |
53 mojo::Node* root) MOJO_OVERRIDE { | 75 mojo::ViewManager* view_manager, |
| 76 mojo::Node* root, |
| 77 mojo::ServiceProviderImpl* exported_services, |
| 78 scoped_ptr<mojo::ServiceProvider> imported_services) MOJO_OVERRIDE { |
54 mojo::View* view = | 79 mojo::View* view = |
55 mojo::View::Create(view_manager); | 80 mojo::View::Create(view_manager); |
56 root->SetActiveView(view); | 81 root->SetActiveView(view); |
57 view->SetColor(kColors[embed_count_++ % arraysize(kColors)]); | 82 view->SetColor(kColors[embed_count_++ % arraysize(kColors)]); |
| 83 |
| 84 mojo::Node* embed = mojo::Node::Create(view_manager); |
| 85 root->AddChild(embed); |
| 86 gfx::Rect bounds = root->bounds(); |
| 87 bounds.Inset(25, 25); |
| 88 embed->SetBounds(bounds); |
| 89 |
| 90 scoped_ptr<mojo::ServiceProviderImpl> registry( |
| 91 new mojo::ServiceProviderImpl); |
| 92 // Expose some services to the embeddee... |
| 93 registry->AddService(&embedder_factory_); |
| 94 scoped_ptr<mojo::ServiceProvider> imported = |
| 95 embed->Embed("mojo:mojo_wm_flow_embedded", registry.Pass()); |
| 96 mojo::ConnectToService(imported.get(), &embeddee_); |
| 97 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck, |
| 98 base::Unretained(this))); |
58 } | 99 } |
59 virtual void OnViewManagerDisconnected( | 100 virtual void OnViewManagerDisconnected( |
60 mojo::ViewManager* view_manager) MOJO_OVERRIDE {} | 101 mojo::ViewManager* view_manager) MOJO_OVERRIDE {} |
61 | 102 |
| 103 void HelloBackAck() { |
| 104 printf("HelloBack() ack'ed\n"); |
| 105 } |
| 106 |
62 int embed_count_; | 107 int embed_count_; |
63 mojo::ViewManagerClientFactory view_manager_client_factory_; | 108 mojo::ViewManagerClientFactory view_manager_client_factory_; |
| 109 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; |
| 110 EmbeddeePtr embeddee_; |
64 | 111 |
65 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); | 112 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); |
66 }; | 113 }; |
67 | 114 |
68 } // namespace examples | 115 } // namespace examples |
69 | 116 |
70 namespace mojo { | 117 namespace mojo { |
71 | 118 |
72 // static | 119 // static |
73 ApplicationDelegate* ApplicationDelegate::Create() { | 120 ApplicationDelegate* ApplicationDelegate::Create() { |
74 return new examples::WMFlowApp; | 121 return new examples::WMFlowApp; |
75 } | 122 } |
76 | 123 |
77 } // namespace | 124 } // namespace |
OLD | NEW |