| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/error_console/error_console.h" | 5 #include "chrome/browser/extensions/error_console/error_console.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/common/chrome_version_info.h" |
| 20 #include "chrome/common/extensions/features/feature_channel.h" |
| 19 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/notification_details.h" | 22 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 22 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
| 25 #include "extensions/browser/extension_registry.h" |
| 23 #include "extensions/browser/extension_system.h" | 26 #include "extensions/browser/extension_system.h" |
| 24 #include "extensions/common/constants.h" | 27 #include "extensions/common/constants.h" |
| 25 #include "extensions/common/extension.h" | 28 #include "extensions/common/extension.h" |
| 26 #include "extensions/common/extension_set.h" | 29 #include "extensions/common/extension_set.h" |
| 27 #include "extensions/common/feature_switch.h" | 30 #include "extensions/common/feature_switch.h" |
| 28 | 31 |
| 29 namespace extensions { | 32 namespace extensions { |
| 30 | 33 |
| 31 namespace { | 34 namespace { |
| 35 |
| 32 // The key into the Extension prefs for an Extension's specific reporting | 36 // The key into the Extension prefs for an Extension's specific reporting |
| 33 // settings. | 37 // settings. |
| 34 const char kStoreExtensionErrorsPref[] = "store_extension_errors"; | 38 const char kStoreExtensionErrorsPref[] = "store_extension_errors"; |
| 35 | 39 |
| 36 // The default mask (for the time being) is to report everything. | 40 // The default mask (for the time being) is to report everything. |
| 37 const int32 kDefaultMask = (1 << ExtensionError::MANIFEST_ERROR) | | 41 const int32 kDefaultMask = (1 << ExtensionError::MANIFEST_ERROR) | |
| 38 (1 << ExtensionError::RUNTIME_ERROR); | 42 (1 << ExtensionError::RUNTIME_ERROR); |
| 39 } | 43 |
| 44 const char kAppsDeveloperToolsExtensionId[] = |
| 45 "ohmmkhmmmpcnpikjeljgnaoabkaalbgc"; |
| 46 |
| 47 } // namespace |
| 40 | 48 |
| 41 void ErrorConsole::Observer::OnErrorConsoleDestroyed() { | 49 void ErrorConsole::Observer::OnErrorConsoleDestroyed() { |
| 42 } | 50 } |
| 43 | 51 |
| 44 ErrorConsole::ErrorConsole(Profile* profile, | 52 ErrorConsole::ErrorConsole(Profile* profile) |
| 45 ExtensionService* extension_service) | 53 : enabled_(false), |
| 46 : enabled_(false), default_mask_(kDefaultMask), profile_(profile) { | 54 default_mask_(kDefaultMask), |
| 55 profile_(profile), |
| 56 registry_observer_(this) { |
| 47 // TODO(rdevlin.cronin): Remove once crbug.com/159265 is fixed. | 57 // TODO(rdevlin.cronin): Remove once crbug.com/159265 is fixed. |
| 48 #if !defined(ENABLE_EXTENSIONS) | 58 #if !defined(ENABLE_EXTENSIONS) |
| 49 return; | 59 return; |
| 50 #endif | 60 #endif |
| 51 | 61 |
| 52 // If we don't have the necessary FeatureSwitch enabled, then return | |
| 53 // immediately. Since we never register for any notifications, this ensures | |
| 54 // the ErrorConsole will never be enabled. | |
| 55 if (!FeatureSwitch::error_console()->IsEnabled()) | |
| 56 return; | |
| 57 | |
| 58 pref_registrar_.Init(profile_->GetPrefs()); | 62 pref_registrar_.Init(profile_->GetPrefs()); |
| 59 pref_registrar_.Add(prefs::kExtensionsUIDeveloperMode, | 63 pref_registrar_.Add(prefs::kExtensionsUIDeveloperMode, |
| 60 base::Bind(&ErrorConsole::OnPrefChanged, | 64 base::Bind(&ErrorConsole::OnPrefChanged, |
| 61 base::Unretained(this))); | 65 base::Unretained(this))); |
| 62 | 66 |
| 63 if (profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode)) | 67 registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
| 64 Enable(extension_service); | 68 |
| 69 CheckEnabled(); |
| 65 } | 70 } |
| 66 | 71 |
| 67 ErrorConsole::~ErrorConsole() { | 72 ErrorConsole::~ErrorConsole() { |
| 68 FOR_EACH_OBSERVER(Observer, observers_, OnErrorConsoleDestroyed()); | 73 FOR_EACH_OBSERVER(Observer, observers_, OnErrorConsoleDestroyed()); |
| 69 } | 74 } |
| 70 | 75 |
| 71 // static | 76 // static |
| 72 ErrorConsole* ErrorConsole::Get(Profile* profile) { | 77 ErrorConsole* ErrorConsole::Get(Profile* profile) { |
| 73 return ExtensionSystem::Get(profile)->error_console(); | 78 return ExtensionSystem::Get(profile)->error_console(); |
| 74 } | 79 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 void ErrorConsole::AddObserver(Observer* observer) { | 141 void ErrorConsole::AddObserver(Observer* observer) { |
| 137 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 138 observers_.AddObserver(observer); | 143 observers_.AddObserver(observer); |
| 139 } | 144 } |
| 140 | 145 |
| 141 void ErrorConsole::RemoveObserver(Observer* observer) { | 146 void ErrorConsole::RemoveObserver(Observer* observer) { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 observers_.RemoveObserver(observer); | 148 observers_.RemoveObserver(observer); |
| 144 } | 149 } |
| 145 | 150 |
| 146 void ErrorConsole::OnPrefChanged() { | 151 bool ErrorConsole::IsEnabledForChromeExtensionsPage() const { |
| 147 bool developer_mode = | 152 return profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode) && |
| 148 profile_->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode); | 153 (FeatureSwitch::error_console()->IsEnabled() || |
| 154 GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV); |
| 155 } |
| 149 | 156 |
| 150 if (developer_mode && !enabled_) | 157 bool ErrorConsole::IsEnabledForAppsDeveloperTools() const { |
| 151 Enable(ExtensionSystem::Get(profile_)->extension_service()); | 158 return ExtensionRegistry::Get(profile_)->enabled_extensions() |
| 152 else if (!developer_mode && enabled_) | 159 .Contains(kAppsDeveloperToolsExtensionId); |
| 160 } |
| 161 |
| 162 void ErrorConsole::CheckEnabled() { |
| 163 bool should_be_enabled = IsEnabledForChromeExtensionsPage() || |
| 164 IsEnabledForAppsDeveloperTools(); |
| 165 if (should_be_enabled && !enabled_) |
| 166 Enable(); |
| 167 if (!should_be_enabled && enabled_) |
| 153 Disable(); | 168 Disable(); |
| 154 } | 169 } |
| 155 | 170 |
| 156 void ErrorConsole::Enable(ExtensionService* extension_service) { | 171 void ErrorConsole::Enable() { |
| 157 enabled_ = true; | 172 enabled_ = true; |
| 158 | 173 |
| 159 notification_registrar_.Add( | 174 notification_registrar_.Add( |
| 160 this, | 175 this, |
| 161 chrome::NOTIFICATION_PROFILE_DESTROYED, | 176 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 162 content::NotificationService::AllBrowserContextsAndSources()); | 177 content::NotificationService::AllBrowserContextsAndSources()); |
| 163 notification_registrar_.Add( | 178 notification_registrar_.Add( |
| 164 this, | 179 this, |
| 165 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 180 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 166 content::Source<Profile>(profile_)); | 181 content::Source<Profile>(profile_)); |
| 167 notification_registrar_.Add( | 182 notification_registrar_.Add( |
| 168 this, | 183 this, |
| 169 chrome::NOTIFICATION_EXTENSION_INSTALLED, | 184 chrome::NOTIFICATION_EXTENSION_INSTALLED, |
| 170 content::Source<Profile>(profile_)); | 185 content::Source<Profile>(profile_)); |
| 171 | 186 |
| 172 if (extension_service) { | 187 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); |
| 173 const ExtensionSet* extensions = extension_service->extensions(); | 188 const ExtensionSet& extensions = |
| 174 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | 189 ExtensionRegistry::Get(profile_)->enabled_extensions(); |
| 175 for (ExtensionSet::const_iterator iter = extensions->begin(); | 190 for (ExtensionSet::const_iterator iter = extensions.begin(); |
| 176 iter != extensions->end(); ++iter) { | 191 iter != extensions.end(); |
| 177 int mask = 0; | 192 ++iter) { |
| 178 if (prefs->ReadPrefAsInteger(iter->get()->id(), | 193 int mask = 0; |
| 179 kStoreExtensionErrorsPref, | 194 if (prefs->ReadPrefAsInteger(iter->get()->id(), |
| 180 &mask)) { | 195 kStoreExtensionErrorsPref, |
| 181 pref_map_[iter->get()->id()] = mask; | 196 &mask)) { |
| 182 } | 197 pref_map_[iter->get()->id()] = mask; |
| 183 AddManifestErrorsForExtension(iter->get()); | |
| 184 } | 198 } |
| 199 AddManifestErrorsForExtension(iter->get()); |
| 185 } | 200 } |
| 186 } | 201 } |
| 187 | 202 |
| 188 void ErrorConsole::Disable() { | 203 void ErrorConsole::Disable() { |
| 189 notification_registrar_.RemoveAll(); | 204 notification_registrar_.RemoveAll(); |
| 190 errors_.RemoveAllErrors(); | 205 errors_.RemoveAllErrors(); |
| 191 enabled_ = false; | 206 enabled_ = false; |
| 192 } | 207 } |
| 193 | 208 |
| 209 void ErrorConsole::OnPrefChanged() { |
| 210 CheckEnabled(); |
| 211 } |
| 212 |
| 213 void ErrorConsole::OnExtensionUnloaded(const Extension* extension) { |
| 214 CheckEnabled(); |
| 215 } |
| 216 |
| 217 void ErrorConsole::OnExtensionLoaded(const Extension* extension) { |
| 218 CheckEnabled(); |
| 219 } |
| 220 |
| 194 void ErrorConsole::AddManifestErrorsForExtension(const Extension* extension) { | 221 void ErrorConsole::AddManifestErrorsForExtension(const Extension* extension) { |
| 195 const std::vector<InstallWarning>& warnings = | 222 const std::vector<InstallWarning>& warnings = |
| 196 extension->install_warnings(); | 223 extension->install_warnings(); |
| 197 for (std::vector<InstallWarning>::const_iterator iter = warnings.begin(); | 224 for (std::vector<InstallWarning>::const_iterator iter = warnings.begin(); |
| 198 iter != warnings.end(); ++iter) { | 225 iter != warnings.end(); ++iter) { |
| 199 ReportError(scoped_ptr<ExtensionError>(new ManifestError( | 226 ReportError(scoped_ptr<ExtensionError>(new ManifestError( |
| 200 extension->id(), | 227 extension->id(), |
| 201 base::UTF8ToUTF16(iter->message), | 228 base::UTF8ToUTF16(iter->message), |
| 202 base::UTF8ToUTF16(iter->key), | 229 base::UTF8ToUTF16(iter->key), |
| 203 base::UTF8ToUTF16(iter->specific)))); | 230 base::UTF8ToUTF16(iter->specific)))); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 234 | 261 |
| 235 AddManifestErrorsForExtension(info->extension); | 262 AddManifestErrorsForExtension(info->extension); |
| 236 break; | 263 break; |
| 237 } | 264 } |
| 238 default: | 265 default: |
| 239 NOTREACHED(); | 266 NOTREACHED(); |
| 240 } | 267 } |
| 241 } | 268 } |
| 242 | 269 |
| 243 } // namespace extensions | 270 } // namespace extensions |
| OLD | NEW |