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