File: share_ranking.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 (592 lines) | stat: -rw-r--r-- 21,363 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// 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_ranking.h"

#include <algorithm>
#include <vector>

#include "base/containers/to_vector.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/share/default_ranking.h"
#include "chrome/browser/share/share_history.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "content/public/browser/storage_partition.h"
#include "ui/base/l10n/l10n_util.h"

#if BUILDFLAG(IS_ANDROID)
#include "base/android/callback_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/locale_utils.h"

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

using base::android::JavaParamRef;
#endif

namespace sharing {

const char* const ShareRanking::kMoreTarget = "$more";

namespace {

const char* const kShareRankingFolder = "share_ranking";
const char* const kShareRankingKey = "share_ranking";

// TODO(ellyjones): This should probably be a field trial.
const int kRecentWindowDays = 7;

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

bool RankingContains(const std::vector<std::string>& ranking,
                     const std::string& element,
                     size_t upto_index = SIZE_MAX) {
  for (size_t i = 0; i < ranking.size() && i < upto_index; ++i) {
    if (ranking[i] == element)
      return true;
  }
  return false;
}

std::vector<std::string> OrderByUses(const std::vector<std::string>& ranking,
                                     const std::map<std::string, int>& uses) {
  std::vector<std::string> ordered_ranking(ranking.begin(), ranking.end());
  std::sort(ordered_ranking.begin(), ordered_ranking.end(),
            [&](const std::string& a, const std::string& b) -> bool {
              int ua = uses.count(a) > 0 ? uses.at(a) : 0;
              int ub = uses.count(b) > 0 ? uses.at(b) : 0;
              return ua > ub;
            });
  return ordered_ranking;
}

std::string HighestUnshown(const std::vector<std::string>& ranking,
                           const std::map<std::string, int>& uses,
                           size_t length) {
  CHECK_GT(length, 0u);
  std::vector<std::string> unshown =
      base::ToVector(base::span(ranking).subspan(length));
  return !unshown.empty() ? OrderByUses(unshown, uses).front() : "";
}

std::string LowestShown(const std::vector<std::string>& ranking,
                        const std::map<std::string, int>& uses,
                        size_t length) {
  CHECK_GT(length, 0u);
  std::vector<std::string> shown =
      base::ToVector(base::span(ranking).first(length));
  return !shown.empty() ? OrderByUses(shown, uses).back() : "";
}

void SwapRankingElement(std::vector<std::string>& ranking,
                        const std::string& from,
                        const std::string& to) {
  auto from_loc = std::ranges::find(ranking, from);
  auto to_loc = std::ranges::find(ranking, to);

  CHECK(from_loc != ranking.end());
  CHECK(to_loc != ranking.end());

  *from_loc = to;
  *to_loc = from;
}

std::vector<std::string> ReplaceUnavailableEntries(
    const std::vector<std::string>& ranking,
    const std::vector<std::string>& available) {
  std::vector<std::string> result;
  std::ranges::transform(
      ranking, std::back_inserter(result), [&](const std::string& e) {
        return RankingContains(available, e) ? e : std::string();
      });
  return result;
}

void FillGaps(std::vector<std::string>& ranking,
              const std::vector<std::string>& available,
              size_t length) {
  // Take the tail of the ranking (the part that won't be shown on the screen),
  // remove items that aren't available on the system. These will be the first
  // apps used for empty slots.
  std::vector<std::string> unused_available =
      base::ToVector(base::span(ranking).subspan(length));
  std::erase_if(unused_available, [&](const std::string& e) {
    return !RankingContains(available, e);
  });

  // Now, append the rest of the system apps (those not already included) to
  // unused_available. These will be the apps that can handle the share type and
  // that are available on the system but not included in the old ranking at
  // all, so these are the lowest priority targets, because they are likely to
  // be things like "Bluetooth" which users almost never share to.
  for (const auto& app : available) {
    if (!RankingContains(unused_available, app))
      unused_available.push_back(app);
  }

  base::span<std::string> candidates(unused_available);

  for (size_t i = 0; i < length && !candidates.empty(); ++i) {
    std::string& candidate = candidates.front();
    if (ranking[i].empty() && !candidate.empty()) {
      ranking[i] = std::move(candidate);
      candidates = candidates.subspan<1>();
    }
  }
}

std::vector<std::string> MaybeUpdateRankingFromHistory(
    const std::vector<std::string>& old_ranking,
    const std::map<std::string, int>& recent_share_history,
    const std::map<std::string, int>& all_share_history,
    size_t length) {
  const double DAMPENING = 1.1;

  if (length == 0) {
    // Special case: if length is 0 here, the only thing that will be shown is
    // the "More" tile, and the logic below to find lowest/highest within the
    // first length tiles will all break. Bail out here.
    return old_ranking;
  }

  const std::string lowest_shown_recent =
      LowestShown(old_ranking, recent_share_history, length);
  const std::string lowest_shown_all =
      LowestShown(old_ranking, all_share_history, length);
  const std::string highest_unshown_recent =
      HighestUnshown(old_ranking, recent_share_history, length);
  const std::string highest_unshown_all =
      HighestUnshown(old_ranking, all_share_history, length);

  std::vector<std::string> new_ranking = old_ranking;

  auto recent_count_for = [&](const std::string& key) {
    return recent_share_history.count(key) > 0 ? recent_share_history.at(key)
                                               : 0;
  };

  auto all_count_for = [&](const std::string& key) {
    return all_share_history.count(key) > 0 ? all_share_history.at(key) : 0;
  };

  if (!highest_unshown_recent.empty() &&
      recent_count_for(highest_unshown_recent) >
          recent_count_for(lowest_shown_recent) * DAMPENING) {
    SwapRankingElement(new_ranking, lowest_shown_recent,
                       highest_unshown_recent);
  } else if (!highest_unshown_all.empty() &&
             all_count_for(highest_unshown_all) >
                 all_count_for(lowest_shown_all) * DAMPENING) {
    SwapRankingElement(new_ranking, lowest_shown_all, highest_unshown_all);
  }

  CHECK_EQ(old_ranking.size(), new_ranking.size());
  return new_ranking;
}

ShareRanking::Ranking AppendUpToLength(
    const ShareRanking::Ranking& ranking,
    const std::map<std::string, int>& history,
    size_t length) {
  std::vector<std::string> history_keys;
  for (const auto& it : history)
    history_keys.push_back(it.first);
  ShareRanking::Ranking all = OrderByUses(history_keys, history);
  ShareRanking::Ranking result = ranking;
  while (result.size() < length && !all.empty()) {
    if (!RankingContains(result, all.front())) {
      result.push_back(all.front());
      all.erase(all.begin());
    }
  }
  return result;
}

#if BUILDFLAG(IS_ANDROID)
void RunJniRankCallback(base::android::ScopedJavaGlobalRef<jobject> callback,
                        JNIEnv* env,
                        std::optional<ShareRanking::Ranking> ranking) {
  auto result = base::android::ToJavaArrayOfStrings(env, ranking.value());
  base::android::RunObjectCallbackAndroid(callback, result);
}
#endif

#if DCHECK_IS_ON()
bool EveryElementInList(const std::vector<std::string>& ranking,
                        const std::vector<std::string>& available) {
  for (const auto& e : ranking) {
    if (!RankingContains(available, e))
      return false;
  }
  return true;
}

bool ElementIndexesAreUnchanged(const std::vector<std::string>& display,
                                const std::vector<std::string>& old,
                                size_t length) {
  for (size_t i = 0; i < display.size() && i < length; ++i) {
    if (RankingContains(old, display[i], length) && display[i] != old[i])
      return false;
  }
  return true;
}

bool AtMostOneSlotChanged(const std::vector<std::string>& old_ranking,
                          const std::vector<std::string>& new_ranking,
                          size_t length) {
  bool change_seen = false;
  for (size_t i = 0; i < old_ranking.size() && i < length; ++i) {
    if (old_ranking[i] != new_ranking[i]) {
      if (change_seen)
        return false;
      else
        change_seen = true;
    }
  }
  return true;
}

#endif  // DCHECK_IS_ON()

std::map<std::string, int> BuildHistoryMap(
    const std::vector<ShareHistory::Target>& flat_history) {
  std::map<std::string, int> result;
  for (const auto& entry : flat_history)
    result[entry.component_name] += entry.count;
  return result;
}

std::vector<std::string> AddMissingItemsFromHistory(
    const std::vector<std::string>& existing,
    const std::map<std::string, int>& history) {
  std::vector<std::string> updated = existing;
  for (const auto& item : history) {
    if (!RankingContains(updated, item.first))
      updated.push_back(item.first);
  }
  return updated;
}

}  // namespace

ShareRanking* ShareRanking::Get(Profile* profile) {
  if (profile->IsOffTheRecord())
    return nullptr;

  base::SupportsUserData::Data* instance =
      profile->GetUserData(kShareRankingKey);
  if (!instance) {
    auto new_instance = std::make_unique<ShareRanking>(profile);
    instance = new_instance.get();
    profile->SetUserData(kShareRankingKey, std::move(new_instance));
  }
  return static_cast<ShareRanking*>(instance);
}

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

ShareRanking::~ShareRanking() = default;

void ShareRanking::UpdateRanking(const std::string& type, Ranking ranking) {
  if (!init_finished_) {
    post_init_callbacks_.AddUnsafe(base::BindOnce(
        &ShareRanking::UpdateRanking, base::Unretained(this), type, ranking));
    return;
  }

  ranking_[type] = ranking;
  if (db_init_status_ == leveldb_proto::Enums::kOK)
    FlushToBackingDb(type);
}

void ShareRanking::GetRanking(const std::string& type,
                              GetRankingCallback callback) {
  if (!init_finished_) {
    post_init_callbacks_.AddUnsafe(base::BindOnce(&ShareRanking::GetRanking,
                                                  base::Unretained(this), type,
                                                  std::move(callback)));
    return;
  }

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

  if (ranking_.contains(type)) {
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback),
                                  std::make_optional(ranking_[type])));
    return;
  }

  db_->GetEntry(type, base::BindOnce(&ShareRanking::OnBackingGetDone,
                                     weak_factory_.GetWeakPtr(), type,
                                     std::move(callback)));
}

