Index: chrome/browser/managed_mode/managed_mode.cc |
diff --git a/chrome/browser/managed_mode/managed_mode.cc b/chrome/browser/managed_mode/managed_mode.cc |
index 5eead29e0d6cd660bfaddeee4cdddea72096b00e..5a6e1cc2c43ce8955db344a5475e5a311a71944d 100644 |
--- a/chrome/browser/managed_mode/managed_mode.cc |
+++ b/chrome/browser/managed_mode/managed_mode.cc |
@@ -8,98 +8,23 @@ |
#include "base/prefs/public/pref_change_registrar.h" |
#include "base/sequenced_task_runner.h" |
#include "chrome/browser/browser_process.h" |
-#include "chrome/browser/extensions/extension_service.h" |
-#include "chrome/browser/extensions/extension_system.h" |
#include "chrome/browser/managed_mode/managed_mode_site_list.h" |
#include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
#include "chrome/browser/policy/url_blacklist_manager.h" |
#include "chrome/browser/prefs/pref_service.h" |
-#include "chrome/browser/prefs/scoped_user_pref_update.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/browser_list.h" |
#include "chrome/browser/ui/browser_window.h" |
#include "chrome/common/chrome_notification_types.h" |
#include "chrome/common/chrome_switches.h" |
-#include "chrome/common/extensions/extension_set.h" |
#include "chrome/common/pref_names.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/notification_service.h" |
#include "grit/generated_resources.h" |
-#include "ui/base/l10n/l10n_util.h" |
using content::BrowserThread; |
-// A bridge from ManagedMode (which lives on the UI thread) to |
-// ManagedModeURLFilter (which might live on a different thread). |
-class ManagedMode::URLFilterContext { |
- public: |
- explicit URLFilterContext( |
- scoped_refptr<base::SequencedTaskRunner> task_runner) |
- : task_runner_(task_runner) {} |
- ~URLFilterContext() {} |
- |
- ManagedModeURLFilter* url_filter() { |
- DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
- return &url_filter_; |
- } |
- |
- void SetDefaultFilteringBehavior( |
- ManagedModeURLFilter::FilteringBehavior behavior) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- // Because ManagedMode is a singleton, we can pass the pointer to |
- // |url_filter_| unretained. |
- task_runner_->PostTask( |
- FROM_HERE, |
- base::Bind(&ManagedModeURLFilter::SetDefaultFilteringBehavior, |
- base::Unretained(&url_filter_), |
- behavior)); |
- } |
- |
- void LoadWhitelists(ScopedVector<ManagedModeSiteList> site_lists) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- task_runner_->PostTask(FROM_HERE, |
- base::Bind(&ManagedModeURLFilter::LoadWhitelists, |
- base::Unretained(&url_filter_), |
- base::Passed(&site_lists))); |
- } |
- |
- void SetManualLists(scoped_ptr<ListValue> whitelist, |
- scoped_ptr<ListValue> blacklist) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- task_runner_->PostTask(FROM_HERE, |
- base::Bind(&ManagedModeURLFilter::SetManualLists, |
- base::Unretained(&url_filter_), |
- base::Passed(&whitelist), |
- base::Passed(&blacklist))); |
- } |
- |
- void AddURLPatternToManualList(bool is_whitelist, |
- const std::string& url) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
- task_runner_->PostTask(FROM_HERE, |
- base::Bind(&ManagedModeURLFilter::AddURLPatternToManualList, |
- base::Unretained(&url_filter_), |
- is_whitelist, |
- url)); |
- } |
- |
- void ShutdownOnUIThread() { |
- if (task_runner_->RunsTasksOnCurrentThread()) { |
- delete this; |
- return; |
- } |
- bool result = task_runner_->DeleteSoon(FROM_HERE, this); |
- DCHECK(result); |
- } |
- |
- private: |
- ManagedModeURLFilter url_filter_; |
- scoped_refptr<base::SequencedTaskRunner> task_runner_; |
- |
- DISALLOW_COPY_AND_ASSIGN(URLFilterContext); |
-}; |
- |
// static |
ManagedMode* ManagedMode::GetInstance() { |
return Singleton<ManagedMode, LeakySingletonTraits<ManagedMode> >::get(); |
@@ -111,17 +36,6 @@ void ManagedMode::RegisterPrefs(PrefServiceSimple* prefs) { |
} |
// static |
-void ManagedMode::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
- prefs->RegisterIntegerPref(prefs::kDefaultManagedModeFilteringBehavior, |
- 2, |
- PrefServiceSyncable::UNSYNCABLE_PREF); |
- prefs->RegisterListPref(prefs::kManagedModeWhitelist, |
- PrefServiceSyncable::UNSYNCABLE_PREF); |
- prefs->RegisterListPref(prefs::kManagedModeBlacklist, |
- PrefServiceSyncable::UNSYNCABLE_PREF); |
-} |
- |
-// static |
void ManagedMode::Init(Profile* profile) { |
GetInstance()->InitImpl(profile); |
} |
@@ -223,141 +137,6 @@ void ManagedMode::LeaveManagedModeImpl() { |
SetInManagedMode(NULL); |
} |
-// static |
-const ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThread() { |
- return GetInstance()->GetURLFilterForIOThreadImpl(); |
-} |
- |
-// static |
-const ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThread() { |
- return GetInstance()->GetURLFilterForUIThreadImpl(); |
-} |
- |
-ManagedModeURLFilter* ManagedMode::GetURLFilterForIOThreadImpl() { |
- return io_url_filter_context_->url_filter(); |
-} |
- |
-ManagedModeURLFilter* ManagedMode::GetURLFilterForUIThreadImpl() { |
- return ui_url_filter_context_->url_filter(); |
-} |
- |
-// static |
-void ManagedMode::AddToManualList(bool is_whitelist, |
- const base::ListValue& list) { |
- GetInstance()->AddToManualListImpl(is_whitelist, list); |
-} |
- |
-void ManagedMode::AddToManualListImpl(bool is_whitelist, |
- const base::ListValue& list) { |
- if (!managed_profile_) |
- return; |
- |
- ListPrefUpdate pref_update(managed_profile_->GetPrefs(), |
- is_whitelist ? prefs::kManagedModeWhitelist : |
- prefs::kManagedModeBlacklist); |
- ListValue* pref_list = pref_update.Get(); |
- |
- for (size_t i = 0; i < list.GetSize(); ++i) { |
- std::string url_pattern; |
- list.GetString(i, &url_pattern); |
- |
- if (!IsInManualList(is_whitelist, url_pattern)) { |
- pref_list->AppendString(url_pattern); |
- AddURLPatternToManualList(is_whitelist, url_pattern); |
- } |
- } |
-} |
- |
-// static |
-void ManagedMode::RemoveFromManualList(bool is_whitelist, |
- const base::ListValue& list) { |
- GetInstance()->RemoveFromManualListImpl(is_whitelist, list); |
-} |
- |
-void ManagedMode::RemoveFromManualListImpl(bool is_whitelist, |
- const base::ListValue& list) { |
- ListPrefUpdate pref_update(managed_profile_->GetPrefs(), |
- is_whitelist ? prefs::kManagedModeWhitelist : |
- prefs::kManagedModeBlacklist); |
- ListValue* pref_list = pref_update.Get(); |
- |
- for (size_t i = 0; i < list.GetSize(); ++i) { |
- std::string pattern; |
- size_t out_index; |
- list.GetString(i, &pattern); |
- StringValue value_to_remove(pattern); |
- |
- pref_list->Remove(value_to_remove, &out_index); |
- } |
-} |
- |
-// static |
-bool ManagedMode::IsInManualList(bool is_whitelist, |
- const std::string& url_pattern) { |
- return GetInstance()->IsInManualListImpl(is_whitelist, url_pattern); |
-} |
- |
-bool ManagedMode::IsInManualListImpl(bool is_whitelist, |
- const std::string& url_pattern) { |
- StringValue pattern(url_pattern); |
- const ListValue* list = managed_profile_->GetPrefs()->GetList( |
- is_whitelist ? prefs::kManagedModeWhitelist : |
- prefs::kManagedModeBlacklist); |
- return list->Find(pattern) != list->end(); |
-} |
- |
-// static |
-scoped_ptr<base::ListValue> ManagedMode::GetBlacklist() { |
- return scoped_ptr<base::ListValue>( |
- GetInstance()->managed_profile_->GetPrefs()->GetList( |
- prefs::kManagedModeBlacklist)->DeepCopy()).Pass(); |
-} |
- |
-std::string ManagedMode::GetDebugPolicyProviderName() const { |
- // Save the string space in official builds. |
-#ifdef NDEBUG |
- NOTREACHED(); |
- return std::string(); |
-#else |
- return "Managed Mode"; |
-#endif |
-} |
- |
-bool ManagedMode::UserMayLoad(const extensions::Extension* extension, |
- string16* error) const { |
- string16 tmp_error; |
- if (ExtensionManagementPolicyImpl(&tmp_error)) |
- return true; |
- |
- // If the extension is already loaded, we allow it, otherwise we'd unload |
- // all existing extensions. |
- ExtensionService* extension_service = |
- extensions::ExtensionSystem::Get(managed_profile_)->extension_service(); |
- |
- // |extension_service| can be NULL in a unit test. |
- if (extension_service && |
- extension_service->GetInstalledExtension(extension->id())) |
- return true; |
- |
- if (error) |
- *error = tmp_error; |
- return false; |
-} |
- |
-bool ManagedMode::UserMayModifySettings(const extensions::Extension* extension, |
- string16* error) const { |
- return ExtensionManagementPolicyImpl(error); |
-} |
- |
-bool ManagedMode::ExtensionManagementPolicyImpl(string16* error) const { |
- if (!IsInManagedModeImpl()) |
- return true; |
- |
- if (error) |
- *error = l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOCKED_MANAGED_MODE); |
- return false; |
-} |
- |
void ManagedMode::OnBrowserAdded(Browser* browser) { |
// Return early if we don't have any queued callbacks. |
if (callbacks_.empty()) |
@@ -384,14 +163,7 @@ void ManagedMode::OnBrowserRemoved(Browser* browser) { |
FinalizeEnter(true); |
} |
-ManagedMode::ManagedMode() |
- : managed_profile_(NULL), |
- io_url_filter_context_( |
- new URLFilterContext( |
- BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO))), |
- ui_url_filter_context_( |
- new URLFilterContext( |
- BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI))) { |
+ManagedMode::ManagedMode() : managed_profile_(NULL) { |
BrowserList::AddObserver(this); |
} |
@@ -401,38 +173,26 @@ ManagedMode::~ManagedMode() { |
BrowserList::RemoveObserver(this); |
DCHECK_EQ(0u, callbacks_.size()); |
DCHECK_EQ(0u, browsers_to_close_.size()); |
- io_url_filter_context_.release()->ShutdownOnUIThread(); |
- ui_url_filter_context_.release()->ShutdownOnUIThread(); |
} |
void ManagedMode::Observe(int type, |
const content::NotificationSource& source, |
const content::NotificationDetails& details) { |
+ // Return early if we don't have any queued callbacks. |
+ if (callbacks_.empty()) |
+ return; |
+ |
switch (type) { |
case chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST: { |
- if (!callbacks_.empty()) |
- FinalizeEnter(false); |
+ FinalizeEnter(false); |
return; |
} |
case chrome::NOTIFICATION_BROWSER_CLOSE_CANCELLED: { |
Browser* browser = content::Source<Browser>(source).ptr(); |
- if (!callbacks_.empty() && browsers_to_close_.find(browser) != |
- browsers_to_close_.end()) |
+ if (browsers_to_close_.find(browser) != browsers_to_close_.end()) |
FinalizeEnter(false); |
return; |
} |
- case chrome::NOTIFICATION_EXTENSION_LOADED: |
- case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
- if (!managed_profile_) |
- break; |
- |
- const extensions::Extension* extension = |
- content::Details<const extensions::Extension>(details).ptr(); |
- if (!extension->GetContentPackSiteList().empty()) |
- UpdateManualListsImpl(); |
- |
- break; |
- } |
default: |
NOTREACHED(); |
} |
@@ -441,6 +201,7 @@ void ManagedMode::Observe(int type, |
void ManagedMode::FinalizeEnter(bool result) { |
if (result) |
SetInManagedMode(managed_profile_); |
+ |
for (std::vector<EnterCallback>::iterator it = callbacks_.begin(); |
it != callbacks_.end(); ++it) { |
it->Run(result); |
@@ -461,49 +222,9 @@ bool ManagedMode::PlatformConfirmLeave() { |
} |
void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { |
- // Register the ManagementPolicy::Provider before changing the pref when |
- // setting it, and unregister it after changing the pref when clearing it, |
- // so pref observers see the correct ManagedMode state. |
- bool in_managed_mode = !!newly_managed_profile; |
- if (in_managed_mode) { |
- DCHECK(!managed_profile_ || managed_profile_ == newly_managed_profile); |
- extensions::ExtensionSystem::Get( |
- newly_managed_profile)->management_policy()->RegisterProvider(this); |
- registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
- content::Source<Profile>(newly_managed_profile)); |
- registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
- content::Source<Profile>(newly_managed_profile)); |
- pref_change_registrar_.reset(new PrefChangeRegistrar()); |
- pref_change_registrar_->Init(newly_managed_profile->GetPrefs()); |
- pref_change_registrar_->Add( |
- prefs::kDefaultManagedModeFilteringBehavior, |
- base::Bind( |
- &ManagedMode::OnDefaultFilteringBehaviorChanged, |
- base::Unretained(this))); |
- } else { |
- extensions::ExtensionSystem::Get( |
- managed_profile_)->management_policy()->UnregisterProvider(this); |
- registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
- content::Source<Profile>(managed_profile_)); |
- registrar_.Remove(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
- content::Source<Profile>(managed_profile_)); |
- pref_change_registrar_.reset(); |
- } |
- |
managed_profile_ = newly_managed_profile; |
- ManagedModeURLFilter::FilteringBehavior behavior = |
- ManagedModeURLFilter::ALLOW; |
- if (in_managed_mode) { |
- int behavior_value = managed_profile_->GetPrefs()->GetInteger( |
- prefs::kDefaultManagedModeFilteringBehavior); |
- behavior = ManagedModeURLFilter::BehaviorFromInt(behavior_value); |
- } |
- io_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
- ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
g_browser_process->local_state()->SetBoolean(prefs::kInManagedMode, |
- in_managed_mode); |
- if (in_managed_mode) |
- UpdateManualListsImpl(); |
+ !!newly_managed_profile); |
// This causes the avatar and the profile menu to get updated. |
content::NotificationService::current()->Notify( |
@@ -511,59 +232,3 @@ void ManagedMode::SetInManagedMode(Profile* newly_managed_profile) { |
content::NotificationService::AllBrowserContextsAndSources(), |
content::NotificationService::NoDetails()); |
} |
- |
-ScopedVector<ManagedModeSiteList> ManagedMode::GetActiveSiteLists() { |
- DCHECK(managed_profile_); |
- ExtensionService* extension_service = |
- extensions::ExtensionSystem::Get(managed_profile_)->extension_service(); |
- const ExtensionSet* extensions = extension_service->extensions(); |
- ScopedVector<ManagedModeSiteList> site_lists; |
- for (ExtensionSet::const_iterator it = extensions->begin(); |
- it != extensions->end(); ++it) { |
- const extensions::Extension* extension = *it; |
- if (!extension_service->IsExtensionEnabled(extension->id())) |
- continue; |
- |
- ExtensionResource site_list = extension->GetContentPackSiteList(); |
- if (!site_list.empty()) |
- site_lists.push_back(new ManagedModeSiteList(extension->id(), site_list)); |
- } |
- |
- return site_lists.Pass(); |
-} |
- |
-void ManagedMode::OnDefaultFilteringBehaviorChanged() { |
- DCHECK(IsInManagedModeImpl()); |
- |
- int behavior_value = managed_profile_->GetPrefs()->GetInteger( |
- prefs::kDefaultManagedModeFilteringBehavior); |
- ManagedModeURLFilter::FilteringBehavior behavior = |
- ManagedModeURLFilter::BehaviorFromInt(behavior_value); |
- io_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
- ui_url_filter_context_->SetDefaultFilteringBehavior(behavior); |
-} |
- |
-// Static |
-void ManagedMode::UpdateManualLists() { |
- GetInstance()->UpdateManualListsImpl(); |
-} |
- |
-void ManagedMode::UpdateManualListsImpl() { |
- io_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); |
- ui_url_filter_context_->LoadWhitelists(GetActiveSiteLists()); |
- io_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); |
- ui_url_filter_context_->SetManualLists(GetWhitelist(), GetBlacklist()); |
-} |
- |
-scoped_ptr<base::ListValue> ManagedMode::GetWhitelist() { |
- return make_scoped_ptr( |
- managed_profile_->GetPrefs()->GetList( |
- prefs::kManagedModeWhitelist)->DeepCopy()); |
-} |
- |
-void ManagedMode::AddURLPatternToManualList( |
- bool is_whitelist, |
- const std::string& url_pattern) { |
- io_url_filter_context_->AddURLPatternToManualList(true, url_pattern); |
- ui_url_filter_context_->AddURLPatternToManualList(true, url_pattern); |
-} |