1 // Copyright (c) 2014 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.
5 #include "chrome/browser/recovery/recovery_install_global_error.h"
7 #include "base/prefs/pref_service.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/component_updater/recovery_component_installer.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/global_error/global_error_service.h"
13 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
14 #include "chrome/browser/upgrade_detector.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "grit/chromium_strings.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
22 RecoveryInstallGlobalError::RecoveryInstallGlobalError(Profile
* profile
)
23 : elevation_needed_(false),
25 has_shown_bubble_view_(false) {
26 GlobalErrorServiceFactory::GetForProfile(profile_
)->AddGlobalError(this);
28 PrefService
* pref
= g_browser_process
->local_state();
29 if (pref
->FindPreference(prefs::kRecoveryComponentNeedsElevation
)) {
31 pref
->GetBoolean(prefs::kRecoveryComponentNeedsElevation
);
33 if (elevation_needed_
) {
34 GlobalErrorServiceFactory::GetForProfile(profile_
)->NotifyErrorsChanged(
38 pref_registrar_
.Init(pref
);
40 prefs::kRecoveryComponentNeedsElevation
,
41 base::Bind(&RecoveryInstallGlobalError::OnElevationRequirementChanged
,
42 base::Unretained(this)));
45 RecoveryInstallGlobalError::~RecoveryInstallGlobalError() {
48 void RecoveryInstallGlobalError::Shutdown() {
49 GlobalErrorServiceFactory::GetForProfile(profile_
)->RemoveGlobalError(this);
52 GlobalError::Severity
RecoveryInstallGlobalError::GetSeverity() {
53 return GlobalError::SEVERITY_HIGH
;
56 bool RecoveryInstallGlobalError::HasMenuItem() {
57 return HasElevationNotification();
60 int RecoveryInstallGlobalError::MenuItemCommandID() {
61 return IDC_ELEVATED_RECOVERY_DIALOG
;
64 base::string16
RecoveryInstallGlobalError::MenuItemLabel() {
65 return l10n_util::GetStringUTF16(IDS_UPDATE_NOW
);
68 int RecoveryInstallGlobalError::MenuItemIconResourceID() {
69 return IDR_UPDATE_MENU_SEVERITY_HIGH
;
72 void RecoveryInstallGlobalError::ExecuteMenuItem(Browser
* browser
) {
73 ShowBubbleView(browser
);
76 bool RecoveryInstallGlobalError::HasBubbleView() {
77 return HasElevationNotification();
80 bool RecoveryInstallGlobalError::HasShownBubbleView() {
81 return has_shown_bubble_view_
;
84 void RecoveryInstallGlobalError::ShowBubbleView(Browser
* browser
) {
85 GlobalErrorWithStandardBubble::ShowBubbleView(browser
);
86 has_shown_bubble_view_
= true;
89 bool RecoveryInstallGlobalError::ShouldCloseOnDeactivate() const {
93 gfx::Image
RecoveryInstallGlobalError::GetBubbleViewIcon() {
94 return ResourceBundle::GetSharedInstance().GetNativeImageNamed(
95 IDR_UPDATE_MENU_SEVERITY_HIGH
);
98 base::string16
RecoveryInstallGlobalError::GetBubbleViewTitle() {
99 return l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TITLE
);
102 std::vector
<base::string16
>
103 RecoveryInstallGlobalError::GetBubbleViewMessages() {
104 return std::vector
<base::string16
>(1,
105 l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TEXT
));
108 base::string16
RecoveryInstallGlobalError::GetBubbleViewAcceptButtonLabel() {
109 return l10n_util::GetStringUTF16(IDS_RUN_RECOVERY
);
112 bool RecoveryInstallGlobalError::ShouldShowCloseButton() const {
116 bool RecoveryInstallGlobalError::ShouldAddElevationIconToAcceptButton() {
120 base::string16
RecoveryInstallGlobalError::GetBubbleViewCancelButtonLabel() {
121 return l10n_util::GetStringUTF16(IDS_DECLINE_RECOVERY
);
124 void RecoveryInstallGlobalError::OnBubbleViewDidClose(Browser
* browser
) {
127 void RecoveryInstallGlobalError::BubbleViewAcceptButtonPressed(
129 component_updater::AcceptedElevatedRecoveryInstall(pref_registrar_
.prefs());
132 void RecoveryInstallGlobalError::BubbleViewCancelButtonPressed(
134 component_updater::DeclinedElevatedRecoveryInstall(pref_registrar_
.prefs());
137 bool RecoveryInstallGlobalError::HasElevationNotification() const {
138 // Do not show this bubble if we already have an upgrade notice.
139 return elevation_needed_
&& !UpgradeDetector::GetInstance()->notify_upgrade();
142 void RecoveryInstallGlobalError::OnElevationRequirementChanged() {
143 PrefService
* pref
= pref_registrar_
.prefs();
144 DCHECK(pref
->FindPreference(prefs::kRecoveryComponentNeedsElevation
));
145 elevation_needed_
= pref
->GetBoolean(prefs::kRecoveryComponentNeedsElevation
);
147 // Got a new elevation request, resets |has_shown_bubble_view_| so the
148 // bubble has a higher priority to show.
149 if (elevation_needed_
)
150 has_shown_bubble_view_
= false;
152 GlobalErrorServiceFactory::GetForProfile(profile_
)->NotifyErrorsChanged(this);