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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
// Copyright 2012 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/windows_event_router.h"
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/tabs/app_base_window.h"
#include "chrome/browser/extensions/api/tabs/app_window_controller.h"
#include "chrome/browser/extensions/api/tabs/windows_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/window_controller.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/tabs.h"
#include "chrome/common/extensions/api/windows.h"
#include "chrome/common/extensions/extension_constants.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_util.h"
#include "extensions/common/constants.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/event_dispatcher.mojom.h"
#if BUILDFLAG(IS_MAC)
#include "chrome/browser/browser_process_platform_part.h"
#endif
using content::BrowserContext;
namespace extensions {
namespace windows = extensions::api::windows;
namespace {
constexpr char kWindowTypesKey[] = "windowTypes";
bool ControllerVisibleToListener(WindowController* window_controller,
const Extension* extension,
const base::Value::Dict* listener_filter) {
if (!window_controller)
return false;
// If there is no filter the visibility is based on the extension.
const base::Value::List* filter_value = nullptr;
if (listener_filter) {
filter_value = listener_filter->FindList(kWindowTypesKey);
}
// TODO(crbug.com/41367902): Remove this.
bool allow_dev_tools_windows = !!filter_value;
if (!window_controller->IsVisibleToTabsAPIForExtension(
extension, allow_dev_tools_windows)) {
return false;
}
return !filter_value ||
window_controller->MatchesFilter(
WindowController::GetFilterFromWindowTypesValues(filter_value));
}
bool WillDispatchWindowEvent(
WindowController* window_controller,
BrowserContext* browser_context,
mojom::ContextType target_context,
const Extension* extension,
const base::Value::Dict* listener_filter,
std::optional<base::Value::List>& event_args_out,
mojom::EventFilteringInfoPtr& event_filtering_info_out) {
bool has_filter =
listener_filter && listener_filter->contains(kWindowTypesKey);
// TODO(crbug.com/41367902): Remove this.
bool allow_dev_tools_windows = has_filter;
if (!window_controller->IsVisibleToTabsAPIForExtension(
extension, allow_dev_tools_windows)) {
return false;
}
event_filtering_info_out = mojom::EventFilteringInfo::New();
// Only set the window type if the listener has set a filter.
// Otherwise we set the window visibility relative to the extension.
if (has_filter) {
event_filtering_info_out->window_type =
window_controller->GetWindowTypeText();
} else {
event_filtering_info_out->has_window_exposed_by_default = true;
event_filtering_info_out->window_exposed_by_default = true;
}
return true;
}
bool WillDispatchWindowFocusedEvent(
WindowController* window_controller,
BrowserContext* browser_context,
mojom::ContextType target_context,
const Extension* extension,
const base::Value::Dict* listener_filter,
std::optional<base::Value::List>& event_args_out,
mojom::EventFilteringInfoPtr& event_filtering_info_out) {
int window_id = extension_misc::kUnknownWindowId;
Profile* new_active_context = nullptr;
bool has_filter =
listener_filter && listener_filter->contains(kWindowTypesKey);
// We might not have a window controller if the focus moves away
// from chromium's windows.
if (window_controller) {
window_id = window_controller->GetWindowId();
new_active_context = window_controller->profile();
}
event_filtering_info_out = mojom::EventFilteringInfo::New();
// Only set the window type if the listener has set a filter,
// otherwise set the visibility to true (if the window is not
// supposed to be visible by the extension, we will clear out the
// window id later).
if (has_filter) {
event_filtering_info_out->window_type =
window_controller ? window_controller->GetWindowTypeText()
: api::tabs::ToString(api::tabs::WindowType::kNormal);
} else {
event_filtering_info_out->has_window_exposed_by_default = true;
event_filtering_info_out->window_exposed_by_default = true;
}
// When switching between windows in the default and incognito profiles,
// dispatch WINDOW_ID_NONE to extensions whose profile lost focus that
// can't see the new focused window across the incognito boundary.
// See crbug.com/46610.
bool cant_cross_incognito =
new_active_context && new_active_context != browser_context &&
!util::CanCrossIncognito(extension, browser_context);
// If the window is not visible by the listener, we also need to
// clear out the window id from the event.
bool visible_to_listener = ControllerVisibleToListener(
window_controller, extension, listener_filter);
event_args_out.emplace();
if (cant_cross_incognito || !visible_to_listener) {
event_args_out->Append(extension_misc::kUnknownWindowId);
} else {
event_args_out->Append(window_id);
}
return true;
}
} // namespace
WindowsEventRouter::WindowsEventRouter(Profile* profile)
: profile_(profile),
focused_profile_(nullptr),
focused_window_id_(extension_misc::kUnknownWindowId) {
DCHECK(!profile->IsOffTheRecord());
observed_app_registry_.Observe(AppWindowRegistry::Get(profile_));
observed_controller_list_.Observe(WindowControllerList::GetInstance());
// Needed for when no suitable window can be passed to an extension as the
// currently focused window. On Mac (even in a toolkit-views build) always
// rely on the notification sent by AppControllerMac after AppKit sends
// NSWindowDidBecomeKeyNotification and there is no [NSApp keyWindo7w]. This
// allows windows not created by toolkit-views to be tracked.
#if BUILDFLAG(IS_MAC)
observed_key_window_notifier_.Observe(
&g_browser_process->platform_part()->key_window_notifier());
#elif defined(TOOLKIT_VIEWS)
views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
#else
#error Unsupported
#endif
AppWindowRegistry* registry = AppWindowRegistry::Get(profile_);
for (AppWindow* app_window : registry->app_windows())
AddAppWindow(app_window);
}
WindowsEventRouter::~WindowsEventRouter() {
#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this);
#endif
}
void WindowsEventRouter::OnAppWindowAdded(AppWindow* app_window) {
if (!profile_->IsSameOrParent(
Profile::FromBrowserContext(app_window->browser_context())))
return;
AddAppWindow(app_window);
}
void WindowsEventRouter::OnAppWindowRemoved(AppWindow* app_window) {
if (!profile_->IsSameOrParent(
Profile::FromBrowserContext(app_window->browser_context())))
return;
app_windows_.erase(app_window->session_id().id());
}
void WindowsEventRouter::OnAppWindowActivated(AppWindow* app_window) {
AppWindowMap::const_iterator iter =
app_windows_.find(app_window->session_id().id());
OnActiveWindowChanged(iter != app_windows_.end() ? iter->second.get()
: nullptr);
}
void WindowsEventRouter::OnWindowControllerAdded(
WindowController* window_controller) {
if (!HasEventListener(windows::OnCreated::kEventName))
return;
if (!profile_->IsSameOrParent(window_controller->profile()))
return;
// Ignore any windows without an associated browser (e.g., AppWindows).
if (!window_controller->GetBrowser())
return;
base::Value::List args;
// Since we don't populate tab info here, the context type doesn't matter.
constexpr WindowController::PopulateTabBehavior populate_behavior =
WindowController::kDontPopulateTabs;
constexpr mojom::ContextType context_type = mojom::ContextType::kUnspecified;
args.Append(window_controller->CreateWindowValueForExtension(
nullptr, populate_behavior, context_type));
DispatchEvent(events::WINDOWS_ON_CREATED, windows::OnCreated::kEventName,
window_controller, std::move(args));
}
void WindowsEventRouter::OnWindowControllerRemoved(
WindowController* window_controller) {
if (!HasEventListener(windows::OnRemoved::kEventName))
return;
if (!profile_->IsSameOrParent(window_controller->profile()))
return;
// Ignore any windows without an associated browser (e.g., AppWindows).
if (!window_controller->GetBrowser())
return;
int window_id = window_controller->GetWindowId();
base::Value::List args;
args.Append(window_id);
DispatchEvent(events::WINDOWS_ON_REMOVED, windows::OnRemoved::kEventName,
window_controller, std::move(args));
}
void WindowsEventRouter::OnWindowBoundsChanged(
WindowController* window_controller) {
if (!HasEventListener(windows::OnBoundsChanged::kEventName))
return;
if (!profile_->IsSameOrParent(window_controller->profile()))
return;
// Ignore any windows without an associated browser (e.g., AppWindows).
if (!window_controller->GetBrowser())
return;
base::Value::List args;
// Since we don't populate tab info here, the context type doesn't matter.
constexpr WindowController::PopulateTabBehavior populate_behavior =
WindowController::kDontPopulateTabs;
constexpr mojom::ContextType context_type = mojom::ContextType::kUnspecified;
args.Append(ExtensionTabUtil::CreateWindowValueForExtension(
*window_controller->GetBrowser(), nullptr, populate_behavior,
context_type));
DispatchEvent(events::WINDOWS_ON_BOUNDS_CHANGED,
windows::OnBoundsChanged::kEventName, window_controller,
std::move(args));
}
#if defined(TOOLKIT_VIEWS) && !BUILDFLAG(IS_MAC)
void WindowsEventRouter::OnNativeFocusChanged(gfx::NativeView focused_now) {
if (!focused_now)
OnActiveWindowChanged(nullptr);
}
#endif
#if BUILDFLAG(IS_MAC)
void WindowsEventRouter::OnNoKeyWindow() {
OnActiveWindowChanged(nullptr);
}
#endif
void WindowsEventRouter::OnActiveWindowChanged(
WindowController* window_controller) {
Profile* window_profile = nullptr;
int window_id = extension_misc::kUnknownWindowId;
if (window_controller &&
profile_->IsSameOrParent(window_controller->profile())) {
window_profile = window_controller->profile();
window_id = window_controller->GetWindowId();
}
if (focused_window_id_ == window_id)
return;
// window_profile is either the default profile for the active window, its
// incognito profile, or nullptr if the previous profile is losing focus.
focused_profile_ = window_profile;
focused_window_id_ = window_id;
if (!HasEventListener(windows::OnFocusChanged::kEventName))
return;
std::unique_ptr<Event> event = std::make_unique<Event>(
events::WINDOWS_ON_FOCUS_CHANGED, windows::OnFocusChanged::kEventName,
base::Value::List());
event->will_dispatch_callback =
base::BindRepeating(&WillDispatchWindowFocusedEvent, window_controller);
EventRouter::Get(profile_)->BroadcastEvent(std::move(event));
}
void WindowsEventRouter::DispatchEvent(events::HistogramValue histogram_value,
const std::string& event_name,
WindowController* window_controller,
base::Value::List args) {
auto event =
std::make_unique<Event>(histogram_value, event_name, std::move(args),
window_controller->profile());
event->will_dispatch_callback =
base::BindRepeating(&WillDispatchWindowEvent, window_controller);
EventRouter::Get(profile_)->BroadcastEvent(std::move(event));
}
bool WindowsEventRouter::HasEventListener(const std::string& event_name) {
return EventRouter::Get(profile_)->HasEventListener(event_name);
}
void WindowsEventRouter::AddAppWindow(AppWindow* app_window) {
std::unique_ptr<AppWindowController> controller(new AppWindowController(
app_window, std::make_unique<AppBaseWindow>(app_window), profile_));
app_windows_[app_window->session_id().id()] = std::move(controller);
}
} // namespace extensions
|