File: contextual_cueing_service.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (193 lines) | stat: -rw-r--r-- 7,253 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
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
// Copyright 2025 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_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_SERVICE_H_
#define CHROME_BROWSER_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_SERVICE_H_

#include <optional>
#include <string>
#include <vector>

#include "base/callback_list.h"
#include "base/containers/lru_cache.h"
#include "base/containers/queue.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_enums.h"
#include "chrome/browser/contextual_cueing/nudge_cap_tracker.h"
#include "chrome/browser/page_content_annotations/page_content_extraction_service.h"
#include "components/keyed_service/core/keyed_service.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "url/gurl.h"
#include "url/origin.h"

class OptimizationGuideKeyedService;
class PrefService;
class TemplateURLService;

namespace content {
class WebContents;
}  // namespace content

namespace predictors {
class LoadingPredictor;
}  // namespace predictors

namespace tabs {
enum class GlicNudgeActivity;
}  // namespace tabs

namespace contextual_cueing {

class ZeroStateSuggestionsRequest;

using GlicSuggestionsCallbackList =
    base::OnceCallbackList<void(std::vector<std::string>)>;
using GlicSuggestionsCallback = GlicSuggestionsCallbackList::CallbackType;

class ContextualCueingService
    : public KeyedService,
      page_content_annotations::PageContentExtractionService::Observer {
 public:
  ContextualCueingService(
      page_content_annotations::PageContentExtractionService*
          page_content_extraction_service,
      OptimizationGuideKeyedService* optimization_guide_keyed_service,
      predictors::LoadingPredictor* loading_predictor,
      PrefService* pref_service,
      TemplateURLService* template_url_service);
  ~ContextualCueingService() override;

  // Reports a page load happened to `url`, and is used to keep track of quiet
  // page loads requirement after a cueing UI is shown.
  void ReportPageLoad();

  // Called when cueing nudge activity happens.
  void OnNudgeActivity(content::WebContents* web_contents,
                       base::TimeTicks document_available_time,
                       tabs::GlicNudgeActivity activity);

  // Should be called when the cueing UI is shown for the tab with `url`.
  void CueingNudgeShown(const GURL& url);

  // Should be called when the cueing UI is dismissed by the user.
  void CueingNudgeDismissed();

  // Should be called when the nudge is clicked on by the user.
  void CueingNudgeClicked();

  // Returns if a nudge should be shown and is not blocked by feature
  // engagement constraints for navigation to `url`, and if not, why.
  NudgeDecision CanShowNudge(const GURL& url);

  base::WeakPtr<ContextualCueingService> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

  // Informs `this` to prepare fetching for zero state suggestions for GLIC.
  // Note that this *will not* actually do the fetch and it is intended for the
  // caller to call `GetContextualGlicZeroStateSuggestions` to actually fetch
  // the suggestions.
  void PrepareToFetchContextualGlicZeroStateSuggestions(
      content::WebContents* web_contents);

  // Returns zero state suggestions for focused tab for GLIC. Virtual for
  // testing.
  virtual void GetContextualGlicZeroStateSuggestionsForFocusedTab(
      content::WebContents* web_contents,
      bool is_fre,
      std::optional<std::vector<std::string>> supported_tools,
      GlicSuggestionsCallback callback);

  // Returns whether suggestions are going to be generated. Invokes `callback`
  // with zero state suggestions for pinned tabs as represented by
  // `pinned_web_contents`. Virtual for testing.
  virtual bool GetContextualGlicZeroStateSuggestionsForPinnedTabs(
      std::vector<content::WebContents*> pinned_web_contents,
      bool is_fre,
      std::optional<std::vector<std::string>> supported_tools,
      const content::WebContents* focused_tab,
      GlicSuggestionsCallback callback);

  // Returns the pinned tabs that are in an outstanding request if there is one.
  std::optional<std::vector<content::WebContents*>>
  GetOutstandingPinnedTabsContents();

 private:
  // page_content_annotations::PageContentExtractionService::Observer:
  void OnPageContentExtracted(
      content::Page& page,
      const optimization_guide::proto::AnnotatedPageContent& page_content)
      override;

  // Returns true if nudge should not be shown due to the backoff rule.
  bool IsNudgeBlockedByBackoffRule() const;

  // Returns true if the given url is of a page type eligible for contextual
  // suggestions.
  bool IsPageTypeEligibleForContextualSuggestions(GURL url) const;

  // Utility method to create the initial zero state suggestions request.
  std::unique_ptr<ZeroStateSuggestionsRequest> MakeZeroStateSuggestionsRequest(
      const std::vector<content::WebContents*>& web_contents_list,
      bool is_fre,
      std::optional<std::vector<std::string>> supported_tools,
      const content::WebContents* focused_tab);

  // Callback invoked when pinned tabs suggestions are received.
  void OnPinnedTabsSuggestionsReceived(
      base::TimeTicks fetch_begin_time,
      ZeroStateSuggestionsRequest* pinned_tabs_request,
      GlicSuggestionsCallback callback,
      std::vector<std::string> suggestions);

  // Tracker to limit the number of nudges shown over a certain duration.
  NudgeCapTracker recent_nudge_tracker_;

  // Number of times the cueing nudge has been dismissed (i.e. closed by the
  // user). This count resets to 0 if nudge is clicked on by the user.
  int dismiss_count_ = 0;

  // The end of the backoff period triggered by the last nudge dismissal.
  std::optional<base::TimeTicks> dismiss_backoff_end_time_;

  // The end of the backoff period triggered by the last shown nudge.
  std::optional<base::TimeTicks> shown_backoff_end_time_;

  // A counter for how many subsequent page load events will be prevented from
  // showing a nudge. This is to limit the frequency at which consecutive page
  // loads can trigger nudges.
  size_t remaining_quiet_loads_ = 0;

  // Maintains the recently visited origins along with their nudge cap tracking.
  base::LRUCache<url::Origin, NudgeCapTracker> recent_visited_origins_;

  // Holds the latest pinned tabs zero state suggestions request.
  std::unique_ptr<ZeroStateSuggestionsRequest>
      pinned_tabs_zero_state_suggestions_request_;

  raw_ptr<page_content_annotations::PageContentExtractionService>
      page_content_extraction_service_ = nullptr;

  raw_ptr<OptimizationGuideKeyedService> optimization_guide_keyed_service_ =
      nullptr;

  raw_ptr<predictors::LoadingPredictor> loading_predictor_ = nullptr;

  raw_ptr<PrefService> pref_service_ = nullptr;

  raw_ptr<TemplateURLService> template_url_service_ = nullptr;

  // Stores model execution url to save look up time.
  GURL mes_url_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<ContextualCueingService> weak_ptr_factory_{this};
};

}  // namespace contextual_cueing

#endif  // CHROME_BROWSER_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_SERVICE_H_