void ShareRanking::Rank(ShareHistory* history,
                        const std::string& type,
                        const std::vector<std::string>& available_on_system,
                        size_t fold,
                        size_t length,
                        bool persist_update,
                        GetRankingCallback callback) {
  auto pending_call = std::make_unique<PendingRankCall>();
  pending_call->type = type;
  pending_call->available_on_system = available_on_system;
  pending_call->fold = fold;
  pending_call->length = length;
  pending_call->persist_update = persist_update;
  pending_call->callback = std::move(callback);
  pending_call->history_db = history->GetWeakPtr();

  history->GetFlatShareHistory(base::BindOnce(&ShareRanking::OnRankGetAllDone,
                                              weak_factory_.GetWeakPtr(),
                                              std::move(pending_call)));
}

void ShareRanking::Clear(const base::Time& start, const base::Time& end) {
  // Since the ranking doesn't contain timestamps (they wouldn't really make
  // sense), there's no way to remove only ranking data that came from the
  // specified deletion interval. Instead, we forget all the ranking data
  // altogether whenever we're clearing any interval - this almost always
  // over-deletes, unfortunately.
  ranking_.clear();
  db_->UpdateEntriesWithRemoveFilter(
      std::make_unique<BackingDb::KeyEntryVector>(),
      base::BindRepeating([](const std::string& key) -> bool { return true; }),
      base::DoNothing());
}

