OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/protector/settings_change_global_error.h" | 5 #include "chrome/browser/protector/settings_change_global_error.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/protector/settings_change_global_error_delegate.h" | 13 #include "chrome/browser/protector/settings_change_global_error_delegate.h" |
14 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
15 #include "chrome/browser/ui/global_error_service.h" | 15 #include "chrome/browser/ui/global_error_service.h" |
16 #include "chrome/browser/ui/global_error_service_factory.h" | 16 #include "chrome/browser/ui/global_error_service_factory.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "grit/chromium_strings.h" |
| 19 #include "grit/generated_resources.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
18 | 21 |
19 using content::BrowserThread; | 22 using content::BrowserThread; |
20 | 23 |
21 namespace protector { | 24 namespace protector { |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 // Timeout before the global error is removed (wrench menu item disappears). | 28 // Timeout before the global error is removed (wrench menu item disappears). |
26 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min | 29 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min |
27 | 30 |
| 31 // IDs of menu item labels. |
| 32 const int kMenuItemLabelIDs[] = { |
| 33 IDS_SEARCH_ENGINE_CHANGE_WRENCH_MENU_ITEM, |
| 34 IDS_HOMEPAGE_CHANGE_WRENCH_MENU_ITEM |
| 35 }; |
| 36 |
| 37 // IDs of bubble title messages. |
| 38 const int kBubbleTitleIDs[] = { |
| 39 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TITLE, |
| 40 IDS_HOMEPAGE_CHANGE_BUBBLE_TITLE |
| 41 }; |
| 42 |
| 43 // IDs of bubble text messages. |
| 44 const int kBubbleMessageIDs[] = { |
| 45 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TEXT, |
| 46 IDS_HOMEPAGE_CHANGE_BUBBLE_TEXT |
| 47 }; |
| 48 |
| 49 // IDs of bubble text messages when the old setting is unknown. |
| 50 const int kBubbleMessageOldUnknownIDs[] = { |
| 51 IDS_SEARCH_ENGINE_CHANGE_UNKNOWN_BUBBLE_TEXT, |
| 52 IDS_HOMEPAGE_CHANGE_UNKNOWN_BUBBLE_TEXT |
| 53 }; |
| 54 |
| 55 // IDs of "Keep Setting" button titles. |
| 56 const int kBubbleKeepSettingIDs[] = { |
| 57 IDS_SEARCH_ENGINE_CHANGE_RESTORE, |
| 58 IDS_HOMEPAGE_CHANGE_RESTORE |
| 59 }; |
| 60 |
| 61 // IDs of "Change Setting" button titles. |
| 62 const int kBubbleChangeSettingIDs[] = { |
| 63 IDS_SEARCH_ENGINE_CHANGE_APPLY, |
| 64 IDS_HOMEPAGE_CHANGE_APPLY |
| 65 }; |
| 66 |
28 } // namespace | 67 } // namespace |
29 | 68 |
30 SettingsChangeGlobalError::SettingsChangeGlobalError( | 69 SettingsChangeGlobalError::SettingsChangeGlobalError( |
31 SettingChange* change, | 70 const SettingChangeVector& changes, |
32 SettingsChangeGlobalErrorDelegate* delegate) | 71 SettingsChangeGlobalErrorDelegate* delegate) |
33 : change_(change), | 72 : changes_(changes), |
34 delegate_(delegate), | 73 delegate_(delegate), |
35 profile_(NULL), | 74 profile_(NULL), |
36 browser_(NULL), | 75 browser_(NULL), |
37 closed_by_button_(false), | 76 closed_by_button_(false), |
38 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 77 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
39 DCHECK(delegate_); | 78 DCHECK(changes.size() > 0); |
40 } | 79 } |
41 | 80 |
42 SettingsChangeGlobalError::~SettingsChangeGlobalError() { | 81 SettingsChangeGlobalError::~SettingsChangeGlobalError() { |
| 82 STLDeleteElements(&changes_); |
43 } | 83 } |
44 | 84 |
45 bool SettingsChangeGlobalError::HasBadge() { | 85 bool SettingsChangeGlobalError::HasBadge() { |
46 return true; | 86 return true; |
47 } | 87 } |
48 | 88 |
49 bool SettingsChangeGlobalError::HasMenuItem() { | 89 bool SettingsChangeGlobalError::HasMenuItem() { |
50 return true; | 90 return true; |
51 } | 91 } |
52 | 92 |
53 int SettingsChangeGlobalError::MenuItemCommandID() { | 93 int SettingsChangeGlobalError::MenuItemCommandID() { |
54 return IDC_SHOW_SETTINGS_CHANGES; | 94 return IDC_SHOW_SETTINGS_CHANGES; |
55 } | 95 } |
56 | 96 |
| 97 // TODO(ivankr): Currently the menu item/bubble only displays a warning about |
| 98 // the first change. We want to fix this so that a single menu item/bubble |
| 99 // can display warning about multiple changes. |
| 100 |
57 string16 SettingsChangeGlobalError::MenuItemLabel() { | 101 string16 SettingsChangeGlobalError::MenuItemLabel() { |
58 return change_->GetTitle(); | 102 return l10n_util::GetStringUTF16(kMenuItemLabelIDs[changes_.front()->type()]); |
59 } | 103 } |
60 | 104 |
61 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) { | 105 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) { |
62 // Cancel previously posted tasks. | 106 // Cancel previously posted tasks. |
63 weak_factory_.InvalidateWeakPtrs(); | 107 weak_factory_.InvalidateWeakPtrs(); |
64 browser_ = browser; | 108 browser_ = browser; |
65 ShowBubbleView(browser_); | 109 ShowBubbleView(browser_); |
66 } | 110 } |
67 | 111 |
68 bool SettingsChangeGlobalError::HasBubbleView() { | 112 bool SettingsChangeGlobalError::HasBubbleView() { |
69 return true; | 113 return true; |
70 } | 114 } |
71 | 115 |
72 string16 SettingsChangeGlobalError::GetBubbleViewTitle() { | 116 string16 SettingsChangeGlobalError::GetBubbleViewTitle() { |
73 return change_->GetTitle(); | 117 return l10n_util::GetStringUTF16(kBubbleTitleIDs[changes_.front()->type()]); |
74 } | 118 } |
75 | 119 |
76 string16 SettingsChangeGlobalError::GetBubbleViewMessage() { | 120 string16 SettingsChangeGlobalError::GetBubbleViewMessage() { |
77 return change_->GetMessage(); | 121 SettingChange* change = changes_.front(); |
| 122 const string16& old_setting = change->GetOldSetting(); |
| 123 if (old_setting.empty()) { |
| 124 return l10n_util::GetStringFUTF16( |
| 125 kBubbleMessageOldUnknownIDs[change->type()], |
| 126 change->GetNewSetting()); |
| 127 } else { |
| 128 return l10n_util::GetStringFUTF16( |
| 129 kBubbleMessageIDs[change->type()], |
| 130 old_setting, |
| 131 change->GetNewSetting()); |
| 132 } |
78 } | 133 } |
79 | 134 |
80 // The Accept and Revert buttons are swapped like the 'server' and 'client' | |
81 // concepts in X11. Accept button (the default one) discards changes | |
82 // (keeps using previous setting) while cancel button applies changes | |
83 // (switches to the new setting). This is sick and blows my mind. - ivankr | |
84 | |
85 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() { | 135 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() { |
86 return change_->GetDiscardButtonText(); | 136 SettingChange* change = changes_.front(); |
| 137 string16 old_setting = change->GetOldSetting(); |
| 138 if (old_setting.empty()) { |
| 139 return l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_OPEN_SETTINGS); |
| 140 } else { |
| 141 return l10n_util::GetStringFUTF16( |
| 142 kBubbleKeepSettingIDs[change->type()], |
| 143 old_setting); |
| 144 } |
87 } | 145 } |
88 | 146 |
89 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { | 147 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { |
90 return change_->GetApplyButtonText(); | 148 SettingChange* change = changes_.front(); |
| 149 return l10n_util::GetStringFUTF16(kBubbleChangeSettingIDs[change->type()], |
| 150 change->GetNewSetting()); |
91 } | 151 } |
92 | 152 |
93 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() { | 153 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() { |
94 closed_by_button_ = true; | 154 closed_by_button_ = true; |
95 delegate_->OnDiscardChange(); | 155 DCHECK(delegate_); |
| 156 VLOG(1) << "Discard changes"; |
| 157 delegate_->OnDiscardChanges(); |
96 } | 158 } |
97 | 159 |
98 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { | 160 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { |
99 closed_by_button_ = true; | 161 closed_by_button_ = true; |
100 delegate_->OnApplyChange(); | 162 DCHECK(delegate_); |
| 163 VLOG(1) << "Apply changes"; |
| 164 delegate_->OnApplyChanges(); |
101 } | 165 } |
102 | 166 |
103 void SettingsChangeGlobalError::RemoveFromProfile() { | 167 void SettingsChangeGlobalError::RemoveFromProfile() { |
| 168 DCHECK(delegate_); |
104 if (profile_) | 169 if (profile_) |
105 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); | 170 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); |
106 if (!closed_by_button_) | 171 if (!closed_by_button_) |
107 delegate_->OnDecisionTimeout(); | 172 delegate_->OnDecisionTimeout(); |
108 delegate_->OnRemovedFromProfile(); | 173 delegate_->OnRemovedFromProfile(); |
109 } | 174 } |
110 | 175 |
111 void SettingsChangeGlobalError::BubbleViewDidClose() { | 176 void SettingsChangeGlobalError::BubbleViewDidClose() { |
112 browser_ = NULL; | 177 browser_ = NULL; |
113 if (!closed_by_button_) { | 178 if (!closed_by_button_) { |
(...skipping 28 matching lines...) Expand all Loading... |
142 | 207 |
143 void SettingsChangeGlobalError::Show() { | 208 void SettingsChangeGlobalError::Show() { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 DCHECK(profile_); | 210 DCHECK(profile_); |
146 browser_ = BrowserList::GetLastActiveWithProfile(profile_); | 211 browser_ = BrowserList::GetLastActiveWithProfile(profile_); |
147 if (browser_) | 212 if (browser_) |
148 ShowBubbleView(browser_); | 213 ShowBubbleView(browser_); |
149 } | 214 } |
150 | 215 |
151 } // namespace protector | 216 } // namespace protector |
OLD | NEW |