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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/error_console/error_console.h"
#include <vector>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/lazy_instance.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/extensions/features/feature_channel.h"
#include "chrome/common/pref_names.h"
#include "components/crx_file/id_util.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/feature_switch.h"
namespace extensions {
namespace {
// The key into the Extension prefs for an Extension's specific reporting
// settings.
const char kStoreExtensionErrorsPref[] = "store_extension_errors";
// This is the default mask for which errors to report. That is, if an extension
// does not have specific preference set, this will be used instead.
const int kDefaultMask = 0;
const char kAppsDeveloperToolsExtensionId[] =
"ohmmkhmmmpcnpikjeljgnaoabkaalbgc";
} // namespace
void ErrorConsole::Observer::OnErrorConsoleDestroyed() {
}
ErrorConsole::ErrorConsole(Profile* profile)
: enabled_(false),
default_mask_(kDefaultMask),
profile_(profile),
prefs_(NULL),
registry_observer_(this) {
pref_registrar_.Init(profile_->GetPrefs());
pref_registrar_.Add(prefs::kExtensionsUIDeveloperMode,
base::Bind(&ErrorConsole::OnPrefChanged,
base::Unretained(this)));
registry_observer_.Add(ExtensionRegistry::Get(profile_));
CheckEnabled();
}
ErrorConsole::~ErrorConsole() {
FOR_EACH_OBSERVER(Observer, observers_, OnErrorConsoleDestroyed());
}
// static
ErrorConsole* ErrorConsole::Get(Profile* profile) {
return ExtensionSystem::Get(profile)->error_console();
}
void ErrorConsole::SetReportingForExtension(const std::string& extension_id,
ExtensionError::Type type,
bool enabled) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!enabled_ || !crx_file::id_util::IdIsValid(extension_id))
return;
int mask = default_mask_;
// This call can fail if the preference isn't set, but we don't really care
// if it does, because we just use the default mask instead.
prefs_->ReadPrefAsInteger(extension_id, kStoreExtensionErrorsPref, &mask);
if (enabled)
mask |= 1 << type;
else
mask &= ~(1 << type);
prefs_->UpdateExtensionPref(extension_id,
kStoreExtensionErrorsPref,
new base::FundamentalValue(mask));
}
void ErrorConsole::SetReportingAllForExtension(
const std::string& extension_id, bool enabled) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!enabled_ || !crx_file::id_util::IdIsValid(extension_id))
return;
int mask = 0;
if (enabled)
mask = (1 << ExtensionError::NUM_ERROR_TYPES) - 1;
prefs_->UpdateExtensionPref(extension_id,
kStoreExtensionErrorsPref,
new base::FundamentalValue(mask));
}
bool ErrorConsole::IsReportingEnabledForExtension(
const std::string& extension_id) const {
DCHECK(thread_checker_.CalledOnValidThread());
if (!enabled_ || !crx_file::id_util::IdIsValid(extension_id))
return false;
return GetMaskForExtension(extension_id) != 0;
}
void ErrorConsole::UseDefaultReportingForExtension(
const std::string& extension_id) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!enabled_ || !crx_file::id_util::IdIsValid(extension_id))
return;
prefs_->UpdateExtensionPref(extension_id, kStoreExtensionErrorsPref, NULL);
}
void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!enabled_ || !crx_file::id_util::IdIsValid(error->extension_id()))
return;
int mask = GetMaskForExtension(error->extension_id());
if (!(mask & (1 << error->type())))
return;
const ExtensionError* weak_error = errors_.AddError(error.Pass());
FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(weak_error));
}
const ErrorList& ErrorConsole::GetErrorsForExtension(
const std::string& extension_id) const {
return errors_.GetErrorsForExtension(extension_id);
}
void ErrorConsole::AddObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
observers_.AddObserver(observer);
}
void ErrorConsole::RemoveObserver(Observer* observer) {
DCHECK(thread_checker_.CalledOnValidThread());
observers_.RemoveObserver(observer);
}
bool ErrorConsole::IsEnabledForChromeExtensionsPage() const {
return profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) &&
(FeatureSwitch::error_console()->IsEnabled() ||
GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV);
}
bool ErrorConsole::IsEnabledForAppsDeveloperTools() const {
return ExtensionRegistry::Get(profile_)->enabled_extensions()
.Contains(kAppsDeveloperToolsExtensionId);
}
void ErrorConsole::CheckEnabled() {
bool should_be_enabled = IsEnabledForChromeExtensionsPage() ||
IsEnabledForAppsDeveloperTools();
if (should_be_enabled && !enabled_)
Enable();
if (!should_be_enabled && enabled_)
Disable();
}
void ErrorConsole::Enable() {
enabled_ = true;
// We postpone the initialization of |prefs_| until now because they can be
// NULL in unit_tests. Any unit tests that enable the error console should
// also create an ExtensionPrefs object.
prefs_ = ExtensionPrefs::Get(profile_);
notification_registrar_.Add(
this,
chrome::NOTIFICATION_PROFILE_DESTROYED,
content::NotificationService::AllBrowserContextsAndSources());
const ExtensionSet& extensions =
ExtensionRegistry::Get(profile_)->enabled_extensions();
for (ExtensionSet::const_iterator iter = extensions.begin();
iter != extensions.end();
++iter) {
AddManifestErrorsForExtension(iter->get());
}
}
void ErrorConsole::Disable() {
notification_registrar_.RemoveAll();
errors_.RemoveAllErrors();
enabled_ = false;
}
void ErrorConsole::OnPrefChanged() {
CheckEnabled();
}
void ErrorConsole::OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) {
CheckEnabled();
}
void ErrorConsole::OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) {
CheckEnabled();
}
void ErrorConsole::OnExtensionInstalled(
content::BrowserContext* browser_context,
const Extension* extension,
bool is_update) {
// We don't want to have manifest errors from previous installs. We want
// to keep runtime errors, though, because extensions are reloaded on a
// refresh of chrome:extensions, and we don't want to wipe our history
// whenever that happens.
errors_.RemoveErrorsForExtensionOfType(extension->id(),
ExtensionError::MANIFEST_ERROR);
AddManifestErrorsForExtension(extension);
}
void ErrorConsole::OnExtensionUninstalled(
content::BrowserContext* browser_context,
const Extension* extension,
extensions::UninstallReason reason) {
errors_.Remove(extension->id());
}
void ErrorConsole::AddManifestErrorsForExtension(const Extension* extension) {
const std::vector<InstallWarning>& warnings =
extension->install_warnings();
for (std::vector<InstallWarning>::const_iterator iter = warnings.begin();
iter != warnings.end(); ++iter) {
ReportError(scoped_ptr<ExtensionError>(new ManifestError(
extension->id(),
base::UTF8ToUTF16(iter->message),
base::UTF8ToUTF16(iter->key),
base::UTF8ToUTF16(iter->specific))));
}
}
void ErrorConsole::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
Profile* profile = content::Source<Profile>(source).ptr();
// If incognito profile which we are associated with is destroyed, also
// destroy all incognito errors.
if (profile->IsOffTheRecord() && profile_->IsSameProfile(profile))
errors_.RemoveIncognitoErrors();
}
int ErrorConsole::GetMaskForExtension(const std::string& extension_id) const {
// Registered preferences take priority over everything else.
int pref = 0;
if (prefs_->ReadPrefAsInteger(extension_id, kStoreExtensionErrorsPref, &pref))
return pref;
// If the extension is unpacked, we report all error types by default.
const Extension* extension =
ExtensionRegistry::Get(profile_)->GetExtensionById(
extension_id, ExtensionRegistry::EVERYTHING);
if (extension && extension->location() == Manifest::UNPACKED)
return (1 << ExtensionError::NUM_ERROR_TYPES) - 1;
// Otherwise, use the default mask.
return default_mask_;
}
} // namespace extensions
|