Currently, bionic only build a subset of the examples. This changes turns on all...
[chromium-blink-merge.git] / mojo / examples / nesting_app / nesting_app.cc
blob9b488d280fa25b624ab10bfba4f31e6d7de12e54
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.
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/services/public/cpp/view_manager/node.h"
13 #include "mojo/services/public/cpp/view_manager/node_observer.h"
14 #include "mojo/services/public/cpp/view_manager/view.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager.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"
22 using mojo::view_manager::Node;
23 using mojo::view_manager::NodeObserver;
24 using mojo::view_manager::View;
25 using mojo::view_manager::ViewManager;
26 using mojo::view_manager::ViewManagerDelegate;
27 using mojo::view_manager::ViewObserver;
29 namespace mojo {
30 namespace examples {
32 namespace {
33 const char kEmbeddedAppURL[] = "mojo:mojo_embedded_app";
36 // An app that embeds another app.
37 // TODO(davemoore): Is this the right name?
38 class NestingApp : public ApplicationDelegate,
39 public ViewManagerDelegate,
40 public ViewObserver,
41 public NodeObserver {
42 public:
43 NestingApp() : nested_(NULL) {}
44 virtual ~NestingApp() {}
46 private:
47 class Navigator : public InterfaceImpl<navigation::Navigator> {
48 public:
49 explicit Navigator(ApplicationConnection* connection,
50 NestingApp* app) : app_(app) {}
51 private:
52 virtual void Navigate(
53 uint32 node_id,
54 navigation::NavigationDetailsPtr navigation_details,
55 navigation::ResponseDetailsPtr response_details) OVERRIDE {
56 GURL url(navigation_details->url.To<std::string>());
57 if (!url.is_valid()) {
58 LOG(ERROR) << "URL is invalid.";
59 return;
61 app_->color_ = url.path().substr(1);
62 app_->NavigateChild();
64 NestingApp* app_;
65 DISALLOW_COPY_AND_ASSIGN(Navigator);
68 // Overridden from ApplicationImpl:
69 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
70 MOJO_OVERRIDE {
71 ViewManager::ConfigureIncomingConnection(connection, this);
72 connection->ConnectToService(&window_manager_);
73 connection->AddService<Navigator>(this);
74 // TODO(davemoore): Is this ok?
75 if (!navigator_.get()) {
76 connection->ConnectToApplication(
77 kEmbeddedAppURL)->ConnectToService(&navigator_);
79 return true;
82 // Overridden from ViewManagerDelegate:
83 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
84 root->AddObserver(this);
86 View* view = View::Create(view_manager);
87 root->SetActiveView(view);
88 view->SetColor(SK_ColorCYAN);
89 view->AddObserver(this);
91 nested_ = Node::Create(view_manager);
92 root->AddChild(nested_);
93 nested_->SetBounds(gfx::Rect(20, 20, 50, 50));
94 nested_->Embed(kEmbeddedAppURL);
96 NavigateChild();
98 virtual void OnViewManagerDisconnected(ViewManager* view_manager) OVERRIDE {
99 base::MessageLoop::current()->Quit();
102 // Overridden from ViewObserver:
103 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
104 if (event->action == ui::ET_MOUSE_RELEASED)
105 window_manager_->CloseWindow(view->node()->id());
108 // Overridden from NodeObserver:
109 virtual void OnNodeDestroyed(Node* node) OVERRIDE {
110 // TODO(beng): reap views & child nodes.
111 nested_ = NULL;
114 void NavigateChild() {
115 if (!color_.empty() && nested_) {
116 navigation::NavigationDetailsPtr details(
117 navigation::NavigationDetails::New());
118 details->url =
119 base::StringPrintf("%s/%s", kEmbeddedAppURL, color_.c_str());
120 navigation::ResponseDetailsPtr response_details(
121 navigation::ResponseDetails::New());
122 navigator_->Navigate(nested_->id(),
123 details.Pass(),
124 response_details.Pass());
128 std::string color_;
129 Node* nested_;
130 navigation::NavigatorPtr navigator_;
131 IWindowManagerPtr window_manager_;
133 DISALLOW_COPY_AND_ASSIGN(NestingApp);
136 } // namespace examples
138 // static
139 ApplicationDelegate* ApplicationDelegate::Create() {
140 return new examples::NestingApp;
143 } // namespace mojo