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/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "mojo/examples/window_manager/window_manager.mojom.h"
10 #include "mojo/public/cpp/application/application.h"
11 #include "mojo/services/public/cpp/view_manager/node.h"
12 #include "mojo/services/public/cpp/view_manager/node_observer.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_delegate.h"
16 #include "mojo/services/public/cpp/view_manager/view_observer.h"
17 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
18 #include "ui/events/event_constants.h"
19 #include "url/gurl.h"
20 #include "url/url_util.h"
21
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;
28
29 namespace mojo {
30 namespace examples {
31
32 class EmbeddedApp : public Application,
33 public ViewManagerDelegate,
34 public ViewObserver,
35 public NodeObserver {
36 public:
EmbeddedApp()37 EmbeddedApp() : view_manager_(NULL) {
38 url::AddStandardScheme("mojo");
39 }
~EmbeddedApp()40 virtual ~EmbeddedApp() {}
41
SetNodeColor(uint32 node_id,SkColor color)42 void SetNodeColor(uint32 node_id, SkColor color) {
43 pending_node_colors_[node_id] = color;
44 ProcessPendingNodeColor(node_id);
45 }
46
47 private:
48 class Navigator : public InterfaceImpl<navigation::Navigator> {
49 public:
Navigator(EmbeddedApp * app)50 explicit Navigator(EmbeddedApp* app) : app_(app) {}
51 private:
Navigate(uint32 node_id,navigation::NavigationDetailsPtr navigation_details,navigation::ResponseDetailsPtr response_details)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;
60 }
61 // TODO(aa): Verify new URL is same origin as current origin.
62 SkColor color = 0x00;
63 if (!base::HexStringToUInt(url.path().substr(1), &color)) {
64 LOG(ERROR) << "Invalid URL, path not convertible to integer";
65 return;
66 }
67 app_->SetNodeColor(node_id, color);
68 }
69 EmbeddedApp* app_;
70 DISALLOW_COPY_AND_ASSIGN(Navigator);
71 };
72
73 // Overridden from Application:
Initialize()74 virtual void Initialize() MOJO_OVERRIDE {
75 ViewManager::Create(this, this);
76 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like
77 // embedder should be able to specify the SP embeddee receives, then
78 // communication can be anonymous.
79 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_);
80 AddService<Navigator>(this);
81 }
82
83 // Overridden from ViewManagerDelegate:
OnRootAdded(ViewManager * view_manager,Node * root)84 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE {
85 View* view = View::Create(view_manager);
86 view->AddObserver(this);
87 root->SetActiveView(view);
88 root->AddObserver(this);
89
90 roots_[root->id()] = root;
91 ProcessPendingNodeColor(root->id());
92 }
93
94 // Overridden from ViewObserver:
OnViewInputEvent(View * view,const EventPtr & event)95 virtual void OnViewInputEvent(View* view, const EventPtr& event) OVERRIDE {
96 if (event->action == ui::ET_MOUSE_RELEASED)
97 window_manager_->CloseWindow(view->node()->id());
98 }
99
100 // Overridden from NodeObserver:
OnNodeActiveViewChange(Node * node,View * old_view,View * new_view,NodeObserver::DispositionChangePhase phase)101 virtual void OnNodeActiveViewChange(
102 Node* node,
103 View* old_view,
104 View* new_view,
105 NodeObserver::DispositionChangePhase phase) OVERRIDE {
106 if (new_view == 0)
107 views_to_reap_[node] = old_view;
108 }
OnNodeDestroy(Node * node,NodeObserver::DispositionChangePhase phase)109 virtual void OnNodeDestroy(
110 Node* node,
111 NodeObserver::DispositionChangePhase phase) OVERRIDE {
112 if (phase != NodeObserver::DISPOSITION_CHANGED)
113 return;
114 DCHECK(roots_.find(node->id()) != roots_.end());
115 roots_.erase(node->id());
116 std::map<Node*, View*>::const_iterator it = views_to_reap_.find(node);
117 if (it != views_to_reap_.end())
118 it->second->Destroy();
119 }
120
ProcessPendingNodeColor(uint32 node_id)121 void ProcessPendingNodeColor(uint32 node_id) {
122 RootMap::iterator root = roots_.find(node_id);
123 if (root == roots_.end())
124 return;
125
126 PendingNodeColors::iterator color = pending_node_colors_.find(node_id);
127 if (color == pending_node_colors_.end())
128 return;
129
130 root->second->active_view()->SetColor(color->second);
131 pending_node_colors_.erase(color);
132 }
133
134 view_manager::ViewManager* view_manager_;
135 IWindowManagerPtr window_manager_;
136 std::map<Node*, View*> views_to_reap_;
137
138 typedef std::map<view_manager::Id, Node*> RootMap;
139 RootMap roots_;
140
141 // We can receive navigations for nodes we don't have yet.
142 typedef std::map<uint32, SkColor> PendingNodeColors;
143 PendingNodeColors pending_node_colors_;
144
145 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp);
146 };
147
148 } // namespace examples
149
150 // static
Create()151 Application* Application::Create() {
152 return new examples::EmbeddedApp;
153 }
154
155 } // namespace mojo
156