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