Upstream version 9.38.198.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/examples/window_manager/window_manager.mojom.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/interface_factory_impl.h"
13 #include "mojo/services/public/cpp/view_manager/view.h"
14 #include "mojo/services/public/cpp/view_manager/view_manager.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
16 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
17 #include "mojo/services/public/cpp/view_manager/view_observer.h"
18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
19 #include "ui/events/event_constants.h"
20 #include "url/gurl.h"
21
22 namespace mojo {
23 namespace examples {
24
25 namespace {
26 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
27 }
28
29 class NestingApp;
30
31 class NavigatorImpl : public InterfaceImpl<Navigator> {
32  public:
33   explicit NavigatorImpl(NestingApp* app) : app_(app) {}
34
35  private:
36   virtual void Navigate(
37       uint32 node_id,
38       NavigationDetailsPtr navigation_details,
39       ResponseDetailsPtr response_details) OVERRIDE;
40
41   NestingApp* app_;
42   DISALLOW_COPY_AND_ASSIGN(NavigatorImpl);
43 };
44
45 // An app that embeds another app.
46 // TODO(davemoore): Is this the right name?
47 class NestingApp
48     : public ApplicationDelegate,
49       public ViewManagerDelegate,
50       public ViewObserver {
51  public:
52   NestingApp()
53       : navigator_factory_(this),
54         view_manager_client_factory_(this),
55         nested_(NULL) {}
56   virtual ~NestingApp() {}
57
58   void set_color(const std::string& color) { color_ = color; }
59
60   void NavigateChild() {
61     if (!color_.empty() && nested_) {
62       NavigationDetailsPtr details(NavigationDetails::New());
63       details->request->url =
64           base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str());
65       ResponseDetailsPtr response_details(ResponseDetails::New());
66       navigator_->Navigate(
67           nested_->id(), details.Pass(), response_details.Pass());
68     }
69   }
70
71  private:
72   // Overridden from ApplicationImpl:
73   virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
74       MOJO_OVERRIDE {
75     connection->ConnectToService(&window_manager_);
76     connection->AddService(&view_manager_client_factory_);
77     connection->AddService(&navigator_factory_);
78     // TODO(davemoore): Is this ok?
79     if (!navigator_) {
80       connection->ConnectToApplication(
81           kEmbeddedAppURL)->ConnectToService(&navigator_);
82     }
83     return true;
84   }
85
86   // Overridden from ViewManagerDelegate:
87   virtual void OnEmbed(ViewManager* view_manager,
88                        View* root,
89                        ServiceProviderImpl* exported_services,
90                        scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
91     root->AddObserver(this);
92     root->SetColor(SK_ColorCYAN);
93
94     nested_ = View::Create(view_manager);
95     root->AddChild(nested_);
96     nested_->SetBounds(gfx::Rect(20, 20, 50, 50));
97     nested_->Embed(kEmbeddedAppURL);
98
99     NavigateChild();
100   }
101   virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
102     base::MessageLoop::current()->Quit();
103   }
104
105   // Overridden from ViewObserver:
106   virtual void OnViewDestroyed(View* view) OVERRIDE {
107     // TODO(beng): reap views & child Views.
108     nested_ = NULL;
109   }
110   virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
111     if (event->action == EVENT_TYPE_MOUSE_RELEASED)
112       window_manager_->CloseWindow(view->id());
113   }
114
115   InterfaceFactoryImplWithContext<NavigatorImpl, NestingApp> navigator_factory_;
116   ViewManagerClientFactory view_manager_client_factory_;
117
118   std::string color_;
119   View* nested_;
120   NavigatorPtr navigator_;
121   IWindowManagerPtr window_manager_;
122
123   DISALLOW_COPY_AND_ASSIGN(NestingApp);
124 };
125
126 void NavigatorImpl::Navigate(uint32 node_id,
127                              NavigationDetailsPtr navigation_details,
128                              ResponseDetailsPtr response_details) {
129   GURL url(navigation_details->request->url.To<std::string>());
130   if (!url.is_valid()) {
131     LOG(ERROR) << "URL is invalid.";
132     return;
133   }
134   app_->set_color(url.path().substr(1));
135   app_->NavigateChild();
136 }
137
138 }  // namespace examples
139
140 // static
141 ApplicationDelegate* ApplicationDelegate::Create() {
142   return new examples::NestingApp;
143 }
144
145 }  // namespace mojo