| 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/task.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/protector/settings_change_global_error_delegate.h" |
| 12 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
| 13 #include "chrome/browser/ui/global_error_service.h" | 15 #include "chrome/browser/ui/global_error_service.h" |
| 14 #include "chrome/browser/ui/global_error_service_factory.h" | 16 #include "chrome/browser/ui/global_error_service_factory.h" |
| 17 #include "content/browser/browser_thread.h" |
| 15 #include "grit/chromium_strings.h" | 18 #include "grit/chromium_strings.h" |
| 16 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 18 | 21 |
| 22 namespace protector { |
| 23 |
| 19 namespace { | 24 namespace { |
| 20 | 25 |
| 21 // Timeout before the global error is removed (wrench menu item disappears). | 26 // Timeout before the global error is removed (wrench menu item disappears). |
| 22 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min | 27 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min |
| 28 |
| 23 // IDs of menu item labels. | 29 // IDs of menu item labels. |
| 24 const int kMenuItemLabelIDs[] = { | 30 const int kMenuItemLabelIDs[] = { |
| 25 IDS_SEARCH_ENGINE_CHANGE_WRENCH_MENU_ITEM, | 31 IDS_SEARCH_ENGINE_CHANGE_WRENCH_MENU_ITEM, |
| 26 IDS_HOMEPAGE_CHANGE_WRENCH_MENU_ITEM | 32 IDS_HOMEPAGE_CHANGE_WRENCH_MENU_ITEM |
| 27 }; | 33 }; |
| 34 |
| 28 // IDs of bubble title messages. | 35 // IDs of bubble title messages. |
| 29 const int kBubbleTitleIDs[] = { | 36 const int kBubbleTitleIDs[] = { |
| 30 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TITLE, | 37 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TITLE, |
| 31 IDS_HOMEPAGE_CHANGE_BUBBLE_TITLE | 38 IDS_HOMEPAGE_CHANGE_BUBBLE_TITLE |
| 32 }; | 39 }; |
| 40 |
| 33 // IDs of bubble text messages. | 41 // IDs of bubble text messages. |
| 34 const int kBubbleMessageIDs[] = { | 42 const int kBubbleMessageIDs[] = { |
| 35 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TEXT, | 43 IDS_SEARCH_ENGINE_CHANGE_BUBBLE_TEXT, |
| 36 IDS_HOMEPAGE_CHANGE_BUBBLE_TEXT | 44 IDS_HOMEPAGE_CHANGE_BUBBLE_TEXT |
| 37 }; | 45 }; |
| 46 |
| 38 // IDs of bubble text messages when the old setting is unknown. | 47 // IDs of bubble text messages when the old setting is unknown. |
| 39 const int kBubbleMessageOldUnknownIDs[] = { | 48 const int kBubbleMessageOldUnknownIDs[] = { |
| 40 IDS_SEARCH_ENGINE_CHANGE_UNKNOWN_BUBBLE_TEXT, | 49 IDS_SEARCH_ENGINE_CHANGE_UNKNOWN_BUBBLE_TEXT, |
| 41 IDS_HOMEPAGE_CHANGE_UNKNOWN_BUBBLE_TEXT | 50 IDS_HOMEPAGE_CHANGE_UNKNOWN_BUBBLE_TEXT |
| 42 }; | 51 }; |
| 52 |
| 43 // IDs of "Keep Setting" button titles. | 53 // IDs of "Keep Setting" button titles. |
| 44 const int kBubbleKeepSettingIDs[] = { | 54 const int kBubbleKeepSettingIDs[] = { |
| 45 IDS_SEARCH_ENGINE_CHANGE_RESTORE, | 55 IDS_SEARCH_ENGINE_CHANGE_RESTORE, |
| 46 IDS_HOMEPAGE_CHANGE_RESTORE | 56 IDS_HOMEPAGE_CHANGE_RESTORE |
| 47 }; | 57 }; |
| 58 |
| 48 // IDs of "Change Setting" button titles. | 59 // IDs of "Change Setting" button titles. |
| 49 const int kBubbleChangeSettingIDs[] = { | 60 const int kBubbleChangeSettingIDs[] = { |
| 50 IDS_SEARCH_ENGINE_CHANGE_APPLY, | 61 IDS_SEARCH_ENGINE_CHANGE_APPLY, |
| 51 IDS_HOMEPAGE_CHANGE_APPLY | 62 IDS_HOMEPAGE_CHANGE_APPLY |
| 52 }; | 63 }; |
| 53 | 64 |
| 54 } // namespace | 65 } // namespace |
| 55 | 66 |
| 56 SettingsChangeGlobalError::SettingsChangeGlobalError( | 67 SettingsChangeGlobalError::SettingsChangeGlobalError( |
| 57 const ChangesVector& changes, | 68 const SettingChangeVector& changes, |
| 58 const base::Closure& apply_changes_cb, | 69 SettingsChangeGlobalErrorDelegate* delegate) |
| 59 const base::Closure& revert_changes_cb) | |
| 60 : changes_(changes), | 70 : changes_(changes), |
| 61 apply_changes_cb_(apply_changes_cb), | 71 delegate_(delegate), |
| 62 revert_changes_cb_(revert_changes_cb), | |
| 63 profile_(NULL), | 72 profile_(NULL), |
| 73 browser_(NULL), |
| 64 closed_by_button_(false), | 74 closed_by_button_(false), |
| 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 75 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 66 DCHECK(changes.size() > 0); | 76 DCHECK(changes.size() > 0); |
| 67 } | 77 } |
| 68 | 78 |
| 69 SettingsChangeGlobalError::~SettingsChangeGlobalError() { | 79 SettingsChangeGlobalError::~SettingsChangeGlobalError() { |
| 80 STLDeleteElements(&changes_); |
| 70 } | 81 } |
| 71 | 82 |
| 72 bool SettingsChangeGlobalError::HasBadge() { | 83 bool SettingsChangeGlobalError::HasBadge() { |
| 73 return true; | 84 return true; |
| 74 } | 85 } |
| 75 | 86 |
| 76 bool SettingsChangeGlobalError::HasMenuItem() { | 87 bool SettingsChangeGlobalError::HasMenuItem() { |
| 77 return true; | 88 return true; |
| 78 } | 89 } |
| 79 | 90 |
| 80 int SettingsChangeGlobalError::MenuItemCommandID() { | 91 int SettingsChangeGlobalError::MenuItemCommandID() { |
| 81 return IDC_SHOW_SETTINGS_CHANGES; | 92 return IDC_SHOW_SETTINGS_CHANGES; |
| 82 } | 93 } |
| 83 | 94 |
| 84 // TODO(ivankr): Currently the menu item/bubble only displays a warning about | 95 // TODO(ivankr): Currently the menu item/bubble only displays a warning about |
| 85 // the first change. We want to fix this so that a single menu item/bubble | 96 // the first change. We want to fix this so that a single menu item/bubble |
| 86 // can display warning about multiple changes. | 97 // can display warning about multiple changes. |
| 87 | 98 |
| 88 string16 SettingsChangeGlobalError::MenuItemLabel() { | 99 string16 SettingsChangeGlobalError::MenuItemLabel() { |
| 89 return l10n_util::GetStringUTF16(kMenuItemLabelIDs[changes_.front().type]); | 100 return l10n_util::GetStringUTF16(kMenuItemLabelIDs[changes_.front()->type()]); |
| 90 } | 101 } |
| 91 | 102 |
| 92 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) { | 103 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) { |
| 93 weak_factory_.InvalidateWeakPtrs(); // Cancel previously posted tasks. | 104 // Cancel previously posted tasks. |
| 94 ShowBubbleView(browser); | 105 weak_factory_.InvalidateWeakPtrs(); |
| 106 browser_ = browser; |
| 107 ShowBubbleView(browser_); |
| 95 } | 108 } |
| 96 | 109 |
| 97 bool SettingsChangeGlobalError::HasBubbleView() { | 110 bool SettingsChangeGlobalError::HasBubbleView() { |
| 98 return true; | 111 return true; |
| 99 } | 112 } |
| 100 | 113 |
| 101 string16 SettingsChangeGlobalError::GetBubbleViewTitle() { | 114 string16 SettingsChangeGlobalError::GetBubbleViewTitle() { |
| 102 return l10n_util::GetStringUTF16(kBubbleTitleIDs[changes_.front().type]); | 115 return l10n_util::GetStringUTF16(kBubbleTitleIDs[changes_.front()->type()]); |
| 103 } | 116 } |
| 104 | 117 |
| 105 string16 SettingsChangeGlobalError::GetBubbleViewMessage() { | 118 string16 SettingsChangeGlobalError::GetBubbleViewMessage() { |
| 106 const Change& change = changes_.front(); | 119 SettingChange* change = changes_.front(); |
| 107 return change.old_setting.empty() ? | 120 const string16& old_setting = change->GetOldSetting(); |
| 108 l10n_util::GetStringFUTF16(kBubbleMessageOldUnknownIDs[change.type], | 121 if (old_setting.empty()) { |
| 109 change.new_setting) : | 122 return l10n_util::GetStringFUTF16( |
| 110 l10n_util::GetStringFUTF16(kBubbleMessageIDs[change.type], | 123 kBubbleMessageOldUnknownIDs[change->type()], |
| 111 change.old_setting, change.new_setting); | 124 change->GetNewSetting()); |
| 125 } else { |
| 126 return l10n_util::GetStringFUTF16( |
| 127 kBubbleMessageIDs[change->type()], |
| 128 old_setting, |
| 129 change->GetNewSetting()); |
| 130 } |
| 112 } | 131 } |
| 113 | 132 |
| 114 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() { | 133 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() { |
| 115 const Change& change = changes_.front(); | 134 SettingChange* change = changes_.front(); |
| 116 return l10n_util::GetStringFUTF16(kBubbleChangeSettingIDs[change.type], | 135 return l10n_util::GetStringFUTF16(kBubbleChangeSettingIDs[change->type()], |
| 117 change.new_setting); | 136 change->GetNewSetting()); |
| 118 } | 137 } |
| 119 | 138 |
| 120 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { | 139 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { |
| 121 const Change& change = changes_.front(); | 140 SettingChange* change = changes_.front(); |
| 122 return change.old_setting.empty() ? | 141 string16 old_setting = change->GetOldSetting(); |
| 123 l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_OPEN_SETTINGS) : | 142 if (old_setting.empty()) { |
| 124 l10n_util::GetStringFUTF16(kBubbleKeepSettingIDs[change.type], | 143 return l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_OPEN_SETTINGS); |
| 125 change.old_setting); | 144 } else { |
| 145 return l10n_util::GetStringFUTF16( |
| 146 kBubbleKeepSettingIDs[change->type()], |
| 147 old_setting); |
| 148 } |
| 126 } | 149 } |
| 127 | 150 |
| 128 bool SettingsChangeGlobalError::IsAcceptButtonDefault() { | 151 bool SettingsChangeGlobalError::IsAcceptButtonDefault() { |
| 129 return false; | 152 return false; |
| 130 } | 153 } |
| 131 | 154 |
| 132 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() { | 155 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() { |
| 156 DCHECK(delegate_); |
| 133 VLOG(1) << "Apply changes"; | 157 VLOG(1) << "Apply changes"; |
| 134 apply_changes_cb_.Run(); | 158 delegate_->OnApplyChanges(); |
| 135 closed_by_button_ = true; | 159 closed_by_button_ = true; |
| 136 } | 160 } |
| 137 | 161 |
| 138 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { | 162 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { |
| 139 VLOG(1) << "Revert changes"; | 163 DCHECK(delegate_); |
| 140 revert_changes_cb_.Run(); | 164 VLOG(1) << "Discard changes"; |
| 165 delegate_->OnDiscardChanges(); |
| 141 closed_by_button_ = true; | 166 closed_by_button_ = true; |
| 142 } | 167 } |
| 143 | 168 |
| 144 void SettingsChangeGlobalError::RemoveFromProfile() { | 169 void SettingsChangeGlobalError::RemoveFromProfile() { |
| 170 DCHECK(delegate_); |
| 145 if (profile_) | 171 if (profile_) |
| 146 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); | 172 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); |
| 147 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | 173 if (!closed_by_button_) |
| 174 delegate_->OnDecisionTimeout(); |
| 148 } | 175 } |
| 149 | 176 |
| 150 void SettingsChangeGlobalError::BubbleViewDidClose() { | 177 void SettingsChangeGlobalError::BubbleViewDidClose() { |
| 178 browser_ = NULL; |
| 151 if (!closed_by_button_) { | 179 if (!closed_by_button_) { |
| 152 BrowserThread::PostDelayedTask( | 180 BrowserThread::PostDelayedTask( |
| 153 BrowserThread::UI, FROM_HERE, | 181 BrowserThread::UI, FROM_HERE, |
| 154 base::Bind(&SettingsChangeGlobalError::RemoveFromProfile, | 182 base::Bind(&SettingsChangeGlobalError::RemoveFromProfile, |
| 155 weak_factory_.GetWeakPtr()), | 183 weak_factory_.GetWeakPtr()), |
| 156 kMenuItemDisplayPeriodMs); | 184 kMenuItemDisplayPeriodMs); |
| 157 } else { | 185 } else { |
| 158 RemoveFromProfile(); | 186 RemoveFromProfile(); |
| 159 } | 187 } |
| 160 } | 188 } |
| 161 | 189 |
| 162 void SettingsChangeGlobalError::ShowForDefaultProfile() { | 190 void SettingsChangeGlobalError::ShowForProfile(Profile* profile) { |
| 163 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 191 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 164 AddToDefaultProfile(); | 192 AddToProfile(profile); |
| 165 } else { | 193 } else { |
| 166 BrowserThread::PostTask( | 194 BrowserThread::PostTask( |
| 167 BrowserThread::UI, FROM_HERE, | 195 BrowserThread::UI, FROM_HERE, |
| 168 base::Bind(&SettingsChangeGlobalError::AddToDefaultProfile, | 196 base::Bind(&SettingsChangeGlobalError::AddToProfile, |
| 169 base::Unretained(this))); | 197 base::Unretained(this), |
| 198 profile)); |
| 170 } | 199 } |
| 171 } | 200 } |
| 172 | 201 |
| 173 void SettingsChangeGlobalError::AddToDefaultProfile() { | 202 void SettingsChangeGlobalError::AddToProfile(Profile* profile) { |
| 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 175 profile_ = ProfileManager::GetDefaultProfile(); | 204 profile_ = profile; |
| 176 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this); | 205 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this); |
| 177 Show(); | 206 Show(); |
| 178 } | 207 } |
| 179 | 208 |
| 180 void SettingsChangeGlobalError::Show() { | 209 void SettingsChangeGlobalError::Show() { |
| 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 182 DCHECK(profile_); | 211 DCHECK(profile_); |
| 183 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); | 212 browser_ = BrowserList::GetLastActiveWithProfile(profile_); |
| 184 if (browser) | 213 if (browser_) |
| 185 ShowBubbleView(browser); | 214 ShowBubbleView(browser_); |
| 186 } | 215 } |
| 216 |
| 217 } // namespace protector |
| OLD | NEW |