summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_warning_set.cc
diff options
context:
space:
mode:
authorbattre@chromium.org <battre@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-11 18:53:25 +0000
committerbattre@chromium.org <battre@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-10-11 18:53:25 +0000
commit7c1490dad18464a5ae6f748cbf645ff7fca4a108 (patch)
tree35bd01dc78a4d167c8e524fb15e55eae558ba82b /chrome/browser/extensions/extension_warning_set.cc
parent4647cbe2346b9db9444634e05198d1f5f84f50b9 (diff)
downloadchromium_src-7c1490dad18464a5ae6f748cbf645ff7fca4a108.zip
chromium_src-7c1490dad18464a5ae6f748cbf645ff7fca4a108.tar.gz
chromium_src-7c1490dad18464a5ae6f748cbf645ff7fca4a108.tar.bz2
Warn user in case extension delays network traffic too much.
This CL adds a badge to the wrench menu and warning messages to chrome://extensions in case an extension delays network traffic too much and thereby causes a bad user experience. BUG=82618 TEST=Install an extension using the webRequest API in a debug build. In debug build the extension should delay the network traffic enough to cause warning messages. You can use https://adblockplus.org/development-builds/experimental-adblock-plus-for-chrome-builds-available-with-better-blocking for example. Review URL: http://codereview.chromium.org/8176001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104929 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/extension_warning_set.cc')
-rw-r--r--chrome/browser/extensions/extension_warning_set.cc167
1 files changed, 167 insertions, 0 deletions
diff --git a/chrome/browser/extensions/extension_warning_set.cc b/chrome/browser/extensions/extension_warning_set.cc
new file mode 100644
index 0000000..c99f18a2
--- /dev/null
+++ b/chrome/browser/extensions/extension_warning_set.cc
@@ -0,0 +1,167 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/extensions/extension_warning_set.h"
+
+#include "chrome/browser/extensions/extension_global_error_badge.h"
+#include "chrome/browser/ui/global_error_service.h"
+#include "chrome/browser/ui/global_error_service_factory.h"
+#include "chrome/common/chrome_notification_types.h"
+#include "content/common/notification_service.h"
+#include "grit/chromium_strings.h"
+#include "grit/generated_resources.h"
+#include "ui/base/l10n/l10n_util.h"
+
+// This class is used to represent warnings if extensions misbehave.
+class ExtensionWarning {
+ public:
+ // Default constructor for storing ExtensionServiceWarning in STL containers
+ // do not use.
+ ExtensionWarning();
+
+ // Constructs a warning of type |type| for extension |extension_id|. This
+ // could be for example the fact that an extension conflicted with others.
+ ExtensionWarning(ExtensionWarningSet::WarningType type,
+ const std::string& extension_id);
+
+ ~ExtensionWarning();
+
+ // Returns the specific warning type.
+ ExtensionWarningSet::WarningType warning_type() const { return type_; }
+
+ // Returns the id of the extension for which this warning is valid.
+ const std::string& extension_id() const { return extension_id_; }
+
+ private:
+ ExtensionWarningSet::WarningType type_;
+ std::string extension_id_;
+
+ // Allow implicit copy and assign operator.
+};
+
+ExtensionWarning::ExtensionWarning() : type_(ExtensionWarningSet::kInvalid) {
+}
+
+ExtensionWarning::ExtensionWarning(
+ ExtensionWarningSet::WarningType type,
+ const std::string& extension_id)
+ : type_(type), extension_id_(extension_id) {
+}
+
+ExtensionWarning::~ExtensionWarning() {
+}
+
+bool operator<(const ExtensionWarning& a, const ExtensionWarning& b) {
+ if (a.warning_type() == b.warning_type())
+ return a.extension_id() < b.extension_id();
+ return a.warning_type() < b.warning_type();
+}
+
+// Static
+string16 ExtensionWarningSet::GetLocalizedWarning(
+ ExtensionWarningSet::WarningType warning_type) {
+ switch (warning_type) {
+ case ExtensionWarningSet::kInvalid:
+ case ExtensionWarningSet::kMaxWarningType:
+ NOTREACHED();
+ return string16();
+ case ExtensionWarningSet::kNetworkDelay:
+ return l10n_util::GetStringFUTF16(
+ IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
+ l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
+ }
+ NOTREACHED(); // Switch statement has no default branch.
+ return string16();
+}
+
+ExtensionWarningSet::ExtensionWarningSet(Profile* profile)
+ : extension_global_error_badge_(NULL), profile_(profile) {
+}
+
+ExtensionWarningSet::~ExtensionWarningSet() {
+}
+
+void ExtensionWarningSet::SetWarning(ExtensionWarningSet::WarningType type,
+ const std::string& extension_id) {
+ ExtensionWarning warning(type, extension_id);
+ bool inserted = warnings_.insert(warning).second;
+ if (inserted) {
+ NotifyWarningsChanged();
+ UpdateWarningBadge();
+ }
+}
+
+void ExtensionWarningSet::ClearWarnings(
+ const std::set<ExtensionWarningSet::WarningType>& types) {
+ bool deleted_anything = false;
+ for (const_iterator i = warnings_.begin(); i != warnings_.end();) {
+ if (types.find(i->warning_type()) != types.end()) {
+ deleted_anything = true;
+ warnings_.erase(i++);
+ } else {
+ ++i;
+ }
+ }
+
+ if (deleted_anything) {
+ NotifyWarningsChanged();
+ UpdateWarningBadge();
+ }
+}
+
+void ExtensionWarningSet::GetWarningsAffectingExtension(
+ const std::string& extension_id,
+ std::set<ExtensionWarningSet::WarningType>* result) const {
+ result->clear();
+ for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
+ if (i->extension_id() == extension_id)
+ result->insert(i->warning_type());
+ }
+}
+
+void ExtensionWarningSet::SuppressBadgeForCurrentWarnings() {
+ badge_suppressions_.insert(warnings_.begin(), warnings_.end());
+ UpdateWarningBadge();
+}
+
+void ExtensionWarningSet::NotifyWarningsChanged() {
+ NotificationService::current()->Notify(
+ chrome::NOTIFICATION_EXTENSION_WARNING_CHANGED,
+ Source<Profile>(profile_),
+ NotificationService::NoDetails());
+}
+
+void ExtensionWarningSet::ActivateBadge() {
+ DCHECK(!extension_global_error_badge_);
+ DCHECK(profile_);
+ extension_global_error_badge_ = new ExtensionGlobalErrorBadge;
+ GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(
+ extension_global_error_badge_);
+}
+
+void ExtensionWarningSet::DeactivateBadge() {
+ DCHECK(extension_global_error_badge_);
+ DCHECK(profile_);
+ GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(
+ extension_global_error_badge_);
+ delete extension_global_error_badge_;
+ extension_global_error_badge_ = NULL;
+}
+
+void ExtensionWarningSet::UpdateWarningBadge() {
+ // We need a badge if a warning exists that has not been suppressed.
+ bool need_warning_badge = false;
+ for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
+ if (badge_suppressions_.find(*i) == badge_suppressions_.end()) {
+ need_warning_badge = true;
+ break;
+ }
+ }
+
+ // Activate or hide the warning badge in case the current state is incorrect.
+ if (extension_global_error_badge_ && !need_warning_badge)
+ DeactivateBadge();
+ else if (!extension_global_error_badge_ && need_warning_badge)
+ ActivateBadge();
+}