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