OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ui/views/mus/window_manager_connection.h" | 5 #include "ui/views/mus/window_manager_connection.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
11 #include "services/shell/public/cpp/connection.h" | 11 #include "services/shell/public/cpp/connection.h" |
12 #include "services/shell/public/cpp/connector.h" | 12 #include "services/shell/public/cpp/connector.h" |
13 #include "services/ui/common/gpu_service.h" | 13 #include "services/ui/common/gpu_service.h" |
14 #include "services/ui/public/cpp/property_type_converters.h" | 14 #include "services/ui/public/cpp/property_type_converters.h" |
15 #include "services/ui/public/cpp/window.h" | 15 #include "services/ui/public/cpp/window.h" |
16 #include "services/ui/public/cpp/window_property.h" | 16 #include "services/ui/public/cpp/window_property.h" |
17 #include "services/ui/public/cpp/window_tree_client.h" | 17 #include "services/ui/public/cpp/window_tree_client.h" |
18 #include "services/ui/public/interfaces/event_matcher.mojom.h" | 18 #include "services/ui/public/interfaces/event_matcher.mojom.h" |
19 #include "services/ui/public/interfaces/window_tree.mojom.h" | 19 #include "services/ui/public/interfaces/window_tree.mojom.h" |
20 #include "ui/views/mus/clipboard_mus.h" | 20 #include "ui/views/mus/clipboard_mus.h" |
21 #include "ui/views/mus/native_widget_mus.h" | 21 #include "ui/views/mus/native_widget_mus.h" |
22 #include "ui/views/mus/screen_mus.h" | 22 #include "ui/views/mus/screen_mus.h" |
23 #include "ui/views/pointer_watcher.h" | 23 #include "ui/views/pointer_watcher.h" |
24 #include "ui/views/touch_event_watcher.h" | |
24 #include "ui/views/views_delegate.h" | 25 #include "ui/views/views_delegate.h" |
25 | 26 |
26 namespace views { | 27 namespace views { |
27 namespace { | 28 namespace { |
28 | 29 |
29 using WindowManagerConnectionPtr = | 30 using WindowManagerConnectionPtr = |
30 base::ThreadLocalPointer<views::WindowManagerConnection>; | 31 base::ThreadLocalPointer<views::WindowManagerConnection>; |
31 | 32 |
32 // Env is thread local so that aura may be used on multiple threads. | 33 // Env is thread local so that aura may be used on multiple threads. |
33 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = | 34 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 return nullptr; | 75 return nullptr; |
75 std::map<std::string, std::vector<uint8_t>> properties = props; | 76 std::map<std::string, std::vector<uint8_t>> properties = props; |
76 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); | 77 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); |
77 properties[ui::mojom::WindowManager::kAppID_Property] = | 78 properties[ui::mojom::WindowManager::kAppID_Property] = |
78 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); | 79 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); |
79 return new NativeWidgetMus(delegate, connector_, NewWindow(properties), | 80 return new NativeWidgetMus(delegate, connector_, NewWindow(properties), |
80 ui::mojom::SurfaceType::DEFAULT); | 81 ui::mojom::SurfaceType::DEFAULT); |
81 } | 82 } |
82 | 83 |
83 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher) { | 84 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher) { |
85 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | |
86 DCHECK(!HasTouchEventWatcher()); | |
84 bool had_watcher = HasPointerWatcher(); | 87 bool had_watcher = HasPointerWatcher(); |
85 pointer_watchers_.AddObserver(watcher); | 88 pointer_watchers_.AddObserver(watcher); |
86 if (!had_watcher) { | 89 if (!had_watcher) { |
87 // Start a watcher for pointer down. | 90 // Start a watcher for pointer down. |
88 // TODO(jamescook): Extend event observers to handle multiple event types. | 91 // TODO(jamescook): Extend event observers to handle multiple event types. |
89 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | 92 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); |
90 matcher->type_matcher = ui::mojom::EventTypeMatcher::New(); | 93 matcher->type_matcher = ui::mojom::EventTypeMatcher::New(); |
91 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | 94 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; |
92 client_->SetEventObserver(std::move(matcher)); | 95 client_->SetEventObserver(std::move(matcher)); |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
96 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { | 99 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { |
97 pointer_watchers_.RemoveObserver(watcher); | 100 pointer_watchers_.RemoveObserver(watcher); |
98 if (!HasPointerWatcher()) { | 101 if (!HasPointerWatcher()) { |
99 // Last PointerWatcher removed, stop the event observer. | 102 // Last PointerWatcher removed, stop the event observer. |
100 client_->SetEventObserver(nullptr); | 103 client_->SetEventObserver(nullptr); |
101 } | 104 } |
102 } | 105 } |
103 | 106 |
107 void WindowManagerConnection::AddTouchEventWatcher(TouchEventWatcher* watcher) { | |
108 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | |
109 DCHECK(!HasPointerWatcher()); | |
110 bool had_watcher = HasTouchEventWatcher(); | |
111 touch_event_watchers_.AddObserver(watcher); | |
112 if (!had_watcher) { | |
113 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | |
114 matcher->pointer_kind_matcher = ui::mojom::PointerKindMatcher::New(); | |
115 matcher->pointer_kind_matcher->pointer_kind = ui::mojom::PointerKind::TOUCH; | |
116 client_->SetEventObserver(std::move(matcher)); | |
117 } | |
118 } | |
119 | |
120 void WindowManagerConnection::RemoveTouchEventWatcher( | |
121 TouchEventWatcher* watcher) { | |
122 touch_event_watchers_.RemoveObserver(watcher); | |
123 if (!HasTouchEventWatcher()) { | |
124 // Last PointerWatcher removed, stop the event observer. | |
125 client_->SetEventObserver(nullptr); | |
126 } | |
127 } | |
128 | |
104 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { | 129 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { |
105 return client_->GetRoots(); | 130 return client_->GetRoots(); |
106 } | 131 } |
107 | 132 |
108 WindowManagerConnection::WindowManagerConnection( | 133 WindowManagerConnection::WindowManagerConnection( |
109 shell::Connector* connector, | 134 shell::Connector* connector, |
110 const shell::Identity& identity) | 135 const shell::Identity& identity) |
111 : connector_(connector), identity_(identity) { | 136 : connector_(connector), identity_(identity) { |
112 lazy_tls_ptr.Pointer()->Set(this); | 137 lazy_tls_ptr.Pointer()->Set(this); |
113 | 138 |
(...skipping 30 matching lines...) Expand all Loading... | |
144 } | 169 } |
145 | 170 |
146 bool WindowManagerConnection::HasPointerWatcher() { | 171 bool WindowManagerConnection::HasPointerWatcher() { |
147 // Check to see if we really have any observers left. This doesn't use | 172 // Check to see if we really have any observers left. This doesn't use |
148 // base::ObserverList<>::might_have_observers() because that returns true | 173 // base::ObserverList<>::might_have_observers() because that returns true |
149 // during iteration over the list even when the last observer is removed. | 174 // during iteration over the list even when the last observer is removed. |
150 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); | 175 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); |
151 return !!iterator.GetNext(); | 176 return !!iterator.GetNext(); |
152 } | 177 } |
153 | 178 |
179 bool WindowManagerConnection::HasTouchEventWatcher() { | |
180 // Check to see if we really have any observers left. This doesn't use | |
181 // base::ObserverList<>::might_have_observers() because that returns true | |
182 // during iteration over the list even when the last observer is removed. | |
183 base::ObserverList<TouchEventWatcher>::Iterator iterator( | |
184 &touch_event_watchers_); | |
185 return !!iterator.GetNext(); | |
186 } | |
187 | |
154 void WindowManagerConnection::OnEmbed(ui::Window* root) {} | 188 void WindowManagerConnection::OnEmbed(ui::Window* root) {} |
155 | 189 |
156 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { | 190 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { |
157 if (client_.get() == client) { | 191 if (client_.get() == client) { |
158 client_.release(); | 192 client_.release(); |
159 } else { | 193 } else { |
160 DCHECK(!client_); | 194 DCHECK(!client_); |
161 } | 195 } |
162 } | 196 } |
163 | 197 |
164 void WindowManagerConnection::OnEventObserved(const ui::Event& event, | 198 void WindowManagerConnection::OnEventObserved(const ui::Event& event, |
165 ui::Window* target) { | 199 ui::Window* target) { |
166 if (!event.IsLocatedEvent()) | 200 if (!event.IsLocatedEvent()) |
167 return; | 201 return; |
168 Widget* target_widget = nullptr; | 202 Widget* target_widget = nullptr; |
169 if (target) { | 203 if (target) { |
170 ui::Window* root = target->GetRoot(); | 204 ui::Window* root = target->GetRoot(); |
171 target_widget = NativeWidgetMus::GetWidgetForWindow(root); | 205 target_widget = NativeWidgetMus::GetWidgetForWindow(root); |
172 } | 206 } |
173 | 207 |
174 // The mojo input events type converter uses the event root_location field | 208 // The mojo input events type converter uses the event root_location field |
175 // to store screen coordinates. Screen coordinates really should be returned | 209 // to store screen coordinates. Screen coordinates really should be returned |
176 // separately. See http://crbug.com/608547 | 210 // separately. See http://crbug.com/608547 |
177 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); | 211 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); |
178 if (event.type() == ui::ET_MOUSE_PRESSED) { | 212 if (HasPointerWatcher()) { |
179 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 213 if (event.type() == ui::ET_MOUSE_PRESSED) { |
180 OnMousePressed(*event.AsMouseEvent(), location_in_screen, | 214 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, |
181 target_widget)); | 215 OnMousePressed(*event.AsMouseEvent(), |
182 } else if (event.type() == ui::ET_TOUCH_PRESSED) { | 216 location_in_screen, target_widget)); |
183 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 217 } else if (event.type() == ui::ET_TOUCH_PRESSED) { |
184 OnTouchPressed(*event.AsTouchEvent(), location_in_screen, | 218 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, |
185 target_widget)); | 219 OnTouchPressed(*event.AsTouchEvent(), |
220 location_in_screen, target_widget)); | |
221 } | |
222 } else { | |
223 DCHECK(HasTouchEventWatcher()); | |
224 // Receives TouchEvent and TouchPointerEvent | |
225 if (event.IsTouchEvent() || event.IsTouchPointerEvent()) { | |
sadrul
2016/07/12 19:21:05
DCHECK(IsTouchEvent() || IsTouchPointerEvent())
riajiang
2016/07/12 20:35:47
Done.
riajiang
2016/07/13 20:03:08
Changed both DCHECKs for (HasTouchEventWatcher())
| |
226 FOR_EACH_OBSERVER( | |
227 TouchEventWatcher, touch_event_watchers_, | |
228 OnTouchEventObserved(*event.AsLocatedEvent(), target_widget)); | |
229 } | |
186 } | 230 } |
187 } | 231 } |
188 | 232 |
189 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { | 233 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { |
190 if (client_) | 234 if (client_) |
191 NativeWidgetMus::NotifyFrameChanged(client_.get()); | 235 NativeWidgetMus::NotifyFrameChanged(client_.get()); |
192 } | 236 } |
193 | 237 |
194 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { | 238 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { |
195 return client_->GetCursorScreenPoint(); | 239 return client_->GetCursorScreenPoint(); |
196 } | 240 } |
197 | 241 |
198 } // namespace views | 242 } // namespace views |
OLD | NEW |