OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/bind.h" | |
6 #include "base/macros.h" | |
7 #include "examples/bitmap_uploader/bitmap_uploader.h" | |
8 #include "examples/wm_flow/app/embedder.mojom.h" | |
9 #include "examples/wm_flow/embedded/embeddee.mojom.h" | |
10 #include "mojo/application/application_runner_chromium.h" | |
11 #include "mojo/public/c/system/main.h" | |
12 #include "mojo/public/cpp/application/application_connection.h" | |
13 #include "mojo/public/cpp/application/application_delegate.h" | |
14 #include "mojo/public/cpp/application/application_impl.h" | |
15 #include "mojo/public/cpp/application/connect.h" | |
16 #include "mojo/public/cpp/application/service_provider_impl.h" | |
17 #include "mojo/public/cpp/bindings/strong_binding.h" | |
18 #include "mojo/services/view_manager/cpp/view.h" | |
19 #include "mojo/services/view_manager/cpp/view_manager.h" | |
20 #include "mojo/services/view_manager/cpp/view_manager_client_factory.h" | |
21 #include "mojo/services/view_manager/cpp/view_manager_delegate.h" | |
22 #include "third_party/skia/include/core/SkColor.h" | |
23 | |
24 namespace examples { | |
25 | |
26 namespace { | |
27 | |
28 class EmbeddeeImpl : public Embeddee { | |
29 public: | |
30 explicit EmbeddeeImpl(mojo::InterfaceRequest<Embeddee> request) | |
31 : binding_(this, request.Pass()) {} | |
32 ~EmbeddeeImpl() override {} | |
33 | |
34 private: | |
35 // Overridden from Embeddee: | |
36 void HelloBack(const mojo::Callback<void()>& callback) override { | |
37 callback.Run(); | |
38 } | |
39 | |
40 mojo::StrongBinding<Embeddee> binding_; | |
41 DISALLOW_COPY_AND_ASSIGN(EmbeddeeImpl); | |
42 }; | |
43 | |
44 } // namespace | |
45 | |
46 class WMFlowEmbedded : public mojo::ApplicationDelegate, | |
47 public mojo::ViewManagerDelegate, | |
48 public mojo::InterfaceFactory<Embeddee> { | |
49 public: | |
50 WMFlowEmbedded() : shell_(nullptr) { | |
51 embeddee_provider_impl_.AddService(this); | |
52 } | |
53 ~WMFlowEmbedded() override {} | |
54 | |
55 private: | |
56 // Overridden from Application: | |
57 void Initialize(mojo::ApplicationImpl* app) override { | |
58 shell_ = app->shell(); | |
59 view_manager_client_factory_.reset( | |
60 new mojo::ViewManagerClientFactory(app->shell(), this)); | |
61 } | |
62 bool ConfigureIncomingConnection( | |
63 mojo::ApplicationConnection* connection) override { | |
64 connection->AddService(view_manager_client_factory_.get()); | |
65 return true; | |
66 } | |
67 | |
68 // Overridden from mojo::ViewManagerDelegate: | |
69 void OnEmbed(mojo::View* root, | |
70 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
71 mojo::ServiceProviderPtr exposed_services) override { | |
72 bitmap_uploader_.reset(new mojo::BitmapUploader(root)); | |
73 bitmap_uploader_->Init(shell_); | |
74 // BitmapUploader does not track view size changes, we would | |
75 // need to subscribe to OnViewBoundsChanged and tell the bitmap uploader | |
76 // to invalidate itself. This is better done once if had a per-view | |
77 // object instead of holding per-view state on the ApplicationDelegate. | |
78 bitmap_uploader_->SetColor(SK_ColorMAGENTA); | |
79 | |
80 embeddee_provider_impl_.Bind(services.Pass()); | |
81 // FIXME: embedder_ is wrong for the same reason the embedee_ storage is | |
82 // wrong in app.cc. We need separate per-instance storage not on the | |
83 // application delegate. | |
84 mojo::ConnectToService(exposed_services.get(), &embedder_); | |
85 embedder_->HelloWorld(base::Bind(&WMFlowEmbedded::HelloWorldAck, | |
86 base::Unretained(this))); | |
87 } | |
88 void OnViewManagerDisconnected(mojo::ViewManager* view_manager) override {} | |
89 | |
90 // Overridden from mojo::InterfaceFactory<Embeddee>: | |
91 void Create(mojo::ApplicationConnection* app, | |
92 mojo::InterfaceRequest<Embeddee> request) override { | |
93 new EmbeddeeImpl(request.Pass()); | |
94 } | |
95 | |
96 void HelloWorldAck() { | |
97 printf("HelloWorld() ack'ed\n"); | |
98 } | |
99 | |
100 mojo::Shell* shell_; | |
101 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_; | |
102 EmbedderPtr embedder_; | |
103 mojo::ServiceProviderImpl embeddee_provider_impl_; | |
104 scoped_ptr<mojo::BitmapUploader> bitmap_uploader_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(WMFlowEmbedded); | |
107 }; | |
108 | |
109 } // namespace examples | |
110 | |
111 MojoResult MojoMain(MojoHandle application_request) { | |
112 mojo::ApplicationRunnerChromium runner(new examples::WMFlowEmbedded); | |
113 return runner.Run(application_request); | |
114 } | |
115 | |
OLD | NEW |