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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
// Copyright 2014 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/management/chrome_management_api_delegate.h"
#include <memory>
#include <utility>
#include "base/check_is_test.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/browser_app_launcher.h"
#include "chrome/browser/extensions/chrome_extension_function_details.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/launch_util.h"
#include "chrome/browser/extensions/manifest_v2_experiment_manager.h"
#include "chrome/browser/extensions/mv2_experiment_stage.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/extensions/extensions_dialogs.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/browser/ui/tab_helpers.h"
#include "chrome/browser/ui/web_applications/web_app_dialog_utils.h"
#include "chrome/browser/ui/webui/extensions/extension_icon_source.h"
#include "chrome/browser/web_applications/commands/fetch_installability_for_chrome_management.h"
#include "chrome/browser/web_applications/extension_status_utils.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_install_params.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/extensions/extension_metrics.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "components/favicon/core/favicon_service.h"
#include "components/security_state/content/security_state_tab_helper.h"
#include "components/services/app_service/public/cpp/app_launch_util.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/api/management/management_api.h"
#include "extensions/browser/api/management/management_api_constants.h"
#include "extensions/browser/disable_reason.h"
#include "extensions/browser/extension_dialog_auto_confirm.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/launch_util.h"
#include "extensions/browser/supervised_user_extensions_delegate.h"
#include "extensions/common/api/management.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/extension_urls.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom.h"
#include "third_party/blink/public/mojom/window_features/window_features.mojom.h"
namespace extensions {
namespace {
using InstallOrLaunchWebAppCallback =
ManagementAPIDelegate::InstallOrLaunchWebAppCallback;
using InstallOrLaunchWebAppResult =
ManagementAPIDelegate::InstallOrLaunchWebAppResult;
using InstallableCheckResult = web_app::InstallableCheckResult;
class ManagementSetEnabledFunctionInstallPromptDelegate
: public InstallPromptDelegate {
public:
ManagementSetEnabledFunctionInstallPromptDelegate(
content::WebContents* web_contents,
content::BrowserContext* browser_context,
const Extension* extension,
base::OnceCallback<void(bool)> callback)
: install_prompt_(new ExtensionInstallPrompt(web_contents)),
callback_(std::move(callback)) {
ExtensionInstallPrompt::PromptType type =
ExtensionInstallPrompt::GetReEnablePromptTypeForExtension(
browser_context, extension);
install_prompt_->ShowDialog(
base::BindOnce(&ManagementSetEnabledFunctionInstallPromptDelegate::
OnInstallPromptDone,
weak_factory_.GetWeakPtr()),
extension, nullptr,
std::make_unique<ExtensionInstallPrompt::Prompt>(type),
ExtensionInstallPrompt::GetDefaultShowDialogCallback());
}
ManagementSetEnabledFunctionInstallPromptDelegate(
const ManagementSetEnabledFunctionInstallPromptDelegate&) = delete;
ManagementSetEnabledFunctionInstallPromptDelegate& operator=(
const ManagementSetEnabledFunctionInstallPromptDelegate&) = delete;
~ManagementSetEnabledFunctionInstallPromptDelegate() override = default;
private:
void OnInstallPromptDone(
ExtensionInstallPrompt::DoneCallbackPayload payload) {
// This dialog doesn't support the "withhold permissions" checkbox.
DCHECK_NE(
payload.result,
ExtensionInstallPrompt::Result::ACCEPTED_WITH_WITHHELD_PERMISSIONS);
std::move(callback_).Run(payload.result ==
ExtensionInstallPrompt::Result::ACCEPTED);
}
// Used for prompting to re-enable items with permissions escalation updates.
std::unique_ptr<ExtensionInstallPrompt> install_prompt_;
base::OnceCallback<void(bool)> callback_;
base::WeakPtrFactory<ManagementSetEnabledFunctionInstallPromptDelegate>
weak_factory_{this};
};
class ManagementUninstallFunctionUninstallDialogDelegate
: public ExtensionUninstallDialog::Delegate,
public UninstallDialogDelegate {
public:
ManagementUninstallFunctionUninstallDialogDelegate(
ManagementUninstallFunctionBase* function,
const Extension* target_extension,
bool show_programmatic_uninstall_ui)
: function_(function) {
ChromeExtensionFunctionDetails details(function);
extension_uninstall_dialog_ = ExtensionUninstallDialog::Create(
Profile::FromBrowserContext(function->browser_context()),
details.GetNativeWindowForUI(), this);
bool uninstall_from_webstore =
(function->extension() &&
function->extension()->id() == kWebStoreAppId) ||
function->source_url().DomainIs(
extension_urls::GetNewWebstoreLaunchURL().host());
UninstallSource source;
UninstallReason reason;
if (uninstall_from_webstore) {
source = UNINSTALL_SOURCE_CHROME_WEBSTORE;
reason = UNINSTALL_REASON_CHROME_WEBSTORE;
} else if (function->source_context_type() == mojom::ContextType::kWebUi) {
source = UNINSTALL_SOURCE_CHROME_EXTENSIONS_PAGE;
// TODO: Update this to a new reason; it shouldn't be lumped in with
// other uninstalls if it's from the chrome://extensions page.
reason = UNINSTALL_REASON_MANAGEMENT_API;
} else {
source = UNINSTALL_SOURCE_EXTENSION;
reason = UNINSTALL_REASON_MANAGEMENT_API;
}
if (show_programmatic_uninstall_ui) {
extension_uninstall_dialog_->ConfirmUninstallByExtension(
target_extension, function->extension(), reason, source);
} else {
extension_uninstall_dialog_->ConfirmUninstall(target_extension, reason,
source);
}
}
ManagementUninstallFunctionUninstallDialogDelegate(
const ManagementUninstallFunctionUninstallDialogDelegate&) = delete;
ManagementUninstallFunctionUninstallDialogDelegate& operator=(
const ManagementUninstallFunctionUninstallDialogDelegate&) = delete;
~ManagementUninstallFunctionUninstallDialogDelegate() override = default;
// ExtensionUninstallDialog::Delegate implementation.
void OnExtensionUninstallDialogClosed(bool did_start_uninstall,
const std::u16string& error) override {
function_->OnExtensionUninstallDialogClosed(did_start_uninstall, error);
}
private:
raw_ptr<ManagementUninstallFunctionBase> function_;
std::unique_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
};
void OnGenerateAppForLinkCompleted(
ManagementGenerateAppForLinkFunction* function,
const webapps::AppId& app_id,
webapps::InstallResultCode code) {
const bool install_success =
code == webapps::InstallResultCode::kSuccessNewInstall;
function->FinishCreateWebApp(app_id, install_success);
}
class ChromeAppForLinkDelegate : public AppForLinkDelegate {
public:
ChromeAppForLinkDelegate() = default;
ChromeAppForLinkDelegate(const ChromeAppForLinkDelegate&) = delete;
ChromeAppForLinkDelegate& operator=(const ChromeAppForLinkDelegate&) = delete;
~ChromeAppForLinkDelegate() override = default;
void OnFaviconForApp(ManagementGenerateAppForLinkFunction* function,
content::BrowserContext* context,
const std::string& title,
const GURL& launch_url,
const favicon_base::FaviconImageResult& image_result) {
// GenerateAppForLink API doesn't allow a manifest ID to be specified, so
// just use the launch_url for both manifest ID and start URL. This is a
// reasonable behavior for "DIY apps" generated for a specific URL but
// should be fixed if used for installing existing "Crafted Apps" (ie.
// apps with an existing manifest that should be used for updates).
GURL start_url = launch_url;
webapps::ManifestId manifest_id =
web_app::GenerateManifestIdFromStartUrlOnly(start_url);
auto web_app_info =
std::make_unique<web_app::WebAppInstallInfo>(manifest_id, start_url);
web_app_info->title = base::UTF8ToUTF16(title);
web_app_info->display_mode = web_app::DisplayMode::kBrowser;
web_app_info->user_display_mode = web_app::mojom::UserDisplayMode::kBrowser;
if (!image_result.image.IsEmpty()) {
web_app_info->icon_bitmaps.any[image_result.image.Width()] =
image_result.image.AsBitmap();
}
auto* provider = web_app::WebAppProvider::GetForWebApps(
Profile::FromBrowserContext(context));
provider->scheduler().InstallFromInfoWithParams(
std::move(web_app_info),
/*overwrite_existing_manifest_fields=*/false,
webapps::WebappInstallSource::MANAGEMENT_API,
base::BindOnce(OnGenerateAppForLinkCompleted,
base::RetainedRef(function)),
web_app::WebAppInstallParams());
}
api::management::ExtensionInfo CreateExtensionInfoFromWebApp(
const ExtensionId& app_id,
content::BrowserContext* context) override {
auto* provider = web_app::WebAppProvider::GetForWebApps(
Profile::FromBrowserContext(context));
DCHECK(provider);
const web_app::WebAppRegistrar& registrar = provider->registrar_unsafe();
api::management::ExtensionInfo info;
info.id = app_id;
info.name = registrar.GetAppShortName(app_id);
info.enabled = registrar.GetInstallState(app_id) ==
web_app::proto::INSTALLED_WITH_OS_INTEGRATION;
info.install_type = api::management::ExtensionInstallType::kOther;
info.is_app = true;
info.type = api::management::ExtensionType::kHostedApp;
info.app_launch_url = registrar.GetAppStartUrl(app_id).spec();
info.icons.emplace();
std::vector<apps::IconInfo> manifest_icons =
registrar.GetAppIconInfos(app_id);
info.icons->reserve(manifest_icons.size());
for (const apps::IconInfo& web_app_icon_info : manifest_icons) {
api::management::IconInfo icon_info;
if (web_app_icon_info.square_size_px) {
icon_info.size = *web_app_icon_info.square_size_px;
}
icon_info.url = web_app_icon_info.url.spec();
info.icons->push_back(std::move(icon_info));
}
switch (registrar.GetAppDisplayMode(app_id)) {
case web_app::DisplayMode::kBrowser:
info.launch_type = api::management::LaunchType::kOpenAsRegularTab;
break;
case web_app::DisplayMode::kMinimalUi:
case web_app::DisplayMode::kStandalone:
info.launch_type = api::management::LaunchType::kOpenAsWindow;
break;
case web_app::DisplayMode::kFullscreen:
info.launch_type = api::management::LaunchType::kOpenFullScreen;
break;
// These modes are not supported by the extension app backend.
case web_app::DisplayMode::kWindowControlsOverlay:
case web_app::DisplayMode::kTabbed:
case web_app::DisplayMode::kBorderless:
case web_app::DisplayMode::kPictureInPicture:
case web_app::DisplayMode::kUndefined:
info.launch_type = api::management::LaunchType::kNone;
break;
}
return info;
}
// Used for favicon loading tasks.
base::CancelableTaskTracker cancelable_task_tracker_;
};
void LaunchWebApp(const webapps::AppId& app_id, Profile* profile) {
// Look at prefs to find the right launch container. If the user has not set a
// preference, the default launch value will be returned.
// TODO(crbug.com/40098656): Make AppLaunchParams launch container Optional or
// add a "default" launch container enum value.
auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
DCHECK(provider);
std::optional<web_app::mojom::UserDisplayMode> display_mode =
provider->registrar_unsafe().GetAppUserDisplayMode(app_id);
auto launch_container = apps::LaunchContainer::kLaunchContainerWindow;
if (display_mode == web_app::mojom::UserDisplayMode::kBrowser) {
launch_container = apps::LaunchContainer::kLaunchContainerTab;
}
if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
// If the profile doesn't have an App Service Proxy available, that means
// this extension has been explicitly permitted to run in an incognito
// context. Treat this as if the extension is running in the original
// profile, so it is allowed to access apps in the original profile.
profile = profile->GetOriginalProfile();
}
apps::AppServiceProxyFactory::GetForProfile(profile)->LaunchAppWithParams(
apps::AppLaunchParams(app_id, launch_container,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
apps::LaunchSource::kFromManagementApi));
}
void OnWebAppInstallCompleted(InstallOrLaunchWebAppCallback callback,
const webapps::AppId& app_id,
webapps::InstallResultCode code) {
InstallOrLaunchWebAppResult result =
IsSuccess(code) ? InstallOrLaunchWebAppResult::kSuccess
: InstallOrLaunchWebAppResult::kUnknownError;
std::move(callback).Run(result);
}
void OnWebAppInstallabilityChecked(
base::WeakPtr<Profile> profile,
InstallOrLaunchWebAppCallback callback,
std::unique_ptr<content::WebContents> web_contents,
InstallableCheckResult result,
std::optional<webapps::AppId> app_id) {
if (!profile) {
return;
}
switch (result) {
case InstallableCheckResult::kAlreadyInstalled:
DCHECK(app_id);
LaunchWebApp(*app_id, profile.get());
std::move(callback).Run(InstallOrLaunchWebAppResult::kSuccess);
return;
case InstallableCheckResult::kNotInstallable:
std::move(callback).Run(InstallOrLaunchWebAppResult::kInvalidWebApp);
return;
case InstallableCheckResult::kInstallable:
content::WebContents* containing_contents = web_contents.get();
chrome::ScopedTabbedBrowserDisplayer displayer(profile.get());
const GURL& url = web_contents->GetLastCommittedURL();
chrome::AddWebContents(displayer.browser(), nullptr,
std::move(web_contents), url,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
blink::mojom::WindowFeatures());
web_app::CreateWebAppFromManifest(
containing_contents, webapps::WebappInstallSource::MANAGEMENT_API,
base::BindOnce(&OnWebAppInstallCompleted, std::move(callback)));
return;
}
NOTREACHED();
}
SupervisedUserExtensionsDelegate*
GetSupervisedUserExtensionsDelegateFromContext(
content::BrowserContext* context) {
SupervisedUserExtensionsDelegate* supervised_user_extensions_delegate =
ManagementAPI::GetFactoryInstance()
->Get(context)
->GetSupervisedUserExtensionsDelegate();
CHECK(supervised_user_extensions_delegate);
return supervised_user_extensions_delegate;
}
} // namespace
ChromeManagementAPIDelegate::ChromeManagementAPIDelegate() = default;
ChromeManagementAPIDelegate::~ChromeManagementAPIDelegate() = default;
bool ChromeManagementAPIDelegate::LaunchAppFunctionDelegate(
const Extension* extension,
content::BrowserContext* context) const {
// Look at prefs to find the right launch container.
// If the user has not set a preference, the default launch value will be
// returned.
// TODO(crbug.com/40098656): Make AppLaunchParams launch container Optional or
// add a "default" launch container enum value.
apps::LaunchContainer launch_container =
GetLaunchContainer(ExtensionPrefs::Get(context), extension);
Profile* profile = Profile::FromBrowserContext(context);
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
if (IsExtensionUnsupportedDeprecatedApp(profile, extension->id())) {
return false;
}
#endif
if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
// If the profile doesn't have an App Service Proxy available, that means
// this extension has been explicitly permitted to run in an incognito
// context. Treat this as if the extension is running in the original
// profile, so it is allowed to access apps in the original profile.
profile = profile->GetOriginalProfile();
}
apps::AppServiceProxyFactory::GetForProfile(profile)->LaunchAppWithParams(
apps::AppLaunchParams(extension->id(), launch_container,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
apps::LaunchSource::kFromManagementApi));
RecordAppLaunchType(extension_misc::APP_LAUNCH_EXTENSION_API,
extension->GetType());
return true;
}
GURL ChromeManagementAPIDelegate::GetFullLaunchURL(
const Extension* extension) const {
return AppLaunchInfo::GetFullLaunchURL(extension);
}
LaunchType ChromeManagementAPIDelegate::GetLaunchType(
const ExtensionPrefs* prefs,
const Extension* extension) const {
return ::extensions::GetLaunchType(prefs, extension);
}
std::unique_ptr<InstallPromptDelegate>
ChromeManagementAPIDelegate::SetEnabledFunctionDelegate(
content::WebContents* web_contents,
content::BrowserContext* browser_context,
const Extension* extension,
base::OnceCallback<void(bool)> callback) const {
return std::make_unique<ManagementSetEnabledFunctionInstallPromptDelegate>(
web_contents, browser_context, extension, std::move(callback));
}
std::unique_ptr<UninstallDialogDelegate>
ChromeManagementAPIDelegate::UninstallFunctionDelegate(
ManagementUninstallFunctionBase* function,
const Extension* target_extension,
bool show_programmatic_uninstall_ui) const {
return std::make_unique<ManagementUninstallFunctionUninstallDialogDelegate>(
function, target_extension, show_programmatic_uninstall_ui);
}
bool ChromeManagementAPIDelegate::CreateAppShortcutFunctionDelegate(
ManagementCreateAppShortcutFunction* function,
const Extension* extension,
std::string* error) const {
Browser* browser = chrome::FindBrowserWithProfile(
Profile::FromBrowserContext(function->browser_context()));
if (!browser) {
// Shouldn't happen if we have user gesture.
*error = extension_management_api_constants::kNoBrowserToCreateShortcut;
return false;
}
chrome::ShowCreateChromeAppShortcutsDialog(
browser->window()->GetNativeWindow(), browser->profile(), extension,
base::BindOnce(
&ManagementCreateAppShortcutFunction::OnCloseShortcutPrompt,
function));
return true;
}
std::unique_ptr<AppForLinkDelegate>
ChromeManagementAPIDelegate::GenerateAppForLinkFunctionDelegate(
ManagementGenerateAppForLinkFunction* function,
content::BrowserContext* context,
const std::string& title,
const GURL& launch_url) const {
favicon::FaviconService* favicon_service =
FaviconServiceFactory::GetForProfile(Profile::FromBrowserContext(context),
ServiceAccessType::EXPLICIT_ACCESS);
DCHECK(favicon_service);
auto delegate = std::make_unique<ChromeAppForLinkDelegate>();
favicon_service->GetFaviconImageForPageURL(
launch_url,
base::BindOnce(&ChromeAppForLinkDelegate::OnFaviconForApp,
base::Unretained(delegate.get()),
base::RetainedRef(function), context, title, launch_url),
&delegate->cancelable_task_tracker_);
return delegate;
}
bool ChromeManagementAPIDelegate::CanContextInstallWebApps(
content::BrowserContext* context) const {
return web_app::AreWebAppsUserInstallable(
Profile::FromBrowserContext(context));
}
void ChromeManagementAPIDelegate::InstallOrLaunchReplacementWebApp(
content::BrowserContext* context,
const GURL& web_app_url,
InstallOrLaunchWebAppCallback callback) const {
Profile* profile = Profile::FromBrowserContext(context);
auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
DCHECK(provider);
// Launch the app if web_app_url happens to match start_url. If not, the app
// could still be installed with different start_url.
webapps::AppId app_id = web_app::GenerateAppIdFromManifestId(web_app_url);
if (provider->registrar_unsafe().IsInstallState(
app_id, {web_app::proto::INSTALLED_WITHOUT_OS_INTEGRATION,
web_app::proto::INSTALLED_WITH_OS_INTEGRATION})) {
LaunchWebApp(
web_app::GenerateAppId(/*manifest_id_path=*/std::nullopt, web_app_url),
profile);
std::move(callback).Run(InstallOrLaunchWebAppResult::kSuccess);
return;
}
std::unique_ptr<content::WebContents> web_contents =
content::WebContents::Create(content::WebContents::CreateParams(profile));
web_app::CreateWebAppInstallTabHelpers(web_contents.get());
base::WeakPtr<content::WebContents> web_contents_ptr =
web_contents->GetWeakPtr();
provider->scheduler().FetchInstallabilityForChromeManagement(
web_app_url, web_contents_ptr,
base::BindOnce(&OnWebAppInstallabilityChecked, profile->GetWeakPtr(),
std::move(callback), std::move(web_contents)));
}
void ChromeManagementAPIDelegate::EnableExtension(
content::BrowserContext* context,
const ExtensionId& extension_id) const {
const Extension* extension =
ExtensionRegistry::Get(context)->GetExtensionById(
extension_id, ExtensionRegistry::EVERYTHING);
// The extension must exist as this method is invoked on enabling an extension
// from the extensions management page (see `ManagementSetEnabledFunction`).
CHECK(extension);
SupervisedUserExtensionsDelegate* extensions_delegate =
GetSupervisedUserExtensionsDelegateFromContext(context);
extensions_delegate->MaybeRecordPermissionsIncreaseMetrics(*extension);
extensions_delegate->RecordExtensionEnablementUmaMetrics(/*enabled=*/true);
// If the extension was disabled for a permissions increase, the Management
// API will have displayed a re-enable prompt to the user, so we know it's
// safe to grant permissions here.
ExtensionRegistrar::Get(context)->GrantPermissionsAndEnableExtension(
*extension);
}
void ChromeManagementAPIDelegate::DisableExtension(
content::BrowserContext* context,
const Extension* source_extension,
const ExtensionId& extension_id,
disable_reason::DisableReason disable_reason) const {
SupervisedUserExtensionsDelegate* extensions_delegate =
GetSupervisedUserExtensionsDelegateFromContext(context);
extensions_delegate->RecordExtensionEnablementUmaMetrics(/*enabled=*/false);
ExtensionRegistrar::Get(context)->DisableExtensionWithSource(
source_extension, extension_id, disable_reason);
}
bool ChromeManagementAPIDelegate::UninstallExtension(
content::BrowserContext* context,
const ExtensionId& transient_extension_id,
UninstallReason reason,
std::u16string* error) const {
return extensions::ExtensionRegistrar::Get(context)->UninstallExtension(
transient_extension_id, reason, error);
}
void ChromeManagementAPIDelegate::SetLaunchType(
content::BrowserContext* context,
const ExtensionId& extension_id,
LaunchType launch_type) const {
::extensions::SetLaunchType(context, extension_id, launch_type);
}
GURL ChromeManagementAPIDelegate::GetIconURL(const Extension* extension,
int icon_size,
ExtensionIconSet::Match match,
bool grayscale) const {
return ExtensionIconSource::GetIconURL(extension, icon_size, match,
grayscale);
}
GURL ChromeManagementAPIDelegate::GetEffectiveUpdateURL(
const Extension& extension,
content::BrowserContext* context) const {
ExtensionManagement* extension_management =
ExtensionManagementFactory::GetForBrowserContext(context);
return extension_management->GetEffectiveUpdateURL(extension);
}
void ChromeManagementAPIDelegate::ShowMv2DeprecationReEnableDialog(
content::BrowserContext* context,
content::WebContents* web_contents,
const Extension& extension,
base::OnceCallback<void(bool)> done_callback) const {
// Extension should only be disabled due to MV2 deprecation in the "disable"
// experiment stage.
auto* mv2_experiment_manager = ManifestV2ExperimentManager::Get(context);
CHECK_EQ(mv2_experiment_manager->GetCurrentExperimentStage(),
MV2ExperimentStage::kDisableWithReEnable);
// Tests can auto confirm the re-enable dialog.
auto confirm_value = ScopedTestDialogAutoConfirm::GetAutoConfirmValue();
switch (confirm_value) {
case ScopedTestDialogAutoConfirm::NONE:
// Continue, auto confirm has not been set.
break;
case ScopedTestDialogAutoConfirm::CANCEL:
CHECK_IS_TEST();
std::move(done_callback).Run(/*enable_allowed=*/false);
return;
case ScopedTestDialogAutoConfirm::ACCEPT:
case ScopedTestDialogAutoConfirm::ACCEPT_AND_OPTION:
CHECK_IS_TEST();
std::move(done_callback).Run(/*enable_allowed=*/true);
return;
}
gfx::NativeWindow parent = web_contents
? web_contents->GetTopLevelNativeWindow()
: gfx::NativeWindow();
::extensions::ShowMv2DeprecationReEnableDialog(
parent, extension.id(), extension.name(), std::move(done_callback));
}
} // namespace extensions
|