// static
void ShareRanking::ComputeRanking(
    const std::map<std::string, int>& all_share_history,
    const std::map<std::string, int>& recent_share_history,
    const Ranking& old_ranking,
    const std::vector<std::string>& available_on_system,
    size_t fold,
    size_t length,
    Ranking* display_ranking,
    Ranking* persisted_ranking) {
  // Preconditions:
  CHECK_GT(fold, 0u);
  CHECK_GT(length, 0u);
  CHECK_LE(fold, length);
  CHECK_GE(old_ranking.size(), length - 1);

  Ranking augmented_old_ranking = AddMissingItemsFromHistory(
      AddMissingItemsFromHistory(old_ranking, all_share_history),
      recent_share_history);

  // If the fold and the length are equal, fill up to length - 1 from history,
  // leaving the last slot for $more. If they aren't equal, the fold must be
  // lower, so fill all the way up to the fold. After that, the code right below
  // will fill the slots between fold and length - 1 with more entries, leaving
  // the length - 1 slot for $more.
  std::vector<std::string> new_ranking = MaybeUpdateRankingFromHistory(
      augmented_old_ranking, all_share_history, recent_share_history,
      fold < length ? fold : length - 1);

  if (fold != length) {
    new_ranking =
        AppendUpToLength(new_ranking, recent_share_history, length - 1);
    new_ranking = AppendUpToLength(new_ranking, all_share_history, length - 1);
  }

  Ranking computed_display_ranking =
      ReplaceUnavailableEntries(new_ranking, available_on_system);

  FillGaps(computed_display_ranking, available_on_system, length - 1);

  computed_display_ranking.resize(length);
  computed_display_ranking[length - 1] = kMoreTarget;

  *persisted_ranking = new_ranking;
  *display_ranking = computed_display_ranking;

  // Postconditions. These ones require a bunch of computation so they're
  // DCHECK-only.
#if DCHECK_IS_ON()
  {
    std::vector<std::string> available = available_on_system;
    available.push_back(kMoreTarget);

    DCHECK(EveryElementInList(*display_ranking, available));
    DCHECK(ElementIndexesAreUnchanged(*display_ranking, old_ranking, fold - 1));
    DCHECK(AtMostOneSlotChanged(old_ranking, *persisted_ranking, fold - 1));

    DCHECK(RankingContains(*display_ranking, kMoreTarget));
  }
#endif  // DCHECK_IS_ON()

  CHECK_EQ(display_ranking->size(), length);
  CHECK_GE(persisted_ranking->size(), length - 1);
}

