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.
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/c/system/main.h"
10 #include "mojo/public/cpp/application/application_connection.h"
11 #include "mojo/public/cpp/application/application_delegate.h"
12 #include "mojo/public/cpp/application/application_impl.h"
13 #include "mojo/public/cpp/application/connect.h"
14 #include "mojo/public/cpp/application/interface_factory_impl.h"
15 #include "mojo/public/cpp/application/service_provider_impl.h"
16 #include "mojo/public/interfaces/application/service_provider.mojom.h"
17 #include "mojo/services/public/cpp/view_manager/view.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager_context.h"
21 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
22 #include "mojo/services/public/cpp/view_manager/view_observer.h"
27 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW };
29 class EmbedderImpl : public mojo::InterfaceImpl<Embedder> {
32 virtual ~EmbedderImpl() {}
35 // Overridden from Embedder:
36 virtual void HelloWorld(const mojo::Callback<void()>& callback) OVERRIDE {
40 DISALLOW_COPY_AND_ASSIGN(EmbedderImpl);
45 // This app starts its life via Connect() rather than by being embed, so it does
46 // not start with a connection to the ViewManager service. It has to obtain a
47 // connection by connecting to the ViewManagerInit service and asking to be
48 // embed without a view context.
49 class WMFlowApp : public mojo::ApplicationDelegate,
50 public mojo::ViewManagerDelegate,
51 public mojo::ViewObserver {
53 WMFlowApp() : embed_count_(0) {}
54 virtual ~WMFlowApp() {}
57 // Overridden from Application:
58 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE {
59 view_manager_client_factory_.reset(
60 new mojo::ViewManagerClientFactory(app->shell(), this));
61 view_manager_context_.reset(new mojo::ViewManagerContext(app));
66 virtual bool ConfigureIncomingConnection(
67 mojo::ApplicationConnection* connection) MOJO_OVERRIDE {
68 connection->AddService(view_manager_client_factory_.get());
72 void OnConnect(bool success) {}
74 // Overridden from mojo::ViewManagerDelegate:
76 mojo::ViewManager* view_manager,
78 mojo::ServiceProviderImpl* exported_services,
79 scoped_ptr<mojo::ServiceProvider> imported_services) MOJO_OVERRIDE {
80 root->AddObserver(this);
81 root->SetColor(kColors[embed_count_++ % arraysize(kColors)]);
83 mojo::View* embed = mojo::View::Create(view_manager);
84 root->AddChild(embed);
85 gfx::Rect bounds = gfx::Rect(root->bounds().size());
87 embed->SetBounds(bounds);
89 scoped_ptr<mojo::ServiceProviderImpl> registry(
90 new mojo::ServiceProviderImpl);
91 // Expose some services to the embeddee...
92 registry->AddService(&embedder_factory_);
93 scoped_ptr<mojo::ServiceProvider> imported =
94 embed->Embed("mojo:mojo_wm_flow_embedded", registry.Pass());
95 mojo::ConnectToService(imported.get(), &embeddee_);
96 embeddee_->HelloBack(base::Bind(&WMFlowApp::HelloBackAck,
97 base::Unretained(this)));
99 virtual void OnViewManagerDisconnected(
100 mojo::ViewManager* view_manager) MOJO_OVERRIDE {}
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) {
110 virtual void OnViewDestroyed(mojo::View* view) MOJO_OVERRIDE {
112 view->RemoveObserver(this);
115 void HelloBackAck() {
116 printf("HelloBack() ack'ed\n");
119 void OpenNewWindow() {
120 view_manager_context_->Embed("mojo:mojo_wm_flow_app");
124 scoped_ptr<mojo::ViewManagerClientFactory> view_manager_client_factory_;
125 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_;
126 scoped_ptr<mojo::ViewManagerContext> view_manager_context_;
127 EmbeddeePtr embeddee_;
129 DISALLOW_COPY_AND_ASSIGN(WMFlowApp);
132 } // namespace examples
134 MojoResult MojoMain(MojoHandle shell_handle) {
135 mojo::ApplicationRunnerChromium runner(new examples::WMFlowApp);
136 return runner.Run(shell_handle);