1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101

ash / components / arc / window / arc_window_watcher.h [blame]

// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_COMPONENTS_ARC_WINDOW_ARC_WINDOW_WATCHER_H_
#define ASH_COMPONENTS_ARC_WINDOW_ARC_WINDOW_WATCHER_H_

#include <vector>

#include "ash/components/arc/arc_export.h"
#include "base/observer_list.h"
#include "ui/aura/env.h"
#include "ui/aura/env_observer.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"

namespace ash {

namespace {
class Tracker;
}

// ArcWindowWatcher provides live monitoring of Arc Windows.
// It distills basic window transition events into consolidated
// ARC-window and Android-task related events.
class ARC_EXPORT ArcWindowWatcher : public aura::EnvObserver {
 public:
  using TrackerList = std::vector<std::unique_ptr<Tracker>>;
  class ArcWindowCountObserver : public base::CheckedObserver {
   public:
    // Notifies that window count has changed.
    virtual void OnArcWindowCountChanged(uint32_t count) = 0;
    virtual void OnWillDestroyWatcher() = 0;
  };

  class ArcWindowDisplayObserver : public base::CheckedObserver {
   public:
    // Notifies that a new window is display. This is guaranteed to happen
    // after the count is updated.
    virtual void OnArcWindowDisplayed(const std::string& pkg_name) = 0;
    virtual void OnWillDestroyWatcher() = 0;
  };

  // Returns the single ArcWindowWatcher instance.
  static ArcWindowWatcher* instance() { return instance_; }

  ArcWindowWatcher();

  ArcWindowWatcher(const ArcWindowWatcher&) = delete;
  ArcWindowWatcher& operator=(const ArcWindowWatcher&) = delete;

  ~ArcWindowWatcher() override;

  // aura::EnvObserver:
  void OnWindowInitialized(aura::Window* window) override;

  uint32_t GetArcWindowCount() const;

  void AddObserver(ArcWindowCountObserver* observer);
  void RemoveObserver(ArcWindowCountObserver* observer);

  // Manage the list of arc-window-display observers.
  void AddObserver(ArcWindowDisplayObserver* observer);
  void RemoveObserver(ArcWindowDisplayObserver* observer);

  // Notifies observers of a change in window count
  void BroadcastArcWindowCount(uint32_t count);

  // Notifies observers that a window got a package name, i.e.,
  // it was displayed by Android.
  void BroadcastArcWindowDisplay(const std::string& pkg_name);

  // Nudging from trackers to maintain internal collection.
  void OnArcWindowAdded();
  void OnTrackerRemoved(Tracker* tracker, aura::Window* arc_window);
  void OnArcWindowRemoved();

  // Query existence of a particular observer, by type.
  bool HasCountObserver(ArcWindowCountObserver* observer) const;
  bool HasDisplayObserver(ArcWindowDisplayObserver* observer) const;

 private:
  base::ObserverList<ArcWindowCountObserver, /* check_empty= */ true>
      arc_window_count_observers_;
  base::ObserverList<ArcWindowDisplayObserver, /* check_empty= */ true>
      arc_window_display_observers_;

  // Some are arc windows, some are unknown.
  TrackerList trackers_;

  // Keeps track of ARC windows. Notice we're not tracking the list of windows,
  // but it is very easy to track that too (when needed), at places where
  // this counter is modified.
  uint32_t arc_window_count_ = 0;

  static ArcWindowWatcher* instance_;
};

}  // namespace ash

#endif  // ASH_COMPONENTS_ARC_WINDOW_ARC_WINDOW_WATCHER_H_