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
|
// Copyright 2024 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/extension_action_dispatcher.h"
#include "base/lazy_instance.h"
#include "components/sessions/content/session_tab_helper.h"
#include "components/sessions/core/session_id.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/extension_action_manager.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/mojom/context_type.mojom.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_tab_util.h"
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
namespace extensions {
static base::LazyInstance<
BrowserContextKeyedAPIFactory<ExtensionActionDispatcher>>::DestructorAtExit
g_extension_action_dispatcher_factory = LAZY_INSTANCE_INITIALIZER;
ExtensionActionDispatcher::ExtensionActionDispatcher(
content::BrowserContext* context)
: browser_context_(context) {}
ExtensionActionDispatcher::~ExtensionActionDispatcher() = default;
// static
BrowserContextKeyedAPIFactory<ExtensionActionDispatcher>*
ExtensionActionDispatcher::GetFactoryInstance() {
return g_extension_action_dispatcher_factory.Pointer();
}
// static
ExtensionActionDispatcher* ExtensionActionDispatcher::Get(
content::BrowserContext* context) {
return BrowserContextKeyedAPIFactory<ExtensionActionDispatcher>::Get(context);
}
void ExtensionActionDispatcher::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void ExtensionActionDispatcher::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void ExtensionActionDispatcher::NotifyChange(ExtensionAction* extension_action,
content::WebContents* web_contents,
content::BrowserContext* context) {
for (auto& observer : observers_) {
observer.OnExtensionActionUpdated(extension_action, web_contents, context);
}
}
void ExtensionActionDispatcher::DispatchExtensionActionClicked(
const ExtensionAction& extension_action,
content::WebContents* web_contents,
const Extension* extension) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
events::HistogramValue histogram_value = events::UNKNOWN;
const char* event_name = nullptr;
switch (extension_action.action_type()) {
case ActionInfo::Type::kAction:
histogram_value = events::ACTION_ON_CLICKED;
event_name = "action.onClicked";
break;
case ActionInfo::Type::kBrowser:
histogram_value = events::BROWSER_ACTION_ON_CLICKED;
event_name = "browserAction.onClicked";
break;
case ActionInfo::Type::kPage:
histogram_value = events::PAGE_ACTION_ON_CLICKED;
event_name = "pageAction.onClicked";
break;
}
if (event_name) {
base::Value::List args;
// The action APIs (browserAction, pageAction, action) are only available
// to privileged extension contexts. As such, we deterministically know that
// the right context type here is privileged.
constexpr mojom::ContextType context_type =
mojom::ContextType::kPrivilegedExtension;
ExtensionTabUtil::ScrubTabBehavior scrub_tab_behavior =
ExtensionTabUtil::GetScrubTabBehavior(extension, context_type,
web_contents);
args.Append(ExtensionTabUtil::CreateTabObject(web_contents,
scrub_tab_behavior, extension)
.ToValue());
DispatchEventToExtension(web_contents->GetBrowserContext(),
extension_action.extension_id(), histogram_value,
event_name, std::move(args));
}
#else
// TODO(crbug.com/393179880): Once we can create JS tab objects via
// ExtensionTabUtil::CreateTabObject() enable this method.
NOTIMPLEMENTED() << "Dispatching actions not yet supported on Android.";
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
}
void ExtensionActionDispatcher::ClearAllValuesForTab(
content::WebContents* web_contents) {
DCHECK(web_contents);
const SessionID tab_id = sessions::SessionTabHelper::IdForTab(web_contents);
content::BrowserContext* browser_context = web_contents->GetBrowserContext();
const ExtensionSet& enabled_extensions =
ExtensionRegistry::Get(browser_context_)->enabled_extensions();
ExtensionActionManager* action_manager =
ExtensionActionManager::Get(browser_context_);
for (const auto& extension : enabled_extensions) {
ExtensionAction* extension_action =
action_manager->GetExtensionAction(*extension);
if (extension_action) {
extension_action->ClearAllValuesForTab(tab_id.id());
NotifyChange(extension_action, web_contents, browser_context);
}
}
}
ExtensionPrefs* ExtensionActionDispatcher::GetExtensionPrefs() {
// This lazy initialization is more than just an optimization, because it
// allows tests to associate a new ExtensionPrefs with the browser context
// before we access it.
if (!extension_prefs_) {
extension_prefs_ = ExtensionPrefs::Get(browser_context_);
}
return extension_prefs_;
}
void ExtensionActionDispatcher::DispatchEventToExtension(
content::BrowserContext* context,
const ExtensionId& extension_id,
events::HistogramValue histogram_value,
const std::string& event_name,
base::Value::List event_args) {
if (!EventRouter::Get(context)) {
return;
}
auto event = std::make_unique<Event>(histogram_value, event_name,
std::move(event_args), context);
event->user_gesture = EventRouter::UserGestureState::kEnabled;
EventRouter::Get(context)->DispatchEventToExtension(extension_id,
std::move(event));
}
void ExtensionActionDispatcher::Shutdown() {
for (auto& observer : observers_) {
observer.OnShuttingDown();
}
}
void ExtensionActionDispatcher::OnActionPinnedStateChanged(
const ExtensionId& extension_id,
bool is_pinned) {
// TODO(crbug.com/360916928): Today, no action APIs are compiled.
// Unfortunately, this means we miss out on the compiled types, which would be
// rather helpful here.
base::Value::List args;
base::Value::Dict change;
change.Set("isOnToolbar", is_pinned);
args.Append(std::move(change));
DispatchEventToExtension(browser_context_, extension_id,
events::ACTION_ON_USER_SETTINGS_CHANGED,
"action.onUserSettingsChanged", std::move(args));
}
} // namespace extensions
|