ShareRanking::PendingRankCall::PendingRankCall() = default;
ShareRanking::PendingRankCall::~PendingRankCall() = default;

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

void ShareRanking::OnInitDone(leveldb_proto::Enums::InitStatus status) {
  db_init_status_ = status;
  init_finished_ = true;
  post_init_callbacks_.Notify();
}

void ShareRanking::OnBackingGetDone(
    std::string key,
    GetRankingCallback callback,
    bool ok,
    std::unique_ptr<proto::ShareRanking> ranking) {
  if (!ok || db_init_status_ != leveldb_proto::Enums::kOK || !ranking) {
    std::move(callback).Run(std::nullopt);
    return;
  }

  ranking_[key].clear();
  for (const auto& it : ranking->targets()) {
    ranking_[key].push_back(it);
  }

  std::move(callback).Run(std::make_optional(ranking_[key]));
}

void ShareRanking::FlushToBackingDb(const std::string& key) {
  proto::ShareRanking proto;
  for (const auto& element : ranking_[key]) {
    proto.mutable_targets()->Add(std::string(element));
  }

  auto keyvals = std::make_unique<BackingDb::KeyEntryVector>();
  keyvals->push_back({key, proto});
  db_->UpdateEntries(std::move(keyvals),
                     std::make_unique<std::vector<std::string>>(),
                     base::DoNothing());
}

