File: history_embeddings_tab_helper.h

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 (126 lines) | stat: -rw-r--r-- 4,994 bytes parent folder | download | duplicates (5)
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
// Copyright 2024 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_EMBEDDINGS_HISTORY_EMBEDDINGS_TAB_HELPER_H_
#define CHROME_BROWSER_HISTORY_EMBEDDINGS_HISTORY_EMBEDDINGS_TAB_HELPER_H_

#include <optional>

#include "base/memory/weak_ptr.h"
#include "base/task/cancelable_task_tracker.h"
#include "base/time/time.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/resource_coordinator/tab_load_tracker.h"
#include "components/history/core/browser/history_types.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "url/gurl.h"

namespace history {
class HistoryService;
}
namespace history_embeddings {
class HistoryEmbeddingsService;
}
namespace passage_embeddings {
class PassageEmbedderModelObserver;
}

namespace content {
class NavigationHandle;
class WeakDocumentPtr;
}

class HistoryEmbeddingsTabHelper
    : public content::WebContentsObserver,
      public content::WebContentsUserData<HistoryEmbeddingsTabHelper>,
      public resource_coordinator::TabLoadTracker::Observer {
 public:
  ~HistoryEmbeddingsTabHelper() override;

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

  // 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(
      content::NavigationHandle* navigation_handle,
      base::Time timestamp,
      const GURL& url);

  // content::WebContentsObserver:
  void DidFinishLoad(content::RenderFrameHost* render_frame_host,
                     const GURL& validated_url) override;

  // resource_coordinator::TabLoadTracker:
  void OnLoadingStateChange(content::WebContents* web_contents,
                            LoadingState old_loading_state,
                            LoadingState new_loading_state) override;

  // Calls `RetrievePassages` for testing purposes only.
  void RetrievePassagesForTesting(
      history::URLID url_id,
      history::VisitID visit_id,
      base::Time visit_time,
      content::WeakDocumentPtr weak_render_frame_host);

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

  // Utility method to delay passage extraction until tabs are done loading.
  // Returns true if actually scheduled; false if weak pointer was invalidated.
  bool ScheduleExtraction(content::WeakDocumentPtr weak_render_frame_host);

  // This is called some time after `DidFinishLoad` to do passage extraction.
  // Calls may be canceled by weak pointer invalidation.
  void ExtractPassages(content::WeakDocumentPtr weak_render_frame_host);

  // Callback for `ExtractPassages()`. It's in a member method to enable
  // cancellation via `weak_factory_`.
  void ExtractPassagesWithHistoryData(
      content::WeakDocumentPtr weak_render_frame_host,
      history::QueryURLResult result);

  // Initiates async passage extraction from the given host's main frame.
  // When the extraction completes, the passages will be given to the
  // HistoryEmbeddingsService to be stored in the database along with their
  // embeddings.
  // It's in a member method to enable cancellation via `weak_factory_`.
  // Note: A `WeakDocumentPtr` is essentially a `WeakPtr<RenderFrameHost>`.
  void RetrievePassages(history::URLID url_id,
                        history::VisitID visit_id,
                        base::Time visit_time,
                        content::WeakDocumentPtr weak_render_frame_host);

  // Invalidates weak pointers and cancels any pending extraction callbacks.
  void CancelExtraction();

  // Helper functions to return the embeddings and history services.
  // `GetHistoryClustersService()` may return nullptr (in tests).
  history_embeddings::HistoryEmbeddingsService* GetHistoryEmbeddingsService();
  passage_embeddings::PassageEmbedderModelObserver*
  GetPassageEmbedderModelObserver();
  // `GetHistoryService()` may return nullptr.
  history::HistoryService* GetHistoryService();

  // Data saved from the `HistoryTabHelper` call to
  // `OnUpdatedHistoryForNavigation` which happens in `DidFinishNavigation`
  // and precedes `DidFinishLoad`.
  std::optional<base::Time> history_visit_time_;
  std::optional<GURL> history_url_;

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

  // This factory frequently invalidates existing weak pointers to cancel
  // delayed passage extraction.
  base::WeakPtrFactory<HistoryEmbeddingsTabHelper> weak_ptr_factory_{this};

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif  // CHROME_BROWSER_HISTORY_EMBEDDINGS_HISTORY_EMBEDDINGS_TAB_HELPER_H_