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.
4 
5 #include "chrome/browser/recovery/recovery_install_global_error.h"
6 
7 #include "base/bind.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/app/vector_icons/vector_icons.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/component_updater/recovery_component_installer.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/global_error/global_error_service.h"
14 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
15 #include "chrome/browser/upgrade_detector/upgrade_detector.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/chromium_strings.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "components/prefs/pref_service.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/base/models/image_model.h"
23 #include "ui/native_theme/native_theme.h"
24 
RecoveryInstallGlobalError(Profile * profile)25 RecoveryInstallGlobalError::RecoveryInstallGlobalError(Profile* profile)
26         : elevation_needed_(false),
27           profile_(profile),
28           has_shown_bubble_view_(false) {
29   GlobalErrorServiceFactory::GetForProfile(profile_)->AddUnownedGlobalError(
30       this);
31 
32   PrefService* pref = g_browser_process->local_state();
33   if (pref->FindPreference(prefs::kRecoveryComponentNeedsElevation)) {
34     elevation_needed_ =
35         pref->GetBoolean(prefs::kRecoveryComponentNeedsElevation);
36   }
37   if (elevation_needed_)
38     GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged();
39 
40   pref_registrar_.Init(pref);
41   pref_registrar_.Add(
42       prefs::kRecoveryComponentNeedsElevation,
43       base::Bind(&RecoveryInstallGlobalError::OnElevationRequirementChanged,
44                  base::Unretained(this)));
45 }
46 
~RecoveryInstallGlobalError()47 RecoveryInstallGlobalError::~RecoveryInstallGlobalError() {}
48 
Shutdown()49 void RecoveryInstallGlobalError::Shutdown() {
50   GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveUnownedGlobalError(
51       this);
52 }
53 
GetSeverity()54 GlobalError::Severity RecoveryInstallGlobalError::GetSeverity() {
55   return GlobalError::SEVERITY_HIGH;
56 }
57 
HasMenuItem()58 bool RecoveryInstallGlobalError::HasMenuItem() {
59   return HasElevationNotification();
60 }
61 
MenuItemCommandID()62 int RecoveryInstallGlobalError::MenuItemCommandID() {
63   return IDC_ELEVATED_RECOVERY_DIALOG;
64 }
65 
MenuItemLabel()66 base::string16 RecoveryInstallGlobalError::MenuItemLabel() {
67   return l10n_util::GetStringUTF16(IDS_UPDATE_NOW);
68 }
69 
MenuItemIcon()70 ui::ImageModel RecoveryInstallGlobalError::MenuItemIcon() {
71   return ui::ImageModel::FromVectorIcon(
72       kBrowserToolsUpdateIcon, ui::NativeTheme::kColorId_AlertSeverityHigh);
73 }
74 
ExecuteMenuItem(Browser * browser)75 void RecoveryInstallGlobalError::ExecuteMenuItem(Browser* browser) {
76   ShowBubbleView(browser);
77 }
78 
HasBubbleView()79 bool RecoveryInstallGlobalError::HasBubbleView() {
80   return HasElevationNotification();
81 }
82 
HasShownBubbleView()83 bool RecoveryInstallGlobalError::HasShownBubbleView() {
84   return has_shown_bubble_view_;
85 }
86 
ShowBubbleView(Browser * browser)87 void RecoveryInstallGlobalError::ShowBubbleView(Browser* browser) {
88   GlobalErrorWithStandardBubble::ShowBubbleView(browser);
89   has_shown_bubble_view_ = true;
90 }
91 
ShouldCloseOnDeactivate() const92 bool RecoveryInstallGlobalError::ShouldCloseOnDeactivate() const {
93   return false;
94 }
95 
GetBubbleViewTitle()96 base::string16 RecoveryInstallGlobalError::GetBubbleViewTitle() {
97   return l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TITLE);
98 }
99 
100 std::vector<base::string16>
GetBubbleViewMessages()101 RecoveryInstallGlobalError::GetBubbleViewMessages() {
102   return std::vector<base::string16>(1,
103       l10n_util::GetStringUTF16(IDS_RECOVERY_BUBBLE_TEXT));
104 }
105 
GetBubbleViewAcceptButtonLabel()106 base::string16 RecoveryInstallGlobalError::GetBubbleViewAcceptButtonLabel() {
107   return l10n_util::GetStringUTF16(IDS_RUN_RECOVERY);
108 }
109 
ShouldShowCloseButton() const110 bool RecoveryInstallGlobalError::ShouldShowCloseButton() const {
111   return true;
112 }
113 
ShouldAddElevationIconToAcceptButton()114 bool RecoveryInstallGlobalError::ShouldAddElevationIconToAcceptButton() {
115   return true;
116 }
117 
GetBubbleViewCancelButtonLabel()118 base::string16 RecoveryInstallGlobalError::GetBubbleViewCancelButtonLabel() {
119   return l10n_util::GetStringUTF16(IDS_DECLINE_RECOVERY);
120 }
121 
OnBubbleViewDidClose(Browser * browser)122 void RecoveryInstallGlobalError::OnBubbleViewDidClose(Browser* browser) {
123 }
124 
BubbleViewAcceptButtonPressed(Browser * browser)125 void RecoveryInstallGlobalError::BubbleViewAcceptButtonPressed(
126     Browser* browser) {
127   component_updater::AcceptedElevatedRecoveryInstall(pref_registrar_.prefs());
128 }
129 
BubbleViewCancelButtonPressed(Browser * browser)130 void RecoveryInstallGlobalError::BubbleViewCancelButtonPressed(
131     Browser* browser) {
132   component_updater::DeclinedElevatedRecoveryInstall(pref_registrar_.prefs());
133 }
134 
HasElevationNotification() const135 bool RecoveryInstallGlobalError::HasElevationNotification() const {
136   // Do not show this bubble if we already have an upgrade notice.
137   return elevation_needed_ && !UpgradeDetector::GetInstance()->notify_upgrade();
138 }
139 
OnElevationRequirementChanged()140 void RecoveryInstallGlobalError::OnElevationRequirementChanged() {
141   PrefService* pref = pref_registrar_.prefs();
142   DCHECK(pref->FindPreference(prefs::kRecoveryComponentNeedsElevation));
143   elevation_needed_ = pref->GetBoolean(prefs::kRecoveryComponentNeedsElevation);
144 
145   // Got a new elevation request, resets |has_shown_bubble_view_| so the
146   // bubble has a higher priority to show.
147   if (elevation_needed_)
148     has_shown_bubble_view_ = false;
149 
150   GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged();
151 }
152