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