File: remote_suggestions_database.h

package info (click to toggle)
chromium 90.0.4430.212-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,450,632 kB
  • sloc: cpp: 19,832,434; javascript: 2,948,838; ansic: 2,312,399; python: 1,464,622; xml: 584,121; java: 514,189; asm: 470,557; objc: 83,463; perl: 77,861; sh: 77,030; cs: 70,789; fortran: 24,137; tcl: 18,916; php: 18,872; makefile: 16,848; ruby: 16,721; pascal: 13,150; sql: 10,199; yacc: 7,507; lex: 1,313; lisp: 840; awk: 329; jsp: 39; sed: 19
file content (156 lines) | stat: -rw-r--r-- 5,884 bytes parent folder | download | duplicates (7)
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
// Copyright 2016 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.

#ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_DATABASE_H_
#define COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_DATABASE_H_

#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/sequenced_task_runner.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/ntp_snippets/remote/remote_suggestion.h"

namespace base {
class FilePath;
}  // namespace base

namespace leveldb_proto {
class ProtoDatabaseProvider;
}  // namespace leveldb_proto

namespace ntp_snippets {

class SnippetImageProto;
class SnippetProto;

// TODO(gaschler): implement a Fake version for testing
class RemoteSuggestionsDatabase {
 public:
  using SnippetsCallback =
      base::OnceCallback<void(RemoteSuggestion::PtrVector)>;
  using SnippetImageCallback = base::OnceCallback<void(std::string)>;

  // Creates a RemoteSuggestionsDatabase backed by real ProtoDatabases.
  RemoteSuggestionsDatabase(
      leveldb_proto::ProtoDatabaseProvider* proto_database_provider,
      const base::FilePath& database_dir);
  // Creates a RemoteSuggestionsDatabase backed by the passed-in ProtoDatabases,
  // useful for testing.
  RemoteSuggestionsDatabase(
      std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database,
      std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>>
          image_database);
  ~RemoteSuggestionsDatabase();

  // Returns whether the database has finished initialization. While this is
  // false, loads may already be started (they'll be serviced after
  // initialization finishes), but no updates are allowed.
  bool IsInitialized() const;

  // Returns whether the database is in an (unrecoverable) error state. If this
  // is true, the database must not be used anymore
  bool IsErrorState() const;

  // Set a callback to be called when the database enters an error state.
  void SetErrorCallback(const base::RepeatingClosure& error_callback);

  // Loads all snippets from storage and passes them to |callback|.
  void LoadSnippets(SnippetsCallback callback);

  // Adds or updates the given snippet.
  void SaveSnippet(const RemoteSuggestion& snippet);
  // Adds or updates all the given snippets.
  void SaveSnippets(const RemoteSuggestion::PtrVector& snippets);

  // Deletes the snippet with the given ID.
  void DeleteSnippet(const std::string& snippet_id);
  // Deletes all the given snippets (identified by their IDs).
  void DeleteSnippets(std::unique_ptr<std::vector<std::string>> snippet_ids);

  // Loads the image data for the snippet with the given ID and passes it to
  // |callback|. Passes an empty string if not found.
  void LoadImage(const std::string& snippet_id, SnippetImageCallback callback);

  // Adds or updates the image data for the given snippet ID.
  void SaveImage(const std::string& snippet_id, const std::string& image_data);

  // Deletes the image data for the given snippet ID.
  void DeleteImage(const std::string& snippet_id);
  // Deletes the image data for the given snippets (identified by their IDs).
  void DeleteImages(std::unique_ptr<std::vector<std::string>> snippet_ids);
  // Deletes all images which are not associated with any of the provided
  // snippets.
  void GarbageCollectImages(
      std::unique_ptr<std::set<std::string>> alive_snippet_ids);

 private:
  friend class RemoteSuggestionsDatabaseTest;

  using KeyEntryVector =
      leveldb_proto::ProtoDatabase<SnippetProto>::KeyEntryVector;

  using ImageKeyEntryVector =
      leveldb_proto::ProtoDatabase<SnippetImageProto>::KeyEntryVector;

  RemoteSuggestionsDatabase(
      leveldb_proto::ProtoDatabaseProvider* proto_database_provider,
      const base::FilePath& database_dir,
      scoped_refptr<base::SequencedTaskRunner> task_runner);

  // Callbacks for ProtoDatabase<SnippetProto> operations.
  void OnDatabaseInited(leveldb_proto::Enums::InitStatus status);
  void OnDatabaseLoaded(SnippetsCallback callback,
                        bool success,
                        std::unique_ptr<std::vector<SnippetProto>> entries);
  void OnDatabaseSaved(bool success);

  // Callbacks for ProtoDatabase<SnippetImageProto> operations.
  void OnImageDatabaseInited(leveldb_proto::Enums::InitStatus status);
  void OnImageDatabaseLoaded(SnippetImageCallback callback,
                             bool success,
                             std::unique_ptr<SnippetImageProto> entry);
  void OnImageDatabaseSaved(bool success);

  void OnDatabaseError();

  void ProcessPendingLoads();

  void LoadSnippetsImpl(SnippetsCallback callback);
  void SaveSnippetsImpl(std::unique_ptr<KeyEntryVector> entries_to_save);

  void LoadImageImpl(const std::string& snippet_id,
                     SnippetImageCallback callback);
  void DeleteUnreferencedImages(
      std::unique_ptr<std::set<std::string>> references,
      bool load_keys_success,
      std::unique_ptr<std::vector<std::string>> image_keys);

  std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetProto>> database_;
  bool database_initialized_;
  std::vector<SnippetsCallback> pending_snippets_callbacks_;

  std::unique_ptr<leveldb_proto::ProtoDatabase<SnippetImageProto>>
      image_database_;
  bool image_database_initialized_;
  std::vector<std::pair<std::string, SnippetImageCallback>>
      pending_image_callbacks_;

  base::RepeatingClosure error_callback_;

  base::WeakPtrFactory<RemoteSuggestionsDatabase> weak_ptr_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(RemoteSuggestionsDatabase);
};

}  // namespace ntp_snippets

#endif  // COMPONENTS_NTP_SNIPPETS_REMOTE_REMOTE_SUGGESTIONS_DATABASE_H_