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
|
// 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/extension_error_controller.h"
#include "chrome/browser/extensions/extension_error_controller_factory.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/pending_extension_manager.h"
#include "extensions/common/extension_set.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/extensions/extension_error_ui_android.h"
#else
#include "chrome/browser/extensions/extension_error_ui_desktop.h"
#endif
namespace extensions {
namespace {
ExtensionErrorUI* CreateDefaultExtensionErrorUI(
ExtensionErrorUI::Delegate* delegate) {
#if BUILDFLAG(IS_ANDROID)
return new ExtensionErrorUIAndroid(delegate);
#else
return new ExtensionErrorUIDesktop(delegate);
#endif
}
ExtensionErrorController::UICreateMethod g_create_ui =
CreateDefaultExtensionErrorUI;
} // namespace
ExtensionErrorController::ExtensionErrorController(
content::BrowserContext* context)
: browser_context_(context), is_first_run_(false) {}
ExtensionErrorController::~ExtensionErrorController() = default;
// static
ExtensionErrorController* ExtensionErrorController::Get(
content::BrowserContext* browser_context) {
return ExtensionErrorControllerFactory::GetForBrowserContext(browser_context);
}
void ExtensionErrorController::ShowErrorIfNeeded() {
if (error_ui_.get()) {
return;
}
IdentifyAlertableExtensions();
// Make sure there's something to show, and that there isn't currently a
// bubble displaying.
if (!blocklisted_extensions_.empty()) {
if (!is_first_run_) {
error_ui_.reset(g_create_ui(this));
if (!error_ui_->ShowErrorInBubbleView()) // Couldn't find a browser.
error_ui_.reset();
} else {
// First run. Just acknowledge all the extensions, silently, by
// shortcutting the display of the UI and going straight to the
// callback for pressing the Accept button.
OnAlertClosed();
}
}
}
// static
void ExtensionErrorController::SetUICreateMethodForTesting(
UICreateMethod method) {
g_create_ui = method;
}
content::BrowserContext* ExtensionErrorController::GetContext() {
return browser_context_;
}
const ExtensionSet& ExtensionErrorController::GetBlocklistedExtensions() {
return blocklisted_extensions_;
}
void ExtensionErrorController::OnAlertAccept() {
error_ui_->Close();
}
void ExtensionErrorController::OnAlertDetails() {
error_ui_->ShowExtensions();
// ShowExtensions() may cause the error UI to close synchronously, e.g. if it
// causes a navigation.
if (error_ui_)
error_ui_->Close();
}
void ExtensionErrorController::OnAlertClosed() {
ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
for (ExtensionSet::const_iterator iter = blocklisted_extensions_.begin();
iter != blocklisted_extensions_.end(); ++iter) {
prefs->AcknowledgeBlocklistedExtension((*iter)->id());
}
blocklisted_extensions_.Clear();
error_ui_.reset();
}
void ExtensionErrorController::IdentifyAlertableExtensions() {
ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context_);
// This should be clear, but in case a bubble crashed somewhere along the
// line, let's make sure we start fresh.
blocklisted_extensions_.Clear();
// Build up the lists of extensions that require acknowledgment. If this is
// the first time, grandfather extensions that would have caused
// notification.
const ExtensionSet& blocklisted_set = registry->blocklisted_extensions();
for (ExtensionSet::const_iterator iter = blocklisted_set.begin();
iter != blocklisted_set.end(); ++iter) {
if (!prefs->IsBlocklistedExtensionAcknowledged((*iter)->id()))
blocklisted_extensions_.Insert(*iter);
}
ExtensionSystem* system = ExtensionSystem::Get(browser_context_);
ManagementPolicy* management_policy = system->management_policy();
PendingExtensionManager* pending_extension_manager =
PendingExtensionManager::Get(browser_context_);
// We only show the error UI for the enabled set. This means that an
// extension that is blocked while browser is not running will never
// be displayed in the UI.
const ExtensionSet& enabled_set = registry->enabled_extensions();
for (ExtensionSet::const_iterator iter = enabled_set.begin();
iter != enabled_set.end();
++iter) {
const Extension* extension = iter->get();
// Skip for extensions that have pending updates. They will be checked again
// once the pending update is finished.
if (pending_extension_manager->IsIdPending(extension->id()))
continue;
// Extensions disabled by policy. Note: this no longer includes blocklisted
// extensions. We use similar triggering logic for the dialog, but the
// strings will be different.
if (!management_policy->UserMayLoad(extension) &&
!prefs->IsBlocklistedExtensionAcknowledged(extension->id())) {
blocklisted_extensions_.Insert(extension);
}
}
}
} // namespace extensions
|