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
|
// 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.
#ifndef EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_
#define EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_
#include <memory>
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/api/management/management_api_delegate.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_event_histogram_value.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/preload_check.h"
#include "extensions/browser/supervised_user_extensions_delegate.h"
#include "extensions/common/extension_id.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
// Note: While this code compiles on Android, its delegates in //chrome do not,
// and therefore the API doesn't fully work yet on Android.
namespace extensions {
class ExtensionRegistry;
class RequirementsChecker;
class ManagementGetAllFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL)
protected:
~ManagementGetAllFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementGetFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET)
protected:
~ManagementGetFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementGetSelfFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.getSelf", MANAGEMENT_GETSELF)
protected:
~ManagementGetSelfFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementGetPermissionWarningsByIdFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
MANAGEMENT_GETPERMISSIONWARNINGSBYID)
protected:
~ManagementGetPermissionWarningsByIdFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementGetPermissionWarningsByManifestFunction
: public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsByManifest",
MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST)
// Called when manifest parsing is finished.
void OnParse(data_decoder::DataDecoder::ValueOrError result);
protected:
~ManagementGetPermissionWarningsByManifestFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementLaunchAppFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP)
protected:
~ManagementLaunchAppFunction() override {}
// ExtensionFunction:
ResponseAction Run() override;
};
class ManagementSetEnabledFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED)
ManagementSetEnabledFunction();
// UMA metrics.
static constexpr char kSetEnabledHasUserGestureHistogramName[] =
"Extensions.Management.SetEnabled.HasUserGesture";
protected:
~ManagementSetEnabledFunction() override;
// ExtensionFunction:
ResponseAction Run() override;
private:
// Called when supervised extension approval flow is completed.
void OnSupervisedExtensionApprovalDone(
SupervisedUserExtensionsDelegate::ExtensionApprovalResult result);
// Verifies if `extension` has supported requirements. When requirements are
// checked, finishes the enable checks if there are any errors. Otherwise,
// continues with the enable checks.
// This is only needed when enabling an extension.
void CheckRequirements(const Extension& extension);
void OnRequirementsChecked(const PreloadCheck::Errors& errors);
// Verifies if extension has a permissions increase. When permissions are
// checked, finishes the enable checks if there are any errors. Otherwise,
// continues with the enable checks.
// This is only needed when enabling an extension.
void CheckPermissionsIncrease();
void OnPermissionsIncreaseChecked(bool permissions_allowed);
// Verifies if extension was disabled due to the MV2 deprecation. When this is
// checked, finishes the enable checks returning an error if `enable_allowed`
// is false.
// This is only needed when enabling an extension.
void CheckManifestV2Deprecation();
void OnManifestV2DeprecationChecked(bool enable_allowed);
// Returns `response_value`. This should be called when enable checks are
// finished.
void FinishEnable(ResponseValue response_value);
// Returns whether `extension_id` has any unsupported requirements.
bool HasUnsupportedRequirements(const ExtensionId& extension_id) const;
// Returns whether `target_extension` needs supervised approval.
bool IsSupervisedExtensionApprovalFlowRequired(
const Extension* target_extension) const;
// Returns the extension corresponding to `extension_id_`. This could be null
// if extension was uninstalled.
const Extension* GetExtension();
// Extension to be enabled or disabled.
ExtensionId extension_id_;
// Permissions increase delegate, which uses an install prompt to show the
// dialog (crbug.com/352038135: permissions increase should have its own
// separate dialog).
std::unique_ptr<InstallPromptDelegate> install_prompt_;
std::unique_ptr<RequirementsChecker> requirements_checker_;
};
class ManagementUninstallFunctionBase : public ExtensionFunction {
public:
ManagementUninstallFunctionBase();
void OnExtensionUninstallDialogClosed(bool did_start_uninstall,
const std::u16string& error);
protected:
// ExtensionFunction:
~ManagementUninstallFunctionBase() override;
bool ShouldKeepWorkerAliveIndefinitely() override;
ResponseAction Uninstall(const ExtensionId& extension_id,
bool show_confirm_dialog);
private:
// Uninstalls the extension without showing the dialog.
void UninstallExtension();
// Finishes and responds to the extension.
void Finish(bool did_start_uninstall, const std::string& error);
std::string target_extension_id_;
std::unique_ptr<UninstallDialogDelegate> uninstall_dialog_;
};
class ManagementUninstallFunction : public ManagementUninstallFunctionBase {
public:
DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL)
ManagementUninstallFunction();
private:
~ManagementUninstallFunction() override;
ResponseAction Run() override;
};
class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase {
public:
DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
MANAGEMENT_UNINSTALLSELF)
ManagementUninstallSelfFunction();
private:
~ManagementUninstallSelfFunction() override;
ResponseAction Run() override;
};
class ManagementCreateAppShortcutFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.createAppShortcut",
MANAGEMENT_CREATEAPPSHORTCUT)
ManagementCreateAppShortcutFunction();
void OnCloseShortcutPrompt(bool created);
static void SetAutoConfirmForTest(bool should_proceed);
protected:
~ManagementCreateAppShortcutFunction() override;
ResponseAction Run() override;
};
class ManagementSetLaunchTypeFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.setLaunchType",
MANAGEMENT_SETLAUNCHTYPE)
protected:
~ManagementSetLaunchTypeFunction() override {}
ResponseAction Run() override;
};
class ManagementGenerateAppForLinkFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.generateAppForLink",
MANAGEMENT_GENERATEAPPFORLINK)
ManagementGenerateAppForLinkFunction();
void FinishCreateWebApp(const std::string& web_app_id, bool install_success);
protected:
~ManagementGenerateAppForLinkFunction() override;
ResponseAction Run() override;
private:
std::unique_ptr<AppForLinkDelegate> app_for_link_delegate_;
};
class ManagementInstallReplacementWebAppFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("management.installReplacementWebApp",
MANAGEMENT_INSTALLREPLACEMENTWEBAPP)
ManagementInstallReplacementWebAppFunction();
protected:
~ManagementInstallReplacementWebAppFunction() override;
ResponseAction Run() override;
private:
void FinishResponse(
ManagementAPIDelegate::InstallOrLaunchWebAppResult result);
};
class ManagementEventRouter : public ExtensionRegistryObserver {
public:
explicit ManagementEventRouter(content::BrowserContext* context);
ManagementEventRouter(const ManagementEventRouter&) = delete;
ManagementEventRouter& operator=(const ManagementEventRouter&) = delete;
~ManagementEventRouter() override;
private:
// ExtensionRegistryObserver implementation.
void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) override;
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) override;
void OnExtensionInstalled(content::BrowserContext* browser_context,
const Extension* extension,
bool is_update) override;
void OnExtensionUninstalled(content::BrowserContext* browser_context,
const Extension* extension,
extensions::UninstallReason reason) override;
// Dispatches management api events to listening extensions.
void BroadcastEvent(const Extension* extension,
events::HistogramValue histogram_value,
const char* event_name);
raw_ptr<content::BrowserContext> browser_context_;
base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observation_{this};
};
class ManagementAPI : public BrowserContextKeyedAPI,
public EventRouter::Observer {
public:
explicit ManagementAPI(content::BrowserContext* context);
ManagementAPI(const ManagementAPI&) = delete;
ManagementAPI& operator=(const ManagementAPI&) = delete;
~ManagementAPI() override;
// KeyedService implementation.
void Shutdown() override;
// BrowserContextKeyedAPI implementation.
static BrowserContextKeyedAPIFactory<ManagementAPI>* GetFactoryInstance();
// EventRouter::Observer implementation.
void OnListenerAdded(const EventListenerInfo& details) override;
// Returns the ManagementAPI delegate.
const ManagementAPIDelegate* GetDelegate() const { return delegate_.get(); }
// Returns the SupervisedUserService delegate, which might be null depending
// on the extensions embedder.
SupervisedUserExtensionsDelegate* GetSupervisedUserExtensionsDelegate()
const {
return supervised_user_extensions_delegate_.get();
}
void set_delegate_for_test(std::unique_ptr<ManagementAPIDelegate> delegate) {
delegate_ = std::move(delegate);
}
void set_supervised_user_extensions_delegate_for_test(
std::unique_ptr<SupervisedUserExtensionsDelegate> delegate) {
supervised_user_extensions_delegate_ = std::move(delegate);
}
private:
friend class BrowserContextKeyedAPIFactory<ManagementAPI>;
raw_ptr<content::BrowserContext> browser_context_;
// BrowserContextKeyedAPI implementation.
static const char* service_name() { return "ManagementAPI"; }
static const bool kServiceIsNULLWhileTesting = true;
static const bool kServiceRedirectedInIncognito = true;
// Created lazily upon OnListenerAdded.
std::unique_ptr<ManagementEventRouter> management_event_router_;
std::unique_ptr<ManagementAPIDelegate> delegate_;
std::unique_ptr<SupervisedUserExtensionsDelegate>
supervised_user_extensions_delegate_;
};
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_MANAGEMENT_MANAGEMENT_API_H_
|