Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / wm / core / capture_controller.cc
blob3024a7d02ff96e2410bffcffe0b844ccc01bbffc
1 // Copyright (c) 2012 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 "ui/wm/core/capture_controller.h"
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_event_dispatcher.h"
9 #include "ui/aura/window_tree_host.h"
11 namespace wm {
13 ////////////////////////////////////////////////////////////////////////////////
14 // CaptureController, public:
16 void CaptureController::Attach(aura::Window* root) {
17 DCHECK_EQ(0u, delegates_.count(root));
18 delegates_[root] = root->GetHost()->dispatcher();
19 aura::client::SetCaptureClient(root, this);
22 void CaptureController::Detach(aura::Window* root) {
23 delegates_.erase(root);
24 aura::client::SetCaptureClient(root, nullptr);
27 ////////////////////////////////////////////////////////////////////////////////
28 // CaptureController, aura::client::CaptureClient implementation:
30 void CaptureController::SetCapture(aura::Window* new_capture_window) {
31 if (capture_window_ == new_capture_window)
32 return;
34 // Make sure window has a root window.
35 DCHECK(!new_capture_window || new_capture_window->GetRootWindow());
36 DCHECK(!capture_window_ || capture_window_->GetRootWindow());
38 aura::Window* old_capture_window = capture_window_;
39 aura::client::CaptureDelegate* old_capture_delegate = capture_delegate_;
41 // Copy the map in case it's modified out from under us.
42 std::map<aura::Window*, aura::client::CaptureDelegate*> delegates =
43 delegates_;
45 // If we're starting a new capture, cancel all touches that aren't
46 // targeted to the capturing window.
47 if (new_capture_window)
48 ui::GestureRecognizer::Get()->CancelActiveTouchesExcept(new_capture_window);
50 capture_window_ = new_capture_window;
51 aura::Window* capture_root_window =
52 capture_window_ ? capture_window_->GetRootWindow() : nullptr;
53 capture_delegate_ = delegates_.find(capture_root_window) == delegates_.end()
54 ? nullptr
55 : delegates_[capture_root_window];
57 for (const auto& it : delegates)
58 it.second->UpdateCapture(old_capture_window, new_capture_window);
60 if (capture_delegate_ != old_capture_delegate) {
61 if (old_capture_delegate)
62 old_capture_delegate->ReleaseNativeCapture();
63 if (capture_delegate_)
64 capture_delegate_->SetNativeCapture();
68 void CaptureController::ReleaseCapture(aura::Window* window) {
69 if (capture_window_ != window)
70 return;
71 SetCapture(nullptr);
74 aura::Window* CaptureController::GetCaptureWindow() {
75 return capture_window_;
78 aura::Window* CaptureController::GetGlobalCaptureWindow() {
79 return capture_window_;
82 ////////////////////////////////////////////////////////////////////////////////
83 // CaptureController, private:
85 CaptureController::CaptureController()
86 : capture_window_(nullptr),
87 capture_delegate_(nullptr) {
90 CaptureController::~CaptureController() {
93 ////////////////////////////////////////////////////////////////////////////////
94 // ScopedCaptureClient:
96 // static
97 CaptureController* ScopedCaptureClient::capture_controller_ = nullptr;
99 ScopedCaptureClient::ScopedCaptureClient(aura::Window* root)
100 : root_window_(root) {
101 root->AddObserver(this);
102 if (!capture_controller_)
103 capture_controller_ = new CaptureController;
104 capture_controller_->Attach(root);
107 ScopedCaptureClient::~ScopedCaptureClient() {
108 Shutdown();
111 // static
112 bool ScopedCaptureClient::IsActive() {
113 return capture_controller_ && capture_controller_->is_active();
116 void ScopedCaptureClient::OnWindowDestroyed(aura::Window* window) {
117 DCHECK_EQ(window, root_window_);
118 Shutdown();
121 void ScopedCaptureClient::Shutdown() {
122 if (!root_window_)
123 return;
125 root_window_->RemoveObserver(this);
126 capture_controller_->Detach(root_window_);
127 if (!capture_controller_->is_active()) {
128 delete capture_controller_;
129 capture_controller_ = nullptr;
131 root_window_ = nullptr;
134 ///////////////////////////////////////////////////////////////////////////////
135 // CaptureController::TestApi
137 void ScopedCaptureClient::TestApi::SetDelegate(
138 aura::client::CaptureDelegate* delegate) {
139 client_->capture_controller_->delegates_[client_->root_window_] = delegate;
142 } // namespace wm