File: clear_browsing_data_job.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (158 lines) | stat: -rw-r--r-- 5,570 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2020 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/enterprise/remote_commands/clear_browsing_data_job.h"

#include <optional>

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/task/single_thread_task_runner.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/policy/core/common/policy_logger.h"
#include "content/public/browser/browsing_data_remover.h"

namespace enterprise_commands {

namespace {

const char kFailedTypesPath[] = "failed_data_types";

const char kClearCacheField[] = "clear_cache";
const char kClearCookiesField[] = "clear_cookies";

// Define the possibly failed data types here for 2 reasons:
//
// 1. This will be easier to keep in sync with the server, as the latter
// doesn't care about *all* the types in BrowsingDataRemover.
//
// 2. Centralize handling the underlying type of the values here.
// BrowsingDataRemover represents failed types as uint64_t, which isn't
// natively supported by base::Value, so this class needs to convert to a
// type that's supported. This will also allow us to use a list instead of a
// bit mask, which will be easier to parse gracefully on the server in case
// more types are added.
enum class DataTypes {
  kCache = 0,
  kCookies = 1,
};

std::string CreatePayload(uint64_t failed_data_types) {
  base::Value::Dict root;
  base::Value::List failed_types_list;

  if (failed_data_types & content::BrowsingDataRemover::DATA_TYPE_CACHE)
    failed_types_list.Append(static_cast<int>(DataTypes::kCache));

  if (failed_data_types & content::BrowsingDataRemover::DATA_TYPE_COOKIES)
    failed_types_list.Append(static_cast<int>(DataTypes::kCookies));

  root.Set(kFailedTypesPath, std::move(failed_types_list));

  std::string payload;
  base::JSONWriter::Write(root, &payload);
  return payload;
}

}  // namespace

ClearBrowsingDataJob::ClearBrowsingDataJob(ProfileManager* profile_manager)
    : job_profile_picker_(profile_manager) {}
ClearBrowsingDataJob::ClearBrowsingDataJob(Profile* profile)
    : job_profile_picker_(profile) {}

ClearBrowsingDataJob::~ClearBrowsingDataJob() = default;

enterprise_management::RemoteCommand_Type ClearBrowsingDataJob::GetType()
    const {
  return enterprise_management::RemoteCommand_Type_BROWSER_CLEAR_BROWSING_DATA;
}

bool ClearBrowsingDataJob::ParseCommandPayload(
    const std::string& command_payload) {
  VLOG_POLICY(2, REMOTE_COMMANDS)
      << "Clear browsing data command payload: " << command_payload;
  std::optional<base::Value::Dict> root =
      base::JSONReader::ReadDict(command_payload);
  if (!root)
    return false;

  if (!job_profile_picker_.ParseCommandPayload(*root)) {
    return false;
  }

  // Not specifying these fields is equivalent to setting them to false.
  clear_cache_ = root->FindBool(kClearCacheField).value_or(false);
  clear_cookies_ = root->FindBool(kClearCookiesField).value_or(false);

  return true;
}

void ClearBrowsingDataJob::RunImpl(CallbackWithResult result_callback) {
  uint64_t types = 0;
  if (clear_cache_)
    types |= content::BrowsingDataRemover::DATA_TYPE_CACHE;

  if (clear_cookies_)
    types |= content::BrowsingDataRemover::DATA_TYPE_COOKIES;

  Profile* profile = job_profile_picker_.GetProfile();
  if (!profile) {
    // If the payload's profile path doesn't correspond to an existing profile,
    // there's nothing to do. The most likely scenario is that the profile was
    // deleted by the time the command was received.
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE,
        base::BindOnce(std::move(result_callback), policy::ResultType::kFailure,
                       CreatePayload(types)));
    return;
  }

  result_callback_ = std::move(result_callback);

  if (types == 0) {
    LOG_POLICY(WARNING, REMOTE_COMMANDS)
        << "Clear browsing data command has not specified any "
           "data types. Please double check the payload to "
           "make sure everything is set as required.";
    // There's nothing to clear, invoke the callback with success result and be
    // done.
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(result_callback_),
                                  policy::ResultType::kSuccess,
                                  CreatePayload(
                                      /*failed_data_types=*/0)));
    return;
  }

  content::BrowsingDataRemover* remover = profile->GetBrowsingDataRemover();
  remover->AddObserver(this);

  remover->RemoveAndReply(
      base::Time(), base::Time::Max(), types,
      content::BrowsingDataRemover::ORIGIN_TYPE_UNPROTECTED_WEB, this);
}

void ClearBrowsingDataJob::OnBrowsingDataRemoverDone(
    uint64_t failed_data_types) {
  Profile* profile = job_profile_picker_.GetProfile();
  DCHECK(profile);

  content::BrowsingDataRemover* remover = profile->GetBrowsingDataRemover();
  remover->RemoveObserver(this);

  std::string payload = CreatePayload(failed_data_types);

  base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(std::move(result_callback_),
                     failed_data_types != 0 ? policy::ResultType::kFailure
                                            : policy::ResultType::kSuccess,
                     std::move(payload)));
}

}  // namespace enterprise_commands