| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extension_warning_badge_service.h" | 5 #include "chrome/browser/extensions/warning_badge_service.h" |
| 6 | 6 |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "chrome/app/chrome_command_ids.h" | 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/extensions/extension_warning_badge_service_factory.h" | 9 #include "chrome/browser/extensions/warning_badge_service_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/browser_commands.h" | 11 #include "chrome/browser/ui/browser_commands.h" |
| 12 #include "chrome/browser/ui/global_error/global_error.h" | 12 #include "chrome/browser/ui/global_error/global_error.h" |
| 13 #include "chrome/browser/ui/global_error/global_error_service.h" | 13 #include "chrome/browser/ui/global_error/global_error_service.h" |
| 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" | 14 #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 15 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace extensions { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 // Non-modal GlobalError implementation that warns the user if extensions | 21 // Non-modal GlobalError implementation that warns the user if extensions |
| 22 // created warnings or errors. If the user clicks on the wrench menu, the user | 22 // created warnings or errors. If the user clicks on the wrench menu, the user |
| 23 // is redirected to chrome://extensions to inspect the errors. | 23 // is redirected to chrome://extensions to inspect the errors. |
| 24 class ErrorBadge : public GlobalError { | 24 class ErrorBadge : public GlobalError { |
| 25 public: | 25 public: |
| 26 explicit ErrorBadge(ExtensionWarningBadgeService* badge_service); | 26 explicit ErrorBadge(WarningBadgeService* badge_service); |
| 27 ~ErrorBadge() override; | 27 ~ErrorBadge() override; |
| 28 | 28 |
| 29 // Implementation for GlobalError: | 29 // Implementation for GlobalError: |
| 30 bool HasMenuItem() override; | 30 bool HasMenuItem() override; |
| 31 int MenuItemCommandID() override; | 31 int MenuItemCommandID() override; |
| 32 base::string16 MenuItemLabel() override; | 32 base::string16 MenuItemLabel() override; |
| 33 void ExecuteMenuItem(Browser* browser) override; | 33 void ExecuteMenuItem(Browser* browser) override; |
| 34 | 34 |
| 35 bool HasBubbleView() override; | 35 bool HasBubbleView() override; |
| 36 bool HasShownBubbleView() override; | 36 bool HasShownBubbleView() override; |
| 37 void ShowBubbleView(Browser* browser) override; | 37 void ShowBubbleView(Browser* browser) override; |
| 38 GlobalErrorBubbleViewBase* GetBubbleView() override; | 38 GlobalErrorBubbleViewBase* GetBubbleView() override; |
| 39 | 39 |
| 40 static int GetMenuItemCommandID(); | 40 static int GetMenuItemCommandID(); |
| 41 | 41 |
| 42 private: | 42 private: |
| 43 ExtensionWarningBadgeService* badge_service_; | 43 WarningBadgeService* badge_service_; |
| 44 | 44 |
| 45 DISALLOW_COPY_AND_ASSIGN(ErrorBadge); | 45 DISALLOW_COPY_AND_ASSIGN(ErrorBadge); |
| 46 }; | 46 }; |
| 47 | 47 |
| 48 ErrorBadge::ErrorBadge(ExtensionWarningBadgeService* badge_service) | 48 ErrorBadge::ErrorBadge(WarningBadgeService* badge_service) |
| 49 : badge_service_(badge_service) {} | 49 : badge_service_(badge_service) {} |
| 50 | 50 |
| 51 ErrorBadge::~ErrorBadge() {} | 51 ErrorBadge::~ErrorBadge() {} |
| 52 | 52 |
| 53 bool ErrorBadge::HasMenuItem() { | 53 bool ErrorBadge::HasMenuItem() { |
| 54 return true; | 54 return true; |
| 55 } | 55 } |
| 56 | 56 |
| 57 int ErrorBadge::MenuItemCommandID() { | 57 int ErrorBadge::MenuItemCommandID() { |
| 58 return GetMenuItemCommandID(); | 58 return GetMenuItemCommandID(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 80 return NULL; | 80 return NULL; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // static | 83 // static |
| 84 int ErrorBadge::GetMenuItemCommandID() { | 84 int ErrorBadge::GetMenuItemCommandID() { |
| 85 return IDC_EXTENSION_ERRORS; | 85 return IDC_EXTENSION_ERRORS; |
| 86 } | 86 } |
| 87 | 87 |
| 88 } // namespace | 88 } // namespace |
| 89 | 89 |
| 90 ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile* profile) | 90 WarningBadgeService::WarningBadgeService(Profile* profile) |
| 91 : profile_(profile), warning_service_observer_(this) { | 91 : profile_(profile), warning_service_observer_(this) { |
| 92 DCHECK(CalledOnValidThread()); | 92 DCHECK(CalledOnValidThread()); |
| 93 warning_service_observer_.Add(WarningService::Get(profile_)); | 93 warning_service_observer_.Add(WarningService::Get(profile_)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {} | 96 WarningBadgeService::~WarningBadgeService() {} |
| 97 | 97 |
| 98 // static | 98 // static |
| 99 ExtensionWarningBadgeService* ExtensionWarningBadgeService::Get( | 99 WarningBadgeService* WarningBadgeService::Get( |
| 100 content::BrowserContext* context) { | 100 content::BrowserContext* context) { |
| 101 return ExtensionWarningBadgeServiceFactory::GetForBrowserContext(context); | 101 return WarningBadgeServiceFactory::GetForBrowserContext(context); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void ExtensionWarningBadgeService::SuppressCurrentWarnings() { | 104 void WarningBadgeService::SuppressCurrentWarnings() { |
| 105 DCHECK(CalledOnValidThread()); | 105 DCHECK(CalledOnValidThread()); |
| 106 size_t old_size = suppressed_warnings_.size(); | 106 size_t old_size = suppressed_warnings_.size(); |
| 107 | 107 |
| 108 const WarningSet& warnings = GetCurrentWarnings(); | 108 const WarningSet& warnings = GetCurrentWarnings(); |
| 109 suppressed_warnings_.insert(warnings.begin(), warnings.end()); | 109 suppressed_warnings_.insert(warnings.begin(), warnings.end()); |
| 110 | 110 |
| 111 if (old_size != suppressed_warnings_.size()) | 111 if (old_size != suppressed_warnings_.size()) |
| 112 UpdateBadgeStatus(); | 112 UpdateBadgeStatus(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 const WarningSet& ExtensionWarningBadgeService::GetCurrentWarnings() const { | 115 const WarningSet& WarningBadgeService::GetCurrentWarnings() const { |
| 116 return WarningService::Get(profile_)->warnings(); | 116 return WarningService::Get(profile_)->warnings(); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void ExtensionWarningBadgeService::ExtensionWarningsChanged() { | 119 void WarningBadgeService::ExtensionWarningsChanged() { |
| 120 DCHECK(CalledOnValidThread()); | 120 DCHECK(CalledOnValidThread()); |
| 121 UpdateBadgeStatus(); | 121 UpdateBadgeStatus(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void ExtensionWarningBadgeService::UpdateBadgeStatus() { | 124 void WarningBadgeService::UpdateBadgeStatus() { |
| 125 const std::set<Warning>& warnings = GetCurrentWarnings(); | 125 const std::set<Warning>& warnings = GetCurrentWarnings(); |
| 126 bool non_suppressed_warnings_exist = false; | 126 bool non_suppressed_warnings_exist = false; |
| 127 for (std::set<Warning>::const_iterator i = warnings.begin(); | 127 for (std::set<Warning>::const_iterator i = warnings.begin(); |
| 128 i != warnings.end(); ++i) { | 128 i != warnings.end(); ++i) { |
| 129 if (!ContainsKey(suppressed_warnings_, *i)) { | 129 if (!ContainsKey(suppressed_warnings_, *i)) { |
| 130 non_suppressed_warnings_exist = true; | 130 non_suppressed_warnings_exist = true; |
| 131 break; | 131 break; |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 ShowBadge(non_suppressed_warnings_exist); | 134 ShowBadge(non_suppressed_warnings_exist); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void ExtensionWarningBadgeService::ShowBadge(bool show) { | 137 void WarningBadgeService::ShowBadge(bool show) { |
| 138 GlobalErrorService* service = | 138 GlobalErrorService* service = |
| 139 GlobalErrorServiceFactory::GetForProfile(profile_); | 139 GlobalErrorServiceFactory::GetForProfile(profile_); |
| 140 GlobalError* error = service->GetGlobalErrorByMenuItemCommandID( | 140 GlobalError* error = service->GetGlobalErrorByMenuItemCommandID( |
| 141 ErrorBadge::GetMenuItemCommandID()); | 141 ErrorBadge::GetMenuItemCommandID()); |
| 142 | 142 |
| 143 // Activate or hide the warning badge in case the current state is incorrect. | 143 // Activate or hide the warning badge in case the current state is incorrect. |
| 144 if (error && !show) { | 144 if (error && !show) { |
| 145 service->RemoveGlobalError(error); | 145 service->RemoveGlobalError(error); |
| 146 delete error; | 146 delete error; |
| 147 } else if (!error && show) { | 147 } else if (!error && show) { |
| 148 service->AddGlobalError(new ErrorBadge(this)); | 148 service->AddGlobalError(new ErrorBadge(this)); |
| 149 } | 149 } |
| 150 } | 150 } |
| 151 | 151 |
| 152 } // namespace extensions | 152 } // namespace extensions |
| OLD | NEW |