chromium/chrome/browser/apps/browser_instance/browser_app_instance_tracker.h

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

#ifndef CHROME_BROWSER_APPS_BROWSER_INSTANCE_BROWSER_APP_INSTANCE_TRACKER_H_
#define CHROME_BROWSER_APPS_BROWSER_INSTANCE_BROWSER_APP_INSTANCE_TRACKER_H_

#include <map>
#include <memory>
#include <set>

#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_multi_source_observation.h"
#include "base/unguessable_token.h"
#include "chrome/browser/apps/browser_instance/browser_app_instance.h"
#include "chrome/browser/apps/browser_instance/browser_app_instance_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_tab_strip_tracker.h"
#include "chrome/browser/ui/browser_tab_strip_tracker_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "components/app_constants/constants.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "ui/wm/public/activation_change_observer.h"
#include "ui/wm/public/activation_client.h"
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

class Browser;
class Profile;

namespace aura {
class Window;
}

namespace apps {

class BrowserAppInstanceObserver;

// BrowserAppInstanceTracker monitors changes to Browsers, TabStripModels and
// browsers' native window activation to maintain a list of running apps and
// notify its registered observers of any changes:
// - apps running in WebContents (web apps, hosted apps, V1 packaged apps)
// - browser instances (registered with app ID |app_constants::kChromeAppId|).
class BrowserAppInstanceTracker : public TabStripModelObserver,
                                  public BrowserTabStripTrackerDelegate,
                                  public AppRegistryCache::Observer,
                                  public BrowserListObserver {};

#if BUILDFLAG(IS_CHROMEOS_LACROS)
// TODO(b/332628771): Remove this class 2 mile stones from this patch.
// Now that activation is observed by Ash even for Lacros windows,
// |BrowserAppInstanceTracker| no longer needs to observe activation changes.
// However to support older Ash, |BrowserAppInstanceTrackerLacros| adds
// |ActivationChangeObserver| functionality to |BrowserAppInstanceTracker| and
// notifies Ash.
class BrowserAppInstanceTrackerLacros : public BrowserAppInstanceTracker,
                                        public wm::ActivationChangeObserver {
 public:
  BrowserAppInstanceTrackerLacros(Profile* profile,
                                  AppRegistryCache& app_registry_cache);
  ~BrowserAppInstanceTrackerLacros() override;
  BrowserAppInstanceTrackerLacros(const BrowserAppInstanceTrackerLacros&) =
      delete;
  BrowserAppInstanceTrackerLacros& operator=(
      const BrowserAppInstanceTrackerLacros&) = delete;

  // wm::ActivationChangeObserver overrides:
  void OnWindowActivated(ActivationReason reason,
                         aura::Window* gained_active,
                         aura::Window* lost_active) override;

 private:
  // Updates the browser instance with the new attributes and notifies
  // observers, if it was updated.
  void MaybeUpdateBrowserWindowInstance(BrowserWindowInstance& instance,
                                        Browser* browser);
  // Called on browser window changes. Sends update events for all open tabs.
  void OnBrowserWindowUpdated(Browser* browser);

  bool IsActivationClientTracked(wm::ActivationClient* client) const;

  // In addition to calling
  // |BrowserAppInstanceTracker::OnBrowserFirstTabAttached| starts observing
  // |ActivationClient| corresponding to |browser|.
  void OnBrowserFirstTabAttached(Browser* browser) override;
  // In addition to calling
  // |BrowserAppInstanceTracker::OnBrowserLastTabDetached| stops observing
  // |ActivationClient| corresponding to |browser|.
  void OnBrowserLastTabDetached(Browser* browser) override;

  // A set of observed activation clients for all browser's windows.
  base::ScopedMultiSourceObservation<wm::ActivationClient,
                                     wm::ActivationChangeObserver>
      activation_client_observations_{this};
};
#endif  // BUILDFLAG(IS_CHROMEOS_LACROS)

}  // namespace apps

#endif  // CHROME_BROWSER_APPS_BROWSER_INSTANCE_BROWSER_APP_INSTANCE_TRACKER_H_