| 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" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 13 #include "chrome/browser/managed_mode/managed_mode_site_list.h" | 13 #include "chrome/browser/managed_mode/managed_mode_site_list.h" |
| 14 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 14 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
| 15 #include "chrome/browser/policy/url_blacklist_manager.h" |
| 15 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_list.h" | 20 #include "chrome/browser/ui/browser_list.h" |
| 19 #include "chrome/browser/ui/browser_window.h" | 21 #include "chrome/browser/ui/browser_window.h" |
| 20 #include "chrome/common/chrome_notification_types.h" | 22 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/chrome_switches.h" | 23 #include "chrome/common/chrome_switches.h" |
| 22 #include "chrome/common/extensions/extension_set.h" | 24 #include "chrome/common/extensions/extension_set.h" |
| 23 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 24 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 25 #include "content/public/browser/notification_service.h" | 27 #include "content/public/browser/notification_service.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 56 | 58 |
| 57 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists) { | 59 void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists) { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 59 task_runner_->PostTask(FROM_HERE, | 61 task_runner_->PostTask(FROM_HERE, |
| 60 base::Bind(&ManagedModeURLFilter::LoadWhitelists, | 62 base::Bind(&ManagedModeURLFilter::LoadWhitelists, |
| 61 base::Unretained(&url_filter_), | 63 base::Unretained(&url_filter_), |
| 62 base::Passed(&site_lists), | 64 base::Passed(&site_lists), |
| 63 base::Bind(&base::DoNothing))); | 65 base::Bind(&base::DoNothing))); |
| 64 } | 66 } |
| 65 | 67 |
| 68 void SetManualLists(scoped_ptr<ListValue> whitelist, |
| 69 scoped_ptr<ListValue> blacklist) { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 task_runner_->PostTask(FROM_HERE, |
| 72 base::Bind(&ManagedModeURLFilter::SetManualLists, |
| 73 base::Unretained(&url_filter_), |
| 74 base::Passed(&whitelist), |
| 75 base::Passed(&blacklist))); |
| 76 } |
| 77 |
| 78 void AddURLPatternToManualList(bool is_whitelist, |
| 79 const std::string& url) { |
| 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 81 task_runner_->PostTask(FROM_HERE, |
| 82 base::Bind(&ManagedModeURLFilter::AddURLPatternToManualList, |
| 83 base::Unretained(&url_filter_), |
| 84 is_whitelist, |
| 85 url)); |
| 86 } |
| 87 |
| 66 void ShutdownOnUIThread() { | 88 void ShutdownOnUIThread() { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 89 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 68 bool result = task_runner_->DeleteSoon(FROM_HERE, this); | 90 bool result = task_runner_->DeleteSoon(FROM_HERE, this); |
| 69 DCHECK(result); | 91 DCHECK(result); |
| 70 } | 92 } |
| 71 | 93 |
| 72 private: | 94 private: |
| 73 ManagedModeURLFilter url_filter_; | 95 ManagedModeURLFilter url_filter_; |
| 74 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 96 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 75 | 97 |
| 76 DISALLOW_COPY_AND_ASSIGN(URLFilterContext); | 98 DISALLOW_COPY_AND_ASSIGN(URLFilterContext); |
| 77 }; | 99 }; |
| 78 | 100 |
| 79 // static | 101 // static |
| 80 ManagedMode* ManagedMode::GetInstance() { | 102 ManagedMode* ManagedMode::GetInstance() { |
| 81 return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); | 103 return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); |
| 82 } | 104 } |
| 83 | 105 |
| 84 // static | 106 // static |
| 85 void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) { | 107 void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) { |
| 86 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); | 108 prefs->RegisterBooleanPref(prefs::kInManagedMode, false); |
| 87 } | 109 } |
| 88 | 110 |
| 89 // static | 111 // static |
| 90 void ManagedMode::RegisterUserPrefs(PrefServiceSyncable* prefs) { | 112 void ManagedMode::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
| 91 prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior, | 113 prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior, |
| 92 2, | 114 2, |
| 93 PrefServiceSyncable::UNSYNCABLE_PREF); | 115 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 116 prefs->RegisterListPref(prefs::kManagedModeWhitelist, |
| 117 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 118 prefs->RegisterListPref(prefs::kManagedModeBlacklist, |
| 119 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 94 } | 120 } |
| 95 | 121 |
| 96 // static | 122 // static |
| 97 void ManagedMode::Init(Profile* profile) { | 123 void ManagedMode::Init(Profile* profile) { |
| 98 GetInstance()->InitImpl(profile); | 124 GetInstance()->InitImpl(profile); |
| 99 } | 125 } |
| 100 | 126 |
| 101 void ManagedMode::InitImpl(Profile* profile) { | 127 void ManagedMode::InitImpl(Profile* profile) { |
| 102 DCHECK(g_browser_process); | 128 DCHECK(g_browser_process); |
| 103 DCHECK(g_browser_process->local_state()); | 129 DCHECK(g_browser_process->local_state()); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 } | 232 } |
| 207 | 233 |
| 208 const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThreadImpl() { | 234 const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThreadImpl() { |
| 209 return io_url_filter_context_->url_filter(); | 235 return io_url_filter_context_->url_filter(); |
| 210 } | 236 } |
| 211 | 237 |
| 212 const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThreadImpl() { | 238 const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThreadImpl() { |
| 213 return ui_url_filter_context_->url_filter(); | 239 return ui_url_filter_context_->url_filter(); |
| 214 } | 240 } |
| 215 | 241 |
| 242 // static |
| 243 void ManagedMode::AddToManualList(bool is_whitelist, |
| 244 const base::ListValue& list) { |
| 245 GetInstance()->AddToManualListImpl(is_whitelist, list); |
| 246 } |
| 247 |
| 248 void ManagedMode::AddToManualListImpl(bool is_whitelist, |
| 249 const base::ListValue& list) { |
| 250 if (!managed_profile_) |
| 251 return; |
| 252 |
| 253 ListPrefUpdate pref_update(managed_profile_->GetPrefs(), |
| 254 is_whitelist ? prefs::kManagedModeWhitelist : |
| 255 prefs::kManagedModeBlacklist); |
| 256 ListValue* pref_list = pref_update.Get(); |
| 257 |
| 258 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 259 std::string url_pattern; |
| 260 list.GetString(i, &url_pattern); |
| 261 |
| 262 if (!IsInManualList(is_whitelist, url_pattern)) { |
| 263 pref_list->AppendString(url_pattern); |
| 264 AddURLPatternToManualList(is_whitelist, url_pattern); |
| 265 } |
| 266 } |
| 267 } |
| 268 |
| 269 // static |
| 270 void ManagedMode::RemoveFromManualList(bool is_whitelist, |
| 271 const base::ListValue& list) { |
| 272 GetInstance()->RemoveFromManualListImpl(is_whitelist, list); |
| 273 } |
| 274 |
| 275 void ManagedMode::RemoveFromManualListImpl(bool is_whitelist, |
| 276 const base::ListValue& list) { |
| 277 ListPrefUpdate pref_update(managed_profile_->GetPrefs(), |
| 278 is_whitelist ? prefs::kManagedModeWhitelist : |
| 279 prefs::kManagedModeBlacklist); |
| 280 ListValue* pref_list = pref_update.Get(); |
| 281 |
| 282 for (size_t i = 0; i < list.GetSize(); ++i) { |
| 283 std::string pattern; |
| 284 size_t out_index; |
| 285 list.GetString(i, &pattern); |
| 286 StringValue value_to_remove(pattern); |
| 287 |
| 288 pref_list->Remove(value_to_remove, &out_index); |
| 289 } |
| 290 } |
| 291 |
| 292 // static |
| 293 bool ManagedMode::IsInManualList(bool is_whitelist, |
| 294 const std::string& url_pattern) { |
| 295 return GetInstance()->IsInManualListImpl(is_whitelist, url_pattern); |
| 296 } |
| 297 |
| 298 bool ManagedMode::IsInManualListImpl(bool is_whitelist, |
| 299 const std::string& url_pattern) { |
| 300 StringValue pattern(url_pattern); |
| 301 const ListValue* list = managed_profile_->GetPrefs()->GetList( |
| 302 is_whitelist ? prefs::kManagedModeWhitelist : |
| 303 prefs::kManagedModeBlacklist); |
| 304 return list->Find(pattern) != list->end(); |
| 305 } |
| 306 |
| 307 // static |
| 308 scoped_ptr<base::ListValue> ManagedMode::GetBlacklist() { |
| 309 return scoped_ptr<base::ListValue>( |
| 310 GetInstance()->managed_profile_->GetPrefs()->GetList( |
| 311 prefs::kManagedModeBlacklist)->DeepCopy()).Pass(); |
| 312 } |
| 313 |
| 216 std::string ManagedMode::GetDebugPolicyProviderName() const { | 314 std::string ManagedMode::GetDebugPolicyProviderName() const { |
| 217 // Save the string space in official builds. | 315 // Save the string space in official builds. |
| 218 #ifdef NDEBUG | 316 #ifdef NDEBUG |
| 219 NOTREACHED(); | 317 NOTREACHED(); |
| 220 return std::string(); | 318 return std::string(); |
| 221 #else | 319 #else |
| 222 return "Managed Mode"; | 320 return "Managed Mode"; |
| 223 #endif | 321 #endif |
| 224 } | 322 } |
| 225 | 323 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 if (in_managed_mode) { | 476 if (in_managed_mode) { |
| 379 int behavior_value = managed_profile_->GetPrefs()->GetInteger( | 477 int behavior_value = managed_profile_->GetPrefs()->GetInteger( |
| 380 prefs::kDefaultManagedModeFilteringBehavior); | 478 prefs::kDefaultManagedModeFilteringBehavior); |
| 381 behavior = ManagedModeURLFilter::BehaviorFromInt(behavior_value); | 479 behavior = ManagedModeURLFilter::BehaviorFromInt(behavior_value); |
| 382 } | 480 } |
| 383 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); | 481 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
| 384 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); | 482 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
| 385 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, | 483 g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, |
| 386 in_managed_mode); | 484 in_managed_mode); |
| 387 if (in_managed_mode) | 485 if (in_managed_mode) |
| 388 UpdateWhitelist(); | 486 UpdateManualListsImpl(); |
| 389 | 487 |
| 390 // This causes the avatar and the profile menu to get updated. | 488 // This causes the avatar and the profile menu to get updated. |
| 391 content::NotificationService::current()->Notify( | 489 content::NotificationService::current()->Notify( |
| 392 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, | 490 chrome::NOTIFICATION_PROFILE_CACHED_INFO_CHANGED, |
| 393 content::NotificationService::AllBrowserContextsAndSources(), | 491 content::NotificationService::AllBrowserContextsAndSources(), |
| 394 content::NotificationService::NoDetails()); | 492 content::NotificationService::NoDetails()); |
| 395 } | 493 } |
| 396 | 494 |
| 397 ScopedVector<ManagedModeSiteList> ManagedMode::GetActiveSiteLists() { | 495 ScopedVector<ManagedModeSiteList> ManagedMode::GetActiveSiteLists() { |
| 398 DCHECK(managed_profile_); | 496 DCHECK(managed_profile_); |
| 399 ScopedVector<ManagedModeSiteList> site_lists; | 497 ScopedVector<ManagedModeSiteList> site_lists; |
| 400 // TODO(bauerb): Get site lists from all extensions. | 498 // TODO(bauerb): Get site lists from all extensions. |
| 401 return site_lists.Pass(); | 499 return site_lists.Pass(); |
| 402 } | 500 } |
| 403 | 501 |
| 404 void ManagedMode::OnDefaultFilteringBehaviorChanged() { | 502 void ManagedMode::OnDefaultFilteringBehaviorChanged() { |
| 405 DCHECK(IsInManagedModeImpl()); | 503 DCHECK(IsInManagedModeImpl()); |
| 406 | 504 |
| 407 int behavior_value = managed_profile_->GetPrefs()->GetInteger( | 505 int behavior_value = managed_profile_->GetPrefs()->GetInteger( |
| 408 prefs::kDefaultManagedModeFilteringBehavior); | 506 prefs::kDefaultManagedModeFilteringBehavior); |
| 409 ManagedModeURLFilter::FilteringBehavior behavior = | 507 ManagedModeURLFilter::FilteringBehavior behavior = |
| 410 ManagedModeURLFilter::BehaviorFromInt(behavior_value); | 508 ManagedModeURLFilter::BehaviorFromInt(behavior_value); |
| 411 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); | 509 io_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
| 412 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); | 510 ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
| 413 } | 511 } |
| 414 | 512 |
| 415 void ManagedMode::UpdateWhitelist() { | 513 // Static |
| 514 void ManagedMode::UpdateManualLists() { |
| 515 GetInstance()->UpdateManualListsImpl(); |
| 516 } |
| 517 |
| 518 void ManagedMode::UpdateManualListsImpl() { |
| 416 io_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); | 519 io_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); |
| 417 ui_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); | 520 ui_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); |
| 521 io_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); |
| 522 ui_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); |
| 418 } | 523 } |
| 524 |
| 525 scoped_ptr<base::ListValue> ManagedMode::GetWhitelist() { |
| 526 return make_scoped_ptr( |
| 527 managed_profile_->GetPrefs()->GetList( |
| 528 prefs::kManagedModeWhitelist)->DeepCopy()); |
| 529 } |
| 530 |
| 531 void ManagedMode::AddURLPatternToManualList( |
| 532 bool is_whitelist, |
| 533 const std::string& url_pattern) { |
| 534 io_url_filter_context_->AddURLPatternToManualList(true, url_pattern); |
| 535 ui_url_filter_context_->AddURLPatternToManualList(true, url_pattern); |
| 536 } |
| OLD | NEW |