023c44b4f400385d7ae7e9891af0daad9c5e0284
[platform/framework/web/crosswalk.git] / src / mojo / examples / wm_flow / wm / wm.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 "mojo/public/cpp/application/application_delegate.h"
6 #include "mojo/public/cpp/application/service_provider_impl.h"
7 #include "mojo/services/public/cpp/view_manager/view_manager.h"
8 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
9 #include "mojo/services/public/cpp/view_manager/window_manager_delegate.h"
10 #include "mojo/services/window_manager/window_manager_app.h"
11
12 namespace examples {
13
14 class SimpleWM : public mojo::ApplicationDelegate,
15                  public mojo::ViewManagerDelegate,
16                  public mojo::WindowManagerDelegate {
17  public:
18   SimpleWM()
19       : window_manager_app_(new mojo::WindowManagerApp(this, this)),
20         view_manager_(NULL),
21         root_(NULL),
22         window_container_(NULL),
23         next_window_origin_(10, 10) {}
24   virtual ~SimpleWM() {}
25
26  private:
27   // Overridden from mojo::ApplicationDelegate:
28   virtual void Initialize(mojo::ApplicationImpl* impl) MOJO_OVERRIDE {
29     window_manager_app_->Initialize(impl);
30   }
31   virtual bool ConfigureIncomingConnection(
32       mojo::ApplicationConnection* connection) MOJO_OVERRIDE {
33     window_manager_app_->ConfigureIncomingConnection(connection);
34     return true;
35   }
36
37   // Overridden from mojo::ViewManagerDelegate:
38   virtual void OnEmbed(
39       mojo::ViewManager* view_manager,
40       mojo::View* root,
41       mojo::ServiceProviderImpl* exported_services,
42       scoped_ptr<mojo::ServiceProvider> remote_service_provider) MOJO_OVERRIDE {
43     view_manager_ = view_manager;
44     root_ = root;
45
46     window_container_ = mojo::View::Create(view_manager_);
47     window_container_->SetBounds(root_->bounds());
48     root_->AddChild(window_container_);
49
50   }
51   virtual void OnViewManagerDisconnected(
52       mojo::ViewManager* view_manager) MOJO_OVERRIDE {
53     view_manager_ = NULL;
54     root_ = NULL;
55   }
56
57   // Overridden from mojo::WindowManagerDelegate:
58   virtual void Embed(
59       const mojo::String& url,
60       mojo::InterfaceRequest<mojo::ServiceProvider> service_provider)
61           MOJO_OVERRIDE {
62     mojo::View* embed_view = mojo::View::Create(view_manager_);
63     embed_view->SetBounds(gfx::Rect(next_window_origin_, gfx::Size(400, 400)));
64     window_container_->AddChild(embed_view);
65
66     // TODO(beng): We're dropping the |service_provider| passed from the client
67     //             on the floor here and passing our own. Seems like we should
68     //             be sending both. I'm not yet sure how this sould work for
69     //             N levels of proxying.
70     embed_view->Embed(url, scoped_ptr<mojo::ServiceProviderImpl>(
71         new mojo::ServiceProviderImpl).Pass());
72     next_window_origin_.Offset(50, 50);
73   }
74   virtual void DispatchEvent(mojo::EventPtr event) MOJO_OVERRIDE {}
75
76   scoped_ptr<mojo::WindowManagerApp> window_manager_app_;
77
78   mojo::ViewManager* view_manager_;
79   mojo::View* root_;
80   mojo::View* window_container_;
81
82   gfx::Point next_window_origin_;
83
84   DISALLOW_COPY_AND_ASSIGN(SimpleWM);
85 };
86
87 }  // namespace examples
88
89 namespace mojo {
90
91 // static
92 ApplicationDelegate* ApplicationDelegate::Create() {
93   return new examples::SimpleWM;
94 }
95
96 }  // namespace