scoped_window_capture_request.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2020 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. #include "ui/aura/scoped_window_capture_request.h"
  5. #include "ui/aura/window.h"
  6. namespace aura {
  7. ScopedWindowCaptureRequest::ScopedWindowCaptureRequest(
  8. ScopedWindowCaptureRequest&& other)
  9. // Do not decrement requests on |other| nor increment them on |this| since
  10. // we are moving the same request into here.
  11. : window_(other.DetachFromCurrentWindow(/*decrement_requests=*/false)) {
  12. if (window_)
  13. AttachToCurrentWindow(/*increment_requests=*/false);
  14. }
  15. ScopedWindowCaptureRequest& ScopedWindowCaptureRequest::operator=(
  16. ScopedWindowCaptureRequest&& rhs) {
  17. // Note that |this| might have been attached to a different window than that
  18. // of |rhs|, so we need to detach from while decrementing the requests.
  19. DetachFromCurrentWindow(/*decrement_requests=*/true);
  20. // However, |rhs| is moving into |this|, so it's essentially the same request,
  21. // therefore, no need to either increment or decrement the requests.
  22. window_ = rhs.DetachFromCurrentWindow(/*decrement_requests=*/false);
  23. if (window_)
  24. AttachToCurrentWindow(/*increment_requests=*/false);
  25. return *this;
  26. }
  27. ScopedWindowCaptureRequest::~ScopedWindowCaptureRequest() {
  28. DetachFromCurrentWindow(/*decrement_requests=*/true);
  29. }
  30. viz::SubtreeCaptureId ScopedWindowCaptureRequest::GetCaptureId() const {
  31. return window_ ? window_->subtree_capture_id() : viz::SubtreeCaptureId();
  32. }
  33. void ScopedWindowCaptureRequest::OnWindowDestroying(Window* window) {
  34. // No need to call OnScopedWindowCaptureRequestRemoved() since the window is
  35. // being destroyed.
  36. DetachFromCurrentWindow(/*decrement_requests=*/false);
  37. }
  38. ScopedWindowCaptureRequest::ScopedWindowCaptureRequest(Window* window)
  39. : window_(window) {
  40. AttachToCurrentWindow(/*increment_requests=*/true);
  41. }
  42. void ScopedWindowCaptureRequest::AttachToCurrentWindow(
  43. bool increment_requests) {
  44. DCHECK(window_);
  45. DCHECK(!window_->IsRootWindow());
  46. if (increment_requests)
  47. window_->OnScopedWindowCaptureRequestAdded();
  48. window_->AddObserver(this);
  49. }
  50. Window* ScopedWindowCaptureRequest::DetachFromCurrentWindow(
  51. bool decrement_requests) {
  52. Window* result = window_;
  53. if (window_) {
  54. window_->RemoveObserver(this);
  55. if (decrement_requests)
  56. window_->OnScopedWindowCaptureRequestRemoved();
  57. window_ = nullptr;
  58. }
  59. return result;
  60. }
  61. } // namespace aura