Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / nesting_app / nesting_app.cc
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/basictypes.h"
6 #include "base/bind.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/stringprintf.h"
9 #include "mojo/application/application_runner_chromium.h"
10 #include "mojo/examples/window_manager/window_manager.mojom.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/interface_factory_impl.h"
16 #include "mojo/services/public/cpp/view_manager/view.h"
17 #include "mojo/services/public/cpp/view_manager/view_manager.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
20 #include "mojo/services/public/cpp/view_manager/view_observer.h"
21 #include "ui/events/event_constants.h"
22 #include "url/gurl.h"
23
24 namespace mojo {
25 namespace examples {
26
27 namespace {
28 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
29 }
30
31 class NestingApp;
32
33 // An app that embeds another app.
34 // TODO(davemoore): Is this the right name?
35 class NestingApp
36     : public ApplicationDelegate,
37       public ViewManagerDelegate,
38       public ViewObserver {
39  public:
40   NestingApp() : nested_(NULL) {}
41   virtual ~NestingApp() {}
42
43  private:
44   // Overridden from ApplicationDelegate:
45   virtual void Initialize(ApplicationImpl* app) MOJO_OVERRIDE {
46     view_manager_client_factory_.reset(
47         new ViewManagerClientFactory(app->shell(), this));
48   }
49
50   // Overridden from ApplicationImpl:
51   virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
52       MOJO_OVERRIDE {
53     connection->ConnectToService(&window_manager_);
54     connection->AddService(view_manager_client_factory_.get());
55     return true;
56   }
57
58   // Overridden from ViewManagerDelegate:
59   virtual void OnEmbed(ViewManager* view_manager,
60                        View* root,
61                        ServiceProviderImpl* exported_services,
62                        scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
63     root->AddObserver(this);
64     root->SetColor(SK_ColorCYAN);
65
66     nested_ = View::Create(view_manager);
67     root->AddChild(nested_);
68     nested_->SetBounds(gfx::Rect(20, 20, 50, 50));
69     nested_->Embed(kEmbeddedAppURL);
70   }
71   virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
72     base::MessageLoop::current()->Quit();
73   }
74
75   // Overridden from ViewObserver:
76   virtual void OnViewDestroyed(View* view) OVERRIDE {
77     // TODO(beng): reap views & child Views.
78     nested_ = NULL;
79   }
80   virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
81     if (event->action == EVENT_TYPE_MOUSE_RELEASED)
82       window_manager_->CloseWindow(view->id());
83   }
84
85   scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
86
87   View* nested_;
88   IWindowManagerPtr window_manager_;
89
90   DISALLOW_COPY_AND_ASSIGN(NestingApp);
91 };
92
93 }  // namespace examples
94 }  // namespace mojo
95
96 MojoResult MojoMain(MojoHandle shell_handle) {
97   mojo::ApplicationRunnerChromium runner(new mojo::examples::NestingApp);
98   return runner.Run(shell_handle);
99 }