Fix RAND_bytes inside the sandbox for the chimera build.
[chromium-blink-merge.git] / ash / display / cursor_window_controller.cc
blob17149ac88a2c47d66c9b0410270be05c5a1e0cb9
1 // Copyright 2014 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.
5 #include "ash/display/cursor_window_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/display/mirror_window_controller.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ui/aura/env.h"
13 #include "ui/aura/window_delegate.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #include "ui/base/cursor/cursors_aura.h"
16 #include "ui/base/hit_test.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/compositor/dip_util.h"
19 #include "ui/compositor/paint_recorder.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/image/image_skia.h"
23 #include "ui/gfx/image/image_skia_operations.h"
25 namespace ash {
27 class CursorWindowDelegate : public aura::WindowDelegate {
28 public:
29 CursorWindowDelegate() : is_cursor_compositing_enabled_(false) {}
30 ~CursorWindowDelegate() override {}
32 // aura::WindowDelegate overrides:
33 gfx::Size GetMinimumSize() const override { return size_; }
34 gfx::Size GetMaximumSize() const override { return size_; }
35 void OnBoundsChanged(const gfx::Rect& old_bounds,
36 const gfx::Rect& new_bounds) override {}
37 ui::TextInputClient* GetFocusedTextInputClient() override { return nullptr; }
38 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
39 return gfx::kNullCursor;
41 int GetNonClientComponent(const gfx::Point& point) const override {
42 return HTNOWHERE;
44 bool ShouldDescendIntoChildForEventHandling(
45 aura::Window* child,
46 const gfx::Point& location) override {
47 return false;
49 bool CanFocus() override { return false; }
50 void OnCaptureLost() override {}
51 void OnPaint(const ui::PaintContext& context) override {
52 // No need to cache the output here, the CursorWindow is not invalidated.
53 ui::PaintRecorder recorder(context);
54 recorder.canvas()->DrawImageInt(cursor_image_, 0, 0);
56 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
57 void OnWindowDestroying(aura::Window* window) override {}
58 void OnWindowDestroyed(aura::Window* window) override {}
59 void OnWindowTargetVisibilityChanged(bool visible) override {}
60 bool HasHitTestMask() const override { return false; }
61 void GetHitTestMask(gfx::Path* mask) const override {}
63 // Sets cursor compositing mode on/off.
64 void SetCursorCompositingEnabled(bool enabled) {
65 is_cursor_compositing_enabled_ = enabled;
68 // Sets the cursor image for the |display|'s scale factor.
69 void SetCursorImage(const gfx::ImageSkia& image,
70 const gfx::Display& display) {
71 float scale_factor = display.device_scale_factor();
72 const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor);
73 if (!is_cursor_compositing_enabled_) {
74 // Note that mirror window's scale factor is always 1.0f, therefore we
75 // need to take 2x's image and paint as if it's 1x image.
76 size_ = image_rep.pixel_size();
77 cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap());
78 } else {
79 size_ = image.size();
80 cursor_image_ = gfx::ImageSkia(
81 gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor));
85 const gfx::Size size() const { return size_; }
87 private:
88 bool is_cursor_compositing_enabled_;
89 gfx::ImageSkia cursor_image_;
90 gfx::Size size_;
92 DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate);
95 CursorWindowController::CursorWindowController()
96 : is_cursor_compositing_enabled_(false),
97 container_(NULL),
98 cursor_type_(ui::kCursorNone),
99 visible_(true),
100 cursor_set_(ui::CURSOR_SET_NORMAL),
101 delegate_(new CursorWindowDelegate()) {
104 CursorWindowController::~CursorWindowController() {
105 SetContainer(NULL);
108 void CursorWindowController::SetCursorCompositingEnabled(bool enabled) {
109 if (is_cursor_compositing_enabled_ != enabled) {
110 is_cursor_compositing_enabled_ = enabled;
111 delegate_->SetCursorCompositingEnabled(enabled);
112 UpdateCursorImage();
113 UpdateContainer();
117 void CursorWindowController::UpdateContainer() {
118 if (is_cursor_compositing_enabled_) {
119 gfx::Screen* screen = Shell::GetScreen();
120 gfx::Display display = screen->GetDisplayNearestPoint(
121 screen->GetCursorScreenPoint());
122 DCHECK(display.is_valid());
123 if (display.is_valid())
124 SetDisplay(display);
125 } else {
126 aura::Window* mirror_window = Shell::GetInstance()->
127 display_controller()->
128 mirror_window_controller()->
129 GetWindow();
130 if (mirror_window)
131 display_ = Shell::GetScreen()->GetPrimaryDisplay();
132 SetContainer(mirror_window);
134 // Updates the hot point based on the current display.
135 UpdateCursorImage();
138 void CursorWindowController::SetDisplay(const gfx::Display& display) {
139 if (!is_cursor_compositing_enabled_)
140 return;
142 display_ = display;
143 aura::Window* root_window = Shell::GetInstance()->display_controller()->
144 GetRootWindowForDisplayId(display.id());
145 if (!root_window)
146 return;
148 SetContainer(GetRootWindowController(root_window)->GetContainer(
149 kShellWindowId_MouseCursorContainer));
150 SetBoundsInScreen(display.bounds());
151 // Updates the hot point based on the current display.
152 UpdateCursorImage();
155 void CursorWindowController::UpdateLocation() {
156 if (!cursor_window_)
157 return;
158 gfx::Point point = aura::Env::GetInstance()->last_mouse_location();
159 if (!is_cursor_compositing_enabled_) {
160 Shell::GetPrimaryRootWindow()->GetHost()->ConvertPointToHost(&point);
161 } else {
162 point.Offset(-bounds_in_screen_.x(), -bounds_in_screen_.y());
164 point.Offset(-hot_point_.x(), -hot_point_.y());
165 gfx::Rect bounds = cursor_window_->bounds();
166 bounds.set_origin(point);
167 cursor_window_->SetBounds(bounds);
170 void CursorWindowController::SetCursor(gfx::NativeCursor cursor) {
171 if (cursor_type_ == cursor.native_type())
172 return;
173 cursor_type_ = cursor.native_type();
174 UpdateCursorImage();
175 UpdateCursorVisibility();
178 void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) {
179 cursor_set_ = cursor_set;
180 UpdateCursorImage();
183 void CursorWindowController::SetVisibility(bool visible) {
184 visible_ = visible;
185 UpdateCursorVisibility();
188 void CursorWindowController::SetContainer(aura::Window* container) {
189 if (container_ == container)
190 return;
191 container_ = container;
192 if (!container) {
193 cursor_window_.reset();
194 return;
197 // Reusing the window does not work when the display is disconnected.
198 // Just creates a new one instead. crbug.com/384218.
199 cursor_window_.reset(new aura::Window(delegate_.get()));
200 cursor_window_->SetTransparent(true);
201 cursor_window_->Init(ui::LAYER_TEXTURED);
202 cursor_window_->set_ignore_events(true);
203 cursor_window_->set_owned_by_parent(false);
204 // Call UpdateCursorImage() to figure out |cursor_window_|'s desired size.
205 UpdateCursorImage();
207 container->AddChild(cursor_window_.get());
208 UpdateCursorVisibility();
209 SetBoundsInScreen(container->bounds());
212 void CursorWindowController::SetBoundsInScreen(const gfx::Rect& bounds) {
213 bounds_in_screen_ = bounds;
214 UpdateLocation();
217 void CursorWindowController::UpdateCursorImage() {
218 int resource_id;
219 // TODO(hshi): support custom cursor set.
220 if (!ui::GetCursorDataFor(cursor_set_,
221 cursor_type_,
222 display_.device_scale_factor(),
223 &resource_id,
224 &hot_point_)) {
225 return;
227 const gfx::ImageSkia* image =
228 ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id);
229 gfx::ImageSkia rotated = *image;
230 if (!is_cursor_compositing_enabled_) {
231 switch (display_.rotation()) {
232 case gfx::Display::ROTATE_0:
233 break;
234 case gfx::Display::ROTATE_90:
235 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
236 *image, SkBitmapOperations::ROTATION_90_CW);
237 hot_point_.SetPoint(
238 rotated.width() - hot_point_.y(),
239 hot_point_.x());
240 break;
241 case gfx::Display::ROTATE_180:
242 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
243 *image, SkBitmapOperations::ROTATION_180_CW);
244 hot_point_.SetPoint(
245 rotated.height() - hot_point_.x(),
246 rotated.width() - hot_point_.y());
247 break;
248 case gfx::Display::ROTATE_270:
249 rotated = gfx::ImageSkiaOperations::CreateRotatedImage(
250 *image, SkBitmapOperations::ROTATION_270_CW);
251 hot_point_.SetPoint(
252 hot_point_.y(),
253 rotated.height() - hot_point_.x());
254 break;
256 } else {
257 hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(),
258 hot_point_);
260 delegate_->SetCursorImage(rotated, display_);
261 if (cursor_window_) {
262 cursor_window_->SetBounds(gfx::Rect(delegate_->size()));
263 cursor_window_->SchedulePaintInRect(
264 gfx::Rect(cursor_window_->bounds().size()));
265 UpdateLocation();
269 void CursorWindowController::UpdateCursorVisibility() {
270 if (!cursor_window_)
271 return;
272 bool visible = (visible_ && cursor_type_ != ui::kCursorNone);
273 if (visible)
274 cursor_window_->Show();
275 else
276 cursor_window_->Hide();
279 } // namespace ash