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, connector_, NewWindow(properties), | 93 return new NativeWidgetMus(delegate, connector_, 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. |
| 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 ui::GpuService::Initialize(connector); | 137 ui::GpuService::Initialize(connector); |
154 | 138 |
155 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); | 139 client_.reset(new ui::WindowTreeClient(this, nullptr, nullptr)); |
156 client_->ConnectViaWindowTreeFactory(connector_); | 140 client_->ConnectViaWindowTreeFactory(connector_); |
157 | 141 |
158 screen_.reset(new ScreenMus(this)); | 142 screen_.reset(new ScreenMus(this)); |
159 screen_->Init(connector); | 143 screen_->Init(connector); |
160 | 144 |
161 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); | 145 std::unique_ptr<ClipboardMus> clipboard(new ClipboardMus); |
162 clipboard->Init(connector); | 146 clipboard->Init(connector); |
163 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); | 147 ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard)); |
164 | 148 |
165 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( | 149 ViewsDelegate::GetInstance()->set_native_widget_factory(base::Bind( |
166 &WindowManagerConnection::CreateNativeWidgetMus, | 150 &WindowManagerConnection::CreateNativeWidgetMus, |
167 base::Unretained(this), | 151 base::Unretained(this), |
168 std::map<std::string, std::vector<uint8_t>>())); | 152 std::map<std::string, std::vector<uint8_t>>())); |
169 } | 153 } |
170 | 154 |
171 bool WindowManagerConnection::HasPointerWatcher() { | 155 bool WindowManagerConnection::HasPointerWatcher() { |
172 // Check to see if we really have any observers left. This doesn't use | 156 // Check to see if we really have any observers left. This doesn't use |
173 // base::ObserverList<>::might_have_observers() because that returns true | 157 // base::ObserverList<>::might_have_observers() because that returns true |
174 // during iteration over the list even when the last observer is removed. | 158 // during iteration over the list even when the last observer is removed. |
175 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); | 159 base::ObserverList<PointerWatcher>::Iterator iterator(&pointer_watchers_); |
176 return !!iterator.GetNext(); | 160 return !!iterator.GetNext(); |
177 } | 161 } |
178 | 162 |
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 | |
188 void WindowManagerConnection::OnEmbed(ui::Window* root) {} | 163 void WindowManagerConnection::OnEmbed(ui::Window* root) {} |
189 | 164 |
190 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { | 165 void WindowManagerConnection::OnDidDestroyClient(ui::WindowTreeClient* client) { |
191 if (client_.get() == client) { | 166 if (client_.get() == client) { |
192 client_.release(); | 167 client_.release(); |
193 } else { | 168 } else { |
194 DCHECK(!client_); | 169 DCHECK(!client_); |
195 } | 170 } |
196 } | 171 } |
197 | 172 |
198 void WindowManagerConnection::OnEventObserved(const ui::Event& event, | 173 void WindowManagerConnection::OnEventObserved(const ui::Event& event, |
199 ui::Window* target) { | 174 ui::Window* target) { |
200 if (!event.IsLocatedEvent()) | 175 if (!event.IsLocatedEvent()) |
201 return; | 176 return; |
202 Widget* target_widget = nullptr; | 177 Widget* target_widget = nullptr; |
203 if (target) { | 178 if (target) { |
204 ui::Window* root = target->GetRoot(); | 179 ui::Window* root = target->GetRoot(); |
205 target_widget = NativeWidgetMus::GetWidgetForWindow(root); | 180 target_widget = NativeWidgetMus::GetWidgetForWindow(root); |
206 } | 181 } |
207 | 182 |
208 // The mojo input events type converter uses the event root_location field | 183 // The mojo input events type converter uses the event root_location field |
209 // to store screen coordinates. Screen coordinates really should be returned | 184 // to store screen coordinates. Screen coordinates really should be returned |
210 // separately. See http://crbug.com/608547 | 185 // separately. See http://crbug.com/608547 |
211 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); | 186 gfx::Point location_in_screen = event.AsLocatedEvent()->root_location(); |
212 if (HasPointerWatcher()) { | 187 |
213 if (event.type() == ui::ET_MOUSE_PRESSED) { | 188 LOG(ERROR) << "JAMES event observed " << event.name(); |
214 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | 189 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, |
215 OnMousePressed(*event.AsMouseEvent(), | 190 OnPointerEventObserved(*event.AsLocatedEvent(), |
216 location_in_screen, target_widget)); | 191 location_in_screen, target_widget)); |
217 } else if (event.type() == ui::ET_TOUCH_PRESSED) { | |
218 FOR_EACH_OBSERVER(PointerWatcher, pointer_watchers_, | |
219 OnTouchPressed(*event.AsTouchEvent(), | |
220 location_in_screen, target_widget)); | |
221 } | |
222 } else if (HasTouchEventWatcher()) { | |
223 if (event.IsTouchEvent() || event.IsTouchPointerEvent()) { | |
224 FOR_EACH_OBSERVER( | |
225 TouchEventWatcher, touch_event_watchers_, | |
226 OnTouchEventObserved(*event.AsLocatedEvent(), target_widget)); | |
227 } | |
228 } | |
229 } | 192 } |
230 | 193 |
231 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { | 194 void WindowManagerConnection::OnWindowManagerFrameValuesChanged() { |
232 if (client_) | 195 if (client_) |
233 NativeWidgetMus::NotifyFrameChanged(client_.get()); | 196 NativeWidgetMus::NotifyFrameChanged(client_.get()); |
234 } | 197 } |
235 | 198 |
236 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { | 199 gfx::Point WindowManagerConnection::GetCursorScreenPoint() { |
237 return client_->GetCursorScreenPoint(); | 200 return client_->GetCursorScreenPoint(); |
238 } | 201 } |
239 | 202 |
240 } // namespace views | 203 } // namespace views |
OLD | NEW |