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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
// 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/devtools/protocol/extensions_handler.h"
#include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/devtools/chrome_devtools_manager_delegate.h"
#include "chrome/browser/devtools/protocol/extensions.h"
#include "chrome/browser/devtools/protocol/protocol.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/storage/storage_area_namespace.h"
#include "extensions/browser/api/storage/storage_utils.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_util.h"
namespace {
// Gets an extension with ID `id`. If no extension is found, returns a
// std::nullopt.
std::optional<const extensions::Extension*> GetExtension(
const std::string& id,
scoped_refptr<content::DevToolsAgentHost> host) {
content::BrowserContext* context = host->GetBrowserContext();
if (!context) {
return std::nullopt;
}
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(context);
const extensions::Extension* extension =
registry->enabled_extensions().GetByID(id);
return extension ? std::optional(extension) : std::nullopt;
}
// Returns true if `host` should be able to access data from `storage_area` for
// the given `extension`.
bool CanAccessStorage(scoped_refptr<content::DevToolsAgentHost> host,
const extensions::Extension& extension,
extensions::StorageAreaNamespace storage_area) {
content::BrowserContext* context = host->GetBrowserContext();
if (!context) {
return false;
}
// Allow a service worker to access extension storage if it corresponds to
// the extension whose storage is being accessed.
if (host->GetType() == content::DevToolsAgentHost::kTypeServiceWorker) {
if (!host->GetProcessHost()) {
return false;
}
return extensions::storage_utils::CanRendererAccessExtensionStorage(
*context, extension, /*storage_area=*/std::nullopt,
/*render_frame_host=*/nullptr, *host->GetProcessHost());
}
// Allow a page or frame target to access extension storage if it is
// associated with a renderer that hosts an extension origin or has an
// extension injected into it.
if (host->GetType() == ChromeDevToolsManagerDelegate::kTypeBackgroundPage ||
host->GetType() == content::DevToolsAgentHost::kTypePage ||
host->GetType() == content::DevToolsAgentHost::kTypeFrame) {
if (!host->GetWebContents() ||
!host->GetWebContents()->GetPrimaryMainFrame()) {
return false;
}
bool can_access_storage = false;
// The content/ layer doesn't expose a way for us to get the frame
// associated with DevToolsAgentHost. As a compromise, allow access if any
// frame associated with the WebContents has access. This is safe as there
// are no instances where a client can attach to one frame in a WebContents
// but not another.
host->GetWebContents()
->GetPrimaryMainFrame()
->ForEachRenderFrameHostWithAction(
[&extension,
&can_access_storage](content::RenderFrameHost* render_frame_host) {
if (extensions::storage_utils::CanRendererAccessExtensionStorage(
*render_frame_host->GetBrowserContext(), extension,
/*storage_area=*/std::nullopt, render_frame_host,
*render_frame_host->GetProcess())) {
can_access_storage = true;
return content::RenderFrameHost::FrameIterationAction::kStop;
}
return content::RenderFrameHost::FrameIterationAction::kContinue;
});
return can_access_storage;
}
return false;
}
struct GetExtensionAndStorageFrontendResult {
raw_ptr<const extensions::Extension> extension;
extensions::StorageAreaNamespace storage_namespace;
raw_ptr<extensions::StorageFrontend> frontend;
std::optional<std::string> error;
};
GetExtensionAndStorageFrontendResult GetExtensionAndStorageFrontend(
const std::string& target_id_,
const protocol::String& extension_id,
const protocol::String& storage_area) {
GetExtensionAndStorageFrontendResult result;
result.extension = nullptr;
result.frontend = nullptr;
auto host = content::DevToolsAgentHost::GetForId(target_id_);
CHECK(host);
content::BrowserContext* context = host->GetBrowserContext();
if (!context) {
result.error = "No associated browser context.";
return result;
}
const extensions::StorageAreaNamespace namespace_result =
extensions::StorageAreaFromString(storage_area);
if (namespace_result == extensions::StorageAreaNamespace::kInvalid) {
result.error = "Storage area is invalid.";
return result;
}
std::optional<const extensions::Extension*> optional_extension =
GetExtension(extension_id, host);
if (!optional_extension ||
!CanAccessStorage(host, **optional_extension, namespace_result)) {
result.error = "Extension not found.";
return result;
}
result.extension = *optional_extension;
result.storage_namespace = namespace_result;
result.frontend = extensions::StorageFrontend::Get(context);
return result;
}
} // namespace
ExtensionsHandler::ExtensionsHandler(protocol::UberDispatcher* dispatcher,
const std::string& target_id,
bool allow_loading_extensions)
: target_id_(target_id),
allow_loading_extensions_(allow_loading_extensions) {
protocol::Extensions::Dispatcher::wire(dispatcher, this);
}
ExtensionsHandler::~ExtensionsHandler() = default;
void ExtensionsHandler::LoadUnpacked(
const protocol::String& path,
std::unique_ptr<ExtensionsHandler::LoadUnpackedCallback> callback) {
if (!allow_loading_extensions_) {
std::move(callback)->sendFailure(
protocol::Response::ServerError("Method not available."));
return;
}
content::BrowserContext* context = ProfileManager::GetLastUsedProfile();
DCHECK(context);
scoped_refptr<extensions::UnpackedInstaller> installer(
extensions::UnpackedInstaller::Create(context));
installer->set_be_noisy_on_failure(false);
installer->set_completion_callback(
base::BindOnce(&ExtensionsHandler::OnLoaded, weak_factory_.GetWeakPtr(),
std::move(callback)));
installer->Load(base::FilePath(base::FilePath::FromUTF8Unsafe(path)));
}
void ExtensionsHandler::OnLoaded(std::unique_ptr<LoadUnpackedCallback> callback,
const extensions::Extension* extension,
const base::FilePath& path,
const std::string& err) {
if (err.empty()) {
std::move(callback)->sendSuccess(extension->id());
return;
}
std::move(callback)->sendFailure(protocol::Response::InvalidRequest(err));
}
void ExtensionsHandler::Uninstall(const protocol::String& id,
std::unique_ptr<UninstallCallback> callback) {
if (!allow_loading_extensions_) {
std::move(callback)->sendFailure(
protocol::Response::ServerError("Method not available."));
return;
}
content::BrowserContext* context = ProfileManager::GetLastUsedProfile();
DCHECK(context);
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(context);
const extensions::Extension* extension = registry->GetInstalledExtension(id);
if (!extension) {
std::move(callback)->sendFailure(protocol::Response::ServerError(
"Uninstall failed. Reason: could not find extension."));
return;
}
if (extension->location() != extensions::mojom::ManifestLocation::kUnpacked) {
std::move(callback)->sendFailure(protocol::Response::ServerError(
"Uninstall failed. Reason: extension is not an unpacked extension."));
return;
}
std::u16string error;
bool initiated =
extensions::ExtensionRegistrar::Get(context)->UninstallExtension(
id, extensions::UNINSTALL_REASON_USER_INITIATED, &error,
base::BindOnce(&ExtensionsHandler::OnUninstalled,
weak_factory_.GetWeakPtr(), std::move(callback)));
if (!initiated) {
std::move(callback)->sendFailure(protocol::Response::ServerError(
"Uninstall failed. Reason: " + base::UTF16ToUTF8(error)));
}
}
void ExtensionsHandler::OnUninstalled(
std::unique_ptr<UninstallCallback> callback) {
std::move(callback)->sendSuccess();
}
void ExtensionsHandler::GetStorageItems(
const protocol::String& id,
const protocol::String& storage_area,
std::unique_ptr<protocol::Array<protocol::String>> keys,
std::unique_ptr<ExtensionsHandler::GetStorageItemsCallback> callback) {
GetExtensionAndStorageFrontendResult result =
GetExtensionAndStorageFrontend(target_id_, id, storage_area);
if (result.error) {
std::move(callback)->sendFailure(
protocol::Response::InvalidRequest(*result.error));
return;
}
result.frontend->GetValues(
result.extension.get(), result.storage_namespace,
keys ? std::optional(std::move(*keys)) : std::nullopt,
base::BindOnce(&ExtensionsHandler::OnGetStorageItemsFinished,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void ExtensionsHandler::OnGetStorageItemsFinished(
std::unique_ptr<ExtensionsHandler::GetStorageItemsCallback> callback,
extensions::StorageFrontend::GetResult result) {
if (!result.status.success) {
std::move(callback)->sendFailure(
protocol::Response::ServerError(*result.status.error));
return;
}
base::Value::Dict data = std::move(*result.data);
std::move(callback)->sendSuccess(
std::make_unique<base::Value::Dict>(std::move(data)));
}
void ExtensionsHandler::SetStorageItems(
const protocol::String& id,
const protocol::String& storage_area,
std::unique_ptr<protocol::DictionaryValue> values,
std::unique_ptr<SetStorageItemsCallback> callback) {
GetExtensionAndStorageFrontendResult result =
GetExtensionAndStorageFrontend(target_id_, id, storage_area);
if (result.error) {
std::move(callback)->sendFailure(
protocol::Response::InvalidRequest(*result.error));
return;
}
result.frontend->Set(
result.extension.get(), result.storage_namespace, values->Clone(),
base::BindOnce(&ExtensionsHandler::OnSetStorageItemsFinished,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void ExtensionsHandler::OnSetStorageItemsFinished(
std::unique_ptr<SetStorageItemsCallback> callback,
extensions::StorageFrontend::ResultStatus status) {
if (!status.success) {
std::move(callback)->sendFailure(
protocol::Response::ServerError(*status.error));
return;
}
std::move(callback)->sendSuccess();
}
void ExtensionsHandler::RemoveStorageItems(
const protocol::String& id,
const protocol::String& storage_area,
std::unique_ptr<protocol::Array<protocol::String>> keys,
std::unique_ptr<RemoveStorageItemsCallback> callback) {
GetExtensionAndStorageFrontendResult result =
GetExtensionAndStorageFrontend(target_id_, id, storage_area);
if (result.error) {
std::move(callback)->sendFailure(
protocol::Response::InvalidRequest(*result.error));
return;
}
result.frontend->Remove(
result.extension.get(), result.storage_namespace, *keys,
base::BindOnce(&ExtensionsHandler::OnRemoveStorageItemsFinished,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void ExtensionsHandler::OnRemoveStorageItemsFinished(
std::unique_ptr<RemoveStorageItemsCallback> callback,
extensions::StorageFrontend::ResultStatus status) {
if (!status.success) {
std::move(callback)->sendFailure(
protocol::Response::ServerError(*status.error));
return;
}
std::move(callback)->sendSuccess();
}
void ExtensionsHandler::ClearStorageItems(
const protocol::String& id,
const protocol::String& storage_area,
std::unique_ptr<ClearStorageItemsCallback> callback) {
GetExtensionAndStorageFrontendResult result =
GetExtensionAndStorageFrontend(target_id_, id, storage_area);
if (result.error) {
std::move(callback)->sendFailure(
protocol::Response::InvalidRequest(*result.error));
return;
}
result.frontend->Clear(
result.extension.get(), result.storage_namespace,
base::BindOnce(&ExtensionsHandler::OnClearStorageItemsFinished,
weak_factory_.GetWeakPtr(), std::move(callback)));
}
void ExtensionsHandler::OnClearStorageItemsFinished(
std::unique_ptr<ClearStorageItemsCallback> callback,
extensions::StorageFrontend::ResultStatus status) {
if (!status.success) {
std::move(callback)->sendFailure(
protocol::Response::ServerError(*status.error));
return;
}
std::move(callback)->sendSuccess();
}
|