File: app_window_helper.cc

package info (click to toggle)
chromium 141.0.7390.107-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie-proposed-updates
  • size: 6,246,428 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (62 lines) | stat: -rw-r--r-- 2,242 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/tabs/app_window_helper.h"

#include "chrome/browser/extensions/api/tabs/app_base_window.h"
#include "chrome/browser/extensions/api/tabs/app_window_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/app_window/app_window.h"

namespace extensions {

AppWindowHelper::AppWindowHelper(Profile* profile,
                                 ActiveWindowChangedCallback callback)
    : profile_(profile), active_window_changed_callback_(std::move(callback)) {
  AppWindowRegistry* registry = AppWindowRegistry::Get(profile);

  app_registry_observation_.Observe(registry);

  for (AppWindow* app_window : registry->app_windows()) {
    AddAppWindow(app_window);
  }
}

AppWindowHelper::~AppWindowHelper() = default;

void AppWindowHelper::OnAppWindowAdded(AppWindow* app_window) {
  // We only observe the AppWindowRegistry for our associated Profile, so this
  // should always match.
  CHECK(profile_->IsSameOrParent(
      Profile::FromBrowserContext(app_window->browser_context())));

  AddAppWindow(app_window);
}

void AppWindowHelper::OnAppWindowRemoved(AppWindow* app_window) {
  // We only observe the AppWindowRegistry for our associated Profile, so this
  // should always match.
  CHECK(profile_->IsSameOrParent(
      Profile::FromBrowserContext(app_window->browser_context())));

  app_windows_.erase(app_window->session_id().id());
}

void AppWindowHelper::OnAppWindowActivated(AppWindow* app_window) {
  AppWindowMap::const_iterator iter =
      app_windows_.find(app_window->session_id().id());
  // We create a new entry in `app_windows_` for every AppWindow when it's
  // created, so there should always be an entry.
  CHECK(iter != app_windows_.end());

  active_window_changed_callback_.Run(iter->second.get());
}

void AppWindowHelper::AddAppWindow(AppWindow* app_window) {
  auto controller = std::make_unique<AppWindowController>(
      app_window, std::make_unique<AppBaseWindow>(app_window), profile_);
  app_windows_[app_window->session_id().id()] = std::move(controller);
}

}  // namespace extensions