| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "services/window_manager/capture_controller.h" | |
| 6 | |
| 7 #include "mojo/services/view_manager/cpp/view_property.h" | |
| 8 #include "mojo/services/view_manager/cpp/view_tracker.h" | |
| 9 #include "services/window_manager/capture_controller_observer.h" | |
| 10 | |
| 11 DECLARE_VIEW_PROPERTY_TYPE(window_manager::CaptureController*); | |
| 12 | |
| 13 namespace window_manager { | |
| 14 | |
| 15 namespace { | |
| 16 DEFINE_VIEW_PROPERTY_KEY(CaptureController*, | |
| 17 kRootViewCaptureController, | |
| 18 nullptr); | |
| 19 } // namespace | |
| 20 | |
| 21 CaptureController::CaptureController() | |
| 22 : capture_view_(nullptr) {} | |
| 23 | |
| 24 CaptureController::~CaptureController() {} | |
| 25 | |
| 26 void CaptureController::AddObserver(CaptureControllerObserver* observer) { | |
| 27 capture_controller_observers_.AddObserver(observer); | |
| 28 } | |
| 29 | |
| 30 void CaptureController::RemoveObserver(CaptureControllerObserver* observer) { | |
| 31 capture_controller_observers_.RemoveObserver(observer); | |
| 32 } | |
| 33 | |
| 34 void CaptureController::SetCapture(mojo::View* view) { | |
| 35 if (capture_view_ == view) | |
| 36 return; | |
| 37 | |
| 38 if (capture_view_) | |
| 39 capture_view_->RemoveObserver(this); | |
| 40 | |
| 41 mojo::View* old_capture_view = capture_view_; | |
| 42 capture_view_ = view; | |
| 43 | |
| 44 if (capture_view_) | |
| 45 capture_view_->AddObserver(this); | |
| 46 | |
| 47 NotifyCaptureChange(capture_view_, old_capture_view); | |
| 48 } | |
| 49 | |
| 50 void CaptureController::ReleaseCapture(mojo::View* view) { | |
| 51 if (capture_view_ != view) | |
| 52 return; | |
| 53 SetCapture(nullptr); | |
| 54 } | |
| 55 | |
| 56 mojo::View* CaptureController::GetCapture() { | |
| 57 return capture_view_; | |
| 58 } | |
| 59 | |
| 60 void CaptureController::NotifyCaptureChange(mojo::View* new_capture, | |
| 61 mojo::View* old_capture) { | |
| 62 mojo::ViewTracker view_tracker; | |
| 63 if (new_capture) | |
| 64 view_tracker.Add(new_capture); | |
| 65 if (old_capture) | |
| 66 view_tracker.Add(old_capture); | |
| 67 | |
| 68 FOR_EACH_OBSERVER( | |
| 69 CaptureControllerObserver, capture_controller_observers_, | |
| 70 OnCaptureChanged(view_tracker.Contains(new_capture) ? new_capture | |
| 71 : nullptr)); | |
| 72 } | |
| 73 | |
| 74 void CaptureController::OnViewDestroying(mojo::View* view) { | |
| 75 if (view == capture_view_) { | |
| 76 view->RemoveObserver(this); | |
| 77 NotifyCaptureChange(nullptr, view); | |
| 78 capture_view_ = nullptr; | |
| 79 } | |
| 80 } | |
| 81 | |
| 82 void SetCaptureController(mojo::View* root_view, | |
| 83 CaptureController* capture_controller) { | |
| 84 DCHECK_EQ(root_view->GetRoot(), root_view); | |
| 85 root_view->SetLocalProperty(kRootViewCaptureController, capture_controller); | |
| 86 } | |
| 87 | |
| 88 CaptureController* GetCaptureController(mojo::View* root_view) { | |
| 89 if (root_view) | |
| 90 DCHECK_EQ(root_view->GetRoot(), root_view); | |
| 91 return root_view ? | |
| 92 root_view->GetLocalProperty(kRootViewCaptureController) : nullptr; | |
| 93 } | |
| 94 | |
| 95 mojo::View* GetCaptureView(mojo::View* view) { | |
| 96 mojo::View* root = view->GetRoot(); | |
| 97 if (!root) | |
| 98 return nullptr; | |
| 99 CaptureController* controller = GetCaptureController(root); | |
| 100 return controller ? controller->GetCapture() : nullptr; | |
| 101 } | |
| 102 | |
| 103 } // namespace window_manager | |
| OLD | NEW |