summaryrefslogtreecommitdiffstats
path: root/chromium/components/policy/core/common/cloud/resource_cache.cc
blob: 8fa8abad7859e30b6d5d3db362e83c4d1e9cb173 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/policy/core/common/cloud/resource_cache.h"

#include "base/base64url.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/sequenced_task_runner.h"

namespace policy {

namespace {

// Decodes all elements of |input| from base64url format and stores the decoded
// elements in |output|.
bool Base64UrlEncode(const std::set<std::string>& input,
                     std::set<std::string>* output) {
  output->clear();
  for (const auto& plain : input) {
    if (plain.empty()) {
      NOTREACHED();
      output->clear();
      return false;
    }

    std::string encoded;
    base::Base64UrlEncode(plain, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
                          &encoded);

    output->insert(encoded);
  }
  return true;
}

}  // namespace

ResourceCache::ResourceCache(
    const base::FilePath& cache_dir,
    scoped_refptr<base::SequencedTaskRunner> task_runner,
    absl::optional<int64_t> max_cache_size)
    : cache_dir_(cache_dir),
      task_runner_(task_runner),
      max_cache_size_(max_cache_size) {
  // Safe to post this without a WeakPtr because this class must be destructed
  // on the same thread.
  if (max_cache_size_.has_value()) {
    task_runner_->PostTask(FROM_HERE,
                           base::BindOnce(&ResourceCache::InitCurrentCacheSize,
                                          base::Unretained(this)));
  }
}

ResourceCache::~ResourceCache() {
  // No RunsTasksInCurrentSequence() check to avoid unit tests failures.
  // In unit tests the browser process instance is deleted only after test ends
  // and test task scheduler is shutted down. Therefore we need to delete some
  // components of BrowserPolicyConnector (ResourceCache and
  // CloudExternalDataManagerBase::Backend) manually when task runner doesn't
  // accept new tasks (DeleteSoon in this case). This leads to the situation
  // when this destructor is called not on |task_runner|.
}

base::FilePath ResourceCache::Store(const std::string& key,
                                    const std::string& subkey,
                                    const std::string& data) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::FilePath subkey_path;
  if (!VerifyKeyPathAndGetSubkeyPath(key, true, subkey, &subkey_path))
    return base::FilePath();
  int64_t size = base::checked_cast<int64_t>(data.size());
  if (max_cache_size_.has_value() &&
      current_cache_size_ - GetCacheDirectoryOrFileSize(subkey_path) + size >
          max_cache_size_.value()) {
    LOG(ERROR) << "Data (" << key << ", " << subkey << ") with size " << size
               << " bytes doesn't fit in cache, left size: "
               << max_cache_size_.value() - current_cache_size_ << " bytes";
    return base::FilePath();
  }
  if (!WriteCacheFile(subkey_path, data))
    return base::FilePath();
  return subkey_path;
}

base::FilePath ResourceCache::Load(const std::string& key,
                                   const std::string& subkey,
                                   std::string* data) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::FilePath subkey_path;
  // Only read from |subkey_path| if it is not a symlink.
  if (!VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path) ||
      base::IsLink(subkey_path)) {
    return base::FilePath();
  }
  data->clear();
  if (!base::ReadFileToString(subkey_path, data))
    return base::FilePath();
  return subkey_path;
}

void ResourceCache::LoadAllSubkeys(
    const std::string& key,
    std::map<std::string, std::string>* contents) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  contents->clear();
  base::FilePath key_path;
  if (!VerifyKeyPath(key, false, &key_path))
    return;

  base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES);
  for (base::FilePath path = enumerator.Next(); !path.empty();
       path = enumerator.Next()) {
    const std::string encoded_subkey = path.BaseName().MaybeAsASCII();
    std::string subkey;
    std::string data;
    // Only read from |subkey_path| if it is not a symlink and its name is
    // a base64-encoded string.
    if (!base::IsLink(path) &&
        base::Base64UrlDecode(encoded_subkey,
                              base::Base64UrlDecodePolicy::REQUIRE_PADDING,
                              &subkey) &&
        !subkey.empty() && base::ReadFileToString(path, &data)) {
      (*contents)[subkey].swap(data);
    }
  }
}

void ResourceCache::Delete(const std::string& key, const std::string& subkey) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::FilePath subkey_path;
  if (VerifyKeyPathAndGetSubkeyPath(key, false, subkey, &subkey_path))
    DeleteCacheFile(subkey_path, false);
  base::FilePath key_path;
  // DeleteCacheFile() does nothing if the directory given to it is not empty.
  // Hence, the call below deletes the directory representing |key| if its last
  // subkey was just removed and does nothing otherwise.
  if (VerifyKeyPath(key, false, &key_path))
    DeleteCacheFile(key_path, false);
}

void ResourceCache::Clear(const std::string& key) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::FilePath key_path;
  if (VerifyKeyPath(key, false, &key_path))
    DeleteCacheFile(key_path, true);
}

