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/managed_mode.h" | 5 #include "chrome/browser/managed_mode.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
9 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/managed_mode_url_filter.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/app_modal_dialogs/app_modal_dialog.h" | |
13 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | |
14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
16 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
23 | 24 |
| 25 using content::BrowserThread; |
| 26 |
| 27 // A bridge from ManagedMode (which lives on the UI thread) to |
| 28 // ManagedModeURLFilter (which lives on the IO thread). |
| 29 class ManagedMode::URLFilterContext { |
| 30 public: |
| 31 URLFilterContext() {} |
| 32 ~URLFilterContext() {} |
| 33 |
| 34 const ManagedModeURLFilter* url_filter() const { |
| 35 return &url_filter_; |
| 36 } |
| 37 |
| 38 void SetActive(bool in_managed_mode) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 40 // Because ManagedMode is a singleton, we can pass the pointer to |
| 41 // |url_filter_| unretained. |
| 42 BrowserThread::PostTask(BrowserThread::IO, |
| 43 FROM_HERE, |
| 44 base::Bind( |
| 45 &ManagedModeURLFilter::SetActive, |
| 46 base::Unretained(&url_filter_), |
| 47 in_managed_mode)); |
| 48 } |
| 49 |
| 50 void ShutdownOnUIThread() { |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 52 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, this); |
| 53 } |
| 54 |
| 55 private: |
| 56 ManagedModeURLFilter url_filter_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(URLFilterContext); |
| 59 }; |
| 60 |
24 // static | 61 // static |
25 ManagedMode* ManagedMode::GetInstance() { | 62 ManagedMode* ManagedMode::GetInstance() { |
26 return Singleton<ManagedMode>::get(); | 63 return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); |
27 } | 64 } |
28 | 65 |
29 // static | 66 // static |
30 void ManagedMode::RegisterPrefs(PrefService* prefs) { | 67 void ManagedMode::RegisterPrefs(PrefService* prefs) { |
31 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); | 68 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); |
32 } | 69 } |
33 | 70 |
34 // static | 71 // static |
35 void ManagedMode::Init(Profile* profile) { | 72 void ManagedMode::Init(Profile* profile) { |
36 GetInstance()->InitImpl(profile); | 73 GetInstance()->InitImpl(profile); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 void ManagedMode::LeaveManagedMode() { | 163 void ManagedMode::LeaveManagedMode() { |
127 GetInstance()->LeaveManagedModeImpl(); | 164 GetInstance()->LeaveManagedModeImpl(); |
128 } | 165 } |
129 | 166 |
130 void ManagedMode::LeaveManagedModeImpl() { | 167 void ManagedMode::LeaveManagedModeImpl() { |
131 bool confirmed = PlatformConfirmLeave(); | 168 bool confirmed = PlatformConfirmLeave(); |
132 if (confirmed) | 169 if (confirmed) |
133 SetInManagedMode(NULL); | 170 SetInManagedMode(NULL); |
134 } | 171 } |
135 | 172 |
| 173 // static |
| 174 const ManagedModeURLFilter* ManagedMode::GetURLFilter() { |
| 175 return GetInstance()->GetURLFilterImpl(); |
| 176 } |
| 177 |
| 178 const ManagedModeURLFilter* ManagedMode::GetURLFilterImpl() { |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 180 return url_filter_context_->url_filter(); |
| 181 } |
| 182 |
136 std::string ManagedMode::GetDebugPolicyProviderName() const { | 183 std::string ManagedMode::GetDebugPolicyProviderName() const { |
137 // Save the string space in official builds. | 184 // Save the string space in official builds. |
138 #ifdef NDEBUG | 185 #ifdef NDEBUG |
139 NOTREACHED(); | 186 NOTREACHED(); |
140 return std::string(); | 187 return std::string(); |
141 #else | 188 #else |
142 return "Managed Mode"; | 189 return "Managed Mode"; |
143 #endif | 190 #endif |
144 } | 191 } |
145 | 192 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 if (browser->profile()->GetOriginalProfile() == managed_profile_) { | 228 if (browser->profile()->GetOriginalProfile() == managed_profile_) { |
182 // Ignore closing browser windows that are in managed mode. | 229 // Ignore closing browser windows that are in managed mode. |
183 return; | 230 return; |
184 } | 231 } |
185 size_t count = browsers_to_close_.erase(browser); | 232 size_t count = browsers_to_close_.erase(browser); |
186 DCHECK_EQ(1u, count); | 233 DCHECK_EQ(1u, count); |
187 if (browsers_to_close_.empty()) | 234 if (browsers_to_close_.empty()) |
188 FinalizeEnter(true); | 235 FinalizeEnter(true); |
189 } | 236 } |
190 | 237 |
191 ManagedMode::ManagedMode() : managed_profile_(NULL) { | 238 ManagedMode::ManagedMode() : managed_profile_(NULL), |
| 239 url_filter_context_(new URLFilterContext) { |
192 BrowserList::AddObserver(this); | 240 BrowserList::AddObserver(this); |
193 } | 241 } |
194 | 242 |
195 ManagedMode::~ManagedMode() { | 243 ManagedMode::~ManagedMode() { |
| 244 // This class usually is a leaky singleton, so this destructor shouldn't be |
| 245 // called. We still do some cleanup, in case we're owned by a unit test. |
196 BrowserList::RemoveObserver(this); | 246 BrowserList::RemoveObserver(this); |
197 DCHECK_EQ(0u, callbacks_.size()); | 247 DCHECK_EQ(0u, callbacks_.size()); |
198 DCHECK_EQ(0u, browsers_to_close_.size()); | 248 DCHECK_EQ(0u, browsers_to_close_.size()); |
| 249 url_filter_context_.release()->ShutdownOnUIThread(); |
199 } | 250 } |
200 | 251 |
201 void ManagedMode::Observe(int type, | 252 void ManagedMode::Observe(int type, |
202 const content::NotificationSource& source, | 253 const content::NotificationSource& source, |
203 const content::NotificationDetails& details) { | 254 const content::NotificationDetails& details) { |
204 // Return early if we don't have any queued callbacks. | 255 // Return early if we don't have any queued callbacks. |
205 if (callbacks_.empty()) | 256 if (callbacks_.empty()) |
206 return; | 257 return; |
207 | 258 |
208 switch (type) { | 259 switch (type) { |
209 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { | 260 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
210 FinalizeEnter(false); | 261 FinalizeEnter(false); |
211 return; | 262 return; |
212 } | 263 } |
213 case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: { | 264 case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: { |
214 Browser* browser = content::Source<Browser>(source).ptr(); | 265 Browser* browser = content::Source<Browser>(source).ptr(); |
215 if (browsers_to_close_.find(browser) != browsers_to_close_.end()) | 266 if (browsers_to_close_.find(browser) != browsers_to_close_.end()) |
216 FinalizeEnter(false); | 267 FinalizeEnter(false); |
217 return; | 268 return; |
218 } | 269 } |
219 default: { | 270 case chrome::NOTIFICATION_EXTENSION_LOADED: |
220 NOTREACHED(); | 271 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 272 if (managed_profile_) |
| 273 UpdateWhitelist(); |
221 break; | 274 break; |
222 } | 275 } |
| 276 default: |
| 277 NOTREACHED(); |
223 } | 278 } |
224 } | 279 } |
225 | 280 |
226 void ManagedMode::FinalizeEnter(bool result) { | 281 void ManagedMode::FinalizeEnter(bool result) { |
227 if (result) | 282 if (result) |
228 SetInManagedMode(managed_profile_); | 283 SetInManagedMode(managed_profile_); |
229 for (std::vector<EnterCallback>::iterator it = callbacks_.begin(); | 284 for (std::vector<EnterCallback>::iterator it = callbacks_.begin(); |
230 it != callbacks_.end(); ++it) { | 285 it != callbacks_.end(); ++it) { |
231 it->Run(result); | 286 it->Run(result); |
232 } | 287 } |
233 callbacks_.clear(); | 288 callbacks_.clear(); |
234 browsers_to_close_.clear(); | 289 browsers_to_close_.clear(); |
235 registrar_.RemoveAll(); | 290 registrar_.RemoveAll(); |
236 } | 291 } |
237 | 292 |
238 bool ManagedMode::PlatformConfirmEnter() { | 293 bool ManagedMode::PlatformConfirmEnter() { |
239 // TODO(bauerb): Show platform-specific confirmation dialog. | 294 // TODO(bauerb): Show platform-specific confirmation dialog. |
240 return true; | 295 return true; |
241 } | 296 } |
242 | 297 |
243 bool ManagedMode::PlatformConfirmLeave() { | 298 bool ManagedMode::PlatformConfirmLeave() { |
244 // TODO(bauerb): Show platform-specific confirmation dialog. | 299 // TODO(bauerb): Show platform-specific confirmation dialog. |
245 return true; | 300 return true; |
246 } | 301 } |
247 | 302 |
248 void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { | 303 void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { |
249 // Register the ManagementPolicy::Provider before changing the pref when | 304 // Register the ManagementPolicy::Provider before changing the pref when |
250 // setting it, and unregister it after changing the pref when clearing it, | 305 // setting it, and unregister it after changing the pref when clearing it, |
251 // so pref observers see the correct ManagedMode state. | 306 // so pref observers see the correct ManagedMode state. |
252 if (newly_managed_profile) { | 307 bool in_managed_mode = !!newly_managed_profile; |
| 308 if (in_managed_mode) { |
253 DCHECK(!managed_profile_ || managed_profile_ == newly_managed_profile); | 309 DCHECK(!managed_profile_ || managed_profile_ == newly_managed_profile); |
254 extensions::ExtensionSystem::Get( | 310 extensions::ExtensionSystem::Get( |
255 newly_managed_profile)->management_policy()->RegisterProvider(this); | 311 newly_managed_profile)->management_policy()->RegisterProvider(this); |
256 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, true); | 312 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 313 content::Source<Profile>(newly_managed_profile)); |
| 314 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 315 content::Source<Profile>(newly_managed_profile)); |
257 } else { | 316 } else { |
258 extensions::ExtensionSystem::Get( | 317 extensions::ExtensionSystem::Get( |
259 managed_profile_)->management_policy()->UnregisterProvider(this); | 318 managed_profile_)->management_policy()->UnregisterProvider(this); |
260 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, false); | 319 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 320 content::Source<Profile>(managed_profile_)); |
| 321 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 322 content::Source<Profile>(managed_profile_)); |
261 } | 323 } |
| 324 |
262 managed_profile_ = newly_managed_profile; | 325 managed_profile_ = newly_managed_profile; |
| 326 url_filter_context_->SetActive(in_managed_mode); |
| 327 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, |
| 328 in_managed_mode); |
| 329 if (in_managed_mode) |
| 330 UpdateWhitelist(); |
263 | 331 |
264 // This causes the avatar and the profile menu to get updated. | 332 // This causes the avatar and the profile menu to get updated. |
265 content::NotificationService::current()->Notify( | 333 content::NotificationService::current()->Notify( |
266 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 334 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
267 content::NotificationService::AllBrowserContextsAndSources(), | 335 content::NotificationService::AllBrowserContextsAndSources(), |
268 content::NotificationService::NoDetails()); | 336 content::NotificationService::NoDetails()); |
269 } | 337 } |
| 338 |
| 339 void ManagedMode::UpdateWhitelist() { |
| 340 DCHECK(managed_profile_); |
| 341 // TODO(bauerb): Update URL filter with whitelist. |
| 342 } |
OLD | NEW |