File: metrics_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 (426 lines) | stat: -rw-r--r-- 19,405 bytes parent folder | download | duplicates (6)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAGE_LOAD_METRICS_BROWSER_METRICS_WEB_CONTENTS_OBSERVER_H_
#define COMPONENTS_PAGE_LOAD_METRICS_BROWSER_METRICS_WEB_CONTENTS_OBSERVER_H_

#include <map>
#include <memory>
#include <set>
#include <string_view>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/time/time.h"
#include "components/page_load_metrics/browser/page_load_metrics_observer.h"
#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "components/page_load_metrics/common/page_load_timing.h"
#include "content/public/browser/auction_result.h"
#include "content/public/browser/render_frame_host_receiver_set.h"
#include "content/public/browser/render_widget_host.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"
#include "net/cookies/canonical_cookie.h"
#include "services/network/public/mojom/fetch_api.mojom-forward.h"
#include "third_party/blink/public/common/input/web_input_event.h"
#include "third_party/blink/public/mojom/loader/resource_load_info.mojom.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-forward.h"
#include "third_party/blink/public/mojom/use_counter/metrics/webdx_feature.mojom.h"

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

namespace page_load_metrics {

struct MemoryUpdate;
class PageLoadMetricsEmbedderInterface;
class PageLoadMetricsMemoryTracker;
class PageLoadTracker;
class MetricsLifecycleObserver;

// MetricsWebContentsObserver tracks page loads and loading metrics
// related data based on IPC messages received from a
// MetricsRenderFrameObserver.
class MetricsWebContentsObserver
    : public content::WebContentsObserver,
      public content::WebContentsUserData<MetricsWebContentsObserver>,
      public content::RenderWidgetHost::InputEventObserver,
      public mojom::PageLoadMetrics {
 public:
  // Record a set of WebFeatures or WebDXFeatures directly from the browser
  // process. This should only be used for features that were detected
  // browser-side; features sources from the renderer should go via
  // MetricsRenderFrameObserver.
  static void RecordFeatureUsage(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::WebFeature>& features);
  static void RecordFeatureUsage(content::RenderFrameHost* render_frame_host,
                                 blink::mojom::WebFeature feature);
  static void RecordFeatureUsage(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::WebDXFeature>& features);
  static void RecordFeatureUsage(content::RenderFrameHost* render_frame_host,
                                 blink::mojom::WebDXFeature feature);

  // Note that the returned metrics is owned by the web contents.
  static MetricsWebContentsObserver* CreateForWebContents(
      content::WebContents* web_contents,
      std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface);

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

  ~MetricsWebContentsObserver() override;

  // Binds a Mojo receiver to the instance associated with the RenderFrameHost.
  static void BindPageLoadMetrics(
      mojo::PendingAssociatedReceiver<mojom::PageLoadMetrics> receiver,
      content::RenderFrameHost* rfh);

  // Any visibility changes that occur after this method should be ignored since
  // they are just clean up prior to destroying the WebContents instance.
  void WebContentsWillSoonBeDestroyed();

  // content::WebContentsObserver implementation:
  void ReadyToCommitNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidRedirectNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidUpdateNavigationHandleTiming(
      content::NavigationHandle* navigation_handle) override;
  void NavigationStopped() override;
  void OnInputEvent(const content::RenderWidgetHost& widget,
                    const blink::WebInputEvent& event) override;
  void OnVisibilityChanged(content::Visibility visibility) override;
  void PrimaryMainFrameRenderProcessGone(
      base::TerminationStatus status) override;
  void RenderFrameHostChanged(content::RenderFrameHost* old_host,
                              content::RenderFrameHost* new_host) override;
  void FrameDeleted(content::FrameTreeNodeId frame_tree_node_id) override;
  void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
  void MediaStartedPlaying(
      const content::WebContentsObserver::MediaPlayerInfo& video_type,
      const content::MediaPlayerId& id) override;
  void WebContentsDestroyed() override;
  void ResourceLoadComplete(
      content::RenderFrameHost* render_frame_host,
      const content::GlobalRequestID& request_id,
      const blink::mojom::ResourceLoadInfo& resource_load_info) override;
  void FrameReceivedUserActivation(
      content::RenderFrameHost* render_frame_host) override;
  void FrameDisplayStateChanged(content::RenderFrameHost* render_frame_host,
                                bool is_display_none) override;
  void FrameSizeChanged(content::RenderFrameHost* render_frame_host,
                        const gfx::Size& frame_size) override;
  void OnCookiesAccessed(content::NavigationHandle* navigation,
                         const content::CookieAccessDetails& details) override;
  void OnCookiesAccessed(content::RenderFrameHost* rfh,
                         const content::CookieAccessDetails& details) override;
  void DidActivatePreviewedPage(base::TimeTicks activaation_time) override;

  void OnStorageAccessed(content::RenderFrameHost* rfh,
                         const GURL& url,
                         const GURL& first_party_url,
                         bool blocked_by_policy,
                         StorageType storage_type);

  // These methods are forwarded from the MetricsNavigationThrottle.
  void WillStartNavigationRequest(content::NavigationHandle* navigation_handle);
  void WillProcessNavigationResponse(
      content::NavigationHandle* navigation_handle);

  // Flush any buffered metrics, as part of the metrics subsystem persisting
  // metrics as the application goes into the background. The application may be
  // killed at any time after this method is invoked without further
  // notification.
  void FlushMetricsOnAppEnterBackground();

  // Returns the delegate for the current committed primary page load, required
  // for `MetricsLifecycleObserver`s.
  const PageLoadMetricsObserverDelegate& GetDelegateForCommittedLoad();

  // Register / unregister `MetricsLifecycleObserver`s.
  void AddLifecycleObserver(MetricsLifecycleObserver* observer);
  void RemoveLifecycleObserver(MetricsLifecycleObserver* observer);

  // public only for testing
  void OnTimingUpdated(
      content::RenderFrameHost* render_frame_host,
      mojom::PageLoadTimingPtr timing,
      mojom::FrameMetadataPtr metadata,
      const std::vector<blink::UseCounterFeature>& new_features,
      const std::vector<mojom::ResourceDataUpdatePtr>& resources,
      mojom::FrameRenderDataUpdatePtr render_data,
      mojom::CpuTimingPtr cpu_timing,
      mojom::InputTimingPtr input_timing_delta,
      const std::optional<blink::SubresourceLoadMetrics>&
          subresource_load_metrics,
      mojom::SoftNavigationMetricsPtr);

  void OnCustomUserTimingUpdated(content::RenderFrameHost* rfh,
                                 mojom::CustomUserTimingMarkPtr custom_timing);

  // Informs the observers of the currently committed primary page load that
  // it's likely that prefetch will occur in this WebContents. This should
  // not be called within WebContentsObserver::DidFinishNavigation methods.
  void OnPrefetchLikely();

  // Called when V8 per-frame memory usage updates are available. Virtual for
  // test classes to override.
  virtual void OnV8MemoryChanged(
      const std::vector<MemoryUpdate>& memory_updates);

  // Called when a `SharedStorageWorkletHost` is created for `rfh`.
  void OnSharedStorageWorkletHostCreated(content::RenderFrameHost* rfh);

  // Called when `sharedStorage.selectURL()` is called for some frame on a page
  // whose main frame is `main_rfh`.
  void OnSharedStorageSelectURLCalled(content::RenderFrameHost* main_rfh);

  // Called when a Fledge auction completes.
  void OnAdAuctionComplete(content::RenderFrameHost* rfh,
                           bool is_server_auction,
                           bool is_on_device_auction,
                           content::AuctionResult result);

  // Returns the time this MetricsWebContentsObserver was created.
  base::TimeTicks GetCreated();

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

 protected:
  // Protected rather than private so that derived test classes can call
  // constructor.
  MetricsWebContentsObserver(
      content::WebContents* web_contents,
      std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface);

 private:
  friend class content::WebContentsUserData<MetricsWebContentsObserver>;
  friend class MetricsLifeCycleObserver;

  // Gets the PageLoadTracker associated with `rfh` if it exists, or nullptr
  // otherwise.
  //
  // Don't use GetPageLoadTrackerLegacy in new code. See also the comment around
  // implementation.
  // TODO(crbug.com/40216775): Remove this.
  PageLoadTracker* GetPageLoadTrackerLegacy(content::RenderFrameHost* rfh);
  PageLoadTracker* GetPageLoadTracker(content::RenderFrameHost* rfh);
  // Gets the alive PageLoadTracker corresponding to the nearest ancestral page
  // if it exists, or nullptr otherwise.
  //
  // Consider to use this instead of GetPageLoadTracker if
  //
  // - There is a race and the target PageLoadTracker can be deleted before
  //   receiving a event; and
  // - PageLoadTracker forwards the event unconditionally with respect to
  //   ObservePolicy.
  PageLoadTracker* GetAncestralAlivePageLoadTracker(
      content::RenderFrameHost* rfh);

  PageLoadTracker* GetPageLoadTrackerIfValid(
      content::RenderFrameHost* render_frame_host);

  // Gets the memory tracker for the BrowserContext if it exists, or nullptr
  // otherwise. The tracker measures per-frame memory usage by V8.
  PageLoadMetricsMemoryTracker* GetMemoryTracker() const;

  void WillStartNavigationRequestImpl(
      content::NavigationHandle* navigation_handle);

  // page_load_metrics::mojom::PageLoadMetrics implementation.
  void UpdateTiming(
      mojom::PageLoadTimingPtr timing,
      mojom::FrameMetadataPtr metadata,
      const std::vector<blink::UseCounterFeature>& new_features,
      std::vector<mojom::ResourceDataUpdatePtr> resources,
      mojom::FrameRenderDataUpdatePtr render_data,
      mojom::CpuTimingPtr cpu_timing,
      mojom::InputTimingPtr input_timing,
      const std::optional<blink::SubresourceLoadMetrics>&
          subresource_load_metrics,
      mojom::SoftNavigationMetricsPtr soft_navigation_metrics) override;
  void AddCustomUserTiming(
      mojom::CustomUserTimingMarkPtr custom_timing) override;

  void SetUpSharedMemoryForUkms(
      base::ReadOnlySharedMemoryRegion smoothness_memory,
      base::ReadOnlySharedMemoryRegion dropped_frames_memory) override;

  // Common part for UpdateThroughput and OnTimingUpdated.
  bool DoesTimingUpdateHaveError(PageLoadTracker* tracker);

  void HandleFailedNavigationForTrackedLoad(
      content::NavigationHandle* navigation_handle,
      std::unique_ptr<PageLoadTracker> tracker);

  void HandleCommittedNavigationForTrackedLoad(
      content::NavigationHandle* navigation_handle,
      std::unique_ptr<PageLoadTracker> tracker);

  void HandleCommittedNavigationForPrerendering(
      content::NavigationHandle* navigation_handle,
      std::unique_ptr<PageLoadTracker> tracker);

  void FinalizeCurrentlyCommittedLoad(
      content::NavigationHandle* newly_committed_navigation,
      PageLoadTracker* newly_committed_navigation_tracker);

  // Return a PageLoadTracker (either provisional or committed) that matches the
  // given request attributes, or nullptr if there are no matching
  // PageLoadTrackers.
  PageLoadTracker* GetTrackerOrNullForRequest(
      const content::GlobalRequestID& request_id,
      content::RenderFrameHost* render_frame_host_or_null,
      network::mojom::RequestDestination request_destination,
      base::TimeTicks creation_time);

  // Notify all loads, provisional and committed, that we performed an action
  // that might abort them.
  void NotifyPageEndAllLoads(PageEndReason page_end_reason,
                             UserInitiatedInfo user_initiated_info);
  void NotifyPageEndAllLoadsWithTimestamp(PageEndReason page_end_reason,
                                          UserInitiatedInfo user_initiated_info,
                                          base::TimeTicks timestamp,
                                          bool is_certainly_browser_timestamp);

  // Register / Unregister input event callback to given RenderFrameHost
  void RegisterInputEventObserver(content::RenderFrameHost* host);
  void UnregisterInputEventObserver(content::RenderFrameHost* host);

  // Notify aborted provisional loads that a new navigation occurred. This is
  // used for more consistent attribution tracking for aborted provisional
  // loads. This method returns the provisional load that was likely aborted
  // by this navigation, to help instantiate the new PageLoadTracker.
  std::unique_ptr<PageLoadTracker> NotifyAbortedProvisionalLoadsNewNavigation(
      content::NavigationHandle* new_navigation,
      UserInitiatedInfo user_initiated_info);

  // Whether metrics should be tracked, and a PageLoadTracker should be created,
  // for the given main frame navigation.
  bool ShouldTrackMainFrameNavigation(
      content::NavigationHandle* navigation_handle) const;

  // Determines if metrics should be collected for a given URL scheme.
  // This is used for both navigation and resource timing updates.
  // If this returns false, the navigation to the URL will not be tracked, and
  // timing updates for resources loaded from the URL will not be propagated to
  // metrics observers.
  bool ShouldTrackScheme(std::string_view scheme) const;

  bool ShouldTrackSchemeForNonWebUI(std::string_view scheme) const;

  void OnBrowserFeatureUsage(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::UseCounterFeature>& new_features);

  // Before deleting PageLoadTracker, check if we need to keep it alive as the
  // page is stored in back-forward cache. The page can either be restored later
  // (we will be notified via DidFinishNavigation and NavigationHandle::
  // IsServedFromBackForwardCache) or will be evicted from the cache (we will be
  // notified via RenderFrameDeleted).
  void MaybeStorePageLoadTrackerForBackForwardCache(
      content::NavigationHandle* next_navigation_handle,
      std::unique_ptr<PageLoadTracker> page_load_tracker);

  // Tries to move a PageLoadTracker from `inactive_pages_` to
  // `primary_page_`, when a navigation activates a back/forward-cached or
  // prerendered page. Returns true if `primary_page_` is updated.
  // Note that FencedFrames is not supported by back/forward-cache and
  // prerendering and this method doesn't support handling both containing
  // FencedFrames.
  bool MaybeActivatePageLoadTracker(
      content::NavigationHandle* navigation_handle);

  // Notifies `tracker` about cookie read or write.
  void OnCookiesAccessedImpl(PageLoadTracker& tracker,
                             const content::CookieAccessDetails& details);

  // True if the web contents is currently in the foreground.
  bool in_foreground_;

  // The PageLoadTrackers must be deleted before the `embedder_interface_`,
  // because they hold a pointer to the `embedder_interface_`.
  std::unique_ptr<PageLoadMetricsEmbedderInterface> embedder_interface_;

  // This map tracks all of the navigations ongoing that are not committed
  // yet. Once a navigation is committed, it moves from the map to
  // `primary_page_`, `active_pages_, or `inactive_pages_`. Note that a
  // PageLoadTracker's NavigationHandle is only valid until commit time, when we
  // remove it from the map.
  std::map<content::NavigationHandle*, std::unique_ptr<PageLoadTracker>>
      provisional_loads_;

  // Tracks aborted provisional loads for a little bit longer than usual (one
  // more navigation commit at the max), in order to better understand how the
  // navigation failed. This is because most provisional loads are destroyed
  // and vanish before we get signal about what caused the abort (new
  // navigation, stop button, etc.).
  std::vector<std::unique_ptr<PageLoadTracker>> aborted_provisional_loads_;

  // Memory updates that are accumulated while there is no PageLoadTracker
  // associated with RenderFrameHost. Will be sent in
  // HandleCommittedNavigationForTrackedLoad, unless the RenderFrameHost is
  // deleted and/or web contents is destroyed.
  std::vector<MemoryUpdate> queued_memory_updates_;

  // This stores the PageLoadTracker for the primary page. GetPageLoadTracker()
  // is available to find a PageLoadTracker for non-primary pages.
  std::unique_ptr<PageLoadTracker> primary_page_;

  // This stores the PageLoadTracker for non-primary pages, such as
  // FencedFrames, or MPArch based Portals in the future.
  base::flat_map<content::RenderFrameHost*, std::unique_ptr<PageLoadTracker>>
      active_pages_;

  // This stores the PageLoadTracker for each main frame of inactive pages,
  // including pages in the back/forward cache and prerendered pages. (The main
  // frame of the active page is in `primary_page_`.)
  base::flat_map<content::RenderFrameHost*, std::unique_ptr<PageLoadTracker>>
      inactive_pages_;

  // This is currently set only for the main frame of each page associated with
  // the WebContents. It maps to the shared memory for the smoothness and the
  // dropped frame count UKMs.
  base::flat_map<content::RenderFrameHost*,
                 std::pair<base::ReadOnlySharedMemoryRegion,
                           base::ReadOnlySharedMemoryRegion>>
      ukm_data_;

  std::vector<mojom::CustomUserTimingMarkPtr> page_load_custom_timings_;

  // Has the MWCO observed at least one navigation?
  bool has_navigated_;

  // Is the main frame a WebUI page?
  bool main_frame_is_webui_ = false;

  base::ObserverList<MetricsLifecycleObserver> lifecycle_observers_;
  content::RenderFrameHostReceiverSet<mojom::PageLoadMetrics>
      page_load_metrics_receivers_;

  bool web_contents_will_soon_be_destroyed_ = false;

  base::TimeTicks created_;

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

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

}  // namespace page_load_metrics

#endif  // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_METRICS_WEB_CONTENTS_OBSERVER_H_