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 "mojo/application/application_runner_chromium.h"
7 #include "mojo/examples/wm_flow/app/embedder.mojom.h"
8 #include "mojo/examples/wm_flow/embedded/embeddee.mojom.h"
9 #include "mojo/public/cpp/application/application_connection.h"
10 #include "mojo/public/cpp/application/application_delegate.h"
11 #include "mojo/public/cpp/application/application_impl.h"
12 #include "mojo/public/cpp/application/connect.h"
13 #include "mojo/public/cpp/application/interface_factory_impl.h"
14 #include "mojo/public/cpp/application/service_provider_impl.h"
15 #include "mojo/services/public/cpp/view_manager/view.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"
18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
19
20 namespace examples {
21
22 namespace {
23
24 class EmbeddeeImpl : public mojo::InterfaceImpl<Embeddee> {
25 public:
EmbeddeeImpl()26 EmbeddeeImpl() {}
~EmbeddeeImpl()27 virtual ~EmbeddeeImpl() {}
28
29 private:
30 // Overridden from Embeddee:
HelloBack(const mojo::Callback<void ()> & callback)31 virtual void HelloBack(const mojo::Callback<void()>& callback) OVERRIDE {
32 callback.Run();
33 }
34
35 DISALLOW_COPY_AND_ASSIGN(EmbeddeeImpl);
36 };
37
38 } // namespace
39
40 class WMFlowEmbedded : public mojo::ApplicationDelegate,
41 public mojo::ViewManagerDelegate {
42 public:
WMFlowEmbedded()43 WMFlowEmbedded() {}
~WMFlowEmbedded()44 virtual ~WMFlowEmbedded() {}
45
46 private:
47 // Overridden from Application:
Initialize(mojo::ApplicationImpl * app)48 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE {
49 view_manager_client_factory_.reset(
50 new mojo::ViewManagerClientFactory(app->shell(), this));
51 }
ConfigureIncomingConnection(mojo::ApplicationConnection * connection)52 virtual bool ConfigureIncomingConnection(
53 mojo::ApplicationConnection* connection) MOJO_OVERRIDE {
54 connection->AddService(view_manager_client_factory_.get());
55 return true;
56 }
57
58 // Overridden from mojo::ViewManagerDelegate:
OnEmbed(mojo::ViewManager * view_manager,mojo::View * root,mojo::ServiceProviderImpl * exported_services,scoped_ptr<mojo::ServiceProvider> imported_services)59 virtual void OnEmbed(
60 mojo::ViewManager* view_manager,
61 mojo::View* root,
62 mojo::ServiceProviderImpl* exported_services,
63 scoped_ptr<mojo::ServiceProvider> imported_services) MOJO_OVERRIDE {
64 root->SetColor(SK_ColorMAGENTA);
65
66 exported_services->AddService(&embeddee_factory_);
67 mojo::ConnectToService(imported_services.get(), &embedder_);
68 embedder_->HelloWorld(base::Bind(&WMFlowEmbedded::HelloWorldAck,
69 base::Unretained(this)));
70 }
OnViewManagerDisconnected(mojo::ViewManager * view_manager)71 virtual void OnViewManagerDisconnected(
72 mojo::ViewManager* view_manager) MOJO_OVERRIDE {}
73
HelloWorldAck()74 void HelloWorldAck() {
75 printf("HelloWorld() ack'ed\n");
76 }
77
78 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_;
79 EmbedderPtr embedder_;
80 mojo::InterfaceFactoryImpl<EmbeddeeImpl> embeddee_factory_;
81
82 DISALLOW_COPY_AND_ASSIGN(WMFlowEmbedded);
83 };
84
85 } // namespace examples
86
MojoMain(MojoHandle shell_handle)87 MojoResult MojoMain(MojoHandle shell_handle) {
88 mojo::ApplicationRunnerChromium runner(new examples::WMFlowEmbedded);
89 return runner.Run(shell_handle);
90 }
91
92