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 <algorithm> | 7 #include <list> |
8 | 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/stl_util.h" |
9 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
10 #include "chrome/browser/extensions/error_console/extension_error.h" | |
11 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/extensions/extension.h" |
13 #include "content/public/browser/notification_details.h" | 15 #include "content/public/browser/notification_details.h" |
14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
16 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
17 | 19 |
18 namespace extensions { | 20 namespace extensions { |
19 | 21 |
| 22 namespace { |
| 23 |
| 24 const size_t kMaxErrorsPerExtension = 100; |
| 25 |
| 26 // Iterate through an error list and remove and delete all errors which were |
| 27 // from an incognito context. |
| 28 void DeleteIncognitoErrorsFromList(ErrorConsole::ErrorList* list) { |
| 29 ErrorConsole::ErrorList::iterator iter = list->begin(); |
| 30 while (iter != list->end()) { |
| 31 if ((*iter)->from_incognito()) { |
| 32 delete *iter; |
| 33 iter = list->erase(iter); |
| 34 } else { |
| 35 ++iter; |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 40 base::LazyInstance<ErrorConsole::ErrorList> g_empty_error_list = |
| 41 LAZY_INSTANCE_INITIALIZER; |
| 42 |
| 43 } // namespace |
| 44 |
20 void ErrorConsole::Observer::OnErrorConsoleDestroyed() { | 45 void ErrorConsole::Observer::OnErrorConsoleDestroyed() { |
21 } | 46 } |
22 | 47 |
23 ErrorConsole::ErrorConsole(Profile* profile) : profile_(profile) { | 48 ErrorConsole::ErrorConsole(Profile* profile) : profile_(profile) { |
24 registrar_.Add(this, | 49 registrar_.Add(this, |
25 chrome::NOTIFICATION_PROFILE_DESTROYED, | 50 chrome::NOTIFICATION_PROFILE_DESTROYED, |
26 content::NotificationService::AllBrowserContextsAndSources()); | 51 content::NotificationService::AllBrowserContextsAndSources()); |
| 52 registrar_.Add(this, |
| 53 chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
| 54 content::Source<Profile>(profile_)); |
27 } | 55 } |
28 | 56 |
29 ErrorConsole::~ErrorConsole() { | 57 ErrorConsole::~ErrorConsole() { |
30 FOR_EACH_OBSERVER(Observer, observers_, OnErrorConsoleDestroyed()); | 58 FOR_EACH_OBSERVER(Observer, observers_, OnErrorConsoleDestroyed()); |
| 59 RemoveAllErrors(); |
31 } | 60 } |
32 | 61 |
33 // static | 62 // static |
34 ErrorConsole* ErrorConsole::Get(Profile* profile) { | 63 ErrorConsole* ErrorConsole::Get(Profile* profile) { |
35 return ExtensionSystem::Get(profile)->error_console(); | 64 return ExtensionSystem::Get(profile)->error_console(); |
36 } | 65 } |
37 | 66 |
38 void ErrorConsole::ReportError(scoped_ptr<ExtensionError> error) { | 67 void ErrorConsole::ReportError(scoped_ptr<const ExtensionError> scoped_error) { |
39 DCHECK(thread_checker_.CalledOnValidThread()); | 68 DCHECK(thread_checker_.CalledOnValidThread()); |
40 errors_.push_back(error.release()); | 69 |
41 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(errors_.back())); | 70 const ExtensionError* error = scoped_error.release(); |
| 71 // If there are too many errors for an extension already, limit ourselves to |
| 72 // the most recent ones. |
| 73 ErrorList* error_list = &errors_[error->extension_id()]; |
| 74 if (error_list->size() >= kMaxErrorsPerExtension) { |
| 75 delete error_list->front(); |
| 76 error_list->pop_front(); |
| 77 } |
| 78 error_list->push_back(error); |
| 79 |
| 80 FOR_EACH_OBSERVER(Observer, observers_, OnErrorAdded(error)); |
42 } | 81 } |
43 | 82 |
44 ErrorConsole::WeakErrorList ErrorConsole::GetErrorsForExtension( | 83 const ErrorConsole::ErrorList& ErrorConsole::GetErrorsForExtension( |
45 const std::string& extension_id) const { | 84 const std::string& extension_id) const { |
46 WeakErrorList result; | 85 ErrorMap::const_iterator iter = errors_.find(extension_id); |
47 for (ErrorList::const_iterator iter = errors_.begin(); | 86 if (iter != errors_.end()) |
48 iter != errors_.end(); ++iter) { | 87 return iter->second; |
49 if ((*iter)->extension_id() == extension_id) | 88 return g_empty_error_list.Get(); |
50 result.push_back(*iter); | |
51 } | |
52 return result; | |
53 } | |
54 | |
55 void ErrorConsole::RemoveError(const ExtensionError* error) { | |
56 ErrorList::iterator iter = std::find(errors_.begin(), errors_.end(), error); | |
57 CHECK(iter != errors_.end()); | |
58 errors_.erase(iter); | |
59 } | |
60 | |
61 void ErrorConsole::RemoveAllErrors() { | |
62 errors_.clear(); | |
63 } | 89 } |
64 | 90 |
65 void ErrorConsole::AddObserver(Observer* observer) { | 91 void ErrorConsole::AddObserver(Observer* observer) { |
66 DCHECK(thread_checker_.CalledOnValidThread()); | 92 DCHECK(thread_checker_.CalledOnValidThread()); |
67 observers_.AddObserver(observer); | 93 observers_.AddObserver(observer); |
68 } | 94 } |
69 | 95 |
70 void ErrorConsole::RemoveObserver(Observer* observer) { | 96 void ErrorConsole::RemoveObserver(Observer* observer) { |
71 DCHECK(thread_checker_.CalledOnValidThread()); | 97 DCHECK(thread_checker_.CalledOnValidThread()); |
72 observers_.RemoveObserver(observer); | 98 observers_.RemoveObserver(observer); |
73 } | 99 } |
74 | 100 |
75 void ErrorConsole::RemoveIncognitoErrors() { | 101 void ErrorConsole::RemoveIncognitoErrors() { |
76 WeakErrorList to_remove; | 102 for (ErrorMap::iterator iter = errors_.begin(); |
77 for (ErrorList::const_iterator iter = errors_.begin(); | |
78 iter != errors_.end(); ++iter) { | 103 iter != errors_.end(); ++iter) { |
79 if ((*iter)->from_incognito()) | 104 DeleteIncognitoErrorsFromList(&(iter->second)); |
80 to_remove.push_back(*iter); | |
81 } | 105 } |
| 106 } |
82 | 107 |
83 for (WeakErrorList::const_iterator iter = to_remove.begin(); | 108 void ErrorConsole::RemoveErrorsForExtension(const std::string& extension_id) { |
84 iter != to_remove.end(); ++iter) { | 109 ErrorMap::iterator iter = errors_.find(extension_id); |
85 RemoveError(*iter); | 110 if (iter != errors_.end()) { |
| 111 STLDeleteContainerPointers(iter->second.begin(), iter->second.end()); |
| 112 errors_.erase(iter); |
86 } | 113 } |
87 } | 114 } |
88 | 115 |
| 116 void ErrorConsole::RemoveAllErrors() { |
| 117 for (ErrorMap::iterator iter = errors_.begin(); iter != errors_.end(); ++iter) |
| 118 STLDeleteContainerPointers(iter->second.begin(), iter->second.end()); |
| 119 errors_.clear(); |
| 120 } |
| 121 |
89 void ErrorConsole::Observe(int type, | 122 void ErrorConsole::Observe(int type, |
90 const content::NotificationSource& source, | 123 const content::NotificationSource& source, |
91 const content::NotificationDetails& details) { | 124 const content::NotificationDetails& details) { |
92 switch (type) { | 125 switch (type) { |
93 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | 126 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
94 Profile* profile = content::Source<Profile>(source).ptr(); | 127 Profile* profile = content::Source<Profile>(source).ptr(); |
95 // If incognito profile which we are associated with is destroyed, also | 128 // If incognito profile which we are associated with is destroyed, also |
96 // destroy all incognito errors. | 129 // destroy all incognito errors. |
97 if (profile->IsOffTheRecord() && profile_->IsSameProfile(profile)) | 130 if (profile->IsOffTheRecord() && profile_->IsSameProfile(profile)) |
98 RemoveIncognitoErrors(); | 131 RemoveIncognitoErrors(); |
99 break; | 132 break; |
100 } | 133 } |
| 134 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: |
| 135 // No need to check the profile here, since we registered to only receive |
| 136 // notifications from our own. |
| 137 RemoveErrorsForExtension( |
| 138 content::Details<Extension>(details).ptr()->id()); |
| 139 break; |
101 default: | 140 default: |
102 NOTREACHED(); | 141 NOTREACHED(); |
103 } | 142 } |
104 } | 143 } |
105 | 144 |
106 } // namespace extensions | 145 } // namespace extensions |
OLD | NEW |