void ShareRanking::OnRankGetAllDone(std::unique_ptr<PendingRankCall> pending,
                                    std::vector<ShareHistory::Target> history) {
  pending->all_history = history;
  if (pending->history_db) {
    auto history_db = pending->history_db;
    history_db->GetFlatShareHistory(
        base::BindOnce(&ShareRanking::OnRankGetRecentDone,
                       weak_factory_.GetWeakPtr(), std::move(pending)),
        kRecentWindowDays);
  } else {
    std::move(pending->callback).Run(std::nullopt);
  }
}
void ShareRanking::OnRankGetRecentDone(
    std::unique_ptr<PendingRankCall> pending,
    std::vector<ShareHistory::Target> history) {
  pending->recent_history = history;
  // Grab the type out of pending before std::move()ing from it.
  std::string type = pending->type;
  GetRanking(type,
             base::BindOnce(&ShareRanking::OnRankGetOldRankingDone,
                            weak_factory_.GetWeakPtr(), std::move(pending)));
}
void ShareRanking::OnRankGetOldRankingDone(
    std::unique_ptr<PendingRankCall> pending,
    std::optional<Ranking> ranking) {
  if (!ranking)
    ranking = GetDefaultInitialRankingForType(pending->type);

  Ranking display, persisted;
  ComputeRanking(BuildHistoryMap(pending->all_history),
                 BuildHistoryMap(pending->recent_history), *ranking,
                 pending->available_on_system, pending->fold, pending->length,
                 &display, &persisted);

  if (pending->persist_update)
    UpdateRanking(pending->type, persisted);

  std::move(pending->callback).Run(display);
}

ShareRanking::Ranking ShareRanking::GetDefaultInitialRankingForType(
    const std::string& type) {
#if BUILDFLAG(IS_ANDROID)
  // On Android, just use the app's default locale string - we don't have a pref
  // locale to consult regardless, and l10n_util::GetApplicationLocale can do
  // blocking disk IO (!) while it checks whether we have a string pack for the
  // various eligible locales. We don't care about strings here, so just go with
  // what the system is set to, and don't block on the UI thread.
  std::string locale = base::android::GetDefaultLocaleString();
#else
  std::string locale = l10n_util::GetApplicationLocale("", false);
#endif
  return initial_ranking_for_test_.value_or(
      DefaultRankingForLocaleAndType(locale, type));
}

}  // namespace sharing

#if BUILDFLAG(IS_ANDROID)

void JNI_ShareRankingBridge_Rank(JNIEnv* env,
                                 Profile* profile,
                                 std::string& type,
                                 std::vector<std::string>& available,
                                 jint jfold,
                                 jint jlength,
                                 jboolean jpersist,
                                 const JavaParamRef<jobject>& jcallback) {
  base::android::ScopedJavaGlobalRef<jobject> callback(jcallback);

  if (profile->IsOffTheRecord()) {
    // For incognito/guest profiles, we use the source ranking from the parent
    // normal profile but never write anything back to that profile, meaning the
    // user will get their existing ranking but no change to it will be made
    // based on incognito activity.
    CHECK(!jpersist);
    profile = profile->GetOriginalProfile();
  }

  auto* history = sharing::ShareHistory::Get(profile);
  auto* ranking = sharing::ShareRanking::Get(profile);

  CHECK(history);
  CHECK(ranking);

  size_t fold = base::checked_cast<size_t>(jfold);
  size_t length = base::checked_cast<size_t>(jlength);

  ranking->Rank(
      history, type, available, fold, length, jpersist,
      base::BindOnce(&sharing::RunJniRankCallback, std::move(callback),
                     // TODO(ellyjones): Is it safe to unretained env here?
                     base::Unretained(env)));
}

#endif  // BUILDFLAG(IS_ANDROID)