OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/ui/ws/threaded_image_cursors.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/threading/thread_task_runner_handle.h" |
| 10 #include "services/ui/common/image_cursors_set.h" |
| 11 #include "ui/base/cursor/image_cursors.h" |
| 12 #include "ui/platform_window/platform_window.h" |
| 13 |
| 14 namespace ui { |
| 15 namespace ws { |
| 16 namespace { |
| 17 |
| 18 void AddImageCursorsOnResourceThread( |
| 19 base::WeakPtr<ui::ImageCursorsSet> image_cursors_set_weak_ptr, |
| 20 std::unique_ptr<ui::ImageCursors> image_cursors) { |
| 21 if (image_cursors_set_weak_ptr) |
| 22 image_cursors_set_weak_ptr->AddImageCursors(std::move(image_cursors)); |
| 23 } |
| 24 |
| 25 void RemoveImageCursorsOnResourceThread( |
| 26 base::WeakPtr<ui::ImageCursorsSet> image_cursors_set_weak_ptr, |
| 27 base::WeakPtr<ui::ImageCursors> image_cursors_weak_ptr) { |
| 28 if (image_cursors_set_weak_ptr && image_cursors_weak_ptr) { |
| 29 image_cursors_set_weak_ptr->RemoveImageCursors( |
| 30 image_cursors_weak_ptr.get()); |
| 31 } |
| 32 } |
| 33 |
| 34 void SetDisplayOnResourceThread( |
| 35 base::WeakPtr<ui::ImageCursors> image_cursors_weak_ptr, |
| 36 const display::Display& display, |
| 37 float scale_factor) { |
| 38 if (image_cursors_weak_ptr) |
| 39 image_cursors_weak_ptr->SetDisplay(display, scale_factor); |
| 40 } |
| 41 |
| 42 void SetCursorSizeOnResourceThread( |
| 43 base::WeakPtr<ui::ImageCursors> image_cursors_weak_ptr, |
| 44 CursorSize cursor_size) { |
| 45 if (image_cursors_weak_ptr) |
| 46 image_cursors_weak_ptr->SetCursorSize(cursor_size); |
| 47 } |
| 48 |
| 49 // Executed on |resource_task_runner_|. Sets cursor type on |
| 50 // |image_cursors_set_weak_ptr_|, and then schedules a task on |
| 51 // |ui_service_task_runner_| to set the corresponding PlatformCursor on the |
| 52 // provided |platform_window|. |
| 53 // |platform_window| pointer needs to be valid while |
| 54 // |threaded_image_cursors_weak_ptr| is not invalidated. |
| 55 void SetCursorOnResourceThread( |
| 56 base::WeakPtr<ui::ImageCursors> image_cursors_weak_ptr, |
| 57 ui::CursorType cursor_type, |
| 58 ui::PlatformWindow* platform_window, |
| 59 scoped_refptr<base::SingleThreadTaskRunner> ui_service_task_runner_, |
| 60 base::WeakPtr<ThreadedImageCursors> threaded_image_cursors_weak_ptr) { |
| 61 if (image_cursors_weak_ptr) { |
| 62 ui::Cursor native_cursor(cursor_type); |
| 63 image_cursors_weak_ptr->SetPlatformCursor(&native_cursor); |
| 64 // |platform_window| is owned by the UI Service thread, so setting the |
| 65 // cursor on it also needs to happen on that thread. |
| 66 ui_service_task_runner_->PostTask( |
| 67 FROM_HERE, base::Bind(&ThreadedImageCursors::SetCursorOnPlatformWindow, |
| 68 threaded_image_cursors_weak_ptr, |
| 69 native_cursor.platform(), platform_window)); |
| 70 } |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 ThreadedImageCursors::ThreadedImageCursors( |
| 76 scoped_refptr<base::SingleThreadTaskRunner>& resource_task_runner, |
| 77 base::WeakPtr<ui::ImageCursorsSet> image_cursors_set_weak_ptr) |
| 78 : resource_task_runner_(resource_task_runner), |
| 79 image_cursors_set_weak_ptr_(image_cursors_set_weak_ptr), |
| 80 weak_ptr_factory_(this) { |
| 81 DCHECK(resource_task_runner_); |
| 82 ui_service_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 83 |
| 84 // Create and initialize the ImageCursors object here and then set it on |
| 85 // |image_cursors_set_weak_ptr__|. Note that it is essential to initialize |
| 86 // the ImageCursors object on the UI Service's thread if we are using Ozone, |
| 87 // so that it uses the right (thread-local) CursorFactoryOzone instance. |
| 88 std::unique_ptr<ui::ImageCursors> image_cursors = |
| 89 base::MakeUnique<ui::ImageCursors>(); |
| 90 image_cursors->Initialize(); |
| 91 image_cursors_weak_ptr_ = image_cursors->GetWeakPtr(); |
| 92 resource_task_runner_->PostTask( |
| 93 FROM_HERE, |
| 94 base::Bind(&AddImageCursorsOnResourceThread, image_cursors_set_weak_ptr_, |
| 95 base::Passed(std::move(image_cursors)))); |
| 96 } |
| 97 |
| 98 ThreadedImageCursors::~ThreadedImageCursors() { |
| 99 resource_task_runner_->PostTask( |
| 100 FROM_HERE, |
| 101 base::Bind(&RemoveImageCursorsOnResourceThread, |
| 102 image_cursors_set_weak_ptr_, image_cursors_weak_ptr_)); |
| 103 } |
| 104 |
| 105 void ThreadedImageCursors::SetDisplay(const display::Display& display, |
| 106 float scale_factor) { |
| 107 resource_task_runner_->PostTask( |
| 108 FROM_HERE, base::Bind(&SetDisplayOnResourceThread, |
| 109 image_cursors_weak_ptr_, display, scale_factor)); |
| 110 } |
| 111 |
| 112 void ThreadedImageCursors::SetCursorSize(CursorSize cursor_size) { |
| 113 resource_task_runner_->PostTask( |
| 114 FROM_HERE, base::Bind(&SetCursorSizeOnResourceThread, |
| 115 image_cursors_weak_ptr_, cursor_size)); |
| 116 } |
| 117 |
| 118 void ThreadedImageCursors::SetCursor(ui::CursorType cursor_type, |
| 119 ui::PlatformWindow* platform_window) { |
| 120 resource_task_runner_->PostTask( |
| 121 FROM_HERE, |
| 122 base::Bind(&SetCursorOnResourceThread, image_cursors_weak_ptr_, |
| 123 cursor_type, platform_window, ui_service_task_runner_, |
| 124 weak_ptr_factory_.GetWeakPtr())); |
| 125 } |
| 126 |
| 127 void ThreadedImageCursors::SetCursorOnPlatformWindow( |
| 128 ui::PlatformCursor platform_cursor, |
| 129 ui::PlatformWindow* platform_window) { |
| 130 platform_window->SetCursor(platform_cursor); |
| 131 } |
| 132 |
| 133 } // namespace ws |
| 134 } // namespace ui |
OLD | NEW |