File: share_history.cc

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 (261 lines) | stat: -rw-r--r-- 8,561 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
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
// 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.

#include "chrome/browser/share/share_history.h"

#include "base/containers/flat_map.h"
#include "base/memory/ptr_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/share/proto/share_history_message.pb.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "content/public/browser/storage_partition.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/jni_string.h"
#include "chrome/browser/profiles/profile.h"

// Must come after other includes, because FromJniType() uses Profile.
#include "chrome/browser/share/jni_headers/ShareHistoryBridge_jni.h"

using base::android::JavaParamRef;
#endif

namespace sharing {

namespace {

const char* const kShareHistoryFolder = "share_history";

// This is the key used for the single entry in the backing LevelDB. The fact
// that it is the same string as the above folder name is a coincidence; please
// do not fold these constants together.
const char* const kShareHistoryKey = "share_history";

constexpr auto kMaxHistoryAge = base::Days(90);

int TodaysDay() {
  return (base::Time::Now() - base::Time::UnixEpoch()).InDays();
}

std::unique_ptr<ShareHistory::BackingDb> MakeDefaultDbForProfile(
    Profile* profile) {
  return profile->GetDefaultStoragePartition()
      ->GetProtoDatabaseProvider()
      ->GetDB<mojom::ShareHistory>(
          leveldb_proto::ProtoDbType::SHARE_HISTORY_DATABASE,
          profile->GetPath().AppendASCII(kShareHistoryFolder),
          base::ThreadPool::CreateSequencedTaskRunner(
              {base::MayBlock(), base::TaskPriority::BEST_EFFORT}));
}

bool DayOverlapsTimeRange(base::Time day_start,
                          base::Time start,
                          base::Time end) {
  const base::Time epoch = base::Time::UnixEpoch();
  int day = (day_start - epoch).InDays();
  int start_day = (start - epoch).InDays();
  int end_day = (end - epoch).InDays();
  return day >= start_day && day <= end_day;
}

}  // namespace

// static
void ShareHistory::CreateForProfile(Profile* profile) {
  CHECK(!profile->IsOffTheRecord());
  auto instance = std::make_unique<ShareHistory>(profile);
  profile->SetUserData(kShareHistoryKey, base::WrapUnique(instance.release()));
}

// static
ShareHistory* ShareHistory::Get(Profile* profile) {
  if (profile->IsOffTheRecord())
    return nullptr;

  base::SupportsUserData::Data* instance =
      profile->GetUserData(kShareHistoryKey);
  if (!instance) {
    CreateForProfile(profile);
    instance = profile->GetUserData(kShareHistoryKey);
  }
  return static_cast<ShareHistory*>(instance);
}

ShareHistory::~ShareHistory() = default;

ShareHistory::ShareHistory() = default;
ShareHistory::ShareHistory(Profile* profile,
                           std::unique_ptr<BackingDb> backing_db)
    : db_(backing_db ? std::move(backing_db)
                     : MakeDefaultDbForProfile(profile)) {
  Init();
}

void ShareHistory::AddShareEntry(const std::string& component_name) {
  if (!init_finished_) {
    post_init_callbacks_.AddUnsafe(base::BindOnce(
        &ShareHistory::AddShareEntry, base::Unretained(this), component_name));
    return;
  }

  if (db_init_status_ != leveldb_proto::Enums::kOK)
    return;

  mojom::DayShareHistory* day_history = DayShareHistoryForToday();
  mojom::TargetShareHistory* target_history =
      TargetShareHistoryByName(day_history, component_name);

  target_history->set_count(target_history->count() + 1);

  FlushToBackingDb();
}

void ShareHistory::GetFlatShareHistory(GetFlatHistoryCallback callback,
                                       int window) {
  if (!init_finished_) {
    post_init_callbacks_.AddUnsafe(base::BindOnce(
        &ShareHistory::GetFlatShareHistory, weak_factory_.GetWeakPtr(),
        std::move(callback), window));
    return;
  }

  if (db_init_status_ != leveldb_proto::Enums::kOK) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), std::vector<Target>()));
    return;
  }

  base::flat_map<std::string, int> counts;
  int today = TodaysDay();
  for (const auto& day : history_.day_histories()) {
    if (window != -1 && today - day.day() > window)
      continue;
    for (const auto& target : day.target_histories()) {
      counts[target.target().component_name()] += target.count();
    }
  }

  std::vector<Target> result;
  for (const auto& count : counts) {
    Target t;
    t.component_name = count.first;
    t.count = count.second;
    result.push_back(t);
  }

  std::sort(result.begin(), result.end(),
            [](const Target& a, const Target& b) { return a.count > b.count; });

  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE, base::BindOnce(std::move(callback), result));
}

