File: browsing_data_lifetime_policy_handler.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (128 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/browsing_data/browsing_data_lifetime_policy_handler.h"

#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/browsing_data/core/browsing_data_policies_utils.h"
#include "components/policy/core/browser/policy_error_map.h"
#include "components/policy/core/common/policy_logger.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/strings/grit/components_strings.h"

BrowsingDataLifetimePolicyHandler::BrowsingDataLifetimePolicyHandler(
    const char* policy_name,
    const char* pref_path,
    policy::Schema schema)
    : policy::SimpleSchemaValidatingPolicyHandler(
          policy_name,
          pref_path,
          schema,
          policy::SchemaOnErrorStrategy::SCHEMA_ALLOW_UNKNOWN,
          SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED,
          SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED),
      pref_path_(pref_path) {}

BrowsingDataLifetimePolicyHandler::~BrowsingDataLifetimePolicyHandler() =
    default;

bool BrowsingDataLifetimePolicyHandler::CheckPolicySettings(
    const policy::PolicyMap& policies,
    policy::PolicyErrorMap* errors) {
  if (!policy::SimpleSchemaValidatingPolicyHandler::CheckPolicySettings(
          policies, errors)) {
    // Reset the sync types set in case the policy fails to be set after being
    // previously set.
    forced_disabled_sync_types_.Clear();
    return false;
  }

  if (!policies.Get(policy_name())) {
    // Reset the sync types set in case the policy has been unset after being
    // previously set.
    forced_disabled_sync_types_.Clear();
    return true;
  }

  // If sync is already disabled or sign in is disabled altogether, the policy
  // requirements are automatically met.
  const auto* sync_disabled =
      policies.GetValue(policy::key::kSyncDisabled, base::Value::Type::BOOLEAN);
  if ((sync_disabled && sync_disabled->GetBool())) {
    return true;
  }

// BrowserSignin policy is not available on ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
  const auto* browser_signin_disabled = policies.GetValue(
      policy::key::kBrowserSignin, base::Value::Type::INTEGER);
  if (browser_signin_disabled && browser_signin_disabled->GetInt() == 0) {
    return true;
  }
#endif

  const base::Value* browsing_data_policy =
      policies.GetValue(this->policy_name(), base::Value::Type::LIST);
  forced_disabled_sync_types_ =
      (policy_name() == policy::key::kBrowsingDataLifetime)
          ? browsing_data::GetSyncTypesForBrowsingDataLifetime(
                *browsing_data_policy)
          : browsing_data::GetSyncTypesForClearBrowsingData(
                *browsing_data_policy);

  if (!forced_disabled_sync_types_.empty()) {
    errors->AddError(this->policy_name(),
                     IDS_POLICY_BROWSING_DATA_DEPENDENCY_APPLIED_INFO,
                     UserSelectableTypeSetToString(forced_disabled_sync_types_),
                     {}, policy::PolicyMap::MessageType::kInfo);
  }

  unsupported_types_ =
      browsing_data::GetBrowsingDataLifetimePlatformUnsupportedTypes(
          *browsing_data_policy);
  if (!unsupported_types_.empty()) {
    errors->AddError(this->policy_name(),
                     IDS_POLICY_BROWSING_DATA_PLATFORM_UNSUPPORTED,
                     base::JoinString(unsupported_types_, ", "), {},
                     policy::PolicyMap::MessageType::kWarning);
  }

  return true;
}

void BrowsingDataLifetimePolicyHandler::ApplyPolicySettings(
    const policy::PolicyMap& policies,
    PrefValueMap* prefs) {
  if (unsupported_types_.empty()) {
    SimpleSchemaValidatingPolicyHandler::ApplyPolicySettings(policies, prefs);
  } else {
    // Make a copy of the policy value so as to remove unsupported types before
    // adding into prefs. Using GetValueUnsafe, GetList, GetDict is ok here
    // because this function is only called if the policy schema is valid.
    base::Value filtered_policy_value =
        policies.GetValueUnsafe(policy_name())->Clone();
    for (auto& item : filtered_policy_value.GetList()) {
      base::Value::List& data_types =
          item.GetDict().Find("data_types")->GetList();
      data_types.erase(
          std::remove_if(data_types.begin(), data_types.end(),
                         [this](const base::Value& type) {
                           return unsupported_types_.contains(type.GetString());
                         }),
          data_types.end());
    }
    prefs->SetValue(pref_path_, std::move(filtered_policy_value));
  }

  // `forced_disabled_sync_types_` will be empty if either SyncDisabled or
  // BrowserSignin policy was set.
  std::string log_message = browsing_data::DisableSyncTypes(
      forced_disabled_sync_types_, prefs, policy_name());
  if (!log_message.empty()) {
    LOG_POLICY(INFO, POLICY_PROCESSING) << log_message;
  }
}