| 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/managed_mode.h" | 5 #include "chrome/browser/managed_mode/managed_mode.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/prefs/public/pref_change_registrar.h" | 8 #include "base/prefs/public/pref_change_registrar.h" |
| 9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/extensions/extension_system.h" | |
| 13 #include "chrome/browser/managed_mode/managed_mode_site_list.h" | 11 #include "chrome/browser/managed_mode/managed_mode_site_list.h" |
| 14 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 12 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
| 15 #include "chrome/browser/policy/url_blacklist_manager.h" | 13 #include "chrome/browser/policy/url_blacklist_manager.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 20 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
| 21 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
| 22 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
| 23 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/extensions/extension_set.h" | |
| 25 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 26 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 27 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
| 28 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | 25 |
| 31 using content::BrowserThread; | 26 using content::BrowserThread; |
| 32 | 27 |
| 33 // A bridge from ManagedMode (which lives on the UI thread) to | |
| 34 // ManagedModeURLFilter (which might live on a different thread). | |
| 35 class ManagedMode::URLFilterContext { | |
| 36 public: | |
| 37 explicit URLFilterContext( | |
| 38 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
| 39 : task_runner_(task_runner) {} | |
| 40 ~URLFilterContext() {} | |
| 41 | |
| 42 ManagedModeURLFilter* url_filter() { | |
| 43 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 44 return &url_filter_; | |
| 45 } | |
| 46 | |
| 47 void SetDefaultFilteringBehavior( | |
| 48 ManagedModeURLFilter::FilteringBehavior behavior) { | |
| 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 50 // Because ManagedMode is a singleton, we can pass the pointer to | |
| 51 // |url_filter_| unretained. | |
| 52 task_runner_->PostTask( | |
| 53 FROM_HERE, | |
| 54 base::Bind(&ManagedModeURLFilter::SetDefaultFilteringBehavior, | |
| 55 base::Unretained(&url_filter_), | |
| 56 behavior)); | |
| 57 } | |
| 58 | |
| 59 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 61 task_runner_->PostTask(FROM_HERE, | |
| 62 base::Bind(&ManagedModeURLFilter::LoadWhitelists, | |
| 63 base::Unretained(&url_filter_), | |
| 64 base::Passed(&site_lists))); | |
| 65 } | |
| 66 | |
| 67 void SetManualLists(scoped_ptr<ListValue> whitelist, | |
| 68 scoped_ptr<ListValue> blacklist) { | |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 70 task_runner_->PostTask(FROM_HERE, | |
| 71 base::Bind(&ManagedModeURLFilter::SetManualLists, | |
| 72 base::Unretained(&url_filter_), | |
| 73 base::Passed(&whitelist), | |
| 74 base::Passed(&blacklist))); | |
| 75 } | |
| 76 | |
| 77 void AddURLPatternToManualList(bool is_whitelist, | |
| 78 const std::string& url) { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 80 task_runner_->PostTask(FROM_HERE, | |
| 81 base::Bind(&ManagedModeURLFilter::AddURLPatternToManualList, | |
| 82 base::Unretained(&url_filter_), | |
| 83 is_whitelist, | |
| 84 url)); | |
| 85 } | |
| 86 | |
| 87 void ShutdownOnUIThread() { | |
| 88 if (task_runner_->RunsTasksOnCurrentThread()) { | |
| 89 delete this; | |
| 90 return; | |
| 91 } | |
| 92 bool result = task_runner_->DeleteSoon(FROM_HERE, this); | |
| 93 DCHECK(result); | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 ManagedModeURLFilter url_filter_; | |
| 98 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(URLFilterContext); | |
| 101 }; | |
| 102 | |
| 103 // static | 28 // static |
| 104 ManagedMode* ManagedMode::GetInstance() { | 29 ManagedMode* ManagedMode::GetInstance() { |
| 105 return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); | 30 return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); |
| 106 } | 31 } |
| 107 | 32 |
| 108 // static | 33 // static |
| 109 void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) { | 34 void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) { |
| 110 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); | 35 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); |
| 111 } | 36 } |
| 112 | 37 |
| 113 // static | 38 // static |
| 114 void ManagedMode::RegisterUserPrefs(PrefServiceSyncable* prefs) { | |
| 115 prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior, | |
| 116 2, | |
| 117 PrefServiceSyncable::UNSYNCABLE_PREF); | |
| 118 prefs->RegisterListPref(prefs::kManagedModeWhitelist, | |
| 119 PrefServiceSyncable::UNSYNCABLE_PREF); | |
| 120 prefs->RegisterListPref(prefs::kManagedModeBlacklist, | |
| 121 PrefServiceSyncable::UNSYNCABLE_PREF); | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 void ManagedMode::Init(Profile* profile) { | 39 void ManagedMode::Init(Profile* profile) { |
| 126 GetInstance()->InitImpl(profile); | 40 GetInstance()->InitImpl(profile); |
| 127 } | 41 } |
| 128 | 42 |
| 129 void ManagedMode::InitImpl(Profile* profile) { | 43 void ManagedMode::InitImpl(Profile* profile) { |
| 130 DCHECK(g_browser_process); | 44 DCHECK(g_browser_process); |
| 131 DCHECK(g_browser_process->local_state()); | 45 DCHECK(g_browser_process->local_state()); |
| 132 | 46 |
| 133 Profile* original_profile = profile->GetOriginalProfile(); | 47 Profile* original_profile = profile->GetOriginalProfile(); |
| 134 // Set the value directly in the PrefService instead of using | 48 // Set the value directly in the PrefService instead of using |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 void ManagedMode::LeaveManagedMode() { | 130 void ManagedMode::LeaveManagedMode() { |
| 217 GetInstance()->LeaveManagedModeImpl(); | 131 GetInstance()->LeaveManagedModeImpl(); |
| 218 } | 132 } |
| 219 | 133 |
| 220 void ManagedMode::LeaveManagedModeImpl() { | 134 void ManagedMode::LeaveManagedModeImpl() { |
| 221 bool confirmed = PlatformConfirmLeave(); | 135 bool confirmed = PlatformConfirmLeave(); |
| 222 if (confirmed) | 136 if (confirmed) |
| 223 SetInManagedMode(NULL); | 137 SetInManagedMode(NULL); |
| 224 } | 138 } |
| 225 | 139 |
| 226 // static | |
| 227 const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThread() { | |
| 228 return GetInstance()->GetURLFilterForIOThreadImpl(); | |
| 229 } | |
| 230 | |
| 231 // static | |
| 232 const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThread() { | |
| 233 return GetInstance()->GetURLFilterForUIThreadImpl(); | |
| 234 } | |
| 235 | |
| 236 ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThreadImpl() { | |
| 237 return io_url_filter_context_->url_filter(); | |
| 238 } | |
| 239 | |
| 240 ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThreadImpl() { | |
| 241 return ui_url_filter_context_->url_filter(); | |
| 242 } | |
| 243 | |
| 244 // static | |
| 245 void ManagedMode::AddToManualList(bool is_whitelist, | |
| 246 const base::ListValue& list) { | |
| 247 GetInstance()->AddToManualListImpl(is_whitelist, list); | |
| 248 } | |
| 249 | |
| 250 void ManagedMode::AddToManualListImpl(bool is_whitelist, | |
| 251 const base::ListValue& list) { | |
| 252 if (!managed_profile_) | |
| 253 return; | |
| 254 | |
| 255 ListPrefUpdate pref_update(managed_profile_->GetPrefs(), | |
| 256 is_whitelist ? prefs::kManagedModeWhitelist : | |
| 257 prefs::kManagedModeBlacklist); | |
| 258 ListValue* pref_list = pref_update.Get(); | |
| 259 | |
| 260 for (size_t i = 0; i < list.GetSize(); ++i) { | |
| 261 std::string url_pattern; | |
| 262 list.GetString(i, &url_pattern); | |
| 263 | |
| 264 if (!IsInManualList(is_whitelist, url_pattern)) { | |
| 265 pref_list->AppendString(url_pattern); | |
| 266 AddURLPatternToManualList(is_whitelist, url_pattern); | |
| 267 } | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 // static | |
| 272 void ManagedMode::RemoveFromManualList(bool is_whitelist, | |
| 273 const base::ListValue& list) { | |
| 274 GetInstance()->RemoveFromManualListImpl(is_whitelist, list); | |
| 275 } | |
| 276 | |
| 277 void ManagedMode::RemoveFromManualListImpl(bool is_whitelist, | |
| 278 const base::ListValue& list) { | |
| 279 ListPrefUpdate pref_update(managed_profile_->GetPrefs(), | |
| 280 is_whitelist ? prefs::kManagedModeWhitelist : | |
| 281 prefs::kManagedModeBlacklist); | |
| 282 ListValue* pref_list = pref_update.Get(); | |
| 283 | |
| 284 for (size_t i = 0; i < list.GetSize(); ++i) { | |
| 285 std::string pattern; | |
| 286 size_t out_index; | |
| 287 list.GetString(i, &pattern); | |
| 288 StringValue value_to_remove(pattern); | |
| 289 | |
| 290 pref_list->Remove(value_to_remove, &out_index); | |
| 291 } | |
| 292 } | |
| 293 | |
| 294 // static | |
| 295 bool ManagedMode::IsInManualList(bool is_whitelist, | |
| 296 const std::string& url_pattern) { | |
| 297 return GetInstance()->IsInManualListImpl(is_whitelist, url_pattern); | |
| 298 } | |
| 299 | |
| 300 bool ManagedMode::IsInManualListImpl(bool is_whitelist, | |
| 301 const std::string& url_pattern) { | |
| 302 StringValue pattern(url_pattern); | |
| 303 const ListValue* list = managed_profile_->GetPrefs()->GetList( | |
| 304 is_whitelist ? prefs::kManagedModeWhitelist : | |
| 305 prefs::kManagedModeBlacklist); | |
| 306 return list->Find(pattern) != list->end(); | |
| 307 } | |
| 308 | |
| 309 // static | |
| 310 scoped_ptr<base::ListValue> ManagedMode::GetBlacklist() { | |
| 311 return scoped_ptr<base::ListValue>( | |
| 312 GetInstance()->managed_profile_->GetPrefs()->GetList( | |
| 313 prefs::kManagedModeBlacklist)->DeepCopy()).Pass(); | |
| 314 } | |
| 315 | |
| 316 std::string ManagedMode::GetDebugPolicyProviderName() const { | |
| 317 // Save the string space in official builds. | |
| 318 #ifdef NDEBUG | |
| 319 NOTREACHED(); | |
| 320 return std::string(); | |
| 321 #else | |
| 322 return "Managed Mode"; | |
| 323 #endif | |
| 324 } | |
| 325 | |
| 326 bool ManagedMode::UserMayLoad(const extensions::Extension* extension, | |
| 327 string16* error) const { | |
| 328 string16 tmp_error; | |
| 329 if (ExtensionManagementPolicyImpl(&tmp_error)) | |
| 330 return true; | |
| 331 | |
| 332 // If the extension is already loaded, we allow it, otherwise we'd unload | |
| 333 // all existing extensions. | |
| 334 ExtensionService* extension_service = | |
| 335 extensions::ExtensionSystem::Get(managed_profile_)->extension_service(); | |
| 336 | |
| 337 // |extension_service| can be NULL in a unit test. | |
| 338 if (extension_service && | |
| 339 extension_service->GetInstalledExtension(extension->id())) | |
| 340 return true; | |
| 341 | |
| 342 if (error) | |
| 343 *error = tmp_error; | |
| 344 return false; | |
| 345 } | |
| 346 | |
| 347 bool ManagedMode::UserMayModifySettings(const extensions::Extension* extension, | |
| 348 string16* error) const { | |
| 349 return ExtensionManagementPolicyImpl(error); | |
| 350 } | |
| 351 | |
| 352 bool ManagedMode::ExtensionManagementPolicyImpl(string16* error) const { | |
| 353 if (!IsInManagedModeImpl()) | |
| 354 return true; | |
| 355 | |
| 356 if (error) | |
| 357 *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_MANAGED_MODE); | |
| 358 return false; | |
| 359 } | |
| 360 | |
| 361 void ManagedMode::OnBrowserAdded(Browser* browser) { | 140 void ManagedMode::OnBrowserAdded(Browser* browser) { |
| 362 // Return early if we don't have any queued callbacks. | 141 // Return early if we don't have any queued callbacks. |
| 363 if (callbacks_.empty()) | 142 if (callbacks_.empty()) |
| 364 return; | 143 return; |
| 365 | 144 |
| 366 DCHECK(managed_profile_); | 145 DCHECK(managed_profile_); |
| 367 if (browser->profile()->GetOriginalProfile() != managed_profile_) | 146 if (browser->profile()->GetOriginalProfile() != managed_profile_) |
| 368 FinalizeEnter(false); | 147 FinalizeEnter(false); |
| 369 } | 148 } |
| 370 | 149 |
| 371 void ManagedMode::OnBrowserRemoved(Browser* browser) { | 150 void ManagedMode::OnBrowserRemoved(Browser* browser) { |
| 372 // Return early if we don't have any queued callbacks. | 151 // Return early if we don't have any queued callbacks. |
| 373 if (callbacks_.empty()) | 152 if (callbacks_.empty()) |
| 374 return; | 153 return; |
| 375 | 154 |
| 376 DCHECK(managed_profile_); | 155 DCHECK(managed_profile_); |
| 377 if (browser->profile()->GetOriginalProfile() == managed_profile_) { | 156 if (browser->profile()->GetOriginalProfile() == managed_profile_) { |
| 378 // Ignore closing browser windows that are in managed mode. | 157 // Ignore closing browser windows that are in managed mode. |
| 379 return; | 158 return; |
| 380 } | 159 } |
| 381 size_t count = browsers_to_close_.erase(browser); | 160 size_t count = browsers_to_close_.erase(browser); |
| 382 DCHECK_EQ(1u, count); | 161 DCHECK_EQ(1u, count); |
| 383 if (browsers_to_close_.empty()) | 162 if (browsers_to_close_.empty()) |
| 384 FinalizeEnter(true); | 163 FinalizeEnter(true); |
| 385 } | 164 } |
| 386 | 165 |
| 387 ManagedMode::ManagedMode() | 166 ManagedMode::ManagedMode() : managed_profile_(NULL) { |
| 388 : managed_profile_(NULL), | |
| 389 io_url_filter_context_( | |
| 390 new URLFilterContext( | |
| 391 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), | |
| 392 ui_url_filter_context_( | |
| 393 new URLFilterContext( | |
| 394 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI))) { | |
| 395 BrowserList::AddObserver(this); | 167 BrowserList::AddObserver(this); |
| 396 } | 168 } |
| 397 | 169 |
| 398 ManagedMode::~ManagedMode() { | 170 ManagedMode::~ManagedMode() { |
| 399 // This class usually is a leaky singleton, so this destructor shouldn't be | 171 // This class usually is a leaky singleton, so this destructor shouldn't be |
| 400 // called. We still do some cleanup, in case we're owned by a unit test. | 172 // called. We still do some cleanup, in case we're owned by a unit test. |
| 401 BrowserList::RemoveObserver(this); | 173 BrowserList::RemoveObserver(this); |
| 402 DCHECK_EQ(0u, callbacks_.size()); | 174 DCHECK_EQ(0u, callbacks_.size()); |
| 403 DCHECK_EQ(0u, browsers_to_close_.size()); | 175 DCHECK_EQ(0u, browsers_to_close_.size()); |
| 404 io_url_filter_context_.release()->ShutdownOnUIThread(); | |
| 405 ui_url_filter_context_.release()->ShutdownOnUIThread(); | |
| 406 } | 176 } |
| 407 | 177 |
| 408 void ManagedMode::Observe(int type, | 178 void ManagedMode::Observe(int type, |
| 409 const content::NotificationSource& source, | 179 const content::NotificationSource& source, |
| 410 const content::NotificationDetails& details) { | 180 const content::NotificationDetails& details) { |
| 181 // Return early if we don't have any queued callbacks. |
| 182 if (callbacks_.empty()) |
| 183 return; |
| 184 |
| 411 switch (type) { | 185 switch (type) { |
| 412 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { | 186 case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
| 413 if (!callbacks_.empty()) | 187 FinalizeEnter(false); |
| 414 FinalizeEnter(false); | |
| 415 return; | 188 return; |
| 416 } | 189 } |
| 417 case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: { | 190 case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: { |
| 418 Browser* browser = content::Source<Browser>(source).ptr(); | 191 Browser* browser = content::Source<Browser>(source).ptr(); |
| 419 if (!callbacks_.empty() && browsers_to_close_.find(browser) != | 192 if (browsers_to_close_.find(browser) != browsers_to_close_.end()) |
| 420 browsers_to_close_.end()) | |
| 421 FinalizeEnter(false); | 193 FinalizeEnter(false); |
| 422 return; | 194 return; |
| 423 } | 195 } |
| 424 case chrome::NOTIFICATION_EXTENSION_LOADED: | |
| 425 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | |
| 426 if (!managed_profile_) | |
| 427 break; | |
| 428 | |
| 429 const extensions::Extension* extension = | |
| 430 content::Details<const extensions::Extension>(details).ptr(); | |
| 431 if (!extension->GetContentPackSiteList().empty()) | |
| 432 UpdateManualListsImpl(); | |
| 433 | |
| 434 break; | |
| 435 } | |
| 436 default: | 196 default: |
| 437 NOTREACHED(); | 197 NOTREACHED(); |
| 438 } | 198 } |
| 439 } | 199 } |
| 440 | 200 |
| 441 void ManagedMode::FinalizeEnter(bool result) { | 201 void ManagedMode::FinalizeEnter(bool result) { |
| 442 if (result) | 202 if (result) |
| 443 SetInManagedMode(managed_profile_); | 203 SetInManagedMode(managed_profile_); |
| 204 |
| 444 for (std::vector<EnterCallback>::iterator it = callbacks_.begin(); | 205 for (std::vector<EnterCallback>::iterator it = callbacks_.begin(); |
| 445 it != callbacks_.end(); ++it) { | 206 it != callbacks_.end(); ++it) { |
| 446 it->Run(result); | 207 it->Run(result); |
| 447 } | 208 } |
| 448 callbacks_.clear(); | 209 callbacks_.clear(); |
| 449 browsers_to_close_.clear(); | 210 browsers_to_close_.clear(); |
| 450 registrar_.RemoveAll(); | 211 registrar_.RemoveAll(); |
| 451 } | 212 } |
| 452 | 213 |
| 453 bool ManagedMode::PlatformConfirmEnter() { | 214 bool ManagedMode::PlatformConfirmEnter() { |
| 454 // TODO(bauerb): Show platform-specific confirmation dialog. | 215 // TODO(bauerb): Show platform-specific confirmation dialog. |
| 455 return true; | 216 return true; |
| 456 } | 217 } |
| 457 | 218 |
| 458 bool ManagedMode::PlatformConfirmLeave() { | 219 bool ManagedMode::PlatformConfirmLeave() { |
| 459 // TODO(bauerb): Show platform-specific confirmation dialog. | 220 // TODO(bauerb): Show platform-specific confirmation dialog. |
| 460 return true; | 221 return true; |
| 461 } | 222 } |
| 462 | 223 |
| 463 void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { | 224 void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { |
| 464 // Register the ManagementPolicy::Provider before changing the pref when | |
| 465 // setting it, and unregister it after changing the pref when clearing it, | |
| 466 // so pref observers see the correct ManagedMode state. | |
| 467 bool in_managed_mode = !!newly_managed_profile; | |
| 468 if (in_managed_mode) { | |
| 469 DCHECK(!managed_profile_ || managed_profile_ == newly_managed_profile); | |
| 470 extensions::ExtensionSystem::Get( | |
| 471 newly_managed_profile)->management_policy()->RegisterProvider(this); | |
| 472 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | |
| 473 content::Source<Profile>(newly_managed_profile)); | |
| 474 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 475 content::Source<Profile>(newly_managed_profile)); | |
| 476 pref_change_registrar_.reset(new PrefChangeRegistrar()); | |
| 477 pref_change_registrar_->Init(newly_managed_profile->GetPrefs()); | |
| 478 pref_change_registrar_->Add( | |
| 479 prefs::kDefaultManagedModeFilteringBehavior, | |
| 480 base::Bind( | |
| 481 &ManagedMode::OnDefaultFilteringBehaviorChanged, | |
| 482 base::Unretained(this))); | |
| 483 } else { | |
| 484 extensions::ExtensionSystem::Get( | |
| 485 managed_profile_)->management_policy()->UnregisterProvider(this); | |
| 486 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_LOADED, | |
| 487 content::Source<Profile>(managed_profile_)); | |
| 488 registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 489 content::Source<Profile>(managed_profile_)); | |
| 490 pref_change_registrar_.reset(); | |
| 491 } | |
| 492 | |
| 493 managed_profile_ = newly_managed_profile; | 225 managed_profile_ = newly_managed_profile; |
| 494 ManagedModeURLFilter::FilteringBehavior behavior = | |
| 495 ManagedModeURLFilter::ALLOW; | |
| 496 if (in_managed_mode) { | |
| 497 int behavior_value = managed_profile_->GetPrefs()->GetInteger( | |
| 498 prefs::kDefaultManagedModeFilteringBehavior); | |
| 499 behavior = ManagedModeURLFilter::BehaviorFromInt(behavior_value); | |
| 500 } | |
| 501 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); | |
| 502 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); | |
| 503 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, | 226 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, |
| 504 in_managed_mode); | 227 !!newly_managed_profile); |
| 505 if (in_managed_mode) | |
| 506 UpdateManualListsImpl(); | |
| 507 | 228 |
| 508 // This causes the avatar and the profile menu to get updated. | 229 // This causes the avatar and the profile menu to get updated. |
| 509 content::NotificationService::current()->Notify( | 230 content::NotificationService::current()->Notify( |
| 510 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 231 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 511 content::NotificationService::AllBrowserContextsAndSources(), | 232 content::NotificationService::AllBrowserContextsAndSources(), |
| 512 content::NotificationService::NoDetails()); | 233 content::NotificationService::NoDetails()); |
| 513 } | 234 } |
| 514 | |
| 515 ScopedVector<ManagedModeSiteList> ManagedMode::GetActiveSiteLists() { | |
| 516 DCHECK(managed_profile_); | |
| 517 ExtensionService* extension_service = | |
| 518 extensions::ExtensionSystem::Get(managed_profile_)->extension_service(); | |
| 519 const ExtensionSet* extensions = extension_service->extensions(); | |
| 520 ScopedVector<ManagedModeSiteList> site_lists; | |
| 521 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 522 it != extensions->end(); ++it) { | |
| 523 const extensions::Extension* extension = *it; | |
| 524 if (!extension_service->IsExtensionEnabled(extension->id())) | |
| 525 continue; | |
| 526 | |
| 527 ExtensionResource site_list = extension->GetContentPackSiteList(); | |
| 528 if (!site_list.empty()) | |
| 529 site_lists.push_back(new ManagedModeSiteList(extension->id(), site_list)); | |
| 530 } | |
| 531 | |
| 532 return site_lists.Pass(); | |
| 533 } | |
| 534 | |
| 535 void ManagedMode::OnDefaultFilteringBehaviorChanged() { | |
| 536 DCHECK(IsInManagedModeImpl()); | |
| 537 | |
| 538 int behavior_value = managed_profile_->GetPrefs()->GetInteger( | |
| 539 prefs::kDefaultManagedModeFilteringBehavior); | |
| 540 ManagedModeURLFilter::FilteringBehavior behavior = | |
| 541 ManagedModeURLFilter::BehaviorFromInt(behavior_value); | |
| 542 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); | |
| 543 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); | |
| 544 } | |
| 545 | |
| 546 // Static | |
| 547 void ManagedMode::UpdateManualLists() { | |
| 548 GetInstance()->UpdateManualListsImpl(); | |
| 549 } | |
| 550 | |
| 551 void ManagedMode::UpdateManualListsImpl() { | |
| 552 io_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); | |
| 553 ui_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); | |
| 554 io_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); | |
| 555 ui_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); | |
| 556 } | |
| 557 | |
| 558 scoped_ptr<base::ListValue> ManagedMode::GetWhitelist() { | |
| 559 return make_scoped_ptr( | |
| 560 managed_profile_->GetPrefs()->GetList( | |
| 561 prefs::kManagedModeWhitelist)->DeepCopy()); | |
| 562 } | |
| 563 | |
| 564 void ManagedMode::AddURLPatternToManualList( | |
| 565 bool is_whitelist, | |
| 566 const std::string& url_pattern) { | |
| 567 io_url_filter_context_->AddURLPatternToManualList(true, url_pattern); | |
| 568 ui_url_filter_context_->AddURLPatternToManualList(true, url_pattern); | |
| 569 } | |
| OLD | NEW |