OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/logging.h" | |
8 #include "base/strings/string_number_conversions.h" | |
7 #include "mojo/examples/window_manager/window_manager.mojom.h" | 9 #include "mojo/examples/window_manager/window_manager.mojom.h" |
8 #include "mojo/public/cpp/application/application.h" | 10 #include "mojo/public/cpp/application/application.h" |
11 #include "mojo/services/navigation/navigation.mojom.h" | |
9 #include "mojo/services/public/cpp/view_manager/view.h" | 12 #include "mojo/services/public/cpp/view_manager/view.h" |
10 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
11 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 14 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
12 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 15 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
13 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" | 16 #include "mojo/services/public/cpp/view_manager/view_tree_node.h" |
14 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" | 17 #include "mojo/services/public/cpp/view_manager/view_tree_node_observer.h" |
15 #include "ui/events/event_constants.h" | 18 #include "ui/events/event_constants.h" |
16 | 19 |
17 using mojo::view_manager::View; | 20 using mojo::view_manager::View; |
18 using mojo::view_manager::ViewManager; | 21 using mojo::view_manager::ViewManager; |
19 using mojo::view_manager::ViewManagerDelegate; | 22 using mojo::view_manager::ViewManagerDelegate; |
20 using mojo::view_manager::ViewObserver; | 23 using mojo::view_manager::ViewObserver; |
21 using mojo::view_manager::ViewTreeNode; | 24 using mojo::view_manager::ViewTreeNode; |
22 using mojo::view_manager::ViewTreeNodeObserver; | 25 using mojo::view_manager::ViewTreeNodeObserver; |
23 | 26 |
24 namespace mojo { | 27 namespace mojo { |
25 namespace examples { | 28 namespace examples { |
26 namespace { | |
27 | |
28 const SkColor kColors[] = { SK_ColorYELLOW, | |
29 SK_ColorRED, | |
30 SK_ColorGREEN, | |
31 SK_ColorMAGENTA }; | |
32 | |
33 } // namespace | |
34 | 29 |
35 class EmbeddedApp : public Application, | 30 class EmbeddedApp : public Application, |
36 public ViewManagerDelegate, | 31 public ViewManagerDelegate, |
37 public ViewObserver, | 32 public ViewObserver, |
38 public ViewTreeNodeObserver { | 33 public ViewTreeNodeObserver { |
39 public: | 34 public: |
40 EmbeddedApp() {} | 35 EmbeddedApp() : view_manager_(NULL) {} |
41 virtual ~EmbeddedApp() {} | 36 virtual ~EmbeddedApp() {} |
42 | 37 |
38 void SetNodeColor(uint32 node_id, SkColor color) { | |
39 pending_node_colors_[node_id] = color; | |
40 ProcessPendingNodeColor(node_id); | |
41 } | |
42 | |
43 private: | 43 private: |
44 class ViewNavigator : public InterfaceImpl<navigation::ViewNavigator> { | |
45 public: | |
46 explicit ViewNavigator(EmbeddedApp* app) : app_(app) { | |
47 } | |
48 | |
49 private: | |
50 virtual void Navigate(uint32 node_id, | |
51 navigation::NavigationDetailsPtr details) OVERRIDE { | |
52 const std::string& url = details->url.To<std::string>(); | |
DaveMoore
2014/06/09 19:59:32
Nit: LOG(INFOS)'s should be removed before checkin
Aaron Boodman
2014/06/09 20:08:58
Done.
| |
53 LOG(INFO) << "Navigate received for view " << node_id << " to: " << url; | |
54 | |
55 // TODO(aa): Verify new URL is same origin as current origin. | |
56 | |
57 size_t path_start = url.rfind('/'); | |
58 if (path_start == url.npos) { | |
59 LOG(ERROR) << "Invalid URL, no path separator"; | |
60 return; | |
61 } | |
62 | |
63 SkColor color = 0x00; | |
64 if (!base::HexStringToUInt(url.substr(path_start + 1), &color)) { | |
65 LOG(ERROR) << "Invalid URL, path not convertible to integer"; | |
66 return; | |
67 } | |
68 | |
69 app_->SetNodeColor(node_id, color); | |
70 } | |
71 | |
72 EmbeddedApp* app_; | |
73 }; | |
74 | |
44 // Overridden from Application: | 75 // Overridden from Application: |
45 virtual void Initialize() MOJO_OVERRIDE { | 76 virtual void Initialize() MOJO_OVERRIDE { |
46 ViewManager::Create(this, this); | 77 ViewManager::Create(this, this); |
78 // TODO(aa): Weird for embeddee to talk to embedder by URL. Seems like | |
79 // embedder should be able to specify the SP embeddee receives, then | |
80 // communication can be anonymous. | |
47 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); | 81 ConnectTo<IWindowManager>("mojo:mojo_window_manager", &window_manager_); |
82 AddService<ViewNavigator>(this); | |
48 } | 83 } |
49 | 84 |
50 // Overridden from ViewManagerDelegate: | 85 // Overridden from ViewManagerDelegate: |
51 virtual void OnRootAdded(ViewManager* view_manager, | 86 virtual void OnRootAdded(ViewManager* view_manager, |
52 ViewTreeNode* root) OVERRIDE { | 87 ViewTreeNode* root) OVERRIDE { |
88 if (!view_manager_) | |
89 view_manager_ = view_manager; | |
90 DCHECK_EQ(view_manager_, view_manager); | |
91 | |
53 View* view = View::Create(view_manager); | 92 View* view = View::Create(view_manager); |
54 view->AddObserver(this); | 93 view->AddObserver(this); |
55 root->SetActiveView(view); | 94 root->SetActiveView(view); |
56 root->AddObserver(this); | 95 root->AddObserver(this); |
57 size_t index = view_manager->roots().size() - 1; | 96 ProcessPendingNodeColor(root->id()); |
58 view->SetColor(kColors[index % arraysize(kColors)]); | |
59 } | 97 } |
60 virtual void OnRootRemoved(ViewManager* view_manager, | 98 virtual void OnRootRemoved(ViewManager* view_manager, |
61 ViewTreeNode* root) OVERRIDE { | 99 ViewTreeNode* root) OVERRIDE { |
62 std::map<ViewTreeNode*, View*>::const_iterator it = | 100 std::map<ViewTreeNode*, View*>::const_iterator it = |
63 views_to_reap_.find(root); | 101 views_to_reap_.find(root); |
64 if (it != views_to_reap_.end()) | 102 if (it != views_to_reap_.end()) |
65 it->second->Destroy(); | 103 it->second->Destroy(); |
66 } | 104 } |
67 | 105 |
68 // Overridden from ViewObserver: | 106 // Overridden from ViewObserver: |
69 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE { | 107 virtual void OnViewInputEvent(View* view, EventPtr event) OVERRIDE { |
70 if (event->action == ui::ET_MOUSE_RELEASED) | 108 if (event->action == ui::ET_MOUSE_RELEASED) |
71 window_manager_->CloseWindow(view->node()->id()); | 109 window_manager_->CloseWindow(view->node()->id()); |
72 } | 110 } |
73 | 111 |
74 // Overridden from ViewTreeNodeObserver: | 112 // Overridden from ViewTreeNodeObserver: |
75 virtual void OnNodeActiveViewChange( | 113 virtual void OnNodeActiveViewChange( |
76 ViewTreeNode* node, | 114 ViewTreeNode* node, |
77 View* old_view, | 115 View* old_view, |
78 View* new_view, | 116 View* new_view, |
79 ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE { | 117 ViewTreeNodeObserver::DispositionChangePhase phase) OVERRIDE { |
80 if (new_view == 0) | 118 if (new_view == 0) |
81 views_to_reap_[node] = old_view; | 119 views_to_reap_[node] = old_view; |
82 } | 120 } |
83 | 121 |
122 void ProcessPendingNodeColor(uint32 node_id) { | |
123 if (!view_manager_) { | |
DaveMoore
2014/06/09 19:59:32
Nit: remove extra braces
Aaron Boodman
2014/06/09 20:08:58
Grump.
| |
124 return; | |
125 } | |
126 | |
127 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | |
128 if (!node) { | |
129 return; | |
130 } | |
131 | |
132 PendingNodeColors::iterator iter = pending_node_colors_.find(node_id); | |
133 if (iter == pending_node_colors_.end()) { | |
134 return; | |
135 } | |
136 | |
137 node->active_view()->SetColor(iter->second); | |
138 pending_node_colors_.erase(iter); | |
139 } | |
140 | |
141 | |
142 view_manager::ViewManager* view_manager_; | |
84 IWindowManagerPtr window_manager_; | 143 IWindowManagerPtr window_manager_; |
85 std::map<ViewTreeNode*, View*> views_to_reap_; | 144 std::map<ViewTreeNode*, View*> views_to_reap_; |
86 | 145 |
146 // We can receive navigations for nodes we don't have yet. | |
147 typedef std::map<uint32, SkColor> PendingNodeColors; | |
148 PendingNodeColors pending_node_colors_; | |
149 | |
87 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); | 150 DISALLOW_COPY_AND_ASSIGN(EmbeddedApp); |
88 }; | 151 }; |
89 | 152 |
90 } // namespace examples | 153 } // namespace examples |
91 | 154 |
92 // static | 155 // static |
93 Application* Application::Create() { | 156 Application* Application::Create() { |
94 return new examples::EmbeddedApp; | 157 return new examples::EmbeddedApp; |
95 } | 158 } |
96 | 159 |
97 } // namespace mojo | 160 } // namespace mojo |
OLD | NEW |