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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
// 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.
#ifndef CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_FUNCTIONS_SHARED_H_
#define CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_FUNCTIONS_SHARED_H_
#include <memory>
#include <optional>
#include "base/memory/scoped_refptr.h"
#include "chrome/browser/extensions/api/developer_private/extension_info_generator.h"
#include "chrome/common/extensions/api/developer_private.h"
#include "chrome/common/extensions/webstore_install_result.h"
#include "extensions/browser/extension_function.h"
#include "extensions/common/extension.h"
#include "ui/base/clipboard/file_info.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace extensions::api {
class DeveloperPrivateAPIFunction : public ExtensionFunction {
protected:
// These constants here are only temporarily during Android desktop
// development and we can move these constants to an anonymous namespace once
// we finish it.
static constexpr char kNoSuchExtensionError[] = "No such extension.";
static constexpr char kRequiresUserGestureError[] =
"This action requires a user gesture.";
static constexpr char kCouldNotShowSelectFileDialogError[] =
"Could not show a file chooser.";
static constexpr char kFileSelectionCanceled[] =
"File selection was canceled.";
static constexpr char kNoSuchRendererError[] = "No such renderer.";
static constexpr char kInvalidPathError[] = "Invalid path.";
static constexpr char kManifestKeyIsRequiredError[] =
"The 'manifestKey' argument is required for manifest files.";
static constexpr char kCouldNotFindWebContentsError[] =
"Could not find a valid web contents.";
static constexpr char kNoOptionsPageForExtensionError[] =
"Extension does not have an options page.";
static constexpr char kCannotRepairHealthyExtension[] =
"Cannot repair a healthy extension.";
static constexpr char kCannotRepairPolicyExtension[] =
"Cannot repair a policy-installed extension.";
static constexpr char kCannotChangeHostPermissions[] =
"Cannot change host permissions for the given extension.";
static constexpr char kCannotSetPinnedWithoutAction[] =
"Cannot set pinned action state for an extension with no action.";
static constexpr char kInvalidHost[] = "Invalid host.";
static constexpr char kInvalidLazyBackgroundPageParameter[] =
"isServiceWorker can not be set for lazy background page based "
"extensions.";
static constexpr char kInvalidRenderProcessId[] =
"render_process_id can be set to -1 for only lazy background page based "
"or "
"service-worker based extensions.";
static constexpr char kFailToUninstallEnterpriseOrComponentExtensions[] =
"Cannot uninstall the enterprise or component extensions in your list.";
static constexpr char kFailToUninstallNoneExistentExtensions[] =
"Cannot uninstall non-existent extensions in your list.";
static constexpr char kUserCancelledError[] = "User cancelled uninstall";
static constexpr char kNoExtensionError[] =
"Extension with ID '*' doesn't exist.";
static constexpr char kExtensionNotAffectedByMV2Deprecation[] =
"Extension with ID '*' is not affected by the MV2 deprecation.";
static constexpr char kCannotRepairNonWebstoreExtension[] =
"Cannot repair an extension that is not installed from the Chrome Web "
"Store.";
static constexpr char kCannotDismissExtensionOnUnsupportedStage[] =
"Cannot dismiss the MV2 deprecation notice for extension with ID '*' on "
"the unsupported stage.";
static constexpr char kUserNotSignedIn[] = "User is not signed in.";
static constexpr char kCannotUploadExtensionToAccount[] =
"Extension with ID '*' cannot be uploaded to the user's account.";
static constexpr char kManifestFile[] = "manifest.json";
~DeveloperPrivateAPIFunction() override;
// Returns the extension with the given `id` from the registry, including
// all possible extensions (enabled, disabled, terminated, etc).
const Extension* GetExtensionById(const ExtensionId& id);
// Returns the extension with the given `id` from the registry, only checking
// enabled extensions.
const Extension* GetEnabledExtensionById(const ExtensionId& id);
};
class DeveloperPrivateAutoUpdateFunction : public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.autoUpdate",
DEVELOPERPRIVATE_AUTOUPDATE)
protected:
~DeveloperPrivateAutoUpdateFunction() override;
ResponseAction Run() override;
private:
void OnComplete();
};
class DeveloperPrivateGetExtensionsInfoFunction
: public DeveloperPrivateAPIFunction {
public:
DeveloperPrivateGetExtensionsInfoFunction();
DeveloperPrivateGetExtensionsInfoFunction(
const DeveloperPrivateGetExtensionsInfoFunction&) = delete;
DeveloperPrivateGetExtensionsInfoFunction& operator=(
const DeveloperPrivateGetExtensionsInfoFunction&) = delete;
DECLARE_EXTENSION_FUNCTION("developerPrivate.getExtensionsInfo",
DEVELOPERPRIVATE_GETEXTENSIONSINFO)
private:
~DeveloperPrivateGetExtensionsInfoFunction() override;
ResponseAction Run() override;
void OnInfosGenerated(
std::vector<api::developer_private::ExtensionInfo> infos);
std::unique_ptr<ExtensionInfoGenerator> info_generator_;
};
class DeveloperPrivateGetExtensionInfoFunction
: public DeveloperPrivateAPIFunction {
public:
DeveloperPrivateGetExtensionInfoFunction();
DeveloperPrivateGetExtensionInfoFunction(
const DeveloperPrivateGetExtensionInfoFunction&) = delete;
DeveloperPrivateGetExtensionInfoFunction& operator=(
const DeveloperPrivateGetExtensionInfoFunction&) = delete;
DECLARE_EXTENSION_FUNCTION("developerPrivate.getExtensionInfo",
DEVELOPERPRIVATE_GETEXTENSIONINFO)
private:
~DeveloperPrivateGetExtensionInfoFunction() override;
ResponseAction Run() override;
void OnInfosGenerated(
std::vector<api::developer_private::ExtensionInfo> infos);
std::unique_ptr<ExtensionInfoGenerator> info_generator_;
};
class DeveloperPrivateGetExtensionSizeFunction
: public DeveloperPrivateAPIFunction {
public:
DeveloperPrivateGetExtensionSizeFunction();
DeveloperPrivateGetExtensionSizeFunction(
const DeveloperPrivateGetExtensionSizeFunction&) = delete;
DeveloperPrivateGetExtensionSizeFunction& operator=(
const DeveloperPrivateGetExtensionSizeFunction&) = delete;
DECLARE_EXTENSION_FUNCTION("developerPrivate.getExtensionSize",
DEVELOPERPRIVATE_GETEXTENSIONSIZE)
private:
~DeveloperPrivateGetExtensionSizeFunction() override;
ResponseAction Run() override;
void OnSizeCalculated(const std::u16string& size);
};
class DeveloperPrivateGetProfileConfigurationFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.getProfileConfiguration",
DEVELOPERPRIVATE_GETPROFILECONFIGURATION)
private:
~DeveloperPrivateGetProfileConfigurationFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateUpdateProfileConfigurationFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.updateProfileConfiguration",
DEVELOPERPRIVATE_UPDATEPROFILECONFIGURATION)
private:
~DeveloperPrivateUpdateProfileConfigurationFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateUpdateExtensionConfigurationFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.updateExtensionConfiguration",
DEVELOPERPRIVATE_UPDATEEXTENSIONCONFIGURATION)
protected:
~DeveloperPrivateUpdateExtensionConfigurationFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateInstallDroppedFileFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.installDroppedFile",
DEVELOPERPRIVATE_INSTALLDROPPEDFILE)
DeveloperPrivateInstallDroppedFileFunction();
DeveloperPrivateInstallDroppedFileFunction(
const DeveloperPrivateInstallDroppedFileFunction&) = delete;
DeveloperPrivateInstallDroppedFileFunction& operator=(
const DeveloperPrivateInstallDroppedFileFunction&) = delete;
private:
~DeveloperPrivateInstallDroppedFileFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
};
class DeveloperPrivateNotifyDragInstallInProgressFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.notifyDragInstallInProgress",
DEVELOPERPRIVATE_NOTIFYDRAGINSTALLINPROGRESS)
DeveloperPrivateNotifyDragInstallInProgressFunction();
DeveloperPrivateNotifyDragInstallInProgressFunction(
const DeveloperPrivateNotifyDragInstallInProgressFunction&) = delete;
DeveloperPrivateNotifyDragInstallInProgressFunction& operator=(
const DeveloperPrivateNotifyDragInstallInProgressFunction&) = delete;
ResponseAction Run() override;
static void SetDropFileForTesting(ui::FileInfo* file_info);
private:
~DeveloperPrivateNotifyDragInstallInProgressFunction() override;
};
class DeveloperPrivateDeleteExtensionErrorsFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.deleteExtensionErrors",
DEVELOPERPRIVATE_DELETEEXTENSIONERRORS)
protected:
~DeveloperPrivateDeleteExtensionErrorsFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateUpdateExtensionCommandFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.updateExtensionCommand",
DEVELOPERPRIVATE_UPDATEEXTENSIONCOMMAND)
protected:
~DeveloperPrivateUpdateExtensionCommandFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateAddHostPermissionFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.addHostPermission",
DEVELOPERPRIVATE_ADDHOSTPERMISSION)
DeveloperPrivateAddHostPermissionFunction();
DeveloperPrivateAddHostPermissionFunction(
const DeveloperPrivateAddHostPermissionFunction&) = delete;
DeveloperPrivateAddHostPermissionFunction& operator=(
const DeveloperPrivateAddHostPermissionFunction&) = delete;
private:
~DeveloperPrivateAddHostPermissionFunction() override;
ResponseAction Run() override;
void OnRuntimePermissionsGranted();
};
class DeveloperPrivateRemoveHostPermissionFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.removeHostPermission",
DEVELOPERPRIVATE_REMOVEHOSTPERMISSION)
DeveloperPrivateRemoveHostPermissionFunction();
DeveloperPrivateRemoveHostPermissionFunction(
const DeveloperPrivateRemoveHostPermissionFunction&) = delete;
DeveloperPrivateRemoveHostPermissionFunction& operator=(
const DeveloperPrivateRemoveHostPermissionFunction&) = delete;
private:
~DeveloperPrivateRemoveHostPermissionFunction() override;
ResponseAction Run() override;
void OnRuntimePermissionsRevoked();
};
class DeveloperPrivateGetUserSiteSettingsFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.getUserSiteSettings",
DEVELOPERPRIVATE_GETUSERSITESETTINGS)
DeveloperPrivateGetUserSiteSettingsFunction();
DeveloperPrivateGetUserSiteSettingsFunction(
const DeveloperPrivateGetUserSiteSettingsFunction&) = delete;
DeveloperPrivateGetUserSiteSettingsFunction& operator=(
const DeveloperPrivateGetUserSiteSettingsFunction&) = delete;
private:
~DeveloperPrivateGetUserSiteSettingsFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateAddUserSpecifiedSitesFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.addUserSpecifiedSites",
DEVELOPERPRIVATE_ADDUSERSPECIFIEDSITES)
DeveloperPrivateAddUserSpecifiedSitesFunction();
DeveloperPrivateAddUserSpecifiedSitesFunction(
const DeveloperPrivateAddUserSpecifiedSitesFunction&) = delete;
DeveloperPrivateAddUserSpecifiedSitesFunction& operator=(
const DeveloperPrivateAddUserSpecifiedSitesFunction&) = delete;
private:
~DeveloperPrivateAddUserSpecifiedSitesFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateRemoveUserSpecifiedSitesFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.removeUserSpecifiedSites",
DEVELOPERPRIVATE_REMOVEUSERSPECIFIEDSITES)
DeveloperPrivateRemoveUserSpecifiedSitesFunction();
DeveloperPrivateRemoveUserSpecifiedSitesFunction(
const DeveloperPrivateRemoveUserSpecifiedSitesFunction&) = delete;
DeveloperPrivateRemoveUserSpecifiedSitesFunction& operator=(
const DeveloperPrivateRemoveUserSpecifiedSitesFunction&) = delete;
private:
~DeveloperPrivateRemoveUserSpecifiedSitesFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateGetUserAndExtensionSitesByEtldFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.getUserAndExtensionSitesByEtld",
DEVELOPERPRIVATE_GETUSERANDEXTENSIONSITESBYETLD)
DeveloperPrivateGetUserAndExtensionSitesByEtldFunction();
DeveloperPrivateGetUserAndExtensionSitesByEtldFunction(
const DeveloperPrivateGetUserAndExtensionSitesByEtldFunction&) = delete;
DeveloperPrivateGetUserAndExtensionSitesByEtldFunction& operator=(
const DeveloperPrivateGetUserAndExtensionSitesByEtldFunction&) = delete;
private:
~DeveloperPrivateGetUserAndExtensionSitesByEtldFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateGetMatchingExtensionsForSiteFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.getMatchingExtensionsForSite",
DEVELOPERPRIVATE_GETMATCHINGEXTENSIONSFORSITE)
DeveloperPrivateGetMatchingExtensionsForSiteFunction();
DeveloperPrivateGetMatchingExtensionsForSiteFunction(
const DeveloperPrivateGetMatchingExtensionsForSiteFunction&) = delete;
DeveloperPrivateGetMatchingExtensionsForSiteFunction& operator=(
const DeveloperPrivateGetMatchingExtensionsForSiteFunction&) = delete;
private:
~DeveloperPrivateGetMatchingExtensionsForSiteFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateUpdateSiteAccessFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.updateSiteAccess",
DEVELOPERPRIVATE_UPDATESITEACCESS)
DeveloperPrivateUpdateSiteAccessFunction();
DeveloperPrivateUpdateSiteAccessFunction(
const DeveloperPrivateUpdateSiteAccessFunction&) = delete;
DeveloperPrivateUpdateSiteAccessFunction& operator=(
const DeveloperPrivateUpdateSiteAccessFunction&) = delete;
private:
~DeveloperPrivateUpdateSiteAccessFunction() override;
ResponseAction Run() override;
void OnSiteSettingsUpdated();
};
class DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"developerPrivate.dismissSafetyHubExtensionsMenuNotification",
DEVELOPERPRIVATE_DISMISSSAFETYHUBEXTENSIONSMENUNOTIFICATION)
DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction();
DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction(
const DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction&) =
delete;
DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction& operator=(
const DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction&) =
delete;
ResponseAction Run() override;
private:
~DeveloperPrivateDismissSafetyHubExtensionsMenuNotificationFunction()
override;
};
class DeveloperPrivateChoosePathFunction
: public DeveloperPrivateAPIFunction,
public ui::SelectFileDialog::Listener {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.choosePath",
DEVELOPERPRIVATE_CHOOSEPATH)
DeveloperPrivateChoosePathFunction();
// ui::SelectFileDialog::Listener:
void FileSelected(const ui::SelectedFileInfo& file, int index) override;
void FileSelectionCanceled() override;
// For testing:
void set_accept_dialog_for_testing(bool accept) {
accept_dialog_for_testing_ = accept;
}
void set_selected_file_for_testing(const ui::SelectedFileInfo& file) {
selected_file_for_testing_ = file;
}
protected:
~DeveloperPrivateChoosePathFunction() override;
ResponseAction Run() override;
private:
// The dialog with the select file picker.
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
// For testing:
// Whether to accept or reject the select file dialog without showing it.
std::optional<bool> accept_dialog_for_testing_;
// File to load when accepting the select file dialog without showing it.
std::optional<ui::SelectedFileInfo> selected_file_for_testing_;
};
class DeveloperPrivateRequestFileSourceFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.requestFileSource",
DEVELOPERPRIVATE_REQUESTFILESOURCE)
DeveloperPrivateRequestFileSourceFunction();
protected:
~DeveloperPrivateRequestFileSourceFunction() override;
ResponseAction Run() override;
private:
void Finish(const std::string& file_contents);
std::optional<api::developer_private::RequestFileSource::Params> params_;
};
class DeveloperPrivateOpenDevToolsFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.openDevTools",
DEVELOPERPRIVATE_OPENDEVTOOLS)
DeveloperPrivateOpenDevToolsFunction();
protected:
~DeveloperPrivateOpenDevToolsFunction() override;
ResponseAction Run() override;
};
class DeveloperPrivateRepairExtensionFunction
: public DeveloperPrivateAPIFunction {
public:
DECLARE_EXTENSION_FUNCTION("developerPrivate.repairExtension",
DEVELOPERPRIVATE_REPAIREXTENSION)
protected:
~DeveloperPrivateRepairExtensionFunction() override;
ResponseAction Run() override;
void OnReinstallComplete(bool success,
const std::string& error,
webstore_install::Result result);
};
} // namespace extensions::api
#endif // CHROME_BROWSER_EXTENSIONS_API_DEVELOPER_PRIVATE_DEVELOPER_PRIVATE_FUNCTIONS_SHARED_H_
|