- add sources.
[platform/framework/web/crosswalk.git] / src / ui / views / widget / desktop_aura / desktop_capture_client.cc
1 // Copyright 2013 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/views/widget/desktop_aura/desktop_capture_client.h"
6
7 #include "ui/aura/root_window.h"
8 #include "ui/aura/window.h"
9
10 namespace views {
11
12 // static
13 DesktopCaptureClient::CaptureClients*
14 DesktopCaptureClient::capture_clients_ = NULL;
15
16 DesktopCaptureClient::DesktopCaptureClient(aura::RootWindow* root)
17     : root_(root),
18       capture_window_(NULL) {
19   if (!capture_clients_)
20     capture_clients_ = new CaptureClients;
21   capture_clients_->insert(this);
22   aura::client::SetCaptureClient(root, this);
23 }
24
25 DesktopCaptureClient::~DesktopCaptureClient() {
26   aura::client::SetCaptureClient(root_, NULL);
27   capture_clients_->erase(this);
28 }
29
30 void DesktopCaptureClient::SetCapture(aura::Window* new_capture_window) {
31   if (capture_window_ == new_capture_window)
32     return;
33
34   // We should only ever be told to capture a child of |root_|. Otherwise
35   // things are going to be really confused.
36   DCHECK(!new_capture_window ||
37          (new_capture_window->GetRootWindow() == root_));
38   DCHECK(!capture_window_ || capture_window_->GetRootWindow());
39
40   aura::Window* old_capture_window = capture_window_;
41
42   // If we're actually starting capture, then cancel any touches/gestures
43   // that aren't already locked to the new window, and transfer any on the
44   // old capture window to the new one.  When capture is released we have no
45   // distinction between the touches/gestures that were in the window all
46   // along (and so shouldn't be canceled) and those that got moved, so
47   // just leave them all where they are.
48   if (new_capture_window) {
49     ui::GestureRecognizer::Get()->TransferEventsTo(old_capture_window,
50         new_capture_window);
51   }
52
53   capture_window_ = new_capture_window;
54
55   aura::client::CaptureDelegate* delegate = root_;
56   delegate->UpdateCapture(old_capture_window, new_capture_window);
57
58   // Initiate native capture updating.
59   if (!capture_window_) {
60     delegate->ReleaseNativeCapture();
61   } else if (!old_capture_window) {
62     delegate->SetNativeCapture();
63
64     // Notify the other roots that we got capture. This is important so that
65     // they reset state.
66     CaptureClients capture_clients(*capture_clients_);
67     for (CaptureClients::iterator i = capture_clients.begin();
68          i != capture_clients.end(); ++i) {
69       if (*i != this) {
70         aura::client::CaptureDelegate* delegate = (*i)->root_;
71         delegate->OnOtherRootGotCapture();
72       }
73     }
74   }  // else case is capture is remaining in our root, nothing to do.
75 }
76
77 void DesktopCaptureClient::ReleaseCapture(aura::Window* window) {
78   if (capture_window_ != window)
79     return;
80   SetCapture(NULL);
81 }
82
83 aura::Window* DesktopCaptureClient::GetCaptureWindow() {
84   return capture_window_;
85 }
86
87 }  // namespace views