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" | |
25 #include "ui/views/views_delegate.h" | 24 #include "ui/views/views_delegate.h" |
26 | 25 |
27 namespace views { | 26 namespace views { |
28 namespace { | 27 namespace { |
29 | 28 |
30 using WindowManagerConnectionPtr = | 29 using WindowManagerConnectionPtr = |
31 base::ThreadLocalPointer<views::WindowManagerConnection>; | 30 base::ThreadLocalPointer<views::WindowManagerConnection>; |
32 | 31 |
33 // Env is thread local so that aura may be used on multiple threads. | 32 // Env is thread local so that aura may be used on multiple threads. |
34 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = | 33 base::LazyInstance<WindowManagerConnectionPtr>::Leaky lazy_tls_ptr = |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 if (init_params.type == Widget::InitParams::TYPE_CONTROL) | 87 if (init_params.type == Widget::InitParams::TYPE_CONTROL) |
89 return nullptr; | 88 return nullptr; |
90 std::map<std::string, std::vector<uint8_t>> properties = props; | 89 std::map<std::string, std::vector<uint8_t>> properties = props; |
91 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); | 90 NativeWidgetMus::ConfigurePropertiesForNewWindow(init_params, &properties); |
92 properties[ui::mojom::WindowManager::kAppID_Property] = | 91 properties[ui::mojom::WindowManager::kAppID_Property] = |
93 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); | 92 mojo::ConvertTo<std::vector<uint8_t>>(identity_.name()); |
94 return new NativeWidgetMus(delegate, NewWindow(properties), | 93 return new NativeWidgetMus(delegate, NewWindow(properties), |
95 ui::mojom::SurfaceType::DEFAULT); | 94 ui::mojom::SurfaceType::DEFAULT); |
96 } | 95 } |
97 | 96 |
98 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher) { | 97 void WindowManagerConnection::AddPointerWatcher(PointerWatcher* watcher, |
99 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | 98 bool want_moves) { |
100 DCHECK(!HasTouchEventWatcher()); | 99 // Pointer watchers cannot be added multiple times. |
| 100 DCHECK(!pointer_watchers_.HasObserver(watcher)); |
| 101 // TODO(jamescook): Support adding pointer watchers with different |
| 102 // |want_moves| values by tracking whether the set as a whole wants moves. |
| 103 // This will involve sending observed move events to a subset of the |
| 104 // watchers. (crbug.com/627146) |
| 105 DCHECK(!HasPointerWatcher() || want_moves == pointer_watcher_want_moves_); |
| 106 pointer_watcher_want_moves_ = want_moves; |
| 107 |
101 bool had_watcher = HasPointerWatcher(); | 108 bool had_watcher = HasPointerWatcher(); |
102 pointer_watchers_.AddObserver(watcher); | 109 pointer_watchers_.AddObserver(watcher); |
103 if (!had_watcher) { | 110 if (!had_watcher) { |
104 // Start a watcher for pointer down. | 111 // First PointerWatcher added, start the watcher on the window server. |
105 // TODO(jamescook): Extend event observers to handle multiple event types. | 112 client_->StartPointerWatcher(want_moves); |
106 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | |
107 matcher->type_matcher = ui::mojom::EventTypeMatcher::New(); | |
108 matcher->type_matcher->type = ui::mojom::EventType::POINTER_DOWN; | |
109 client_->SetEventObserver(std::move(matcher)); | |
110 } | 113 } |
111 } | 114 } |
112 | 115 |
113 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { | 116 void WindowManagerConnection::RemovePointerWatcher(PointerWatcher* watcher) { |
114 pointer_watchers_.RemoveObserver(watcher); | 117 pointer_watchers_.RemoveObserver(watcher); |
115 if (!HasPointerWatcher()) { | 118 if (!HasPointerWatcher()) { |
116 // Last PointerWatcher removed, stop the event observer. | 119 // Last PointerWatcher removed, stop the watcher on the window server. |
117 client_->SetEventObserver(nullptr); | 120 client_->StopPointerWatcher(); |
| 121 pointer_watcher_want_moves_ = false; |
118 } | 122 } |
119 } | 123 } |
120 | 124 |
121 void WindowManagerConnection::AddTouchEventWatcher(TouchEventWatcher* watcher) { | |
122 // TODO(riajiang): Support multiple event matchers (crbug.com/627146). | |
123 DCHECK(!HasPointerWatcher()); | |
124 bool had_watcher = HasTouchEventWatcher(); | |
125 touch_event_watchers_.AddObserver(watcher); | |
126 if (!had_watcher) { | |
127 ui::mojom::EventMatcherPtr matcher = ui::mojom::EventMatcher::New(); | |
128 matcher->pointer_kind_matcher = ui::mojom::PointerKindMatcher::New(); | |
129 matcher->pointer_kind_matcher->pointer_kind = ui::mojom::PointerKind::TOUCH; | |
130 client_->SetEventObserver(std::move(matcher)); | |
131 } | |
132 } | |
133 | |
134 void WindowManagerConnection::RemoveTouchEventWatcher( | |
135 TouchEventWatcher* watcher) { | |
136 touch_event_watchers_.RemoveObserver(watcher); | |
137 if (!HasTouchEventWatcher()) { | |
138 // Last TouchEventWatcher removed, stop the event observer. | |
139 client_->SetEventObserver(nullptr); | |
140 } | |
141 } | |
142 | |
143 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { | 125 const std::set<ui::Window*>& WindowManagerConnection::GetRoots() const { |
144 return client_->GetRoots(); | 126 return client_->GetRoots(); |
145 } | 127 } |
146 | 128 |
147 WindowManagerConnection::WindowManagerConnection( | 129 WindowManagerConnection::WindowManagerConnection( |
148 shell::Connector* connector, | 130 shell::Connector* connector, |
149 const shell::Identity& identity) | 131 const shell::Identity& identity) |
150 : connector_(connector), identity_(identity) { | 132 : connector_(connector), |
| 133 identity_(identity), |
| 134 pointer_watcher_want_moves_(false) { |
151 lazy_tls_ptr.Pointer()->Set(this); | 135 lazy_tls_ptr.Pointer()->Set(this); |
152 | 136 |
153 gpu_service_ = ui::GpuService::Initialize(connector); | 137 gpu_service_ = ui::GpuService::Initialize(connector); |
154 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); | 138 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); |
155 client_->ConnectViaWindowTreeFactory(connector_); | 139 client_->ConnectViaWindowTreeFactory(connector_); |
156 | 140 |
157 screen_.reset(new ScreenMus(this)); | 141 screen_.reset(new ScreenMus(this)); |
158 screen_->Init(connector); | 142 screen_->Init(connector); |
159 | 143 |
160 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); | 144 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); |
161 clipboard->Init(connector); | 145 clipboard->Init(connector); |
162 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); | 146 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); |
163 | 147 |
164 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( | 148 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( |
165 &WindowManagerConnection::CreateNativeWidgetMus, | 149 &WindowManagerConnection::CreateNativeWidgetMus, |
166 base::Unretained(this), | 150 base::Unretained(this), |
167 std::map<std::string, std::vector<uint8_t>>())); | 151 std::map<std::string, std::vector<uint8_t>>())); |
168 } | 152 } |
169 | 153 |
170 bool WindowManagerConnection::HasPointerWatcher() { | 154 bool WindowManagerConnection::HasPointerWatcher() { |
171 // Check to see if we really have any observers left. This doesn't use | 155 // Check to see if we really have any observers left. This doesn't use |
172 // base::ObserverList<>::might_have_observers() because that returns true | 156 // base::ObserverList<>::might_have_observers() because that returns true |
173 // during iteration over the list even when the last observer is removed. | 157 // during iteration over the list even when the last observer is removed. |
174 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); | 158 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); |
175 return !!iterator.GetNext(); | 159 return !!iterator.GetNext(); |
176 } | 160 } |
177 | 161 |
178 bool WindowManagerConnection::HasTouchEventWatcher() { | |
179 // Check to see if we really have any observers left. This doesn't use | |
180 // base::ObserverList<>::might_have_observers() because that returns true | |
181 // during iteration over the list even when the last observer is removed. | |
182 base::ObserverList<TouchEventWatcher>::Iterator iterator( | |
183 &touch_event_watchers_); | |
184 return !!iterator.GetNext(); | |
185 } | |
186 | |
187 void WindowManagerConnection::OnEmbed(ui::Window* root) {} | 162 void WindowManagerConnection::OnEmbed(ui::Window* root) {} |
188 | 163 |
189 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { | 164 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { |
190 if (client_.get() == client) { | 165 if (client_.get() == client) { |
191 client_.release(); | 166 client_.release(); |
192 } else { | 167 } else { |
193 DCHECK(!client_); | 168 DCHECK(!client_); |
194 } | 169 } |
195 } | 170 } |
196 | 171 |
197 void WindowManagerConnection::OnEventObserved(const ui::Event& event, | 172 void WindowManagerConnection::OnPointerEventObserved( |
198 ui::Window* target) { | 173 const ui::PointerEvent& event, |
199 if (!event.IsLocatedEvent()) | 174 ui::Window* target) { |
200 return; | 175 DCHECK(event.IsLocatedEvent()); |
201 Widget* target_widget = nullptr; | 176 Widget* target_widget = nullptr; |
202 if (target) { | 177 if (target) { |
203 ui::Window* root = target->GetRoot(); | 178 ui::Window* root = target->GetRoot(); |
204 target_widget = NativeWidgetMus::GetWidgetForWindow(root); | 179 target_widget = NativeWidgetMus::GetWidgetForWindow(root); |
205 } | 180 } |
206 | 181 |
207 // The mojo input events type converter uses the event root_location field | 182 // The mojo input events type converter uses the event root_location field |
208 // to store screen coordinates. Screen coordinates really should be returned | 183 // to store screen coordinates. Screen coordinates really should be returned |
209 // separately. See http://crbug.com/608547 | 184 // separately. See http://crbug.com/608547 |
210 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); | 185 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); |
211 if (HasPointerWatcher()) { | 186 FOR_EACH_OBSERVER( |
212 if (event.type() == ui::ET_MOUSE_PRESSED) { | 187 PointerWatcher, pointer_watchers_, |
213 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 188 OnPointerEventObserved(event, location_in_screen, target_widget)); |
214 OnMousePressed(*event.AsMouseEvent(), | |
215 location_in_screen, target_widget)); | |
216 } else if (event.type() == ui::ET_TOUCH_PRESSED) { | |
217 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | |
218 OnTouchPressed(*event.AsTouchEvent(), | |
219 location_in_screen, target_widget)); | |
220 } | |
221 } else if (HasTouchEventWatcher()) { | |
222 if (event.IsTouchEvent() || event.IsTouchPointerEvent()) { | |
223 FOR_EACH_OBSERVER( | |
224 TouchEventWatcher, touch_event_watchers_, | |
225 OnTouchEventObserved(*event.AsLocatedEvent(), target_widget)); | |
226 } | |
227 } | |
228 } | 189 } |
229 | 190 |
230 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { | 191 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { |
231 if (client_) | 192 if (client_) |
232 NativeWidgetMus::NotifyFrameChanged(client_.get()); | 193 NativeWidgetMus::NotifyFrameChanged(client_.get()); |
233 } | 194 } |
234 | 195 |
235 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { | 196 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { |
236 return client_->GetCursorScreenPoint(); | 197 return client_->GetCursorScreenPoint(); |
237 } | 198 } |
238 | 199 |
239 } // namespace views | 200 } // namespace views |
OLD | NEW |