File: share_history.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 (106 lines) | stat: -rw-r--r-- 4,012 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 2021 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_SHARE_SHARE_HISTORY_H_
#define CHROME_BROWSER_SHARE_SHARE_HISTORY_H_

#include <string>
#include <vector>

#include "base/callback_list.h"
#include "base/memory/weak_ptr.h"
#include "base/supports_user_data.h"
#include "build/build_config.h"
#include "chrome/browser/share/proto/share_history_message.pb.h"
#include "components/leveldb_proto/public/proto_database.h"

class Profile;

namespace sharing {

// A ShareHistory instance stores and provides access to the history of which
// targets the user has shared to. This history is stored per-profile and
// cleared when the user clears browsing data.
class ShareHistory : public base::SupportsUserData::Data {
 public:
  struct Target {
    std::string component_name;
    int count;
  };

  using BackingDb = leveldb_proto::ProtoDatabase<mojom::ShareHistory>;
  using GetFlatHistoryCallback = base::OnceCallback<void(std::vector<Target>)>;

  static void CreateForProfile(Profile* profile);
  static ShareHistory* Get(Profile* profile);

  explicit ShareHistory(Profile* profile,
                        std::unique_ptr<BackingDb> backing_db = nullptr);
  ~ShareHistory() override;

  virtual void AddShareEntry(const std::string& component_name);

  // Returns the flattened share history. Each entry in this list contains
  // the total count of shares the corresponding target has received over
  // the past |window| days. It is required that |window| <= the backend's
  // WINDOW value. A window of -1 means all available history.
  virtual void GetFlatShareHistory(GetFlatHistoryCallback callback,
                                   int window = -1);

  virtual void Clear(const base::Time& start = base::Time(),
                     const base::Time& end = base::Time());

  // Don't call this.
  //
  // TODO(ellyjones): There should be a better way to deal with this - it's used
  // to deal with the fact that ShareHistory's destruction order wrt
  // ShareRanking is undefined, so ShareHistory can get torn down while
  // ShareRanking has a pending async call to it, after the pending call's reply
  // has been posted but before the posted response has been run.
  base::WeakPtr<ShareHistory> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

 protected:
  // Constructor for test fakes only - this constructor leaves this object in an
  // invalid state, so you had better override the public methods with their own
  // implementations that don't rely on the base ones!
  ShareHistory();

 private:
  void Init();
  void OnInitDone(leveldb_proto::Enums::InitStatus status);
  void OnInitialReadDone(bool ok, std::unique_ptr<mojom::ShareHistory> history);

  void FlushToBackingDb();

  // These two methods get or create entries in the in-memory protobuf; they do
  // not actually write back to the DB.
  mojom::DayShareHistory* DayShareHistoryForToday();
  mojom::TargetShareHistory* TargetShareHistoryByName(
      mojom::DayShareHistory* history,
      const std::string& target_name);

  bool init_finished_ = false;
  leveldb_proto::Enums::InitStatus db_init_status_;

  base::OnceClosureList post_init_callbacks_;

  std::unique_ptr<BackingDb> db_;

  // Cached copy of the data that resides on disk; initialization is complete
  // when this has first been loaded. After that point, any changes are synced
  // back to disk asynchronously, but the in-memory version is authoritative.
  mojom::ShareHistory history_;

  // Used so that callbacks supplied to the backing LevelDB won't call us back
  // if the Profile we're attached to is destroyed while we have a pending
  // callback. This *shouldn't* happen, since the LevelDB is *usually* attached
  // to the Profile too, but it seems like it might be possible anyway.
  base::WeakPtrFactory<ShareHistory> weak_factory_{this};
};

}  // namespace sharing

#endif  // CHROME_BROWSER_SHARE_SHARE_HISTORY_H_