scoped_window_event_targeting_blocker.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2019 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_event_targeting_blocker.h"
  5. #include "ui/aura/window.h"
  6. namespace aura {
  7. ScopedWindowEventTargetingBlocker::ScopedWindowEventTargetingBlocker(
  8. Window* window)
  9. : window_(window) {
  10. if (window_->event_targeting_blocker_count_ == 0) {
  11. window_->restore_event_targeting_policy_ = window_->event_targeting_policy_;
  12. window_->SetEventTargetingPolicy(EventTargetingPolicy::kNone);
  13. }
  14. // Increase |Window::event_targeting_blocker_count_| after setting the event
  15. // targeting policy to kNone as Window::SetEventTargetingPolicy() relies on
  16. // |Window::event_targeting_blocker_count_| to see if the policy is allowed
  17. // to be changed.
  18. window_->event_targeting_blocker_count_++;
  19. window_->AddObserver(this);
  20. }
  21. ScopedWindowEventTargetingBlocker::~ScopedWindowEventTargetingBlocker() {
  22. if (!window_)
  23. return;
  24. window_->RemoveObserver(this);
  25. window_->event_targeting_blocker_count_--;
  26. DCHECK_GE(window_->event_targeting_blocker_count_, 0);
  27. if (window_->event_targeting_blocker_count_ == 0)
  28. window_->SetEventTargetingPolicy(window_->restore_event_targeting_policy_);
  29. }
  30. void ScopedWindowEventTargetingBlocker::OnWindowDestroying(Window* window) {
  31. DCHECK_EQ(window, window_);
  32. window_->RemoveObserver(this);
  33. window_ = nullptr;
  34. }
  35. } // namespace aura