File: optimization_guide_web_contents_observer.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 (172 lines) | stat: -rw-r--r-- 6,453 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
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
// Copyright 2019 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_OPTIMIZATION_GUIDE_OPTIMIZATION_GUIDE_WEB_CONTENTS_OBSERVER_H_
#define CHROME_BROWSER_OPTIMIZATION_GUIDE_OPTIMIZATION_GUIDE_WEB_CONTENTS_OBSERVER_H_

#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/optimization_guide/core/hints/insertion_ordered_set.h"
#include "components/optimization_guide/core/hints/optimization_guide_navigation_data.h"
#include "content/public/browser/navigation_handle_user_data.h"
#include "content/public/browser/page_user_data.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace content {
class NavigationHandle;
}  // namespace content

namespace optimization_guide {
class ChromeHintsManager;
class ChromeHintsManagerFetchingTest;
}  // namespace optimization_guide

class OptimizationGuideKeyedService;

// Observes navigation events.
class OptimizationGuideWebContentsObserver
    : public content::WebContentsObserver,
      public content::WebContentsUserData<
          OptimizationGuideWebContentsObserver> {
 public:
  OptimizationGuideWebContentsObserver(
      const OptimizationGuideWebContentsObserver&) = delete;
  OptimizationGuideWebContentsObserver& operator=(
      const OptimizationGuideWebContentsObserver&) = delete;

  ~OptimizationGuideWebContentsObserver() override;

  // Notifies |this| to flush |last_navigation_data| so metrics are recorded.
  void FlushLastNavigationData();

  // Tell the observer that hints for this URL should be fetched once we reach
  // onload in this |web_contents|. Note that |web_contents| must be the
  // WebContents being observed by this object.
  void AddURLsToBatchFetchBasedOnPrediction(std::vector<GURL> urls,
                                            content::WebContents* web_contents);

 private:
  friend class content::WebContentsUserData<
      OptimizationGuideWebContentsObserver>;
  friend class optimization_guide::ChromeHintsManagerFetchingTest;

  explicit OptimizationGuideWebContentsObserver(
      content::WebContents* web_contents);

  // Gets the OptimizationGuideNavigationData associated with the
  // |navigation_handle|. If one does not exist already, one will be created for
  // it.
  OptimizationGuideNavigationData* GetOrCreateOptimizationGuideNavigationData(
      content::NavigationHandle* navigation_handle);

  // content::WebContentsObserver implementation:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidRedirectNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void WebContentsDestroyed() override;

  void DocumentOnLoadCompletedInPrimaryMainFrame() override;

  // Ask |hints_manager| to fetch hints for navigations that were predicted for
  // the current page load.
  void FetchHintsUsingManager(
      optimization_guide::ChromeHintsManager* hints_manager,
      base::WeakPtr<content::Page> page);

  // Notifies |optimization_guide_keyed_service_| that the navigation has
  // finished.
  void NotifyNavigationFinish(
      std::unique_ptr<OptimizationGuideNavigationData> navigation_data,
      const std::vector<GURL>& navigation_redirect_chain);

  // Data scoped to a single page. PageData has the same lifetime as the page's
  // main document.
  class PageData : public content::PageUserData<PageData> {
   public:
    explicit PageData(content::Page& page);
    PageData(const PageData&) = delete;
    PageData& operator=(const PageData&) = delete;
    ~PageData() override;

    bool is_sent_batched_hints_request() const {
      return sent_batched_hints_request_;
    }
    void set_sent_batched_hints_request() {
      sent_batched_hints_request_ = true;
    }
    void InsertHintTargetUrls(const std::vector<GURL>& urls);
    std::vector<GURL> GetHintsTargetUrls();

    void SetNavigationData(
        std::unique_ptr<OptimizationGuideNavigationData> navigation_data) {
      navigation_data_ = std::move(navigation_data);
    }

    PAGE_USER_DATA_KEY_DECL();

   private:
    // List of predicted URLs to fetch hints for once the page reaches onload.
    optimization_guide::InsertionOrderedSet<GURL> hints_target_urls_;

    // Whether a hints request for predicted URLs has been fired off for this
    // page loads. Used to avoid sending more than one predicted URLs hints
    // request per page load.
    bool sent_batched_hints_request_ = false;

    // The navigation data for the completed navigation.
    std::unique_ptr<OptimizationGuideNavigationData> navigation_data_;
  };

  class NavigationHandleData
      : public content::NavigationHandleUserData<NavigationHandleData> {
   public:
    explicit NavigationHandleData(content::NavigationHandle&);
    NavigationHandleData(const NavigationHandleData&) = delete;
    NavigationHandleData& operator=(const NavigationHandleData&) = delete;
    ~NavigationHandleData() override;

    std::unique_ptr<OptimizationGuideNavigationData>
    TakeOptimizationGuideNavigationData() {
      return std::move(optimization_guide_navigation_data_);
    }

    OptimizationGuideNavigationData* GetOptimizationGuideNavigationData() {
      return optimization_guide_navigation_data_.get();
    }

    void SetOptimizationGuideNavigationData(
        std::unique_ptr<OptimizationGuideNavigationData> navigation_data) {
      optimization_guide_navigation_data_ = std::move(navigation_data);
    }

    NAVIGATION_HANDLE_USER_DATA_KEY_DECL();

   private:
    // The data related to a given navigation ID.
    std::unique_ptr<OptimizationGuideNavigationData>
        optimization_guide_navigation_data_;
  };

  // Returns the PageData for the specified |page|.
  PageData& GetPageData(content::Page& page);

  // Initialized in constructor. It may be null if the
  // OptimizationGuideKeyedService feature is not enabled.
  raw_ptr<OptimizationGuideKeyedService> optimization_guide_keyed_service_ =
      nullptr;

  base::WeakPtrFactory<OptimizationGuideWebContentsObserver> weak_factory_{
      this};

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

#endif  // CHROME_BROWSER_OPTIMIZATION_GUIDE_OPTIMIZATION_GUIDE_WEB_CONTENTS_OBSERVER_H_