Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(499)

Side by Side Diff: chrome/browser/protector/settings_change_global_error.cc

Issue 8342049: Added Protector, hooked up DSE verification with error bubble. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed comment Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
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() {
79 STLDeleteElements(&changes_);
70 } 80 }
71 81
72 bool SettingsChangeGlobalError::HasBadge() { 82 bool SettingsChangeGlobalError::HasBadge() {
73 return true; 83 return true;
74 } 84 }
75 85
76 bool SettingsChangeGlobalError::HasMenuItem() { 86 bool SettingsChangeGlobalError::HasMenuItem() {
77 return true; 87 return true;
78 } 88 }
79 89
80 int SettingsChangeGlobalError::MenuItemCommandID() { 90 int SettingsChangeGlobalError::MenuItemCommandID() {
81 return IDC_SHOW_SETTINGS_CHANGES; 91 return IDC_SHOW_SETTINGS_CHANGES;
82 } 92 }
83 93
84 // TODO(ivankr): Currently the menu item/bubble only displays a warning about 94 // 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 95 // the first change. We want to fix this so that a single menu item/bubble
86 // can display warning about multiple changes. 96 // can display warning about multiple changes.
87 97
88 string16 SettingsChangeGlobalError::MenuItemLabel() { 98 string16 SettingsChangeGlobalError::MenuItemLabel() {
89 return l10n_util::GetStringUTF16(kMenuItemLabelIDs[changes_.front().type]); 99 return l10n_util::GetStringUTF16(kMenuItemLabelIDs[changes_.front()->type()]);
90 } 100 }
91 101
92 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) { 102 void SettingsChangeGlobalError::ExecuteMenuItem(Browser* browser) {
93 weak_factory_.InvalidateWeakPtrs(); // Cancel previously posted tasks. 103 // Cancel previously posted tasks.
94 ShowBubbleView(browser); 104 weak_factory_.InvalidateWeakPtrs();
105 browser_ = browser;
106 ShowBubbleView(browser_);
95 } 107 }
96 108
97 bool SettingsChangeGlobalError::HasBubbleView() { 109 bool SettingsChangeGlobalError::HasBubbleView() {
98 return true; 110 return true;
99 } 111 }
100 112
101 string16 SettingsChangeGlobalError::GetBubbleViewTitle() { 113 string16 SettingsChangeGlobalError::GetBubbleViewTitle() {
102 return l10n_util::GetStringUTF16(kBubbleTitleIDs[changes_.front().type]); 114 return l10n_util::GetStringUTF16(kBubbleTitleIDs[changes_.front()->type()]);
103 } 115 }
104 116
105 string16 SettingsChangeGlobalError::GetBubbleViewMessage() { 117 string16 SettingsChangeGlobalError::GetBubbleViewMessage() {
106 const Change& change = changes_.front(); 118 SettingChange* change = changes_.front();
107 return change.old_setting.empty() ? 119 const string16& old_setting = change->GetOldSetting();
108 l10n_util::GetStringFUTF16(kBubbleMessageOldUnknownIDs[change.type], 120 if (old_setting.empty()) {
109 change.new_setting) : 121 return l10n_util::GetStringFUTF16(
110 l10n_util::GetStringFUTF16(kBubbleMessageIDs[change.type], 122 kBubbleMessageOldUnknownIDs[change->type()],
111 change.old_setting, change.new_setting); 123 change->GetNewSetting());
124 } else {
125 return l10n_util::GetStringFUTF16(
126 kBubbleMessageIDs[change->type()],
127 old_setting,
128 change->GetNewSetting());
129 }
112 } 130 }
113 131
114 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() { 132 string16 SettingsChangeGlobalError::GetBubbleViewAcceptButtonLabel() {
115 const Change& change = changes_.front(); 133 SettingChange* change = changes_.front();
116 return l10n_util::GetStringFUTF16(kBubbleChangeSettingIDs[change.type], 134 return l10n_util::GetStringFUTF16(kBubbleChangeSettingIDs[change->type()],
117 change.new_setting); 135 change->GetNewSetting());
118 } 136 }
119 137
120 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { 138 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() {
121 const Change& change = changes_.front(); 139 SettingChange* change = changes_.front();
122 return change.old_setting.empty() ? 140 string16 old_setting = change->GetOldSetting();
123 l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_OPEN_SETTINGS) : 141 if (old_setting.empty()) {
124 l10n_util::GetStringFUTF16(kBubbleKeepSettingIDs[change.type], 142 return l10n_util::GetStringUTF16(IDS_SETTINGS_CHANGE_OPEN_SETTINGS);
125 change.old_setting); 143 } else {
144 return l10n_util::GetStringFUTF16(
145 kBubbleKeepSettingIDs[change->type()],
146 old_setting);
147 }
126 } 148 }
127 149
128 bool SettingsChangeGlobalError::IsAcceptButtonDefault() { 150 bool SettingsChangeGlobalError::IsAcceptButtonDefault() {
129 return false; 151 return false;
130 } 152 }
131 153
132 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() { 154 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed() {
155 DCHECK(delegate_);
133 VLOG(1) << "Apply changes"; 156 VLOG(1) << "Apply changes";
134 apply_changes_cb_.Run(); 157 delegate_->OnApplyChanges();
135 closed_by_button_ = true; 158 closed_by_button_ = true;
136 } 159 }
137 160
138 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() { 161 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed() {
139 VLOG(1) << "Revert changes"; 162 DCHECK(delegate_);
140 revert_changes_cb_.Run(); 163 VLOG(1) << "Discard changes";
164 delegate_->OnDiscardChanges();
141 closed_by_button_ = true; 165 closed_by_button_ = true;
142 } 166 }
143 167
144 void SettingsChangeGlobalError::RemoveFromProfile() { 168 void SettingsChangeGlobalError::RemoveFromProfile() {
169 DCHECK(delegate_);
145 if (profile_) 170 if (profile_)
146 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); 171 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this);
147 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); 172 if (!closed_by_button_)
173 delegate_->OnDecisionTimeout();
148 } 174 }
149 175
150 void SettingsChangeGlobalError::BubbleViewDidClose() { 176 void SettingsChangeGlobalError::BubbleViewDidClose() {
177 browser_ = NULL;
151 if (!closed_by_button_) { 178 if (!closed_by_button_) {
152 BrowserThread::PostDelayedTask( 179 BrowserThread::PostDelayedTask(
153 BrowserThread::UI, FROM_HERE, 180 BrowserThread::UI, FROM_HERE,
154 base::Bind(&SettingsChangeGlobalError::RemoveFromProfile, 181 base::Bind(&SettingsChangeGlobalError::RemoveFromProfile,
155 weak_factory_.GetWeakPtr()), 182 weak_factory_.GetWeakPtr()),
156 kMenuItemDisplayPeriodMs); 183 kMenuItemDisplayPeriodMs);
157 } else { 184 } else {
158 RemoveFromProfile(); 185 RemoveFromProfile();
159 } 186 }
160 } 187 }
161 188
162 void SettingsChangeGlobalError::ShowForDefaultProfile() { 189 void SettingsChangeGlobalError::ShowForProfile(Profile* profile) {
163 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { 190 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
164 AddToDefaultProfile(); 191 AddToProfile(profile);
165 } else { 192 } else {
166 BrowserThread::PostTask( 193 BrowserThread::PostTask(
167 BrowserThread::UI, FROM_HERE, 194 BrowserThread::UI, FROM_HERE,
168 base::Bind(&SettingsChangeGlobalError::AddToDefaultProfile, 195 base::Bind(&SettingsChangeGlobalError::AddToProfile,
169 base::Unretained(this))); 196 base::Unretained(this),
197 profile));
170 } 198 }
171 } 199 }
172 200
173 void SettingsChangeGlobalError::AddToDefaultProfile() { 201 void SettingsChangeGlobalError::AddToProfile(Profile* profile) {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
175 profile_ = ProfileManager::GetDefaultProfile(); 203 profile_ = profile;
176 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this); 204 GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this);
177 Show(); 205 Show();
178 } 206 }
179 207
180 void SettingsChangeGlobalError::Show() { 208 void SettingsChangeGlobalError::Show() {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
182 DCHECK(profile_); 210 DCHECK(profile_);
183 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); 211 browser_ = BrowserList::GetLastActiveWithProfile(profile_);
184 if (browser) 212 if (browser_)
185 ShowBubbleView(browser); 213 ShowBubbleView(browser_);
186 } 214 }
215
216 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698