app.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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/examples/wm_flow/app/embedder.mojom.h" 7#include "mojo/examples/wm_flow/embedded/embeddee.mojom.h" 8#include "mojo/public/cpp/application/application_connection.h" 9#include "mojo/public/cpp/application/application_delegate.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" 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#include "mojo/services/public/cpp/view_manager/view_observer.h" 20#include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h" 21 22namespace examples { 23namespace { 24void ConnectCallback(bool success) {} 25 26const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW }; 27 28class 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 42} // namespace 43 44// This app starts its life via Connect() rather than by being embed, so it does 45// not start with a connection to the ViewManager service. It has to obtain a 46// connection by connecting to the ViewManagerInit service and asking to be 47// embed without a view context. 48class WMFlowApp : public mojo::ApplicationDelegate, 49 public mojo::ViewManagerDelegate, 50 public mojo::ViewObserver { 51 public: 52 WMFlowApp() 53 : embed_count_(0), 54 view_manager_client_factory_(this), 55 app_(NULL) {} 56 virtual ~WMFlowApp() {} 57 58 private: 59 // Overridden from Application: 60 virtual void Initialize(mojo::ApplicationImpl* app) MOJO_OVERRIDE { 61 app_ = app; 62 OpenNewWindow(); 63 OpenNewWindow(); 64 OpenNewWindow(); 65 } 66 virtual bool ConfigureIncomingConnection( 67 mojo::ApplicationConnection* connection) MOJO_OVERRIDE { 68 connection->AddService(&view_manager_client_factory_); 69 return true; 70 } 71 72 void OnConnect(bool success) {} 73 74 // Overridden from mojo::ViewManagerDelegate: 75 virtual void OnEmbed( 76 mojo::ViewManager* view_manager, 77 mojo::View* root, 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)]); 82 83 mojo::View* embed = mojo::View::Create(view_manager); 84 root->AddChild(embed); 85 gfx::Rect bounds = gfx::Rect(root->bounds().size()); 86 bounds.Inset(25, 25); 87 embed->SetBounds(bounds); 88 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))); 98 } 99 virtual void OnViewManagerDisconnected( 100 mojo::ViewManager* view_manager) MOJO_OVERRIDE {} 101 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) { 107 OpenNewWindow(); 108 } 109 } 110 virtual void OnViewDestroyed(mojo::View* view) MOJO_OVERRIDE { 111 --embed_count_; 112 view->RemoveObserver(this); 113 } 114 115 void HelloBackAck() { 116 printf("HelloBack() ack'ed\n"); 117 } 118 119 void OpenNewWindow() { 120 mojo::ViewManagerInitServicePtr init_svc; 121 app_->ConnectToService("mojo:mojo_view_manager", &init_svc); 122 mojo::ServiceProviderPtr sp; 123 init_svc->Embed("mojo:mojo_wm_flow_app", sp.Pass(), 124 base::Bind(&ConnectCallback)); 125 } 126 127 int embed_count_; 128 mojo::ViewManagerClientFactory view_manager_client_factory_; 129 mojo::InterfaceFactoryImpl<EmbedderImpl> embedder_factory_; 130 EmbeddeePtr embeddee_; 131 mojo::ApplicationImpl* app_; 132 133 DISALLOW_COPY_AND_ASSIGN(WMFlowApp); 134}; 135 136} // namespace examples 137 138namespace mojo { 139 140// static 141ApplicationDelegate* ApplicationDelegate::Create() { 142 return new examples::WMFlowApp; 143} 144 145} // namespace 146