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

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

Issue 9500020: ProtectorService supports multiple change instances. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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"
12 #include "chrome/browser/platform_util.h" 11 #include "chrome/browser/platform_util.h"
13 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/protector/base_setting_change.h" 13 #include "chrome/browser/protector/base_setting_change.h"
15 #include "chrome/browser/protector/settings_change_global_error_delegate.h" 14 #include "chrome/browser/protector/settings_change_global_error_delegate.h"
16 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/browser_window.h"
18 #include "chrome/browser/ui/global_error_service.h" 17 #include "chrome/browser/ui/global_error_service.h"
19 #include "chrome/browser/ui/global_error_service_factory.h" 18 #include "chrome/browser/ui/global_error_service_factory.h"
20 #include "content/public/browser/browser_thread.h" 19 #include "content/public/browser/browser_thread.h"
21 20
22 using content::BrowserThread; 21 using content::BrowserThread;
23 22
24 namespace protector { 23 namespace protector {
25 24
26 namespace { 25 namespace {
27 26
28 // Timeout before the global error is removed (wrench menu item disappears). 27 // Timeout before the global error is removed (wrench menu item disappears).
29 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min 28 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min
30 29
31 } // namespace 30 } // namespace
32 31
32 // static
33 std::bitset<IDC_SHOW_SETTINGS_CHANGE_LAST -
34 IDC_SHOW_SETTINGS_CHANGE_FIRST + 1>
35 SettingsChangeGlobalError::menu_ids_;
36
33 SettingsChangeGlobalError::SettingsChangeGlobalError( 37 SettingsChangeGlobalError::SettingsChangeGlobalError(
34 BaseSettingChange* change, 38 BaseSettingChange* change,
35 SettingsChangeGlobalErrorDelegate* delegate) 39 SettingsChangeGlobalErrorDelegate* delegate)
36 : change_(change), 40 : change_(change),
37 delegate_(delegate), 41 delegate_(delegate),
38 profile_(NULL), 42 profile_(NULL),
39 closed_by_button_(false), 43 closed_by_button_(false),
40 show_on_browser_activation_(false), 44 show_on_browser_activation_(false),
41 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { 45 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
46 menu_id_(-1) {
42 DCHECK(delegate_); 47 DCHECK(delegate_);
48 for (int i = IDC_SHOW_SETTINGS_CHANGE_FIRST;
49 i <= IDC_SHOW_SETTINGS_CHANGE_LAST; i++) {
50 if (!menu_ids_[i - IDC_SHOW_SETTINGS_CHANGE_FIRST]) {
51 menu_id_ = i;
52 menu_ids_.set(i - IDC_SHOW_SETTINGS_CHANGE_FIRST);
53 break;
54 }
55 }
56 DCHECK(menu_id_ >= 0) << "Out of command IDs for SettingsChangeGlobalError";
43 } 57 }
44 58
45 SettingsChangeGlobalError::~SettingsChangeGlobalError() { 59 SettingsChangeGlobalError::~SettingsChangeGlobalError() {
60 if (profile_)
61 RemoveFromProfile();
62 if (menu_id_ >= 0)
63 menu_ids_.reset(menu_id_ - IDC_SHOW_SETTINGS_CHANGE_FIRST);
46 } 64 }
47 65
48 bool SettingsChangeGlobalError::HasBadge() { 66 bool SettingsChangeGlobalError::HasBadge() {
49 return true; 67 return true;
50 } 68 }
51 69
52 int SettingsChangeGlobalError::GetBadgeResourceID() { 70 int SettingsChangeGlobalError::GetBadgeResourceID() {
53 return change_->GetBadgeIconID(); 71 return change_->GetBadgeIconID();
54 } 72 }
55 73
56 bool SettingsChangeGlobalError::HasMenuItem() { 74 bool SettingsChangeGlobalError::HasMenuItem() {
57 return true; 75 return true;
58 } 76 }
59 77
60 int SettingsChangeGlobalError::MenuItemCommandID() { 78 int SettingsChangeGlobalError::MenuItemCommandID() {
61 return IDC_SHOW_SETTINGS_CHANGES; 79 return menu_id_;
62 } 80 }
63 81
64 string16 SettingsChangeGlobalError::MenuItemLabel() { 82 string16 SettingsChangeGlobalError::MenuItemLabel() {
65 return change_->GetBubbleTitle(); 83 return change_->GetBubbleTitle();
66 } 84 }
67 85
68 int SettingsChangeGlobalError::MenuItemIconResourceID() { 86 int SettingsChangeGlobalError::MenuItemIconResourceID() {
69 return change_->GetMenuItemIconID(); 87 return change_->GetMenuItemIconID();
70 } 88 }
71 89
(...skipping 26 matching lines...) Expand all
98 return change_->GetDiscardButtonText(); 116 return change_->GetDiscardButtonText();
99 } 117 }
100 118
101 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { 119 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() {
102 return change_->GetApplyButtonText(); 120 return change_->GetApplyButtonText();
103 } 121 }
104 122
105 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed( 123 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed(
106 Browser* browser) { 124 Browser* browser) {
107 closed_by_button_ = true; 125 closed_by_button_ = true;
108 delegate_->OnDiscardChange(browser); 126 delegate_->OnDiscardChange(this, browser);
109 } 127 }
110 128
111 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed( 129 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed(
112 Browser* browser) { 130 Browser* browser) {
113 closed_by_button_ = true; 131 closed_by_button_ = true;
114 delegate_->OnApplyChange(browser); 132 delegate_->OnApplyChange(this, browser);
115 } 133 }
116 134
117 void SettingsChangeGlobalError::OnBrowserSetLastActive( 135 void SettingsChangeGlobalError::OnBrowserSetLastActive(
118 const Browser* browser) { 136 const Browser* browser) {
119 if (show_on_browser_activation_ && browser && browser->is_type_tabbed()) { 137 if (show_on_browser_activation_ && browser && browser->is_type_tabbed()) {
120 // A tabbed browser window got activated, show the error bubble again. 138 // A tabbed browser window got activated, show the error bubble again.
121 // Calling Show() immediately from here does not always work because the 139 // Calling Show() immediately from here does not always work because the
122 // old browser window may still have focus. 140 // old browser window may still have focus.
123 // Multiple posted Show() calls are fine since the first successful one 141 // Multiple posted Show() calls are fine since the first successful one
124 // will invalidate all the weak pointers. 142 // will invalidate all the weak pointers.
125 // Note that Show() will display the bubble in the last active browser 143 // Note that Show() will display the bubble in the last active browser
126 // (which may not be |browser| at the moment Show() is executed). 144 // (which may not be |browser| at the moment Show() is executed).
127 BrowserThread::PostTask( 145 BrowserThread::PostTask(
128 BrowserThread::UI, FROM_HERE, 146 BrowserThread::UI, FROM_HERE,
129 base::Bind(&SettingsChangeGlobalError::Show, 147 base::Bind(&SettingsChangeGlobalError::Show,
130 weak_factory_.GetWeakPtr())); 148 weak_factory_.GetWeakPtr()));
131 } 149 }
132 } 150 }
133 151
134 void SettingsChangeGlobalError::RemoveFromProfile() { 152 void SettingsChangeGlobalError::RemoveFromProfile() {
135 if (profile_) 153 if (profile_) {
136 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); 154 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this);
155 profile_ = NULL;
156 }
137 BrowserList::RemoveObserver(this); 157 BrowserList::RemoveObserver(this);
138 // This will delete |this|. 158 // This will delete |this|.
139 delegate_->OnRemovedFromProfile(); 159 delegate_->OnRemovedFromProfile(this);
140 } 160 }
141 161
142 void SettingsChangeGlobalError::OnBubbleViewDidClose(Browser* browser) { 162 void SettingsChangeGlobalError::OnBubbleViewDidClose(Browser* browser) {
143 if (!closed_by_button_) { 163 if (!closed_by_button_) {
144 BrowserThread::PostDelayedTask( 164 BrowserThread::PostDelayedTask(
145 BrowserThread::UI, FROM_HERE, 165 BrowserThread::UI, FROM_HERE,
146 base::Bind(&SettingsChangeGlobalError::OnInactiveTimeout, 166 base::Bind(&SettingsChangeGlobalError::OnInactiveTimeout,
147 weak_factory_.GetWeakPtr()), 167 weak_factory_.GetWeakPtr()),
148 kMenuItemDisplayPeriodMs); 168 kMenuItemDisplayPeriodMs);
169 #if !defined(TOOLKIT_GTK)
Ivan Korotkov 2012/02/29 16:57:41 Disregard that, please (comes from a different CL)
170 // TODO(ivankr): the logic for redisplaying bubble is disabled on Gtk, see
171 // http://crbug.com/115719.
149 if (browser->window() && 172 if (browser->window() &&
150 !platform_util::IsWindowActive(browser->window()->GetNativeHandle())) { 173 !platform_util::IsWindowActive(browser->window()->GetNativeHandle())) {
151 // Bubble closed because the entire window lost activation, display 174 // Bubble closed because the entire window lost activation, display
152 // again when a window gets active. 175 // again when a window gets active.
153 show_on_browser_activation_ = true; 176 show_on_browser_activation_ = true;
154 } 177 }
178 #endif
155 } else { 179 } else {
156 RemoveFromProfile(); 180 RemoveFromProfile();
157 } 181 }
158 } 182 }
159 183
160 void SettingsChangeGlobalError::ShowForProfile(Profile* profile) { 184 void SettingsChangeGlobalError::ShowForProfile(Profile* profile) {
161 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { 185 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
162 AddToProfile(profile); 186 AddToProfile(profile);
163 } else { 187 } else {
164 BrowserThread::PostTask( 188 BrowserThread::PostTask(
(...skipping 25 matching lines...) Expand all
190 214
191 void SettingsChangeGlobalError::ShowInBrowser(Browser* browser) { 215 void SettingsChangeGlobalError::ShowInBrowser(Browser* browser) {
192 show_on_browser_activation_ = false; 216 show_on_browser_activation_ = false;
193 // Cancel any previously posted tasks so that the global error 217 // Cancel any previously posted tasks so that the global error
194 // does not get removed on timeout while still showing the bubble. 218 // does not get removed on timeout while still showing the bubble.
195 weak_factory_.InvalidateWeakPtrs(); 219 weak_factory_.InvalidateWeakPtrs();
196 ShowBubbleView(browser); 220 ShowBubbleView(browser);
197 } 221 }
198 222
199 void SettingsChangeGlobalError::OnInactiveTimeout() { 223 void SettingsChangeGlobalError::OnInactiveTimeout() {
200 delegate_->OnDecisionTimeout(); 224 delegate_->OnDecisionTimeout(this);
201 RemoveFromProfile(); 225 RemoveFromProfile();
202 } 226 }
203 227
204 } // namespace protector 228 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698