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" |
(...skipping 19 matching lines...) Expand all Loading... | |
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) |
53 : enabled_(false), | 53 : enabled_(false), |
54 default_mask_(kDefaultMask), | 54 default_mask_(kDefaultMask), |
not at google - send to devlin
2014/04/16 21:28:31
why is default_mask_ a member variable rather than
Devlin
2014/04/16 22:50:41
because we also have the option to set the default
| |
55 profile_(profile), | 55 profile_(profile), |
56 registry_observer_(this) { | 56 registry_observer_(this) { |
57 // TODO(rdevlin.cronin): Remove once crbug.com/159265 is fixed. | 57 // TODO(rdevlin.cronin): Remove once crbug.com/159265 is fixed. |
58 #if !defined(ENABLE_EXTENSIONS) | 58 #if !defined(ENABLE_EXTENSIONS) |
59 return; | 59 return; |
60 #endif | 60 #endif |
61 | 61 |
62 pref_registrar_.Init(profile_->GetPrefs()); | 62 pref_registrar_.Init(profile_->GetPrefs()); |
63 pref_registrar_.Add(prefs::kExtensionsUIDeveloperMode, | 63 pref_registrar_.Add(prefs::kExtensionsUIDeveloperMode, |
64 base::Bind(&ErrorConsole::OnPrefChanged, | 64 base::Bind(&ErrorConsole::OnPrefChanged, |
(...skipping 13 matching lines...) Expand all Loading... | |
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 ErrorPreferenceMap::iterator pref = pref_map_.find(extension_id); |
not at google - send to devlin
2014/04/16 21:28:31
I'm not actually sure that having pref_map_ is mu
Devlin
2014/04/16 22:50:41
Oh, I know that we don't read from disk each time,
not at google - send to devlin
2014/04/16 23:22:07
I feel like we should have a helper function to ge
Devlin
2014/04/16 23:58:22
Per chat discussion, removed pref_map_ in favor of
| |
89 int32 mask = pref == pref_map_.end() ? default_mask_ : pref->second; | |
90 mask = enabled ? mask | (1 << type) : mask &~(1 << type); | |
not at google - send to devlin
2014/04/16 21:28:31
I find this pretty odd to read and parse. I would
Devlin
2014/04/16 22:50:41
No! Too many lines spoil the code! ;)
Done.
| |
89 | 91 |
90 if (pref == pref_map_.end()) { | 92 SetExtensionPref(extension_id, mask); |
91 pref = pref_map_.insert( | 93 } |
92 std::pair<std::string, int32>(extension_id, default_mask_)).first; | 94 |
95 void ErrorConsole::SetReportingForExtensionForAllTypes( | |
96 const std::string& extension_id, bool enabled) { | |
97 DCHECK(thread_checker_.CalledOnValidThread()); | |
98 if (!enabled_ || !Extension::IdIsValid(extension_id)) | |
99 return; | |
100 | |
101 int32 mask = 0; | |
102 if (enabled) { | |
103 mask |= (1 << ExtensionError::MANIFEST_ERROR) | | |
104 (1 << ExtensionError::RUNTIME_ERROR); | |
not at google - send to devlin
2014/04/16 21:28:31
have a value like ExtensionError::NUM_ERROR or som
Devlin
2014/04/16 22:50:41
Hmm... it does break a few other things. But I su
not at google - send to devlin
2014/04/16 23:22:07
you could also be mega-sneaky and do "mask = -1" o
Devlin
2014/04/16 23:58:22
masks are ints, not size_ts, so wouldn't -1 be som
not at google - send to devlin
2014/04/17 00:15:53
oh, read it as uint32, my bad.
| |
93 } | 105 } |
94 | 106 |
95 pref->second = | 107 SetExtensionPref(extension_id, mask); |
96 enabled ? pref->second | (1 << type) : pref->second &~(1 << type); | 108 } |
97 | 109 |
98 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( | 110 bool ErrorConsole::IsReportingEnabledForExtension( |
99 extension_id, | 111 const std::string& extension_id) const { |
100 kStoreExtensionErrorsPref, | 112 return ShouldReportErrorForExtension(extension_id, |
101 base::Value::CreateIntegerValue(pref->second)); | 113 ExtensionError::MANIFEST_ERROR) || |
114 ShouldReportErrorForExtension(extension_id, | |
115 ExtensionError::RUNTIME_ERROR); | |
not at google - send to devlin
2014/04/16 21:28:31
it would be nice if this method could look more li
Devlin
2014/04/16 22:50:41
Good point. Done.
| |
102 } | 116 } |
103 | 117 |
104 void ErrorConsole::UseDefaultReportingForExtension( | 118 void ErrorConsole::UseDefaultReportingForExtension( |
105 const std::string& extension_id) { | 119 const std::string& extension_id) { |
106 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
107 if (!enabled_ || !Extension::IdIsValid(extension_id)) | 121 if (!enabled_ || !Extension::IdIsValid(extension_id)) |
108 return; | 122 return; |
109 | 123 |
110 pref_map_.erase(extension_id); | 124 pref_map_.erase(extension_id); |
111 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( | 125 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( |
112 extension_id, | 126 extension_id, |
113 kStoreExtensionErrorsPref, | 127 kStoreExtensionErrorsPref, |
114 NULL); | 128 NULL); |
115 } | 129 } |
116 | 130 |
117 void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) { | 131 void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) { |
118 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
119 if (!enabled_ || !Extension::IdIsValid(error->extension_id())) | 133 if (!enabled_ || |
120 return; | 134 !Extension::IdIsValid(error->extension_id()) || |
121 | 135 !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; | 136 return; |
130 } | 137 } |
131 | 138 |
132 const ExtensionError* weak_error = errors_.AddError(error.Pass()); | 139 const ExtensionError* weak_error = errors_.AddError(error.Pass()); |
133 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(weak_error)); | 140 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(weak_error)); |
134 } | 141 } |
135 | 142 |
136 const ErrorList& ErrorConsole::GetErrorsForExtension( | 143 const ErrorList& ErrorConsole::GetErrorsForExtension( |
137 const std::string& extension_id) const { | 144 const std::string& extension_id) const { |
138 return errors_.GetErrorsForExtension(extension_id); | 145 return errors_.GetErrorsForExtension(extension_id); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 ExtensionError::MANIFEST_ERROR); | 269 ExtensionError::MANIFEST_ERROR); |
263 | 270 |
264 AddManifestErrorsForExtension(info->extension); | 271 AddManifestErrorsForExtension(info->extension); |
265 break; | 272 break; |
266 } | 273 } |
267 default: | 274 default: |
268 NOTREACHED(); | 275 NOTREACHED(); |
269 } | 276 } |
270 } | 277 } |
271 | 278 |
279 void ErrorConsole::SetExtensionPref(const std::string& extension_id, | |
280 int32 mask) { | |
281 pref_map_[extension_id] = mask; | |
282 ExtensionPrefs::Get(profile_)->UpdateExtensionPref( | |
283 extension_id, | |
284 kStoreExtensionErrorsPref, | |
285 base::Value::CreateIntegerValue(mask)); | |
286 } | |
287 | |
288 bool ErrorConsole::ShouldReportErrorForExtension( | |
289 const std::string& extension_id, ExtensionError::Type type) const { | |
290 // Registered preferences take priority over everything else. | |
291 ErrorPreferenceMap::const_iterator pref = pref_map_.find(extension_id); | |
292 if (pref != pref_map_.end()) | |
293 return (pref->second & (1 << type)) != 0; | |
294 | |
295 // If the default mask says to report the error, do so. | |
296 if ((default_mask_ & (1 << type)) != 0) | |
297 return true; | |
298 | |
299 // One last check: If the extension is unpacked, we report all errors by | |
300 // default. | |
301 const Extension* extension = | |
302 ExtensionRegistry::Get(profile_)->GetExtensionById( | |
303 extension_id, ExtensionRegistry::EVERYTHING); | |
304 if (extension && extension->location() == Manifest::UNPACKED) | |
305 return true; | |
306 | |
307 return false; | |
308 } | |
309 | |
272 } // namespace extensions | 310 } // namespace extensions |
OLD | NEW |