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

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

Issue 8176001: Warn user in case extension delays network traffic too much. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Merged with ToT Created 9 years, 2 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
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/extension_warning_set.h"
6
7 #include "chrome/browser/extensions/extension_global_error_badge.h"
8 #include "chrome/browser/ui/global_error_service.h"
9 #include "chrome/browser/ui/global_error_service_factory.h"
10 #include "chrome/common/chrome_notification_types.h"
11 #include "content/common/notification_service.h"
12 #include "grit/chromium_strings.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15
16 // This class is used to represent warnings if extensions misbehave.
17 class ExtensionWarning {
18 public:
19 // Default constructor for storing ExtensionServiceWarning in STL containers
20 // do not use.
21 ExtensionWarning();
22
23 // Constructs a warning of type |type| for extension |extension_id|. This
24 // could be for example the fact that an extension conflicted with others.
25 ExtensionWarning(ExtensionWarningSet::WarningType type,
26 const std::string& extension_id);
27
28 ~ExtensionWarning();
29
30 // Returns the specific warning type.
31 ExtensionWarningSet::WarningType warning_type() const { return type_; }
32
33 // Returns the id of the extension for which this warning is valid.
34 const std::string& extension_id() const { return extension_id_; }
35
36 private:
37 ExtensionWarningSet::WarningType type_;
38 std::string extension_id_;
39
40 // Allow implicit copy and assign operator.
41 };
42
43 ExtensionWarning::ExtensionWarning() : type_(ExtensionWarningSet::kInvalid) {
44 }
45
46 ExtensionWarning::ExtensionWarning(
47 ExtensionWarningSet::WarningType type,
48 const std::string& extension_id)
49 : type_(type), extension_id_(extension_id) {
50 }
51
52 ExtensionWarning::~ExtensionWarning() {
53 }
54
55 bool operator<(const ExtensionWarning& a, const ExtensionWarning& b) {
56 if (a.warning_type() == b.warning_type())
57 return a.extension_id() < b.extension_id();
58 return a.warning_type() < b.warning_type();
59 }
60
61 // Static
62 string16 ExtensionWarningSet::GetLocalizedWarning(
63 ExtensionWarningSet::WarningType warning_type) {
64 switch (warning_type) {
65 case ExtensionWarningSet::kInvalid:
66 case ExtensionWarningSet::kMaxWarningType:
67 NOTREACHED();
68 return string16();
69 case ExtensionWarningSet::kNetworkDelay:
70 return l10n_util::GetStringFUTF16(
71 IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
72 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
73 }
74 NOTREACHED(); // Switch statement has no default branch.
75 return string16();
76 }
77
78 ExtensionWarningSet::ExtensionWarningSet(Profile* profile)
79 : extension_global_error_badge_(NULL), profile_(profile) {
80 }
81
82 ExtensionWarningSet::~ExtensionWarningSet() {
83 }
84
85 void ExtensionWarningSet::SetWarning(ExtensionWarningSet::WarningType type,
86 const std::string& extension_id) {
87 ExtensionWarning warning(type, extension_id);
88 bool inserted = warnings_.insert(warning).second;
89 if (inserted) {
90 NotifyWarningsChanged();
91 UpdateWarningBadge();
92 }
93 }
94
95 void ExtensionWarningSet::ClearWarnings(
96 const std::set<ExtensionWarningSet::WarningType>& types) {
97 bool deleted_anything = false;
98 for (const_iterator i = warnings_.begin(); i != warnings_.end();) {
99 if (types.find(i->warning_type()) != types.end()) {
100 deleted_anything = true;
101 warnings_.erase(i++);
102 } else {
103 ++i;
104 }
105 }
106
107 if (deleted_anything) {
108 NotifyWarningsChanged();
109 UpdateWarningBadge();
110 }
111 }
112
113 void ExtensionWarningSet::GetWarningsAffectingExtension(
114 const std::string& extension_id,
115 std::set<ExtensionWarningSet::WarningType>* result) const {
116 result->clear();
117 for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
118 if (i->extension_id() == extension_id)
119 result->insert(i->warning_type());
120 }
121 }
122
123 void ExtensionWarningSet::SuppressBadgeForCurrentWarnings() {
124 badge_suppressions_.insert(warnings_.begin(), warnings_.end());
125 UpdateWarningBadge();
126 }
127
128 void ExtensionWarningSet::NotifyWarningsChanged() {
129 NotificationService::current()->Notify(
130 chrome::NOTIFICATION_EXTENSION_WARNING_CHANGED,
131 Source<Profile>(profile_),
132 NotificationService::NoDetails());
133 }
134
135 void ExtensionWarningSet::ActivateBadge() {
136 DCHECK(!extension_global_error_badge_);
137 DCHECK(profile_);
138 extension_global_error_badge_ = new ExtensionGlobalErrorBadge;
139 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(
140 extension_global_error_badge_);
141 }
142
143 void ExtensionWarningSet::DeactivateBadge() {
144 DCHECK(extension_global_error_badge_);
145 DCHECK(profile_);
146 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(
147 extension_global_error_badge_);
148 delete extension_global_error_badge_;
149 extension_global_error_badge_ = NULL;
150 }
151
152 void ExtensionWarningSet::UpdateWarningBadge() {
153 // We need a badge if a warning exists that has not been suppressed.
154 bool need_warning_badge = false;
155 for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
156 if (badge_suppressions_.find(*i) == badge_suppressions_.end()) {
157 need_warning_badge = true;
158 break;
159 }
160 }
161
162 // Activate or hide the warning badge in case the current state is incorrect.
163 if (extension_global_error_badge_ && !need_warning_badge)
164 DeactivateBadge();
165 else if (!extension_global_error_badge_ && need_warning_badge)
166 ActivateBadge();
167 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extension_warning_set.h ('k') | chrome/browser/extensions/extension_warning_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698