File: omnibox_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 (142 lines) | stat: -rw-r--r-- 5,754 bytes parent folder | download | duplicates (3)
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
// Copyright 2020 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_UI_OMNIBOX_OMNIBOX_TAB_HELPER_H_
#define CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_TAB_HELPER_H_

#include <optional>

#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "base/scoped_observation.h"
#include "base/timer/elapsed_timer.h"
#include "chrome/browser/page_content_annotations/page_content_extraction_service.h"
#include "components/omnibox/common/omnibox_focus_state.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "third_party/blink/public/mojom/content_extraction/frame_metadata_observer_registry.mojom.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"

namespace content {
class WebContents;
class RenderFrameHost;
}  // namespace content

class Profile;

// Serves as a bridge between omnibox and other UIs. Allows registration of
// observers to listen for omnibox updates. Also allows the omnibox to change
// rendering based on the web contents.
class OmniboxTabHelper
    : public content::WebContentsUserData<OmniboxTabHelper>,
      public page_content_annotations::PageContentExtractionService::Observer,
      public content::WebContentsObserver,
      public blink::mojom::FrameMetadataObserver {
 public:
  // Observer to listen for updates from OmniboxTabHelper.
  class Observer : public base::CheckedObserver {
   public:
    // Invoked when the omnibox input state is changed.
    virtual void OnOmniboxInputStateChanged() = 0;

    // Invoked when the omnibox input is in progress.
    virtual void OnOmniboxInputInProgress(bool in_progress) = 0;

    // Called to indicate that the omnibox focus state changed with the given
    // |reason|.
    virtual void OnOmniboxFocusChanged(OmniboxFocusState state,
                                       OmniboxFocusChangeReason reason) = 0;

    // Invoked when the omnibox popup visibility changes.
    virtual void OnOmniboxPopupVisibilityChanged(bool popup_is_open) = 0;
  };

  // Invoked when the frame metadata changes.
  void OnPaidContentMetadataChanged(bool has_paid_content) override;

  ~OmniboxTabHelper() override;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Calls forwarded from omnibox. See OmniboxClient for details.
  void OnInputStateChanged();
  void OnInputInProgress(bool in_progress);
  void OnFocusChanged(OmniboxFocusState state, OmniboxFocusChangeReason reason);
  void OnPopupVisibilityChanged(
      bool popup_is_open,
      metrics::OmniboxEventProto::PageClassification page_classification);

  // Returns true if the current page has the paywall signal in the Annotated
  // Page Content. Returns false if the page does not have the paywall signal.
  // Returns std::nullopt if the page content wasn't yet extracted and therefore
  // the signal could not be calculated.
  std::optional<bool> IsPagePaywalled();

  // Returns the previously observed omnibox focus state.
  OmniboxFocusState focus_state() const;

 private:
  OmniboxTabHelper(content::WebContents* contents, Profile* profile);
  friend class content::WebContentsUserData<OmniboxTabHelper>;
  WEB_CONTENTS_USER_DATA_KEY_DECL();

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

  // content::WebContentsObserver
  void PrimaryPageChanged(content::Page& page) override;
  void PrimaryMainDocumentElementAvailable() override;
  void DOMContentLoaded(content::RenderFrameHost* render_frame_host) override;

  // Logs the timings from a navigation to the omnibox being focused, IFF they
  // have not already been logged for this navigation.
  void MaybeLogNavigationToPopupShownTimings(
      metrics::OmniboxEventProto::PageClassification page_classification);

  // Logs the paywall signal for the current page.
  void MaybeLogPaywallSignal();

  // Adds the frame metadata observer.
  void AddMetadataObserver(content::Page& page);

  // Whether the current page has a paywall signal in the Annotated Page
  // Content. std::nullopt if the page content wasn't yet extracted and
  // therefore the signal could not be calculated.
  std::optional<bool> page_has_apc_paywall_signal_;

  // The time when the primary page changed.
  std::optional<base::ElapsedTimer> primary_page_changed_time_;

  // The time when the primary main document element was available.
  std::optional<base::ElapsedTimer>
      primary_main_document_element_available_time_;

  // The time when the DOMContentLoaded event was fired.
  std::optional<base::ElapsedTimer> dom_content_loaded_time_;

  // Whether the timings from a navigation to the omnibox being focused have
  // been logged for this navigation.
  bool logged_current_navigation_timings_ = false;
  OmniboxFocusState focus_state_ = OmniboxFocusState::OMNIBOX_FOCUS_NONE;

  // Observer to observer Annotated Page Content updates. Updates are fire on
  // every page, not only the current tab. The page content is generated a few
  // seconds after page load, once the page has stabilized.
  base::ScopedObservation<
      page_content_annotations::PageContentExtractionService,
      page_content_annotations::PageContentExtractionService::Observer>
      page_content_service_observation_{this};

  base::ObserverList<Observer> observers_;

  mojo::Receiver<blink::mojom::FrameMetadataObserver>
      frame_metadata_observer_receiver_{this};
};

#endif  // CHROME_BROWSER_UI_OMNIBOX_OMNIBOX_TAB_HELPER_H_