void ResourceCache::FilterSubkeys(const std::string& key,
                                  const SubkeyFilter& test) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());

  base::FilePath key_path;
  if (!VerifyKeyPath(key, false, &key_path))
    return;

  base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES);
  for (base::FilePath subkey_path = enumerator.Next();
       !subkey_path.empty(); subkey_path = enumerator.Next()) {
    std::string subkey;
    // Delete files with invalid names, and files whose subkey doesn't pass the
    // filter.
    if (!base::Base64UrlDecode(subkey_path.BaseName().MaybeAsASCII(),
                               base::Base64UrlDecodePolicy::REQUIRE_PADDING,
                               &subkey) ||
        subkey.empty() || test.Run(subkey)) {
      DeleteCacheFile(subkey_path, true);
    }
  }

  // Delete() does nothing if the directory given to it is not empty. Hence, the
  // call below deletes the directory representing |key| if all of its subkeys
  // were just removed and does nothing otherwise.
  DeleteCacheFile(key_path, false);
}

void ResourceCache::PurgeOtherKeys(const std::set<std::string>& keys_to_keep) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  std::set<std::string> encoded_keys_to_keep;
  if (!Base64UrlEncode(keys_to_keep, &encoded_keys_to_keep))
    return;

  base::FileEnumerator enumerator(
      cache_dir_, false, base::FileEnumerator::DIRECTORIES);
  for (base::FilePath path = enumerator.Next(); !path.empty();
       path = enumerator.Next()) {
    const std::string name(path.BaseName().MaybeAsASCII());
    if (encoded_keys_to_keep.find(name) == encoded_keys_to_keep.end())
      DeleteCacheFile(path, true);
  }
}

void ResourceCache::PurgeOtherSubkeys(
    const std::string& key,
    const std::set<std::string>& subkeys_to_keep) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  base::FilePath key_path;
  if (!VerifyKeyPath(key, false, &key_path))
    return;

  std::set<std::string> encoded_subkeys_to_keep;
  if (!Base64UrlEncode(subkeys_to_keep, &encoded_subkeys_to_keep))
    return;

  base::FileEnumerator enumerator(key_path, false, base::FileEnumerator::FILES);
  for (base::FilePath path = enumerator.Next(); !path.empty();
       path = enumerator.Next()) {
    const std::string name(path.BaseName().MaybeAsASCII());
    if (encoded_subkeys_to_keep.find(name) == encoded_subkeys_to_keep.end())
      DeleteCacheFile(path, false);
  }
  // Delete() does nothing if the directory given to it is not empty. Hence, the
  // call below deletes the directory representing |key| if all of its subkeys
  // were just removed and does nothing otherwise.
  DeleteCacheFile(key_path, false);
}

bool ResourceCache::VerifyKeyPath(const std::string& key,
                                  bool allow_create,
                                  base::FilePath* path) {
  if (key.empty()) {
    NOTREACHED();
    return false;
  }

  std::string encoded;
  base::Base64UrlEncode(key, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
                        &encoded);

  *path = cache_dir_.AppendASCII(encoded);
  return allow_create ? base::CreateDirectory(*path) :
                        base::DirectoryExists(*path);
}

bool ResourceCache::VerifyKeyPathAndGetSubkeyPath(const std::string& key,
                                                  bool allow_create_key,
                                                  const std::string& subkey,
                                                  base::FilePath* path) {
  if (subkey.empty()) {
    NOTREACHED();
    return false;
  }

  base::FilePath key_path;
  if (!VerifyKeyPath(key, allow_create_key, &key_path))
    return false;

  std::string encoded;
  base::Base64UrlEncode(subkey, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
                        &encoded);

  *path = key_path.AppendASCII(encoded);
  return true;
}

void ResourceCache::InitCurrentCacheSize() {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  current_cache_size_ = GetCacheDirectoryOrFileSize(cache_dir_);
}

bool ResourceCache::WriteCacheFile(const base::FilePath& path,
                                   const std::string& data) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  DCHECK(cache_dir_.IsParent(path));
  bool success = DeleteCacheFile(path, false);
  int size = base::checked_cast<int>(data.size());
  int bytes_written = base::WriteFile(path, data.data(), size);
  if (max_cache_size_.has_value())
    current_cache_size_ += bytes_written;
  return success && bytes_written == size;
}

bool ResourceCache::DeleteCacheFile(const base::FilePath& path,
                                    bool recursive) {
  DCHECK(task_runner_->RunsTasksInCurrentSequence());
  DCHECK(cache_dir_.IsParent(path));
  int64_t size = GetCacheDirectoryOrFileSize(path);
  bool success;
  if (recursive)
    success = base::DeletePathRecursively(path);
  else
    success = base::DeleteFile(path);
  if (success && max_cache_size_.has_value())
    current_cache_size_ -= size;
  return success;
}

int64_t ResourceCache::GetCacheDirectoryOrFileSize(
    const base::FilePath& path) const {
  DCHECK(path == cache_dir_ || cache_dir_.IsParent(path));
  if (base::IsLink(path)) {
    DLOG(WARNING) << "Symlink " << path.LossyDisplayName()
                  << " detected in cache directory";
    return 0;
  }
  int64_t path_size = 0;
  if (base::DirectoryExists(path)) {
    int types = base::FileEnumerator::FILES | base::FileEnumerator::DIRECTORIES;
    base::FileEnumerator enumerator(path, /* recursive */ false, types);
    for (base::FilePath child_path = enumerator.Next(); !child_path.empty();
         child_path = enumerator.Next()) {
      path_size += GetCacheDirectoryOrFileSize(child_path);
    }
  } else if (!base::GetFileSize(path, &path_size)) {
    path_size = 0;
  }
  return path_size;
}

}  // namespace policy