Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: chrome/browser/extensions/error_console/error_console.cc

Issue 238073002: Provide UI for per-extension enabling/disabling of error collection. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed pref map for Ben Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 19 matching lines...) Expand all
30 #include "extensions/common/feature_switch.h" 30 #include "extensions/common/feature_switch.h"
31 31
32 namespace extensions { 32 namespace extensions {
33 33
34 namespace { 34 namespace {
35 35
36 // 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
37 // settings. 37 // settings.
38 const char kStoreExtensionErrorsPref[] = "store_extension_errors"; 38 const char kStoreExtensionErrorsPref[] = "store_extension_errors";
39 39
40 // The default mask (for the time being) is to report everything. 40 // This is the default mask for which errors to report. That is, if an extension
41 const int32 kDefaultMask = (1 << ExtensionError::MANIFEST_ERROR) | 41 // does not have specific preference set, this will be used instead.
42 (1 << ExtensionError::RUNTIME_ERROR); 42 const int32 kDefaultMask = 0;
43 43
44 const char kAppsDeveloperToolsExtensionId[] = 44 const char kAppsDeveloperToolsExtensionId[] =
45 "ohmmkhmmmpcnpikjeljgnaoabkaalbgc"; 45 "ohmmkhmmmpcnpikjeljgnaoabkaalbgc";
46 46
47 } // namespace 47 } // namespace
48 48
49 void ErrorConsole::Observer::OnErrorConsoleDestroyed() { 49 void ErrorConsole::Observer::OnErrorConsoleDestroyed() {
50 } 50 }
51 51
52 ErrorConsole::ErrorConsole(Profile* profile) 52 ErrorConsole::ErrorConsole(Profile* profile)
(...skipping 25 matching lines...) Expand all
78 return ExtensionSystem::Get(profile)->error_console(); 78 return ExtensionSystem::Get(profile)->error_console();
79 } 79 }
80 80
81 void ErrorConsole::SetReportingForExtension(const std::string& extension_id, 81 void ErrorConsole::SetReportingForExtension(const std::string& extension_id,
82 ExtensionError::Type type, 82 ExtensionError::Type type,
83 bool enabled) { 83 bool enabled) {
84 DCHECK(thread_checker_.CalledOnValidThread()); 84 DCHECK(thread_checker_.CalledOnValidThread());
85 if (!enabled_ || !Extension::IdIsValid(extension_id)) 85 if (!enabled_ || !Extension::IdIsValid(extension_id))
86 return; 86 return;
87 87
88 ErrorPreferenceMap::iterator pref = pref_map_.find(extension_id); 88 int mask = default_mask_;
89 // This call can fail if the preference isn't set, but we don't really care
90 // if it does, because we just use the default mask instead.
91 ExtensionPrefs::Get(profile_)->ReadPrefAsInteger(
92 extension_id, kStoreExtensionErrorsPref, &mask);
not at google - send to devlin 2014/04/17 00:15:53 write a GetPref() method? TBH the "extension" in S
93 if (enabled)
94 mask |= 1 << type;
95 else
96 mask &= ~(1 << type);
89 97
90 if (pref == pref_map_.end()) { 98 SetExtensionPref(extension_id, mask);
91 pref = pref_map_.insert( 99 }
92 std::pair<std::string, int32>(extension_id, default_mask_)).first;
93 }
94 100
95 pref->second = 101 void ErrorConsole::SetReportingAllForExtension(
96 enabled ? pref->second | (1 << type) : pref->second &~(1 << type); 102 const std::string& extension_id, bool enabled) {
103 DCHECK(thread_checker_.CalledOnValidThread());
104 if (!enabled_ || !Extension::IdIsValid(extension_id))
105 return;
97 106
98 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( 107 int32 mask = 0;
99 extension_id, 108 if (enabled)
100 kStoreExtensionErrorsPref, 109 mask = (1 << ExtensionError::NUM_ERROR_TYPES) - 1;
101 base::Value::CreateIntegerValue(pref->second)); 110
111 SetExtensionPref(extension_id, mask);
112 }
113
114 bool ErrorConsole::IsReportingEnabledForExtension(
115 const std::string& extension_id) const {
116 int mask = default_mask_;
117 // This call can fail if the preference isn't set, but we don't really care
118 // if it does, because we just use the default mask instead.
119 ExtensionPrefs::Get(profile_)->ReadPrefAsInteger(
120 extension_id, kStoreExtensionErrorsPref, &mask);
121 return mask != 0;
102 } 122 }
103 123
104 void ErrorConsole::UseDefaultReportingForExtension( 124 void ErrorConsole::UseDefaultReportingForExtension(
105 const std::string& extension_id) { 125 const std::string& extension_id) {
106 DCHECK(thread_checker_.CalledOnValidThread()); 126 DCHECK(thread_checker_.CalledOnValidThread());
107 if (!enabled_ || !Extension::IdIsValid(extension_id)) 127 if (!enabled_ || !Extension::IdIsValid(extension_id))
108 return; 128 return;
109 129
110 pref_map_.erase(extension_id);
111 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( 130 ExtensionPrefs::Get(profile_)->UpdateExtensionPref(
112 extension_id, 131 extension_id,
113 kStoreExtensionErrorsPref, 132 kStoreExtensionErrorsPref,
114 NULL); 133 NULL);
115 } 134 }
116 135
117 void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) { 136 void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) {
118 DCHECK(thread_checker_.CalledOnValidThread()); 137 DCHECK(thread_checker_.CalledOnValidThread());
119 if (!enabled_ || !Extension::IdIsValid(error->extension_id())) 138 if (!enabled_ ||
120 return; 139 !Extension::IdIsValid(error->extension_id()) ||
121 140 !ShouldReportErrorForExtension(error->extension_id(), error->type())) {
122 ErrorPreferenceMap::const_iterator pref =
123 pref_map_.find(error->extension_id());
124 // Check the mask to see if we report the error. If we don't have a specific
125 // entry, use the default mask.
126 if ((pref == pref_map_.end() &&
127 ((default_mask_ & (1 << error->type())) == 0)) ||
128 (pref != pref_map_.end() && (pref->second & (1 << error->type())) == 0)) {
129 return; 141 return;
130 } 142 }
131 143
132 const ExtensionError* weak_error = errors_.AddError(error.Pass()); 144 const ExtensionError* weak_error = errors_.AddError(error.Pass());
133 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(weak_error)); 145 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(weak_error));
134 } 146 }
135 147
136 const ErrorList& ErrorConsole::GetErrorsForExtension( 148 const ErrorList& ErrorConsole::GetErrorsForExtension(
137 const std::string& extension_id) const { 149 const std::string& extension_id) const {
138 return errors_.GetErrorsForExtension(extension_id); 150 return errors_.GetErrorsForExtension(extension_id);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 content::NotificationService::AllBrowserContextsAndSources()); 189 content::NotificationService::AllBrowserContextsAndSources());
178 notification_registrar_.Add( 190 notification_registrar_.Add(
179 this, 191 this,
180 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 192 chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
181 content::Source<Profile>(profile_)); 193 content::Source<Profile>(profile_));
182 notification_registrar_.Add( 194 notification_registrar_.Add(
183 this, 195 this,
184 chrome::NOTIFICATION_EXTENSION_INSTALLED, 196 chrome::NOTIFICATION_EXTENSION_INSTALLED,
185 content::Source<Profile>(profile_)); 197 content::Source<Profile>(profile_));
186 198
187 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_);
188 const ExtensionSet& extensions = 199 const ExtensionSet& extensions =
189 ExtensionRegistry::Get(profile_)->enabled_extensions(); 200 ExtensionRegistry::Get(profile_)->enabled_extensions();
190 for (ExtensionSet::const_iterator iter = extensions.begin(); 201 for (ExtensionSet::const_iterator iter = extensions.begin();
191 iter != extensions.end(); 202 iter != extensions.end();
192 ++iter) { 203 ++iter) {
193 int mask = 0;
194 if (prefs->ReadPrefAsInteger(iter->get()->id(),
195 kStoreExtensionErrorsPref,
196 &mask)) {
197 pref_map_[iter->get()->id()] = mask;
198 }
199 AddManifestErrorsForExtension(iter->get()); 204 AddManifestErrorsForExtension(iter->get());
200 } 205 }
201 } 206 }
202 207
203 void ErrorConsole::Disable() { 208 void ErrorConsole::Disable() {
204 notification_registrar_.RemoveAll(); 209 notification_registrar_.RemoveAll();
205 errors_.RemoveAllErrors(); 210 errors_.RemoveAllErrors();
206 enabled_ = false; 211 enabled_ = false;
207 } 212 }
208 213
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 ExtensionError::MANIFEST_ERROR); 267 ExtensionError::MANIFEST_ERROR);
263 268
264 AddManifestErrorsForExtension(info->extension); 269 AddManifestErrorsForExtension(info->extension);
265 break; 270 break;
266 } 271 }
267 default: 272 default:
268 NOTREACHED(); 273 NOTREACHED();
269 } 274 }
270 } 275 }
271 276
277 void ErrorConsole::SetExtensionPref(const std::string& extension_id,
278 int32 mask) {
279 ExtensionPrefs::Get(profile_)->UpdateExtensionPref(
280 extension_id,
281 kStoreExtensionErrorsPref,
282 base::Value::CreateIntegerValue(mask));
283 }
284
285 bool ErrorConsole::ShouldReportErrorForExtension(
286 const std::string& extension_id, ExtensionError::Type type) const {
287 // Registered preferences take priority over everything else.
288 int pref = 0;
289 if (ExtensionPrefs::Get(profile_)->ReadPrefAsInteger(
290 extension_id, kStoreExtensionErrorsPref, &pref)) {
291 return (pref & (1 << type)) != 0;
292 }
293
294 // If the default mask says to report the error, do so.
295 if ((default_mask_ & (1 << type)) != 0)
296 return true;
297
298 // One last check: If the extension is unpacked, we report all errors by
299 // default.
300 const Extension* extension =
301 ExtensionRegistry::Get(profile_)->GetExtensionById(
302 extension_id, ExtensionRegistry::EVERYTHING);
303 if (extension && extension->location() == Manifest::UNPACKED)
304 return true;
305
306 return false;
307 }
308
272 } // namespace extensions 309 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698