| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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_badge_service.h" |
| 6 |
| 7 #include "base/stl_util.h" |
| 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/extensions/extension_system.h" |
| 10 #include "chrome/browser/ui/global_error/global_error.h" |
| 11 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 12 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 13 #include "chrome/browser/ui/browser_commands.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 namespace { |
| 20 // Non-modal GlobalError implementation that warns the user if extensions |
| 21 // created warnings or errors. If the user clicks on the wrench menu, the user |
| 22 // is redirected to chrome://extensions to inspect the errors. |
| 23 class ErrorBadge : public GlobalError { |
| 24 public: |
| 25 explicit ErrorBadge(ExtensionWarningBadgeService* badge_service); |
| 26 virtual ~ErrorBadge(); |
| 27 |
| 28 // Implementation for GlobalError: |
| 29 virtual bool HasBadge() OVERRIDE; |
| 30 |
| 31 virtual bool HasMenuItem() OVERRIDE; |
| 32 virtual int MenuItemCommandID() OVERRIDE; |
| 33 virtual string16 MenuItemLabel() OVERRIDE; |
| 34 virtual void ExecuteMenuItem(Browser* browser) OVERRIDE; |
| 35 |
| 36 virtual bool HasBubbleView() OVERRIDE; |
| 37 virtual string16 GetBubbleViewTitle() OVERRIDE; |
| 38 virtual string16 GetBubbleViewMessage() OVERRIDE; |
| 39 virtual string16 GetBubbleViewAcceptButtonLabel() OVERRIDE; |
| 40 virtual string16 GetBubbleViewCancelButtonLabel() OVERRIDE; |
| 41 virtual void OnBubbleViewDidClose(Browser* browser) OVERRIDE; |
| 42 virtual void BubbleViewAcceptButtonPressed(Browser* browser) OVERRIDE; |
| 43 virtual void BubbleViewCancelButtonPressed(Browser* browser) OVERRIDE; |
| 44 |
| 45 static int GetMenuItemCommandID(); |
| 46 |
| 47 private: |
| 48 ExtensionWarningBadgeService* badge_service_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(ErrorBadge); |
| 51 }; |
| 52 |
| 53 ErrorBadge::ErrorBadge(ExtensionWarningBadgeService* badge_service) |
| 54 : badge_service_(badge_service) {} |
| 55 |
| 56 ErrorBadge::~ErrorBadge() {} |
| 57 |
| 58 bool ErrorBadge::HasBadge() { |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool ErrorBadge::HasMenuItem() { |
| 63 return true; |
| 64 } |
| 65 |
| 66 int ErrorBadge::MenuItemCommandID() { |
| 67 return GetMenuItemCommandID(); |
| 68 } |
| 69 |
| 70 string16 ErrorBadge::MenuItemLabel() { |
| 71 return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM); |
| 72 } |
| 73 |
| 74 void ErrorBadge::ExecuteMenuItem(Browser* browser) { |
| 75 // Suppress all current warnings in the extension service from triggering |
| 76 // a badge on the wrench menu in the future of this session. |
| 77 badge_service_->SuppressCurrentWarnings(); |
| 78 |
| 79 chrome::ExecuteCommand(browser, IDC_MANAGE_EXTENSIONS); |
| 80 } |
| 81 |
| 82 bool ErrorBadge::HasBubbleView() { |
| 83 return false; |
| 84 } |
| 85 |
| 86 string16 ErrorBadge::GetBubbleViewTitle() { |
| 87 return string16(); |
| 88 } |
| 89 |
| 90 string16 ErrorBadge::GetBubbleViewMessage() { |
| 91 return string16(); |
| 92 } |
| 93 |
| 94 string16 ErrorBadge::GetBubbleViewAcceptButtonLabel() { |
| 95 return string16(); |
| 96 } |
| 97 |
| 98 string16 ErrorBadge::GetBubbleViewCancelButtonLabel() { |
| 99 return string16(); |
| 100 } |
| 101 |
| 102 void ErrorBadge::OnBubbleViewDidClose(Browser* browser) { |
| 103 } |
| 104 |
| 105 void ErrorBadge::BubbleViewAcceptButtonPressed(Browser* browser) { |
| 106 NOTREACHED(); |
| 107 } |
| 108 |
| 109 void ErrorBadge::BubbleViewCancelButtonPressed(Browser* browser) { |
| 110 NOTREACHED(); |
| 111 } |
| 112 |
| 113 // static |
| 114 int ErrorBadge::GetMenuItemCommandID() { |
| 115 return IDC_EXTENSION_ERRORS; |
| 116 } |
| 117 |
| 118 } // namespace |
| 119 |
| 120 |
| 121 ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile* profile) |
| 122 : profile_(profile) { |
| 123 DCHECK(CalledOnValidThread()); |
| 124 } |
| 125 |
| 126 ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {} |
| 127 |
| 128 void ExtensionWarningBadgeService::SuppressCurrentWarnings() { |
| 129 DCHECK(CalledOnValidThread()); |
| 130 size_t old_size = suppressed_warnings_.size(); |
| 131 |
| 132 const ExtensionWarningSet& warnings = GetCurrentWarnings(); |
| 133 suppressed_warnings_.insert(warnings.begin(), warnings.end()); |
| 134 |
| 135 if (old_size != suppressed_warnings_.size()) |
| 136 UpdateBadgeStatus(); |
| 137 } |
| 138 |
| 139 const ExtensionWarningSet& |
| 140 ExtensionWarningBadgeService::GetCurrentWarnings() const { |
| 141 return ExtensionSystem::Get(profile_)->warning_service()->warnings(); |
| 142 } |
| 143 |
| 144 void ExtensionWarningBadgeService::ExtensionWarningsChanged() { |
| 145 DCHECK(CalledOnValidThread()); |
| 146 UpdateBadgeStatus(); |
| 147 } |
| 148 |
| 149 void ExtensionWarningBadgeService::UpdateBadgeStatus() { |
| 150 const std::set<ExtensionWarning>& warnings = GetCurrentWarnings(); |
| 151 bool non_suppressed_warnings_exist = false; |
| 152 for (std::set<ExtensionWarning>::const_iterator i = warnings.begin(); |
| 153 i != warnings.end(); ++i) { |
| 154 if (!ContainsKey(suppressed_warnings_, *i)) { |
| 155 non_suppressed_warnings_exist = true; |
| 156 break; |
| 157 } |
| 158 } |
| 159 ShowBadge(non_suppressed_warnings_exist); |
| 160 } |
| 161 |
| 162 void ExtensionWarningBadgeService::ShowBadge(bool show) { |
| 163 GlobalErrorService* service = |
| 164 GlobalErrorServiceFactory::GetForProfile(profile_); |
| 165 GlobalError* error = service->GetGlobalErrorByMenuItemCommandID( |
| 166 ErrorBadge::GetMenuItemCommandID()); |
| 167 |
| 168 // Activate or hide the warning badge in case the current state is incorrect. |
| 169 if (error && !show) { |
| 170 service->RemoveGlobalError(error); |
| 171 delete error; |
| 172 } else if (!error && show) { |
| 173 service->AddGlobalError(new ErrorBadge(this)); |
| 174 } |
| 175 } |
| 176 |
| 177 } // extensions |
| OLD | NEW |