OLD | NEW |
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 <bitset> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
10 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
11 #include "chrome/app/chrome_command_ids.h" | |
12 #include "chrome/browser/platform_util.h" | 14 #include "chrome/browser/platform_util.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/protector/base_setting_change.h" | 16 #include "chrome/browser/protector/base_setting_change.h" |
15 #include "chrome/browser/protector/settings_change_global_error_delegate.h" | 17 #include "chrome/browser/protector/settings_change_global_error_delegate.h" |
16 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
18 #include "chrome/browser/ui/global_error_service.h" | 20 #include "chrome/browser/ui/global_error_service.h" |
19 #include "chrome/browser/ui/global_error_service_factory.h" | 21 #include "chrome/browser/ui/global_error_service_factory.h" |
20 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
21 | 23 |
22 using content::BrowserThread; | 24 using content::BrowserThread; |
23 | 25 |
24 namespace protector { | 26 namespace protector { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 // Timeout before the global error is removed (wrench menu item disappears). | 30 // Timeout before the global error is removed (wrench menu item disappears). |
29 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min | 31 const int kMenuItemDisplayPeriodMs = 10*60*1000; // 10 min |
30 | 32 |
| 33 // Unset bits indicate available command IDs. |
| 34 static base::LazyInstance< |
| 35 std::bitset<IDC_SHOW_SETTINGS_CHANGE_LAST - |
| 36 IDC_SHOW_SETTINGS_CHANGE_FIRST + 1> > menu_ids = |
| 37 LAZY_INSTANCE_INITIALIZER; |
| 38 |
31 } // namespace | 39 } // namespace |
32 | 40 |
33 SettingsChangeGlobalError::SettingsChangeGlobalError( | 41 SettingsChangeGlobalError::SettingsChangeGlobalError( |
34 BaseSettingChange* change, | 42 BaseSettingChange* change, |
35 SettingsChangeGlobalErrorDelegate* delegate) | 43 SettingsChangeGlobalErrorDelegate* delegate) |
36 : change_(change), | 44 : change_(change), |
37 delegate_(delegate), | 45 delegate_(delegate), |
38 profile_(NULL), | 46 profile_(NULL), |
39 closed_by_button_(false), | 47 closed_by_button_(false), |
40 show_on_browser_activation_(false), | 48 show_on_browser_activation_(false), |
41 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 49 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 50 menu_id_(-1) { |
42 DCHECK(delegate_); | 51 DCHECK(delegate_); |
| 52 for (int i = IDC_SHOW_SETTINGS_CHANGE_FIRST; |
| 53 i <= IDC_SHOW_SETTINGS_CHANGE_LAST; i++) { |
| 54 if (!menu_ids.Get()[i - IDC_SHOW_SETTINGS_CHANGE_FIRST]) { |
| 55 menu_id_ = i; |
| 56 menu_ids.Get().set(i - IDC_SHOW_SETTINGS_CHANGE_FIRST); |
| 57 break; |
| 58 } |
| 59 } |
| 60 DCHECK(menu_id_ >= 0) << "Out of command IDs for SettingsChangeGlobalError"; |
43 } | 61 } |
44 | 62 |
45 SettingsChangeGlobalError::~SettingsChangeGlobalError() { | 63 SettingsChangeGlobalError::~SettingsChangeGlobalError() { |
| 64 if (profile_) |
| 65 RemoveFromProfile(); |
| 66 if (menu_id_ >= 0) |
| 67 menu_ids.Get().reset(menu_id_ - IDC_SHOW_SETTINGS_CHANGE_FIRST); |
46 } | 68 } |
47 | 69 |
48 bool SettingsChangeGlobalError::HasBadge() { | 70 bool SettingsChangeGlobalError::HasBadge() { |
49 return true; | 71 return true; |
50 } | 72 } |
51 | 73 |
52 int SettingsChangeGlobalError::GetBadgeResourceID() { | 74 int SettingsChangeGlobalError::GetBadgeResourceID() { |
53 return change_->GetBadgeIconID(); | 75 return change_->GetBadgeIconID(); |
54 } | 76 } |
55 | 77 |
56 bool SettingsChangeGlobalError::HasMenuItem() { | 78 bool SettingsChangeGlobalError::HasMenuItem() { |
57 return true; | 79 return true; |
58 } | 80 } |
59 | 81 |
60 int SettingsChangeGlobalError::MenuItemCommandID() { | 82 int SettingsChangeGlobalError::MenuItemCommandID() { |
61 return IDC_SHOW_SETTINGS_CHANGES; | 83 return menu_id_; |
62 } | 84 } |
63 | 85 |
64 string16 SettingsChangeGlobalError::MenuItemLabel() { | 86 string16 SettingsChangeGlobalError::MenuItemLabel() { |
65 return change_->GetBubbleTitle(); | 87 return change_->GetBubbleTitle(); |
66 } | 88 } |
67 | 89 |
68 int SettingsChangeGlobalError::MenuItemIconResourceID() { | 90 int SettingsChangeGlobalError::MenuItemIconResourceID() { |
69 return change_->GetMenuItemIconID(); | 91 return change_->GetMenuItemIconID(); |
70 } | 92 } |
71 | 93 |
(...skipping 26 matching lines...) Expand all Loading... |
98 return change_->GetDiscardButtonText(); | 120 return change_->GetDiscardButtonText(); |
99 } | 121 } |
100 | 122 |
101 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { | 123 string16 SettingsChangeGlobalError::GetBubbleViewCancelButtonLabel() { |
102 return change_->GetApplyButtonText(); | 124 return change_->GetApplyButtonText(); |
103 } | 125 } |
104 | 126 |
105 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed( | 127 void SettingsChangeGlobalError::BubbleViewAcceptButtonPressed( |
106 Browser* browser) { | 128 Browser* browser) { |
107 closed_by_button_ = true; | 129 closed_by_button_ = true; |
108 delegate_->OnDiscardChange(browser); | 130 delegate_->OnDiscardChange(this, browser); |
109 } | 131 } |
110 | 132 |
111 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed( | 133 void SettingsChangeGlobalError::BubbleViewCancelButtonPressed( |
112 Browser* browser) { | 134 Browser* browser) { |
113 closed_by_button_ = true; | 135 closed_by_button_ = true; |
114 delegate_->OnApplyChange(browser); | 136 delegate_->OnApplyChange(this, browser); |
115 } | 137 } |
116 | 138 |
117 void SettingsChangeGlobalError::OnBrowserSetLastActive( | 139 void SettingsChangeGlobalError::OnBrowserSetLastActive( |
118 const Browser* browser) { | 140 const Browser* browser) { |
119 if (show_on_browser_activation_ && browser && browser->is_type_tabbed()) { | 141 if (show_on_browser_activation_ && browser && browser->is_type_tabbed()) { |
120 // A tabbed browser window got activated, show the error bubble again. | 142 // A tabbed browser window got activated, show the error bubble again. |
121 // Calling Show() immediately from here does not always work because the | 143 // Calling Show() immediately from here does not always work because the |
122 // old browser window may still have focus. | 144 // old browser window may still have focus. |
123 // Multiple posted Show() calls are fine since the first successful one | 145 // Multiple posted Show() calls are fine since the first successful one |
124 // will invalidate all the weak pointers. | 146 // will invalidate all the weak pointers. |
125 // Note that Show() will display the bubble in the last active browser | 147 // Note that Show() will display the bubble in the last active browser |
126 // (which may not be |browser| at the moment Show() is executed). | 148 // (which may not be |browser| at the moment Show() is executed). |
127 BrowserThread::PostTask( | 149 BrowserThread::PostTask( |
128 BrowserThread::UI, FROM_HERE, | 150 BrowserThread::UI, FROM_HERE, |
129 base::Bind(&SettingsChangeGlobalError::Show, | 151 base::Bind(&SettingsChangeGlobalError::Show, |
130 weak_factory_.GetWeakPtr())); | 152 weak_factory_.GetWeakPtr())); |
131 } | 153 } |
132 } | 154 } |
133 | 155 |
134 void SettingsChangeGlobalError::RemoveFromProfile() { | 156 void SettingsChangeGlobalError::RemoveFromProfile() { |
135 if (profile_) | 157 if (profile_) { |
136 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); | 158 GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this); |
| 159 profile_ = NULL; |
| 160 } |
137 BrowserList::RemoveObserver(this); | 161 BrowserList::RemoveObserver(this); |
138 // This will delete |this|. | 162 // This will delete |this|. |
139 delegate_->OnRemovedFromProfile(); | 163 delegate_->OnRemovedFromProfile(this); |
140 } | 164 } |
141 | 165 |
142 void SettingsChangeGlobalError::OnBubbleViewDidClose(Browser* browser) { | 166 void SettingsChangeGlobalError::OnBubbleViewDidClose(Browser* browser) { |
143 if (!closed_by_button_) { | 167 if (!closed_by_button_) { |
144 BrowserThread::PostDelayedTask( | 168 BrowserThread::PostDelayedTask( |
145 BrowserThread::UI, FROM_HERE, | 169 BrowserThread::UI, FROM_HERE, |
146 base::Bind(&SettingsChangeGlobalError::OnInactiveTimeout, | 170 base::Bind(&SettingsChangeGlobalError::OnInactiveTimeout, |
147 weak_factory_.GetWeakPtr()), | 171 weak_factory_.GetWeakPtr()), |
148 kMenuItemDisplayPeriodMs); | 172 kMenuItemDisplayPeriodMs); |
149 #if !defined(TOOLKIT_GTK) | 173 #if !defined(TOOLKIT_GTK) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 | 218 |
195 void SettingsChangeGlobalError::ShowInBrowser(Browser* browser) { | 219 void SettingsChangeGlobalError::ShowInBrowser(Browser* browser) { |
196 show_on_browser_activation_ = false; | 220 show_on_browser_activation_ = false; |
197 // Cancel any previously posted tasks so that the global error | 221 // Cancel any previously posted tasks so that the global error |
198 // does not get removed on timeout while still showing the bubble. | 222 // does not get removed on timeout while still showing the bubble. |
199 weak_factory_.InvalidateWeakPtrs(); | 223 weak_factory_.InvalidateWeakPtrs(); |
200 ShowBubbleView(browser); | 224 ShowBubbleView(browser); |
201 } | 225 } |
202 | 226 |
203 void SettingsChangeGlobalError::OnInactiveTimeout() { | 227 void SettingsChangeGlobalError::OnInactiveTimeout() { |
204 delegate_->OnDecisionTimeout(); | 228 delegate_->OnDecisionTimeout(this); |
205 RemoveFromProfile(); | 229 RemoveFromProfile(); |
206 } | 230 } |
207 | 231 |
208 } // namespace protector | 232 } // namespace protector |
OLD | NEW |