summaryrefslogtreecommitdiff
path: root/chromium/content/browser/media/capture/cursor_renderer_aura.cc
blob: ec13d0da4689d85f016e981d7008bcd409b078ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/media/capture/cursor_renderer_aura.h"

#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/cursor/cursors_aura.h"
#include "ui/events/event_utils.h"
#include "ui/wm/public/activation_client.h"

namespace content {

// static
std::unique_ptr<CursorRenderer> CursorRenderer::Create(
    CursorRenderer::CursorDisplaySetting display) {
  return std::make_unique<CursorRendererAura>(display);
}

CursorRendererAura::CursorRendererAura(CursorDisplaySetting display)
    : CursorRenderer(display) {}

CursorRendererAura::~CursorRendererAura() {
  SetTargetView(nullptr);
}

void CursorRendererAura::SetTargetView(aura::Window* window) {
  if (window_) {
    window_->RemoveObserver(this);
    window_->RemovePreTargetHandler(this);
  }
  window_ = window;
  OnMouseHasGoneIdle();
  if (window_) {
    window_->AddObserver(this);
    window_->AddPreTargetHandler(this);
  }
}

bool CursorRendererAura::IsCapturedViewActive() {
  if (!window_) {
    DVLOG(2) << "Skipping update with no window being tracked";
    return false;
  }

  // If we are sharing the root window, or namely the whole screen, we will
  // render the mouse cursor. For ordinary window, we only render the mouse
  // cursor when the window is active.
  if (!window_->IsRootWindow()) {
    wm::ActivationClient* activation_client =
        wm::GetActivationClient(window_->GetRootWindow());
    if (!activation_client) {
      DVLOG(2) << "Assume window inactive with invalid activation_client";
      return false;
    }
    aura::Window* active_window = activation_client->GetActiveWindow();
    if (!active_window) {
      DVLOG(2) << "Skipping update as there is no active window";
      return false;
    }
    if (!active_window->Contains(window_)) {
      // Return early if the target window is not active.
      DVLOG(2) << "Skipping update on an inactive window";
      return false;
    }
  }
  return true;
}

gfx::Size CursorRendererAura::GetCapturedViewSize() {
  if (!window_) {
    return gfx::Size();
  }

  const gfx::Rect window_bounds = window_->GetBoundsInScreen();
  const gfx::Size view_size(window_bounds.width(), window_bounds.height());
  return view_size;
}

gfx::Point CursorRendererAura::GetCursorPositionInView() {
  if (!window_) {
    return gfx::Point(-1, -1);
  }

  // Convert from screen coordinates to view coordinates.
  aura::Window* const root_window = window_->GetRootWindow();
  if (!root_window)
    return gfx::Point(-1, -1);
  aura::client::ScreenPositionClient* const client =
      aura::client::GetScreenPositionClient(root_window);
  if (!client)
    return gfx::Point(-1, -1);
  gfx::Point cursor_position = aura::Env::GetInstance()->last_mouse_location();
  client->ConvertPointFromScreen(window_, &cursor_position);
  return cursor_position;
}

gfx::NativeCursor CursorRendererAura::GetLastKnownCursor() {
  if (!window_) {
    return gfx::NativeCursor();
  }

  return window_->GetHost()->last_cursor();
}

SkBitmap CursorRendererAura::GetLastKnownCursorImage(gfx::Point* hot_point) {
  if (!window_) {
    return SkBitmap();
  }

  gfx::NativeCursor cursor = window_->GetHost()->last_cursor();
  *hot_point = cursor.GetHotspot();
  return cursor.GetBitmap();
}

void CursorRendererAura::OnMouseEvent(ui::MouseEvent* event) {
  gfx::Point mouse_location(event->x(), event->y());
  switch (event->type()) {
    case ui::ET_MOUSE_MOVED:
      OnMouseMoved(mouse_location);
      break;
    case ui::ET_MOUSE_PRESSED:
    case ui::ET_MOUSE_RELEASED:
    case ui::ET_MOUSEWHEEL:
      OnMouseClicked(mouse_location);
      break;
    default:
      return;
  }
}

void CursorRendererAura::OnWindowDestroying(aura::Window* window) {
  DCHECK_EQ(window_, window);
  window_->RemovePreTargetHandler(this);
  window_->RemoveObserver(this);
  window_ = nullptr;
}

}  // namespace content