File: history_service_observer.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 (119 lines) | stat: -rw-r--r-- 5,002 bytes parent folder | download | duplicates (9)
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
// 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.

#include "components/segmentation_platform/internal/signals/history_service_observer.h"

#include "base/metrics/user_metrics.h"
#include "base/task/single_thread_task_runner.h"
#include "base/trace_event/trace_event.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/segmentation_platform/internal/database/segment_info_database.h"
#include "components/segmentation_platform/internal/database/storage_service.h"
#include "components/segmentation_platform/internal/signals/history_delegate_impl.h"
#include "components/segmentation_platform/internal/signals/url_signal_handler.h"
#include "components/segmentation_platform/internal/ukm_data_manager.h"

namespace segmentation_platform {

HistoryServiceObserver::HistoryServiceObserver(
    history::HistoryService* history_service,
    StorageService* storage_service,
    const std::string& profile_id,
    base::RepeatingClosure models_refresh_callback)
    : storage_service_(storage_service),
      url_signal_handler_(
          storage_service->ukm_data_manager()->GetOrCreateUrlHandler()),
      models_refresh_callback_(models_refresh_callback),
      profile_id_(profile_id),
      history_delegate_(
          std::make_unique<HistoryDelegateImpl>(history_service,
                                                url_signal_handler_,
                                                profile_id)) {
  history_observation_.Observe(history_service);
}
HistoryServiceObserver::HistoryServiceObserver()
    : storage_service_(nullptr), url_signal_handler_(nullptr) {}

HistoryServiceObserver::~HistoryServiceObserver() = default;

void HistoryServiceObserver::OnURLVisited(
    history::HistoryService* history_service,
    const history::URLRow& url_row,
    const history::VisitRow& new_visit) {
  url_signal_handler_->OnHistoryVisit(url_row.url(), profile_id_);
  history_delegate_->OnUrlAdded(url_row.url());
}

void HistoryServiceObserver::OnHistoryDeletions(
    history::HistoryService* history_service,
    const history::DeletionInfo& deletion_info) {
  TRACE_EVENT0("segmentation_platform",
               "HistoryServiceObserver::OnHistoryDeletions");

  // If the history deletion was not from expiration or if the whole history
  // database was removed, delete the segment results computed based on URL
  // data.
  if (deletion_info.IsAllHistory() || !deletion_info.is_from_expiration()) {
    base::RecordAction(
        base::UserMetricsAction("SegmentationPurgeTriggeredByHistoryDelete"));
    DeleteResultsForHistoryBasedSegments();
  }

  if (deletion_info.IsAllHistory()) {
    url_signal_handler_->OnUrlsRemovedFromHistory({}, /*all_urls=*/true);
    return;
  }
  std::vector<GURL> urls;
  for (const auto& info : deletion_info.deleted_rows())
    urls.push_back(info.url());
  url_signal_handler_->OnUrlsRemovedFromHistory(urls, /*all_urls=*/false);
  history_delegate_->OnUrlRemoved(urls);
}

void HistoryServiceObserver::SetHistoryBasedSegments(
    base::flat_set<proto::SegmentId> history_based_segments) {
  history_based_segments_ = std::move(history_based_segments);
  // If a delete is pending, clear the results now.
  if (pending_deletion_based_on_history_based_segments_) {
    DeleteResultsForHistoryBasedSegments();

    // Only clear results once on first init. This method can be called multiple
    // times during the session when model updates.
    pending_deletion_based_on_history_based_segments_ = false;
  }
}

void HistoryServiceObserver::DeleteResultsForHistoryBasedSegments() {
  if (!history_based_segments_) {
    // Set the delete flag to clear the history based results when
    // SetHistoryBasedSegments() is called.
    pending_deletion_based_on_history_based_segments_ = true;
    return;
  }
  for (const auto segment_id : *history_based_segments_) {
    // For Server models.
    storage_service_->segment_info_database()->SaveSegmentResult(
        segment_id, proto::ModelSource::SERVER_MODEL_SOURCE, std::nullopt,
        base::DoNothing());

    // For Default models.
    storage_service_->segment_info_database()->SaveSegmentResult(
        segment_id, proto::ModelSource::DEFAULT_MODEL_SOURCE, std::nullopt,
        base::DoNothing());
  }

  // If a model refresh was recently posted, then cancel the task and restart
  // the 30 second timer. This is to avoid running models often when user clears
  // multiple history entries at once.
  if (posted_model_refresh_task_) {
    posted_model_refresh_task_->Cancel();
  }
  posted_model_refresh_task_ = std::make_unique<base::CancelableOnceClosure>(
      base::BindOnce(models_refresh_callback_));
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
      FROM_HERE, posted_model_refresh_task_->callback(), base::Minutes(1));
}

}  // namespace segmentation_platform