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.
4 
5 #include "components/content_settings/core/browser/website_settings_info.h"
6 
7 #include <utility>
8 
9 #include "base/logging.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "components/pref_registry/pref_registry_syncable.h"
13 #include "components/prefs/pref_registry.h"
14 
15 namespace {
16 
17 const char kPrefPrefix[] = "profile.content_settings.exceptions.";
18 const char kDefaultPrefPrefix[] = "profile.default_content_setting_values.";
19 
GetPreferenceName(const std::string & name,const char * prefix)20 std::string GetPreferenceName(const std::string& name, const char* prefix) {
21   std::string pref_name = name;
22   base::ReplaceChars(pref_name, "-", "_", &pref_name);
23   return std::string(prefix).append(pref_name);
24 }
25 
26 }  // namespace
27 
28 namespace content_settings {
29 
WebsiteSettingsInfo(ContentSettingsType type,const std::string & name,std::unique_ptr<base::Value> initial_default_value,SyncStatus sync_status,LossyStatus lossy_status,ScopingType scoping_type,IncognitoBehavior incognito_behavior)30 WebsiteSettingsInfo::WebsiteSettingsInfo(
31     ContentSettingsType type,
32     const std::string& name,
33     std::unique_ptr<base::Value> initial_default_value,
34     SyncStatus sync_status,
35     LossyStatus lossy_status,
36     ScopingType scoping_type,
37     IncognitoBehavior incognito_behavior)
38     : type_(type),
39       name_(name),
40       pref_name_(GetPreferenceName(name, kPrefPrefix)),
41       default_value_pref_name_(GetPreferenceName(name, kDefaultPrefPrefix)),
42       initial_default_value_(std::move(initial_default_value)),
43       sync_status_(sync_status),
44       lossy_status_(lossy_status),
45       scoping_type_(scoping_type),
46       incognito_behavior_(incognito_behavior) {
47   // For legacy reasons the default value is currently restricted to be an int.
48   // TODO(raymes): We should migrate the underlying pref to be a dictionary
49   // rather than an int.
50   DCHECK(!initial_default_value_ || initial_default_value_->is_int());
51 }
52 
~WebsiteSettingsInfo()53 WebsiteSettingsInfo::~WebsiteSettingsInfo() {}
54 
GetPrefRegistrationFlags() const55 uint32_t WebsiteSettingsInfo::GetPrefRegistrationFlags() const {
56   uint32_t flags = PrefRegistry::NO_REGISTRATION_FLAGS;
57 
58   if (sync_status_ == SYNCABLE)
59     flags |= user_prefs::PrefRegistrySyncable::SYNCABLE_PREF;
60 
61   if (lossy_status_ == LOSSY)
62     flags |= PrefRegistry::LOSSY_PREF;
63 
64   return flags;
65 }
66 
SupportsEmbeddedExceptions() const67 bool WebsiteSettingsInfo::SupportsEmbeddedExceptions() const {
68   // Note that REQUESTING_ORIGIN_AND_TOP_LEVEL_ORIGIN_SCOPE supports embedded
69   // exceptions but because these are deprecated and planned to be removed they
70   // aren't included here.
71   if (scoping_type_ == COOKIES_SCOPE ||
72       scoping_type_ == SINGLE_ORIGIN_WITH_EMBEDDED_EXCEPTIONS_SCOPE) {
73     return true;
74   }
75 
76   return false;
77 }
78 
79 }  // namespace content_settings
80