void ShareHistory::Clear(const base::Time& start, const base::Time& end) {
  google::protobuf::RepeatedPtrField<mojom::DayShareHistory> histories_to_keep;
  for (const auto& day : history_.day_histories()) {
    base::Time day_start = base::Time::UnixEpoch() + base::Days(day.day());
    if (!DayOverlapsTimeRange(day_start, start, end)) {
      mojom::DayShareHistory this_day;
      this_day.CopyFrom(day);
      histories_to_keep.Add(std::move(this_day));
    }
  }

  history_.mutable_day_histories()->Swap(&histories_to_keep);

  FlushToBackingDb();
}

void ShareHistory::Init() {
  db_->Init(
      base::BindOnce(&ShareHistory::OnInitDone, weak_factory_.GetWeakPtr()));
}

void ShareHistory::OnInitDone(leveldb_proto::Enums::InitStatus status) {
  db_init_status_ = status;
  if (status != leveldb_proto::Enums::kOK) {
    // If the LevelDB initialization failed, follow the same state transitions
    // as in the happy case, but without going through LevelDB; i.e., act as
    // though the initial read failed, instead of the LevelDB initialization, so
    // that control always ends up in OnInitialReadDone.
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(&ShareHistory::OnInitialReadDone,
                                  weak_factory_.GetWeakPtr(), false,
                                  std::make_unique<mojom::ShareHistory>()));
    return;
  }

  db_->GetEntry(kShareHistoryKey,
                base::BindOnce(&ShareHistory::OnInitialReadDone,
                               weak_factory_.GetWeakPtr()));
}

void ShareHistory::OnInitialReadDone(
    bool ok,
    std::unique_ptr<mojom::ShareHistory> history) {
  if (ok && history)
    history_ = *history;
  init_finished_ = true;
  post_init_callbacks_.Notify();

  Clear(base::Time(), base::Time::Now() - kMaxHistoryAge);
}

void ShareHistory::FlushToBackingDb() {
  auto keyvals = std::make_unique<BackingDb::KeyEntryVector>();
  keyvals->push_back({kShareHistoryKey, history_});

  db_->UpdateEntries(std::move(keyvals),
                     std::make_unique<std::vector<std::string>>(),
                     base::DoNothing());
}

mojom::DayShareHistory* ShareHistory::DayShareHistoryForToday() {
  int today = TodaysDay();
  for (auto& day : *history_.mutable_day_histories()) {
    if (day.day() == today)
      return &day;
  }

  mojom::DayShareHistory* day = history_.mutable_day_histories()->Add();
  day->set_day(today);
  return day;
}

mojom::TargetShareHistory* ShareHistory::TargetShareHistoryByName(
    mojom::DayShareHistory* history,
    const std::string& target_name) {
  for (auto& target : *history->mutable_target_histories()) {
    if (target.target().component_name() == target_name)
      return &target;
  }

  mojom::TargetShareHistory* target =
      history->mutable_target_histories()->Add();
  target->mutable_target()->set_component_name(target_name);
  return target;
}

}  // namespace sharing

#if BUILDFLAG(IS_ANDROID)
void JNI_ShareHistoryBridge_AddShareEntry(JNIEnv* env,
                                          Profile* profile,
                                          const JavaParamRef<jstring>& name) {
  auto* instance = sharing::ShareHistory::Get(profile);
  if (instance)
    instance->AddShareEntry(base::android::ConvertJavaStringToUTF8(env, name));
}

void JNI_ShareHistoryBridge_Clear(JNIEnv* env, Profile* profile) {
  auto* instance = sharing::ShareHistory::Get(profile);
  if (instance)
    instance->Clear();
}
#endif