File: power_bookmark_backend.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (122 lines) | stat: -rw-r--r-- 5,239 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_BACKEND_H_
#define COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_BACKEND_H_

#include <memory>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_checker.h"
#include "components/power_bookmarks/common/power_bookmark_observer.h"
#include "components/power_bookmarks/storage/power_bookmark_database.h"
#include "components/power_bookmarks/storage/power_bookmark_sync_bridge.h"
#include "components/sync/protocol/power_bookmark_specifics.pb.h"

namespace syncer {
class DataTypeControllerDelegate;
}  // namespace syncer

namespace power_bookmarks {

struct SearchParams;

// Class responsible for marshalling calls from the browser thread which the
// service is called from and the background thread which the database is
// run on. Calls to this class should be posted on the background task_runner.
class PowerBookmarkBackend : public PowerBookmarkSyncBridge::Delegate {
 public:
  // `database_dir` the directory to create the backend database in.
  // `frontend_task_runner` the task runner which the service runs, used to
  // communicate observer events back through the service.
  // `service_observer` the observer the backend uses to plumb events through to
  // observers of PowerBookmarkService.
  PowerBookmarkBackend(
      const base::FilePath& database_dir,
      scoped_refptr<base::SequencedTaskRunner> frontend_task_runner,
      base::WeakPtr<PowerBookmarkObserver> service_observer);
  PowerBookmarkBackend(const PowerBookmarkBackend&) = delete;
  PowerBookmarkBackend& operator=(const PowerBookmarkBackend&) = delete;
  virtual ~PowerBookmarkBackend();

  void Init(bool use_database);

  // For sync codebase only: gets a weak reference to the sync controller
  // delegate.
  base::WeakPtr<syncer::DataTypeControllerDelegate> GetSyncControllerDelegate();

  // Returns a vector of Powers for the given `url`. Use `power_type` to
  // restrict which type is returned or use POWER_TYPE_UNSPECIFIED to return
  // everything.
  std::vector<std::unique_ptr<Power>> GetPowersForURL(
      const GURL& url,
      const sync_pb::PowerBookmarkSpecifics::PowerType& power_type);

  // Returns a vector of PowerOverviews for the given `power_type`.
  std::vector<std::unique_ptr<PowerOverview>> GetPowerOverviewsForType(
      const sync_pb::PowerBookmarkSpecifics::PowerType& power_type);

  // Returns a vector of Powers matching the given `search_params`.
  std::vector<std::unique_ptr<Power>> SearchPowers(
      const SearchParams& search_params);

  // Returns a vector of PowerOverviews matching the given `search_params`.
  std::vector<std::unique_ptr<PowerOverview>> SearchPowerOverviews(
      const SearchParams& search_params);

  // Create the given `power` in the database. If it already exists, then it
  // will be updated. Returns whether the operation was successful.
  bool CreatePower(std::unique_ptr<Power> power);
  // Update the given `power` in the database. If it doesn't exist, then it
  // will be created instead. Returns whether the operation was successful.
  bool UpdatePower(std::unique_ptr<Power> power);
  // Delete the given `guid` in the database, if it exists. Returns whether
  // the operation was successful.
  bool DeletePower(const base::Uuid& guid);
  // Delete all powers for the given `url`. Success of the operation is
  // returned through the given `callback`. Use `power_type` to restrict which
  // type is deleted or use POWER_TYPE_UNSPECIFIED to delete everything.
  bool DeletePowersForURL(
      const GURL& url,
      const sync_pb::PowerBookmarkSpecifics::PowerType& power_type);

  // PowerBookmarkSyncBridge::Delegate
  std::vector<std::unique_ptr<Power>> GetAllPowers() override;
  std::vector<std::unique_ptr<Power>> GetPowersForGUIDs(
      const std::vector<std::string>& guids) override;
  std::unique_ptr<Power> GetPowerForGUID(const std::string& guid) override;
  bool CreateOrMergePowerFromSync(const Power& power) override;
  bool DeletePowerFromSync(const std::string& guid) override;
  PowerBookmarkSyncMetadataDatabase* GetSyncMetadataDatabase() override;
  std::unique_ptr<Transaction> BeginTransaction() override;
  void NotifyPowersChanged() override;

 private:
  // Commit the change. If success then notify the observer, otherwise report
  // error to sync.
  bool CommitAndNotify(Transaction& transaction);

  const base::FilePath database_dir_;

  std::unique_ptr<PowerBookmarkDatabase> db_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Sync Bridge implementation. Only initialized when the sqlite database is
  // used.
  std::unique_ptr<PowerBookmarkSyncBridge> bridge_;

  scoped_refptr<base::SequencedTaskRunner> frontend_task_runner_;

  // Observer that serves the frontend of power bookmarks.
  // Needs to be called on the frontend task runner.
  base::WeakPtr<PowerBookmarkObserver> service_observer_;

  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace power_bookmarks

#endif  // COMPONENTS_POWER_BOOKMARKS_STORAGE_POWER_BOOKMARK_BACKEND_H_