1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
7 #include "chrome/browser/profiles/off_the_record_profile_impl.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "components/content_settings/core/browser/host_content_settings_map.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "content/public/browser/browser_thread.h"
13 #if defined(ENABLE_EXTENSIONS)
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "extensions/browser/extension_system.h"
16 #include "extensions/browser/extension_system_provider.h"
17 #include "extensions/browser/extensions_browser_client.h"
20 #if defined(ENABLE_SUPERVISED_USERS)
21 #include "chrome/browser/content_settings/content_settings_supervised_provider.h"
22 #include "chrome/browser/supervised_user/supervised_user_settings_service.h"
23 #include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
26 HostContentSettingsMapFactory::HostContentSettingsMapFactory()
27 : RefcountedBrowserContextKeyedServiceFactory(
28 "HostContentSettingsMap",
29 BrowserContextDependencyManager::GetInstance()) {
30 #if defined(ENABLE_SUPERVISED_USERS)
31 DependsOn(SupervisedUserSettingsServiceFactory::GetInstance());
33 #if defined(ENABLE_EXTENSIONS)
35 extensions::ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
39 HostContentSettingsMapFactory::~HostContentSettingsMapFactory() {
43 HostContentSettingsMap
* HostContentSettingsMapFactory::GetForProfile(
45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
47 return static_cast<HostContentSettingsMap
*>(
48 GetInstance()->GetServiceForBrowserContext(profile
, true).get());
52 HostContentSettingsMapFactory
* HostContentSettingsMapFactory::GetInstance() {
53 return Singleton
<HostContentSettingsMapFactory
>::get();
56 scoped_refptr
<RefcountedKeyedService
>
57 HostContentSettingsMapFactory::BuildServiceInstanceFor(
58 content::BrowserContext
* context
) const {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
61 Profile
* profile
= static_cast<Profile
*>(context
);
62 bool off_the_record
= profile
->GetProfileType() == Profile::INCOGNITO_PROFILE
;
64 // If off the record, retrieve the host content settings map of the parent
65 // profile in order to ensure the preferences have been migrated.
67 GetForProfile(profile
->GetOriginalProfile());
69 scoped_refptr
<HostContentSettingsMap
> settings_map(
70 new HostContentSettingsMap(profile
->GetPrefs(), off_the_record
));
72 #if defined(ENABLE_EXTENSIONS)
73 ExtensionService
*ext_service
=
74 extensions::ExtensionSystem::Get(profile
)->extension_service();
75 // This may be null in testing or when the extenion_service hasn't been
76 // initialized, in which case it will be registered then.
78 ext_service
->RegisterContentSettings(settings_map
.get());
79 #endif // defined(ENABLE_EXTENSIONS)
80 #if defined(ENABLE_SUPERVISED_USERS)
81 SupervisedUserSettingsService
* supervised_service
=
82 SupervisedUserSettingsServiceFactory::GetForProfile(profile
);
83 // This may be null in testing.
84 if (supervised_service
) {
85 scoped_ptr
<content_settings::SupervisedProvider
> supervised_provider(
86 new content_settings::SupervisedProvider(supervised_service
));
87 settings_map
->RegisterProvider(HostContentSettingsMap::SUPERVISED_PROVIDER
,
88 supervised_provider
.Pass());
90 #endif // defined(ENABLE_SUPERVISED_USERS)
95 content::BrowserContext
* HostContentSettingsMapFactory::GetBrowserContextToUse(
96 content::BrowserContext
* context
) const {