1 // Copyright 2020 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.
4 
5 #include "weblayer/browser/host_content_settings_map_factory.h"
6 
7 #include <utility>
8 
9 #include "base/feature_list.h"
10 #include "components/content_settings/core/browser/content_settings_pref_provider.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "components/keyed_service/content/browser_context_dependency_manager.h"
13 #include "components/user_prefs/user_prefs.h"
14 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h"
16 
17 namespace weblayer {
18 
HostContentSettingsMapFactory()19 HostContentSettingsMapFactory::HostContentSettingsMapFactory()
20     : RefcountedBrowserContextKeyedServiceFactory(
21           "HostContentSettingsMap",
22           BrowserContextDependencyManager::GetInstance()) {}
23 
24 HostContentSettingsMapFactory::~HostContentSettingsMapFactory() = default;
25 
26 // static
GetForBrowserContext(content::BrowserContext * browser_context)27 HostContentSettingsMap* HostContentSettingsMapFactory::GetForBrowserContext(
28     content::BrowserContext* browser_context) {
29   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
30 
31   return static_cast<HostContentSettingsMap*>(
32       GetInstance()->GetServiceForBrowserContext(browser_context, true).get());
33 }
34 
35 // static
GetInstance()36 HostContentSettingsMapFactory* HostContentSettingsMapFactory::GetInstance() {
37   return base::Singleton<HostContentSettingsMapFactory>::get();
38 }
39 
40 scoped_refptr<RefcountedKeyedService>
BuildServiceInstanceFor(content::BrowserContext * context) const41 HostContentSettingsMapFactory::BuildServiceInstanceFor(
42     content::BrowserContext* context) const {
43   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 
45   scoped_refptr<HostContentSettingsMap> settings_map =
46       base::MakeRefCounted<HostContentSettingsMap>(
47           user_prefs::UserPrefs::Get(context), context->IsOffTheRecord(),
48           /*store_last_modified=*/true,
49           /*migrate_requesting_and_top_level_origin_settings=*/true);
50 
51   return settings_map;
52 }
53 
GetBrowserContextToUse(content::BrowserContext * context) const54 content::BrowserContext* HostContentSettingsMapFactory::GetBrowserContextToUse(
55     content::BrowserContext* context) const {
56   return context;
57 }
58 
59 }  // namespace weblayer
60