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 <map> | 5 #include <map> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "examples/bitmap_uploader/bitmap_uploader.h" | 10 #include "examples/bitmap_uploader/bitmap_uploader.h" |
11 #include "examples/wm_flow/app/embedder.mojom.h" | 11 #include "examples/wm_flow/app/embedder.mojom.h" |
12 #include "examples/wm_flow/embedded/embeddee.mojom.h" | 12 #include "examples/wm_flow/embedded/embeddee.mojom.h" |
13 #include "mojo/application/application_runner_chromium.h" | 13 #include "mojo/application/application_runner_chromium.h" |
| 14 #include "mojo/common/weak_binding_set.h" |
14 #include "mojo/public/c/system/main.h" | 15 #include "mojo/public/c/system/main.h" |
15 #include "mojo/public/cpp/application/application_connection.h" | 16 #include "mojo/public/cpp/application/application_connection.h" |
16 #include "mojo/public/cpp/application/application_delegate.h" | 17 #include "mojo/public/cpp/application/application_delegate.h" |
17 #include "mojo/public/cpp/application/application_impl.h" | 18 #include "mojo/public/cpp/application/application_impl.h" |
18 #include "mojo/public/cpp/application/connect.h" | 19 #include "mojo/public/cpp/application/connect.h" |
19 #include "mojo/public/cpp/application/interface_factory_impl.h" | 20 #include "mojo/public/cpp/application/interface_factory_impl.h" |
20 #include "mojo/public/cpp/application/service_provider_impl.h" | 21 #include "mojo/public/cpp/application/service_provider_impl.h" |
21 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 22 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
22 #include "mojo/services/view_manager/public/cpp/view.h" | 23 #include "mojo/services/view_manager/public/cpp/view.h" |
23 #include "mojo/services/view_manager/public/cpp/view_manager.h" | 24 #include "mojo/services/view_manager/public/cpp/view_manager.h" |
(...skipping 16 matching lines...) Expand all Loading... |
40 | 41 |
41 private: | 42 private: |
42 // Overridden from Embedder: | 43 // Overridden from Embedder: |
43 virtual void HelloWorld(const mojo::Callback<void()>& callback) override { | 44 virtual void HelloWorld(const mojo::Callback<void()>& callback) override { |
44 callback.Run(); | 45 callback.Run(); |
45 } | 46 } |
46 | 47 |
47 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl); | 48 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl); |
48 }; | 49 }; |
49 | 50 |
| 51 class EmbedderImplProvider : public mojo::ServiceProvider { |
| 52 public: |
| 53 EmbedderImplProvider() {} |
| 54 ~EmbedderImplProvider() override {} |
| 55 |
| 56 void AddBinding(mojo::InterfaceRequest<mojo::ServiceProvider> request) { |
| 57 embeddee_exposed_services_.AddBinding(this, request.Pass()); |
| 58 } |
| 59 |
| 60 private: |
| 61 // mojo::ServiceProvider implementation |
| 62 void ConnectToService(const mojo::String& interface_name, |
| 63 mojo::ScopedMessagePipeHandle pipe_handle) override { |
| 64 if (interface_name != Embedder::Name_) |
| 65 return; |
| 66 auto request = mojo::MakeRequest<Embedder>(pipe_handle.Pass()); |
| 67 mojo::BindToRequest(new EmbedderImpl, &request); |
| 68 } |
| 69 |
| 70 mojo::WeakBindingSet<mojo::ServiceProvider> embeddee_exposed_services_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(EmbedderImplProvider); |
| 73 }; |
| 74 |
50 } // namespace | 75 } // namespace |
51 | 76 |
52 // This app starts its life via Connect() rather than by being embed, so it does | 77 // This app starts its life via Connect() rather than by being embed, so it does |
53 // not start with a connection to the ViewManager service. It has to obtain a | 78 // not start with a connection to the ViewManager service. It has to obtain a |
54 // connection by connecting to the ViewManagerInit service and asking to be | 79 // connection by connecting to the ViewManagerInit service and asking to be |
55 // embed without a view context. | 80 // embed without a view context. |
56 class WMFlowApp : public mojo::ApplicationDelegate, | 81 class WMFlowApp : public mojo::ApplicationDelegate, |
57 public mojo::ViewManagerDelegate, | 82 public mojo::ViewManagerDelegate, |
58 public mojo::ViewObserver { | 83 public mojo::ViewObserver { |
59 public: | 84 public: |
(...skipping 16 matching lines...) Expand all Loading... |
76 OpenNewWindow(); | 101 OpenNewWindow(); |
77 OpenNewWindow(); | 102 OpenNewWindow(); |
78 } | 103 } |
79 virtual bool ConfigureIncomingConnection( | 104 virtual bool ConfigureIncomingConnection( |
80 mojo::ApplicationConnection* connection) override { | 105 mojo::ApplicationConnection* connection) override { |
81 connection->AddService(view_manager_client_factory_.get()); | 106 connection->AddService(view_manager_client_factory_.get()); |
82 return true; | 107 return true; |
83 } | 108 } |
84 | 109 |
85 // Overridden from mojo::ViewManagerDelegate: | 110 // Overridden from mojo::ViewManagerDelegate: |
86 virtual void OnEmbed( | 111 virtual void OnEmbed(mojo::View* root, |
87 mojo::View* root, | 112 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
88 mojo::ServiceProviderImpl* exported_services, | 113 mojo::ServiceProviderPtr exposed_services) override { |
89 scoped_ptr<mojo::ServiceProvider> imported_services) override { | |
90 root->AddObserver(this); | 114 root->AddObserver(this); |
91 mojo::BitmapUploader* uploader = new mojo::BitmapUploader(root); | 115 mojo::BitmapUploader* uploader = new mojo::BitmapUploader(root); |
92 uploaders_[root] = uploader; | 116 uploaders_[root] = uploader; |
93 uploader->Init(shell_); | 117 uploader->Init(shell_); |
94 // BitmapUploader does not track view size changes, we would | 118 // BitmapUploader does not track view size changes, we would |
95 // need to subscribe to OnViewBoundsChanged and tell the bitmap uploader | 119 // need to subscribe to OnViewBoundsChanged and tell the bitmap uploader |
96 // to invalidate itself. This is better done once if had a per-view | 120 // to invalidate itself. This is better done once if had a per-view |
97 // object instead of holding per-view state on the ApplicationDelegate. | 121 // object instead of holding per-view state on the ApplicationDelegate. |
98 uploader->SetColor(kColors[embed_count_++ % arraysize(kColors)]); | 122 uploader->SetColor(kColors[embed_count_++ % arraysize(kColors)]); |
99 | 123 |
100 mojo::View* embed = root->view_manager()->CreateView(); | 124 mojo::View* embed = root->view_manager()->CreateView(); |
101 root->AddChild(embed); | 125 root->AddChild(embed); |
102 mojo::Rect bounds; | 126 mojo::Rect bounds; |
103 bounds.x = bounds.y = 25; | 127 bounds.x = bounds.y = 25; |
104 bounds.width = root->bounds().width - 50; | 128 bounds.width = root->bounds().width - 50; |
105 bounds.height = root->bounds().height - 50; | 129 bounds.height = root->bounds().height - 50; |
106 embed->SetBounds(bounds); | 130 embed->SetBounds(bounds); |
107 embed->SetVisible(true); | 131 embed->SetVisible(true); |
108 | 132 |
109 scoped_ptr<mojo::ServiceProviderImpl> registry( | 133 mojo::ServiceProviderPtr embedee_exposed_services; |
110 new mojo::ServiceProviderImpl); | 134 embeddee_provider_.AddBinding(GetProxy(&embedee_exposed_services)); |
111 // Expose some services to the embeddee... | |
112 registry->AddService(&embedder_factory_); | |
113 | 135 |
114 GURL embedded_app_url = url_.Resolve("wm_flow_embedded.mojo"); | 136 GURL embedded_app_url = url_.Resolve("wm_flow_embedded.mojo"); |
115 scoped_ptr<mojo::ServiceProvider> imported = | 137 mojo::ServiceProviderPtr imported; |
116 embed->Embed(embedded_app_url.spec(), registry.Pass()); | 138 embed->Embed(embedded_app_url.spec(), GetProxy(&imported), |
| 139 embedee_exposed_services.Pass()); |
117 // FIXME: This is wrong. We're storing per-view state on this per-app | 140 // FIXME: This is wrong. We're storing per-view state on this per-app |
118 // delegate. Every time a new view is created embedee_ is replaced | 141 // delegate. Every time a new view is created embedee_ is replaced |
119 // causing the previous one to shut down. This class should not | 142 // causing the previous one to shut down. This class should not |
120 // be a ViewManagerDelegate, but rather a separate object should be | 143 // be a ViewManagerDelegate, but rather a separate object should be |
121 // created for each embedding. | 144 // created for each embedding. |
122 mojo::ConnectToService(imported.get(), &embeddee_); | 145 mojo::ConnectToService(imported.get(), &embeddee_); |
123 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck, | 146 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck, |
124 base::Unretained(this))); | 147 base::Unretained(this))); |
125 } | 148 } |
126 virtual void OnViewManagerDisconnected( | 149 virtual void OnViewManagerDisconnected( |
(...skipping 20 matching lines...) Expand all Loading... |
147 | 170 |
148 void HelloBackAck() { | 171 void HelloBackAck() { |
149 printf("HelloBack() ack'ed\n"); | 172 printf("HelloBack() ack'ed\n"); |
150 } | 173 } |
151 | 174 |
152 void OpenNewWindow() { view_manager_context_->Embed(url_.spec()); } | 175 void OpenNewWindow() { view_manager_context_->Embed(url_.spec()); } |
153 | 176 |
154 mojo::Shell* shell_; | 177 mojo::Shell* shell_; |
155 int embed_count_; | 178 int embed_count_; |
156 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | 179 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; |
157 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; | |
158 scoped_ptr<mojo::ViewManagerContext> view_manager_context_; | 180 scoped_ptr<mojo::ViewManagerContext> view_manager_context_; |
159 EmbeddeePtr embeddee_; | 181 EmbeddeePtr embeddee_; |
| 182 EmbedderImplProvider embeddee_provider_; |
160 ViewToUploader uploaders_; | 183 ViewToUploader uploaders_; |
161 GURL url_; | 184 GURL url_; |
162 | 185 |
163 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); | 186 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); |
164 }; | 187 }; |
165 | 188 |
166 } // namespace examples | 189 } // namespace examples |
167 | 190 |
168 MojoResult MojoMain(MojoHandle shell_handle) { | 191 MojoResult MojoMain(MojoHandle shell_handle) { |
169 mojo::ApplicationRunnerChromium runner(new examples::WMFlowApp); | 192 mojo::ApplicationRunnerChromium runner(new examples::WMFlowApp); |
170 return runner.Run(shell_handle); | 193 return runner.Run(shell_handle); |
171 } | 194 } |
OLD | NEW |