File: contextual_cueing_helper.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (101 lines) | stat: -rw-r--r-- 3,678 bytes parent folder | download | duplicates (2)
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
// 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_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_HELPER_H_
#define CHROME_BROWSER_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_HELPER_H_

#include <optional>

#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "base/types/expected.h"
#include "chrome/browser/contextual_cueing/contextual_cueing_enums.h"
#include "components/optimization_guide/core/optimization_guide_decision.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

class OptimizationGuideKeyedService;

namespace tabs {
class GlicNudgeController;
}  // namespace tabs

namespace contextual_cueing {

class ContextualCueingService;
class ScopedNudgeDecisionRecorder;

class ContextualCueingHelper
    : public content::WebContentsObserver,
      public content::WebContentsUserData<ContextualCueingHelper> {
 public:
  // Creates ContextualCueingHelper and attaches it the `web_contents` if
  // contextual cueing is enabled.
  static void MaybeCreateForWebContents(content::WebContents* web_contents);

  ContextualCueingHelper(const ContextualCueingHelper&) = delete;
  ContextualCueingHelper& operator=(const ContextualCueingHelper&) = delete;
  ~ContextualCueingHelper() override;

  // content::WebContentsObserver:
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void PrimaryMainDocumentElementAvailable() override;
  void OnFirstContentfulPaintInPrimaryMainFrame() override;
  void DocumentOnLoadCompletedInPrimaryMainFrame() override;

  // Returns when the last primary main frame navigation was committed if the
  // navigation was a same document navigation.
  std::optional<base::TimeTicks> last_same_doc_navigation_committed() const {
    return last_same_doc_navigation_committed_;
  }

  // Returns whether the last primary main frame navigation that was committed
  // has already past FCP.
  bool has_first_contentful_paint() const {
    return has_first_contentful_paint_;
  }

  tabs::GlicNudgeController* GetGlicNudgeController();

 private:
  ContextualCueingHelper(content::WebContents* contents,
                         OptimizationGuideKeyedService* ogks,
                         ContextualCueingService* ccs);

  // Called when optimization guide metadata is received.
  void OnOptimizationGuideCueingMetadata(
      base::TimeTicks document_available_time,
      optimization_guide::OptimizationGuideDecision decision,
      const optimization_guide::OptimizationMetadata& metadata);

  void OnCueingDecision(
      std::unique_ptr<ScopedNudgeDecisionRecorder> decision_recorder,
      base::TimeTicks document_available_time,
      base::expected<std::string, NudgeDecision> decision_result);

  bool IsBrowserBlockingNudges(ScopedNudgeDecisionRecorder* recorder);

  // When the last same doc navigation was committed.
  std::optional<base::TimeTicks> last_same_doc_navigation_committed_;

  bool has_first_contentful_paint_ = false;

  // Not owned and guaranteed to outlive `this`.
  raw_ptr<OptimizationGuideKeyedService> optimization_guide_keyed_service_ =
      nullptr;

  // Not owned and guaranteed to outlive `this`.
  raw_ptr<ContextualCueingService> contextual_cueing_service_ = nullptr;

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

  friend WebContentsUserData<ContextualCueingHelper>;
  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

}  // namespace contextual_cueing

#endif  // CHROME_BROWSER_CONTEXTUAL_CUEING_CONTEXTUAL_CUEING_HELPER_H_