File: history_clusters_tab_helper.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (107 lines) | stat: -rw-r--r-- 4,689 bytes parent folder | download | duplicates (4)
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
// 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_HISTORY_CLUSTERS_HISTORY_CLUSTERS_TAB_HELPER_H_
#define CHROME_BROWSER_HISTORY_CLUSTERS_HISTORY_CLUSTERS_TAB_HELPER_H_

#include <vector>

#include "base/gtest_prod_util.h"
#include "base/task/cancelable_task_tracker.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/history_types.h"
#include "components/page_load_metrics/common/page_end_reason.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace history_clusters {
class HistoryClustersService;
}

class HistoryClustersTabHelper
    : public content::WebContentsObserver,
      public content::WebContentsUserData<HistoryClustersTabHelper> {
 public:
  ~HistoryClustersTabHelper() override;

  HistoryClustersTabHelper(const HistoryClustersTabHelper&) = delete;
  HistoryClustersTabHelper& operator=(const HistoryClustersTabHelper&) = delete;

  // Called when the user copies the URL from the location bar.
  void OnOmniboxUrlCopied();
  // Called when the user shares the URL via mobile sharing hub.
  void OnOmniboxUrlShared();

  // Called by `HistoryTabHelper` right after submitting a new navigation for
  // `web_contents()` to HistoryService. We need close coordination with
  // History's conception of the visit lifetime.
  void OnUpdatedHistoryForNavigation(int64_t navigation_id,
                                     base::Time timestamp,
                                     const GURL& url);

  // Invoked for navigations that are tracked by UKM. Specifically, same-app
  // navigations aren't tracked individually in UKM and therefore won't receive
  // UKM's `page_end_reason` signal. Visits for such navigations will be
  // completed as soon as both the history rows query completes and the history
  // navigation ends. Visits that are tracked by UKM will additionally wait for
  // a UKM `page_end_reason`.
  void TagNavigationAsExpectingUkmNavigationComplete(int64_t navigation_id);

  // Updates the visit with `navigation_id` with `total_foreground_duration` and
  // `page_end_reason`. This also records the page end metrics, if necessary. It
  // returns a copy of the completed `AnnotatedVisit`'s
  // `VisitContextAnnotations`, if available.
  //
  // This should only be called once per navigation, as this may flush the visit
  // to HistoryClustersService.
  history::VisitContextAnnotations OnUkmNavigationComplete(
      int64_t navigation_id,
      base::TimeDelta total_foreground_duration,
      const page_load_metrics::PageEndReason page_end_reason);

  // content::WebContentsObserver:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void WebContentsDestroyed() override;
  void OnVisibilityChanged(content::Visibility visibility) override;

 private:
  FRIEND_TEST_ALL_PREFIXES(UkmPageLoadMetricsObserverTest,
                           DurationSinceLastVisitSeconds);

  explicit HistoryClustersTabHelper(content::WebContents* web_contents);
  friend class content::WebContentsUserData<HistoryClustersTabHelper>;

  void StartNewNavigationIfNeeded(int64_t navigation_id);

  // Computes and stores the page-end metrics. Can be called multiple times,
  // because we have a flag to prevent multiple recordings. Returns true if it
  // recorded page end metrics.
  void RecordPageEndMetricsIfNeeded(int64_t navigation_id);

  // Helper functions to return the memories and history services.
  // `GetHistoryClustersService()` may return nullptr (in tests).
  history_clusters::HistoryClustersService* GetHistoryClustersService();
  // `GetHistoryService()` may return nullptr.
  history::HistoryService* GetHistoryService();

  // The navigations initiated in this tab. Used for:
  // 1) On `OnUpdatedHistoryForNavigation()`, the last navigation will be
  //    assumed ended and its page end metrics will be recorded.
  // 2) On `OnOmniboxUrlCopied()`, the last navigation will be assumed to be the
  //    subject.
  // 3) On `WebContentsDestroyed()`, the `AnnotatedVisit` corresponding to these
  //    IDs will be assumed ended and their page end metrics will be recorded if
  //    they haven't already.
  std::vector<int64_t> navigation_ids_;

  // Task tracker for calls for the history service.
  base::CancelableTaskTracker task_tracker_;

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif  // CHROME_BROWSER_HISTORY_CLUSTERS_HISTORY_CLUSTERS_TAB_HELPER_H_