File: context_database.h

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 (106 lines) | stat: -rw-r--r-- 4,034 bytes parent folder | download | duplicates (6)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_CONTENT_CACHE_CONTEXT_DATABASE_H_
#define CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_CONTENT_CACHE_CONTEXT_DATABASE_H_

#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/threading/sequence_bound.h"
#include "sql/database.h"
#include "sql/meta_table.h"

namespace ash::file_system_provider {

// The persistent data store for items that are cached via an FSP mount. There
// is 1:1 mapping of `ContextDatabase` per FSP mount and when the content cache
// is removed, the database is removed with it.
class ContextDatabase {
 public:
  explicit ContextDatabase(const base::FilePath& db_path);

  ContextDatabase(const ContextDatabase&) = delete;
  ContextDatabase& operator=(const ContextDatabase&) = delete;

  ~ContextDatabase();

  // Initialize the database either in-memory (if the constructor `db_path` was
  // empty) or at the path specified by the `db_path` in the constructor.
  bool Initialize();

  // Insert an item into the database and returns the unique ID that references
  // this item. In the event the `fsp_path` and `version_tag` have previously
  // been used, this will remove the conflicted record and insert a new one.
  bool AddItem(const base::FilePath& fsp_path,
               const std::string& version_tag,
               base::Time accessed_time,
               int64_t* inserted_id);

  // Represents a row returned from the SQLite database with the additional
  // field of `item_exists` that will be set to `false` if the item requested is
  // not available.
  struct Item {
    Item(int64_t id,
         const std::string& fsp_path,
         const std::string& version_tag,
         base::Time accessed_time);

    // The `id` that is stored in the database.
    int64_t id;

    // The path in the FSP filesystem that references this file. Is represented
    // as an absolute path, e.g. `/a.txt`.
    base::FilePath fsp_path;

    // The version tag that this file is identified by, useful to invalidate if
    // the underlying version changes but not the path nor size.
    const std::string version_tag;

    // The last time the file was accessed, this is persisted as the users
    // cryptohome is mounted with `MS_NOATIME` meaning the files atime is not
    // updated after creation.
    base::Time accessed_time;
  };

  // Retrieve an item by `item_id`. Return a `nullptr` upon an error retrieving
  // the item. Return `std::nullopt` if the item doesn't exist.
  std::unique_ptr<std::optional<ContextDatabase::Item>> GetItemById(
      int64_t item_id);

  // Update the accessed time for the supplied `item_id`.
  bool UpdateAccessedTime(int64_t item_id, base::Time new_accessed_time);

  // Retrieve all the items currently stored in the database. Returns a map
  // keyed by the item id.
  using IdToItemMap = std::map<int64_t, Item>;
  IdToItemMap GetAllItems();

  // Remove all the items in the database with the associated ids.
  bool RemoveItemsByIds(std::vector<int64_t> item_ids);

  // Used in testing to interact with the database bypassing the `ContentCache`.
  base::WeakPtr<ContextDatabase> GetWeakPtr();

 private:
  SEQUENCE_CHECKER(sequence_checker_);

  // Remove the database and poison any subsequent requests.
  bool Raze();

  static const int kCurrentVersionNumber;
  static const int kCompatibleVersionNumber;

  const base::FilePath db_path_;
  sql::Database db_ GUARDED_BY_CONTEXT(sequence_checker_);
  sql::MetaTable meta_table_ GUARDED_BY_CONTEXT(sequence_checker_);
  base::WeakPtrFactory<ContextDatabase> weak_ptr_factory_{this};
};

using OptionalContextDatabase = std::optional<std::unique_ptr<ContextDatabase>>;
using BoundContextDatabase =
    base::SequenceBound<std::unique_ptr<ContextDatabase>>;

}  // namespace ash::file_system_provider

#endif  // CHROME_BROWSER_ASH_FILE_SYSTEM_PROVIDER_CONTENT_CACHE_CONTEXT_DATABASE_H_