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