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. 4 5 #include "ui/wm/core/capture_controller.h" 6 7 #include "ui/aura/window.h" 8 #include "ui/aura/window_event_dispatcher.h" 9 #include "ui/aura/window_tree_host.h" 10 11 namespace wm { 12 13 //////////////////////////////////////////////////////////////////////////////// 14 // CaptureController, public: 15 16 void CaptureController::Attach(aura::Window* root) { 17 DCHECK_EQ(0u, root_windows_.count(root)); 18 root_windows_.insert(root); 19 aura::client::SetCaptureClient(root, this); 20 } 21 22 void CaptureController::Detach(aura::Window* root) { 23 root_windows_.erase(root); 24 aura::client::SetCaptureClient(root, NULL); 25 } 26 27 //////////////////////////////////////////////////////////////////////////////// 28 // CaptureController, aura::client::CaptureClient implementation: 29 30 void CaptureController::SetCapture(aura::Window* new_capture_window) { 31 if (capture_window_ == new_capture_window) 32 return; 33 34 // Make sure window has a root window. 35 DCHECK(!new_capture_window || new_capture_window->GetRootWindow()); 36 DCHECK(!capture_window_ || capture_window_->GetRootWindow()); 37 38 aura::Window* old_capture_window = capture_window_; 39 aura::Window* old_capture_root = old_capture_window ? 40 old_capture_window->GetRootWindow() : NULL; 41 42 // Copy the list in case it's modified out from under us. 43 RootWindows root_windows(root_windows_); 44 45 // If we're actually starting capture, then cancel any touches/gestures 46 // that aren't already locked to the new window, and transfer any on the 47 // old capture window to the new one. When capture is released we have no 48 // distinction between the touches/gestures that were in the window all 49 // along (and so shouldn't be canceled) and those that got moved, so 50 // just leave them all where they are. 51 if (new_capture_window) { 52 ui::GestureRecognizer::Get()->TransferEventsTo(old_capture_window, 53 new_capture_window); 54 } 55 56 capture_window_ = new_capture_window; 57 58 for (RootWindows::const_iterator i = root_windows.begin(); 59 i != root_windows.end(); ++i) { 60 aura::client::CaptureDelegate* delegate = (*i)->GetHost()->dispatcher(); 61 delegate->UpdateCapture(old_capture_window, new_capture_window); 62 } 63 64 aura::Window* capture_root = 65 capture_window_ ? capture_window_->GetRootWindow() : NULL; 66 if (capture_root != old_capture_root) { 67 if (old_capture_root) { 68 aura::client::CaptureDelegate* delegate = 69 old_capture_root->GetHost()->dispatcher(); 70 delegate->ReleaseNativeCapture(); 71 } 72 if (capture_root) { 73 aura::client::CaptureDelegate* delegate = 74 capture_root->GetHost()->dispatcher(); 75 delegate->SetNativeCapture(); 76 } 77 } 78 } 79 80 void CaptureController::ReleaseCapture(aura::Window* window) { 81 if (capture_window_ != window) 82 return; 83 SetCapture(NULL); 84 } 85 86 aura::Window* CaptureController::GetCaptureWindow() { 87 return capture_window_; 88 } 89 90 aura::Window* CaptureController::GetGlobalCaptureWindow() { 91 return capture_window_; 92 } 93 94 //////////////////////////////////////////////////////////////////////////////// 95 // CaptureController, private: 96 97 CaptureController::CaptureController() 98 : capture_window_(NULL) { 99 } 100 101 CaptureController::~CaptureController() { 102 } 103 104 //////////////////////////////////////////////////////////////////////////////// 105 // ScopedCaptureClient: 106 107 // static 108 CaptureController* ScopedCaptureClient::capture_controller_ = NULL; 109 110 ScopedCaptureClient::ScopedCaptureClient(aura::Window* root) 111 : root_window_(root) { 112 root->AddObserver(this); 113 if (!capture_controller_) 114 capture_controller_ = new CaptureController; 115 capture_controller_->Attach(root); 116 } 117 118 ScopedCaptureClient::~ScopedCaptureClient() { 119 Shutdown(); 120 } 121 122 // static 123 bool ScopedCaptureClient::IsActive() { 124 return capture_controller_ && capture_controller_->is_active(); 125 } 126 127 void ScopedCaptureClient::OnWindowDestroyed(aura::Window* window) { 128 DCHECK_EQ(window, root_window_); 129 Shutdown(); 130 } 131 132 void ScopedCaptureClient::Shutdown() { 133 if (!root_window_) 134 return; 135 136 root_window_->RemoveObserver(this); 137 capture_controller_->Detach(root_window_); 138 if (!capture_controller_->is_active()) { 139 delete capture_controller_; 140 capture_controller_ = NULL; 141 } 142 root_window_ = NULL; 143 } 144 145 } // namespace wm 146