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 #ifndef SERVICES_UI_WS_THREADED_IMAGE_CURSORS_H_ |
| 6 #define SERVICES_UI_WS_THREADED_IMAGE_CURSORS_H_ |
| 7 |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "ui/base/cursor/cursor.h" |
| 10 |
| 11 namespace base { |
| 12 class SingleThreadTaskRunner; |
| 13 } |
| 14 |
| 15 namespace display { |
| 16 class Display; |
| 17 } |
| 18 |
| 19 namespace ui { |
| 20 class ImageCursors; |
| 21 class ImageCursorsSet; |
| 22 class PlatformWindow; |
| 23 |
| 24 namespace ws { |
| 25 |
| 26 // Wrapper around ui::ImageCursors, which executes its methods asynchronously |
| 27 // via the provided task runner. This is needed because ui::ImageCursors needs |
| 28 // to load resources when executing some of its methods, which cannot happen on |
| 29 // the UI Service's thread when it runs inside the Window Manager process. |
| 30 // |
| 31 // Note: We could execute the methods synchronously when UI Service runs in its |
| 32 // own process (and thus the provided task runner matches the task runner of the |
| 33 // UI Service's thread), but we don't do that in the interest of sharing code. |
| 34 class ThreadedImageCursors { |
| 35 public: |
| 36 // |resource_runner| is the task runner for the thread which can be used to |
| 37 // load resources; |image_cursors_set_weak_ptr_| points to an object that |
| 38 // lives on |resource_runner|'s thread. |
| 39 // We create an ImageCursors object here and then pass the ownership to |
| 40 // |image_cursors_set_weak_ptr_|. All operations on the ImageCursors object |
| 41 // happen on |resource_runner|, which is why we need the owner to live on |
| 42 // |resource_runner|'s thread. |
| 43 ThreadedImageCursors( |
| 44 scoped_refptr<base::SingleThreadTaskRunner>& resource_task_runner, |
| 45 base::WeakPtr<ui::ImageCursorsSet> image_cursors_set_weak_ptr_); |
| 46 ~ThreadedImageCursors(); |
| 47 |
| 48 // Executes ui::ImageCursors::SetDisplay asynchronously. |
| 49 // Sets the display the cursors are loaded for. |scale_factor| determines the |
| 50 // size of the image to load. Returns true if the cursor image is reloaded. |
| 51 void SetDisplay(const display::Display& display, float scale_factor); |
| 52 |
| 53 // Asynchronously sets the size of the mouse cursor icon. |
| 54 void SetCursorSize(CursorSize cursor_size); |
| 55 |
| 56 // Asynchronously sets the cursor type and then sets the corresponding |
| 57 // PlatformCursor on the provided |platform_window|. |
| 58 // |platform_window| pointer needs to be valid while this object is alive. |
| 59 void SetCursor(ui::CursorType cursor_type, |
| 60 ui::PlatformWindow* platform_window); |
| 61 |
| 62 // Helper method. Sets |platform_cursor| on the |platform_window|. |
| 63 void SetCursorOnPlatformWindow(ui::PlatformCursor platform_cursor, |
| 64 ui::PlatformWindow* platform_window); |
| 65 |
| 66 private: |
| 67 // The object used for performing the actual cursor operations. |
| 68 // Created on UI Service's thread, but is used on the |
| 69 // |resource_task_runner_|'s thread, because it needs to load resources. |
| 70 base::WeakPtr<ui::ImageCursors> image_cursors_weak_ptr_; |
| 71 |
| 72 // Task runner for the thread which owns the cursor resources. |
| 73 // |image_cursors_set_weak_ptr__| and |image_cursors_weak_ptr_| should only |
| 74 // be dereferenced on this task runner. |
| 75 scoped_refptr<base::SingleThreadTaskRunner> resource_task_runner_; |
| 76 |
| 77 // Task runner of the UI Service thread (where this object is created and |
| 78 // destroyed). |
| 79 scoped_refptr<base::SingleThreadTaskRunner> ui_service_task_runner_; |
| 80 |
| 81 // Lives on |resource_runner_|'s thread, used to own the object behid |
| 82 // |image_cursors_weak_ptr_|. |
| 83 base::WeakPtr<ui::ImageCursorsSet> image_cursors_set_weak_ptr_; |
| 84 |
| 85 base::WeakPtrFactory<ThreadedImageCursors> weak_ptr_factory_; |
| 86 |
| 87 DISALLOW_COPY_AND_ASSIGN(ThreadedImageCursors); |
| 88 }; |
| 89 |
| 90 } // namespace ws |
| 91 } // namespace ui |
| 92 |
| 93 #endif // SERVICES_UI_WS_THREADED_IMAGE_CURSORS_H_ |
OLD | NEW |