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 "mojo/services/window_manager/window_manager_app.h" | 5 #include "mojo/services/window_manager/window_manager_app.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "mojo/aura/aura_init.h" | 8 #include "mojo/aura/aura_init.h" |
9 #include "mojo/aura/window_tree_host_mojo.h" | 9 #include "mojo/aura/window_tree_host_mojo.h" |
10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/services/public/cpp/view_manager/node.h" | 11 #include "mojo/services/public/cpp/view_manager/node.h" |
12 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 12 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
13 #include "mojo/services/window_manager/window_manager_service_impl.h" | 13 #include "mojo/services/window_manager/window_manager_service_impl.h" |
14 #include "ui/aura/window.h" | |
15 #include "ui/aura/window_property.h" | |
14 #include "ui/wm/core/capture_controller.h" | 16 #include "ui/wm/core/capture_controller.h" |
17 #include "ui/wm/core/focus_controller.h" | |
18 #include "ui/wm/core/focus_rules.h" | |
19 #include "ui/wm/public/activation_client.h" | |
20 | |
21 DECLARE_WINDOW_PROPERTY_TYPE(mojo::view_manager::Node*); | |
15 | 22 |
16 namespace mojo { | 23 namespace mojo { |
24 namespace { | |
25 | |
26 DEFINE_WINDOW_PROPERTY_KEY(view_manager::Node*, kNodeKey, NULL); | |
27 | |
28 view_manager::Id GetIdForWindow(aura::Window* window) { | |
29 return window ? window->GetProperty(kNodeKey)->id() : 0; | |
30 } | |
31 | |
32 class WMFocusRules : public wm::FocusRules { | |
33 public: | |
34 WMFocusRules() {} | |
35 virtual ~WMFocusRules() {} | |
36 | |
37 private: | |
38 // Overridden from wm::FocusRules: | |
39 virtual bool IsToplevelWindow(aura::Window* window) const MOJO_OVERRIDE { | |
40 return true; | |
41 } | |
42 virtual bool CanActivateWindow(aura::Window* window) const MOJO_OVERRIDE { | |
43 return true; | |
44 } | |
45 virtual bool CanFocusWindow(aura::Window* window) const MOJO_OVERRIDE { | |
46 return true; | |
47 } | |
48 virtual aura::Window* GetToplevelWindow( | |
49 aura::Window* window) const MOJO_OVERRIDE { | |
50 return window; | |
51 } | |
52 virtual aura::Window* GetActivatableWindow( | |
53 aura::Window* window) const MOJO_OVERRIDE { | |
54 return window; | |
55 } | |
56 virtual aura::Window* GetFocusableWindow( | |
57 aura::Window* window) const MOJO_OVERRIDE { | |
58 return window; | |
59 } | |
60 virtual aura::Window* GetNextActivatableWindow( | |
61 aura::Window* ignore) const MOJO_OVERRIDE { | |
62 return NULL; | |
63 } | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(WMFocusRules); | |
66 }; | |
67 | |
68 } // namespace | |
17 | 69 |
18 //////////////////////////////////////////////////////////////////////////////// | 70 //////////////////////////////////////////////////////////////////////////////// |
19 // WindowManagerApp, public: | 71 // WindowManagerApp, public: |
20 | 72 |
21 WindowManagerApp::WindowManagerApp() : view_manager_(NULL), root_(NULL) {} | 73 WindowManagerApp::WindowManagerApp() : view_manager_(NULL), root_(NULL) {} |
22 WindowManagerApp::~WindowManagerApp() {} | 74 WindowManagerApp::~WindowManagerApp() {} |
sky
2014/07/17 23:15:42
Do you need to delete windows in node_id_to_window
| |
23 | 75 |
24 void WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) { | 76 void WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) { |
25 DCHECK(connections_.find(connection) == connections_.end()); | 77 DCHECK(connections_.find(connection) == connections_.end()); |
26 connections_.insert(connection); | 78 connections_.insert(connection); |
27 } | 79 } |
28 | 80 |
29 void WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) { | 81 void WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) { |
30 DCHECK(connections_.find(connection) != connections_.end()); | 82 DCHECK(connections_.find(connection) != connections_.end()); |
31 connections_.erase(connection); | 83 connections_.erase(connection); |
32 } | 84 } |
33 | 85 |
34 view_manager::Id WindowManagerApp::OpenWindow() { | 86 view_manager::Id WindowManagerApp::OpenWindow() { |
35 view_manager::Node* node = view_manager::Node::Create(view_manager_); | 87 view_manager::Node* node = view_manager::Node::Create(view_manager_); |
36 root_->AddChild(node); | 88 root_->AddChild(node); |
sky
2014/07/17 23:15:42
Are you guaranteed to have a root_ by the time you
Ben Goodger (Google)
2014/07/18 19:50:44
No. In practice the test waits for IsReady(), but
| |
37 return node->id(); | 89 return node->id(); |
38 } | 90 } |
39 | 91 |
92 view_manager::Id WindowManagerApp::OpenWindowWithURL(const String& url) { | |
93 view_manager::Node* node = view_manager::Node::Create(view_manager_); | |
94 root_->AddChild(node); | |
95 node->Embed(url); | |
96 return node->id(); | |
97 } | |
98 | |
40 void WindowManagerApp::SetCapture(view_manager::Id node) { | 99 void WindowManagerApp::SetCapture(view_manager::Id node) { |
41 capture_client_->capture_client()->SetCapture(GetWindowForNodeId(node)); | 100 capture_client_->capture_client()->SetCapture(GetWindowForNodeId(node)); |
42 // TODO(beng): notify connected clients that capture has changed, probably | 101 // TODO(beng): notify connected clients that capture has changed, probably |
43 // by implementing some capture-client observer. | 102 // by implementing some capture-client observer. |
44 } | 103 } |
45 | 104 |
105 void WindowManagerApp::FocusWindow(view_manager::Id node) { | |
106 aura::Window* window = GetWindowForNodeId(node); | |
107 DCHECK(window); | |
108 focus_client_->FocusWindow(window); | |
109 } | |
110 | |
111 void WindowManagerApp::ActivateWindow(view_manager::Id node) { | |
112 aura::Window* window = GetWindowForNodeId(node); | |
113 DCHECK(window); | |
114 activation_client_->ActivateWindow(window); | |
115 } | |
116 | |
46 bool WindowManagerApp::IsReady() const { | 117 bool WindowManagerApp::IsReady() const { |
47 return view_manager_ && root_; | 118 return view_manager_ && root_; |
48 } | 119 } |
49 | 120 |
50 //////////////////////////////////////////////////////////////////////////////// | 121 //////////////////////////////////////////////////////////////////////////////// |
51 // WindowManagerApp, ApplicationDelegate implementation: | 122 // WindowManagerApp, ApplicationDelegate implementation: |
52 | 123 |
53 void WindowManagerApp::Initialize(ApplicationImpl* impl) { | 124 void WindowManagerApp::Initialize(ApplicationImpl* impl) { |
54 aura_init_.reset(new AuraInit); | 125 aura_init_.reset(new AuraInit); |
55 } | 126 } |
56 | 127 |
57 bool WindowManagerApp::ConfigureIncomingConnection( | 128 bool WindowManagerApp::ConfigureIncomingConnection( |
58 ApplicationConnection* connection) { | 129 ApplicationConnection* connection) { |
59 connection->AddService<WindowManagerServiceImpl>(this); | 130 connection->AddService<WindowManagerServiceImpl>(this); |
60 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); | 131 view_manager::ViewManager::ConfigureIncomingConnection(connection, this); |
61 return true; | 132 return true; |
62 } | 133 } |
63 | 134 |
64 //////////////////////////////////////////////////////////////////////////////// | 135 //////////////////////////////////////////////////////////////////////////////// |
65 // WindowManagerApp, view_manager::ViewManagerDelegate implementation: | 136 // WindowManagerApp, view_manager::ViewManagerDelegate implementation: |
66 | 137 |
67 void WindowManagerApp::OnRootAdded(view_manager::ViewManager* view_manager, | 138 void WindowManagerApp::OnRootAdded(view_manager::ViewManager* view_manager, |
68 view_manager::Node* root) { | 139 view_manager::Node* root) { |
69 DCHECK(!view_manager_ && !root_); | 140 DCHECK(!view_manager_ && !root_); |
70 view_manager_ = view_manager; | 141 view_manager_ = view_manager; |
71 root_ = root; | 142 root_ = root; |
143 root_->AddObserver(this); | |
sky
2014/07/17 23:15:42
Do you need to remove this in the destructor if no
| |
72 | 144 |
73 window_tree_host_.reset(new WindowTreeHostMojo(root_, this)); | 145 window_tree_host_.reset(new WindowTreeHostMojo(root_, this)); |
74 | 146 |
147 RegisterSubtree(root_->id(), window_tree_host_->window()); | |
148 | |
75 capture_client_.reset( | 149 capture_client_.reset( |
76 new wm::ScopedCaptureClient(window_tree_host_->window())); | 150 new wm::ScopedCaptureClient(window_tree_host_->window())); |
151 wm::FocusController* focus_controller = | |
152 new wm::FocusController(new WMFocusRules); | |
153 activation_client_ = focus_controller; | |
154 focus_client_.reset(focus_controller); | |
155 | |
156 focus_client_->AddObserver(this); | |
sky
2014/07/17 23:15:42
Remove these observers?
| |
157 activation_client_->AddObserver(this); | |
77 | 158 |
78 // TODO(beng): Create the universe. | 159 // TODO(beng): Create the universe. |
79 | 160 |
80 for (Connections::const_iterator it = connections_.begin(); | 161 for (Connections::const_iterator it = connections_.begin(); |
81 it != connections_.end(); ++it) { | 162 it != connections_.end(); ++it) { |
82 (*it)->NotifyReady(); | 163 (*it)->NotifyReady(); |
83 } | 164 } |
84 } | 165 } |
85 | 166 |
86 void WindowManagerApp::OnViewManagerDisconnected( | 167 void WindowManagerApp::OnViewManagerDisconnected( |
87 view_manager::ViewManager* view_manager) { | 168 view_manager::ViewManager* view_manager) { |
88 DCHECK_EQ(view_manager_, view_manager); | 169 DCHECK_EQ(view_manager_, view_manager); |
89 view_manager_ = NULL; | 170 view_manager_ = NULL; |
90 base::MessageLoop::current()->Quit(); | 171 base::MessageLoop::current()->Quit(); |
91 } | 172 } |
92 | 173 |
93 //////////////////////////////////////////////////////////////////////////////// | 174 //////////////////////////////////////////////////////////////////////////////// |
175 // WindowManagerApp, view_manager::NodeObserver implementation: | |
176 | |
177 void WindowManagerApp::OnTreeChanged( | |
178 const view_manager::NodeObserver::TreeChangeParams& params) { | |
179 DCHECK_EQ(params.receiver, root_); | |
180 DCHECK(params.old_parent || params.new_parent); | |
181 if (!params.target) | |
182 return; | |
183 | |
184 if (params.new_parent) { | |
185 if (node_id_to_window_map_.find(params.target->id()) == | |
186 node_id_to_window_map_.end()) { | |
187 NodeIdToWindowMap::const_iterator it = | |
188 node_id_to_window_map_.find(params.new_parent->id()); | |
189 DCHECK(it != node_id_to_window_map_.end()); | |
190 RegisterSubtree(params.target->id(), it->second); | |
191 } | |
192 } else if (params.old_parent) { | |
193 UnregisterSubtree(params.target->id()); | |
194 } | |
195 } | |
196 | |
197 void WindowManagerApp::OnNodeDestroyed(view_manager::Node* node) { | |
198 DCHECK_EQ(root_, node); | |
199 root_->RemoveObserver(this); | |
200 root_ = NULL; | |
201 } | |
sky
2014/07/17 23:15:42
Do you need to UnregisterSubstree here? If not, wh
Ben Goodger (Google)
2014/07/18 19:50:44
I removed this method. It's actually never called
| |
202 | |
203 //////////////////////////////////////////////////////////////////////////////// | |
94 // WindowManagerApp, WindowTreeHostMojoDelegate implementation: | 204 // WindowManagerApp, WindowTreeHostMojoDelegate implementation: |
95 | 205 |
96 void WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) { | 206 void WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) { |
97 // We draw nothing. | 207 // We draw nothing. |
98 NOTREACHED(); | 208 NOTREACHED(); |
99 } | 209 } |
100 | 210 |
101 //////////////////////////////////////////////////////////////////////////////// | 211 //////////////////////////////////////////////////////////////////////////////// |
212 // WindowManagerApp, aura::client::FocusChangeObserver implementation: | |
213 | |
214 void WindowManagerApp::OnWindowFocused(aura::Window* gained_focus, | |
215 aura::Window* lost_focus) { | |
216 for (Connections::const_iterator it = connections_.begin(); | |
217 it != connections_.end(); ++it) { | |
218 (*it)->NotifyNodeFocused(GetIdForWindow(gained_focus), | |
219 GetIdForWindow(lost_focus)); | |
220 } | |
221 } | |
222 | |
223 //////////////////////////////////////////////////////////////////////////////// | |
224 // WindowManagerApp, aura::client::ActivationChangeObserver implementation: | |
225 | |
226 void WindowManagerApp::OnWindowActivated(aura::Window* gained_active, | |
227 aura::Window* lost_active) { | |
228 for (Connections::const_iterator it = connections_.begin(); | |
229 it != connections_.end(); ++it) { | |
230 (*it)->NotifyWindowActivated(GetIdForWindow(gained_active), | |
231 GetIdForWindow(lost_active)); | |
232 } | |
233 } | |
234 | |
235 //////////////////////////////////////////////////////////////////////////////// | |
102 // WindowManagerApp, private: | 236 // WindowManagerApp, private: |
103 | 237 |
104 aura::Window* WindowManagerApp::GetWindowForNodeId( | 238 aura::Window* WindowManagerApp::GetWindowForNodeId( |
105 view_manager::Id node) const { | 239 view_manager::Id node) const { |
106 NOTIMPLEMENTED(); | 240 NodeIdToWindowMap::const_iterator it = node_id_to_window_map_.find(node); |
107 return NULL; | 241 return it != node_id_to_window_map_.end() ? it->second : NULL; |
242 } | |
243 | |
244 void WindowManagerApp::RegisterSubtree(view_manager::Id id, | |
245 aura::Window* parent) { | |
246 view_manager::Node* node = view_manager_->GetNodeById(id); | |
247 DCHECK(node_id_to_window_map_.find(id) == node_id_to_window_map_.end()); | |
248 aura::Window* window = new aura::Window(NULL); | |
249 window->SetProperty(kNodeKey, node); | |
250 parent->AddChild(window); | |
251 node_id_to_window_map_[id] = window; | |
252 view_manager::Node::Children::const_iterator it = node->children().begin(); | |
253 for (; it != node->children().end(); ++it) | |
254 RegisterSubtree((*it)->id(), window); | |
255 } | |
256 | |
257 void WindowManagerApp::UnregisterSubtree(view_manager::Id id) { | |
258 view_manager::Node* node = view_manager_->GetNodeById(id); | |
259 NodeIdToWindowMap::const_iterator it = node_id_to_window_map_.find(id); | |
260 DCHECK(it != node_id_to_window_map_.end()); | |
261 aura::Window* window = it->second; | |
sky
2014/07/17 23:15:42
put in scoped_ptr so you don't need delete on 266.
| |
262 node_id_to_window_map_.erase(it); | |
263 view_manager::Node::Children::const_iterator child = node->children().begin(); | |
264 for (; child != node->children().end(); ++child) | |
265 UnregisterSubtree((*child)->id()); | |
266 delete window; | |
108 } | 267 } |
109 | 268 |
110 //////////////////////////////////////////////////////////////////////////////// | 269 //////////////////////////////////////////////////////////////////////////////// |
111 // ApplicationDelegate, public: | 270 // ApplicationDelegate, public: |
112 | 271 |
113 // static | 272 // static |
114 ApplicationDelegate* ApplicationDelegate::Create() { | 273 ApplicationDelegate* ApplicationDelegate::Create() { |
115 return new WindowManagerApp; | 274 return new WindowManagerApp; |
116 } | 275 } |
117 | 276 |
118 } // namespace mojo | 277 } // namespace mojo |
OLD | NEW |