This source file includes following definitions.
- HasMenuItem
- MenuItemCommandID
- MenuItemLabel
- ExecuteMenuItem
- HasBubbleView
- HasShownBubbleView
- ShowBubbleView
- GetBubbleView
- GetMenuItemCommandID
- SuppressCurrentWarnings
- GetCurrentWarnings
- ExtensionWarningsChanged
- UpdateBadgeStatus
- ShowBadge
#include "chrome/browser/extensions/extension_warning_badge_service.h"
#include "base/stl_util.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/global_error/global_error.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "extensions/browser/extension_system.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace extensions {
namespace {
class ErrorBadge : public GlobalError {
public:
explicit ErrorBadge(ExtensionWarningBadgeService* badge_service);
virtual ~ErrorBadge();
virtual bool HasMenuItem() OVERRIDE;
virtual int MenuItemCommandID() OVERRIDE;
virtual base::string16 MenuItemLabel() OVERRIDE;
virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
virtual bool HasBubbleView() OVERRIDE;
virtual bool HasShownBubbleView() OVERRIDE;
virtual void ShowBubbleView(Browser* browser) OVERRIDE;
virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE;
static int GetMenuItemCommandID();
private:
ExtensionWarningBadgeService* badge_service_;
DISALLOW_COPY_AND_ASSIGN(ErrorBadge);
};
ErrorBadge::ErrorBadge(ExtensionWarningBadgeService* badge_service)
: badge_service_(badge_service) {}
ErrorBadge::~ErrorBadge() {}
bool ErrorBadge::HasMenuItem() {
return true;
}
int ErrorBadge::MenuItemCommandID() {
return GetMenuItemCommandID();
}
base::string16 ErrorBadge::MenuItemLabel() {
return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM);
}
void ErrorBadge::ExecuteMenuItem(Browser* browser) {
badge_service_->SuppressCurrentWarnings();
chrome::ExecuteCommand(browser, IDC_MANAGE_EXTENSIONS);
}
bool ErrorBadge::HasBubbleView() { return false; }
bool ErrorBadge::HasShownBubbleView() { return false; }
void ErrorBadge::ShowBubbleView(Browser* browser) { NOTREACHED(); }
GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() {
return NULL;
}
int ErrorBadge::GetMenuItemCommandID() {
return IDC_EXTENSION_ERRORS;
}
}
ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile* profile)
: profile_(profile) {
DCHECK(CalledOnValidThread());
}
ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {}
void ExtensionWarningBadgeService::SuppressCurrentWarnings() {
DCHECK(CalledOnValidThread());
size_t old_size = suppressed_warnings_.size();
const ExtensionWarningSet& warnings = GetCurrentWarnings();
suppressed_warnings_.insert(warnings.begin(), warnings.end());
if (old_size != suppressed_warnings_.size())
UpdateBadgeStatus();
}
const ExtensionWarningSet&
ExtensionWarningBadgeService::GetCurrentWarnings() const {
return ExtensionSystem::Get(profile_)->warning_service()->warnings();
}
void ExtensionWarningBadgeService::ExtensionWarningsChanged() {
DCHECK(CalledOnValidThread());
UpdateBadgeStatus();
}
void ExtensionWarningBadgeService::UpdateBadgeStatus() {
const std::set<ExtensionWarning>& warnings = GetCurrentWarnings();
bool non_suppressed_warnings_exist = false;
for (std::set<ExtensionWarning>::const_iterator i = warnings.begin();
i != warnings.end(); ++i) {
if (!ContainsKey(suppressed_warnings_, *i)) {
non_suppressed_warnings_exist = true;
break;
}
}
ShowBadge(non_suppressed_warnings_exist);
}
void ExtensionWarningBadgeService::ShowBadge(bool show) {
GlobalErrorService* service =
GlobalErrorServiceFactory::GetForProfile(profile_);
GlobalError* error = service->GetGlobalErrorByMenuItemCommandID(
ErrorBadge::GetMenuItemCommandID());
if (error && !show) {
service->RemoveGlobalError(error);
delete error;
} else if (!error && show) {
service->AddGlobalError(new ErrorBadge(this));
}
}
}