File: browser_conditions.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (206 lines) | stat: -rw-r--r-- 7,345 bytes parent folder | download | duplicates (5)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// 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/glic/widget/browser_conditions.h"

#include "base/memory/raw_ptr.h"
#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "base/timer/timer.h"
#include "chrome/browser/glic/glic_enabling.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "ui/views/widget/widget_observer.h"

#if BUILDFLAG(IS_WIN)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/gfx/win/hwnd_util.h"
#endif  // BUILDFLAG(IS_WIN)

namespace glic {

namespace {
#if BUILDFLAG(IS_WIN)

struct IsBrowserTopmostWindowState {
  HWND browser_hwnd = nullptr;
  bool browser_is_topmost_window = false;
};

// Window enumerator used to determine if the top most visible window, other
// than the system tray, is the browser window it is looking for. This is called
// in z-order, i.e., topmost window first.
// Window enumerator, so returning FALSE stops enumerating.
// `lParam` is a pointer to IsBrowserTopmostWindowState, which contains the
// browser HWND it is looking for. When finished enumerating, it sets
// topmost_visible_non_opaque_hwnd to the topmost non system tray HWND it
// finds.
BOOL CALLBACK IsBrowserWindowTopmostWindowEnumerator(HWND hwnd, LPARAM lParam) {
  struct IsBrowserTopmostWindowState* state =
      reinterpret_cast<struct IsBrowserTopmostWindowState*>(lParam);
  if (hwnd == state->browser_hwnd) {
    state->browser_is_topmost_window = true;
    return FALSE;
  } else if (gfx::GetClassName(hwnd) != L"Shell_TrayWnd" &&
             gfx::IsWindowVisibleAndFullyOpaque(hwnd, nullptr)) {
    state->browser_is_topmost_window = false;
    return FALSE;
  }
  return TRUE;
}

bool IsBrowserWindowTopmostWindow(Browser* browser) {
  HWND browser_hwnd =
      browser->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget();

  struct IsBrowserTopmostWindowState state{browser_hwnd, false};
  EnumWindows(&IsBrowserWindowTopmostWindowEnumerator,
              reinterpret_cast<LPARAM>(&state));
  return state.browser_is_topmost_window;
}

#endif  // BUILDFLAG(IS_WIN)

bool IsBrowserGlicCompatible(Profile* profile, Browser* browser) {
  // A browser is not compatible if it:
  // - is not a TYPE_NORMAL browser
  // - is from a glic-disabled profile
  // - uses a different Profile from glic
  // WARNING: updating these conditions will require updating
  // BrowserAttachObservation.
  return GlicEnabling::IsEnabledForProfile(browser->profile()) &&
         browser->is_type_normal() && browser->profile() == profile;
}

}  // namespace

bool IsBrowserGlicAttachable(Profile* profile, Browser* browser) {
  return IsBrowserGlicCompatible(profile, browser) &&
         browser->window()->IsVisible() && !browser->window()->IsMinimized();
}

Browser* FindBrowserForAttachment(Profile* profile) {
  // TODO (crbug.com/390472495) Determine which browser to attach to. Currently
  // attaches to the last focused glic-compatible browser.
  for (Browser* browser : BrowserList::GetInstance()->OrderedByActivation()) {
    if (IsBrowserGlicAttachable(profile, browser)) {
      return browser;
    }
  }
  return nullptr;
}

bool IsBrowserInForeground(Browser* browser) {
  if (browser->IsActive()) {
    return true;
  }
#if BUILDFLAG(IS_WIN)
  // On Windows, clicking the status bar icon makes an active browser window
  // inactive, but it will still be the last active browser. Attach to the
  // last active browser if it's the foremost visible window, other than the
  // system tray.
  return IsBrowserWindowTopmostWindow(browser);
#else
  return false;
#endif  // BUILDFLAG(IS_WIN)
}

class BrowserAttachObservationImpl : public BrowserAttachObservation,
                                     public BrowserListObserver,
                                     public views::WidgetObserver {
 public:
  BrowserAttachObservationImpl(Profile* profile,
                               BrowserAttachObserver* observer)
      : profile_(profile),
        observer_(observer),
        browser_list_observation_(this),
        browser_widget_observations_(this) {
    for (auto browser : *BrowserList::GetInstance()) {
      OnBrowserAdded(browser);
    }
    browser_list_observation_.Observe(BrowserList::GetInstance());
    current_value_ = FindBrowserForAttachment(profile_);
  }

  bool CanAttachToBrowser() const override { return current_value_ != nullptr; }

  // BrowserListObserver implementation.
  void OnBrowserSetLastActive(Browser* browser) override {
    // BrowserList updates the active browser list before this call, so
    // `CheckForChange` will find the correct browser.
    CheckForChange();
  }
  void OnBrowserAdded(Browser* browser) override {
    if (IsBrowserGlicCompatible(profile_, browser)) {
      browser_widget_observations_.AddObservation(
          browser->GetBrowserView().GetWidget());
    }
  }
  void OnBrowserRemoved(Browser* browser) override {
    if (current_value_ == browser) {
      // BrowserList updates the active browser list before this call, so
      // `CheckForChange` will find the correct browser.
      CheckForChange();
    }
  }

  // views::WidgetObserver implementation.
  void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override {
    // Note: visibility change takes effect after this call, so PostMessage is
    // critical here.
    check_for_change_timer_.Start(
        FROM_HERE, base::TimeDelta(),
        base::BindOnce(&BrowserAttachObservationImpl::CheckForChange,
                       base::Unretained(this)));
  }
  void OnWidgetShowStateChanged(views::Widget* widget) override {
    CheckForChange();
  }
  void OnWidgetDestroyed(views::Widget* widget) override {
    // Note: widget observer removal has to be done at widget destruction time
    // because when OnBrowserRemoved is called, the widget has already
    // been destroyed.
    if (browser_widget_observations_.IsObservingSource(widget)) {
      browser_widget_observations_.RemoveObservation(widget);
    }
  }

 private:
  void CheckForChange() {
    SetBrowserForAttachment(FindBrowserForAttachment(profile_));
  }

  void SetBrowserForAttachment(Browser* browser) {
    if (current_value_ == browser) {
      return;
    }
    bool could_attach = current_value_ != nullptr;
    bool can_attach = browser != nullptr;
    current_value_ = browser;
    observer_->BrowserForAttachmentChanged(browser);
    if (could_attach != can_attach) {
      observer_->CanAttachToBrowserChanged(can_attach);
    }
  }

  raw_ptr<Profile> profile_;
  raw_ptr<Browser> current_value_;
  raw_ptr<BrowserAttachObserver> observer_;
  base::OneShotTimer check_for_change_timer_;
  base::ScopedObservation<BrowserList, BrowserListObserver>
      browser_list_observation_;
  base::ScopedMultiSourceObservation<views::Widget, views::WidgetObserver>
      browser_widget_observations_;
};

std::unique_ptr<BrowserAttachObservation> ObserveBrowserForAttachment(
    Profile* profile,
    BrowserAttachObserver* observer) {
  return std::make_unique<BrowserAttachObservationImpl>(profile, observer);
}

}  // namespace glic