Home | History | Annotate | Download | only in app
      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/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"
     23 
     24 namespace examples {
     25 namespace {
     26 
     27 const SkColor kColors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorYELLOW };
     28 
     29 class EmbedderImpl : public mojo::InterfaceImpl<Embedder> {
     30  public:
     31   EmbedderImpl() {}
     32   virtual ~EmbedderImpl() {}
     33 
     34  private:
     35   // Overridden from Embedder:
     36   virtual void HelloWorld(const mojo::Callback<void()>& callback) OVERRIDE {
     37     callback.Run();
     38   }
     39 
     40   DISALLOW_COPY_AND_ASSIGN(EmbedderImpl);
     41 };
     42 
     43 }  // namespace
     44 
     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 {
     52  public:
     53   WMFlowApp() : embed_count_(0) {}
     54   virtual ~WMFlowApp() {}
     55 
     56  private:
     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));
     62     OpenNewWindow();
     63     OpenNewWindow();
     64     OpenNewWindow();
     65   }
     66   virtual bool ConfigureIncomingConnection(
     67       mojo::ApplicationConnection* connection) MOJO_OVERRIDE {
     68     connection->AddService(view_manager_client_factory_.get());
     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     view_manager_context_->Embed("mojo:mojo_wm_flow_app");
    121   }
    122 
    123   int embed_count_;
    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_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(WMFlowApp);
    130 };
    131 
    132 }  // namespace examples
    133 
    134 MojoResult MojoMain(MojoHandle shell_handle) {
    135   mojo::ApplicationRunnerChromium runner(new examples::WMFlowApp);
    136   return runner.Run(shell_handle);
    137 }
    138