File: no_state_prefetch_link_manager.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 (156 lines) | stat: -rw-r--r-- 5,888 bytes parent folder | download | duplicates (8)
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
// Copyright 2012 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_NO_STATE_PREFETCH_BROWSER_NO_STATE_PREFETCH_LINK_MANAGER_H_
#define COMPONENTS_NO_STATE_PREFETCH_BROWSER_NO_STATE_PREFETCH_LINK_MANAGER_H_

#include <stddef.h>
#include <stdint.h>

#include <list>
#include <memory>
#include <optional>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_handle.h"
#include "third_party/blink/public/mojom/prerender/prerender.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"

FORWARD_DECLARE_TEST(WebViewTest, NoPrerenderer);

namespace prerender {

class NoStatePrefetchManager;

// NoStatePrefetchLinkManager implements the API on Link elements for all
// documents being rendered in this chrome instance.  It receives messages from
// the renderer indicating addition, cancelation and abandonment of link
// elements, and controls the NoStatePrefetchManager accordingly.
class NoStatePrefetchLinkManager : public KeyedService,
                                   public NoStatePrefetchHandle::Observer {
 public:
  explicit NoStatePrefetchLinkManager(NoStatePrefetchManager* manager);

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

  ~NoStatePrefetchLinkManager() override;

  // Called when a <link rel=prerender ...> element has been inserted into the
  // document. Returns the link trigger id that is used for canceling or
  // abandoning prefetch. Returns std::nullopt if the prefetch was not started.
  virtual std::optional<int> OnStartLinkTrigger(
      int launcher_render_process_id,
      int launcher_render_view_id,
      int launcher_render_frame_id,
      blink::mojom::PrerenderAttributesPtr attributes,
      const url::Origin& initiator_origin);

  // Called when a <link rel=prerender ...> element has been explicitly removed
  // from a document.
  virtual void OnCancelLinkTrigger(int link_trigger_id);

  // Called when a renderer launching <link rel=prerender ...> has navigated
  // away from the launching page, the launching renderer process has crashed,
  // or perhaps the renderer process was fast-closed when the last render view
  // in it was closed.
  virtual void OnAbandonLinkTrigger(int link_trigger_id);

 private:
  friend class PrerenderBrowserTest;
  friend class NoStatePrefetchTest;
  // WebViewTest.NoPrerenderer needs to access the private IsEmpty() method.
  FRIEND_TEST_ALL_PREFIXES(::WebViewTest, NoPrerenderer);

  // Used to store state about a <link rel=prerender ...> that triggers
  // NoStatePrefetch.
  struct LinkTrigger {
    LinkTrigger(int launcher_render_process_id,
                int launcher_render_view_id,
                blink::mojom::PrerenderAttributesPtr attributes,
                const url::Origin& initiator_origin,
                base::TimeTicks creation_time,
                NoStatePrefetchContents* deferred_launcher);
    ~LinkTrigger();

    LinkTrigger(const LinkTrigger& other) = delete;
    LinkTrigger& operator=(const LinkTrigger& other) = delete;

    // Parameters from NoStatePrefetchLinkManager::OnStartLinkTrigger():
    const int launcher_render_process_id;
    const int launcher_render_view_id;
    const GURL url;
    const blink::mojom::PrerenderTriggerType trigger_type;
    const content::Referrer referrer;
    const url::Origin initiator_origin;
    const gfx::Size size;

    // The time at which this trigger was added to NoStatePrefetchLinkManager.
    const base::TimeTicks creation_time;

    // If non-null, this trigger was launched by an unswapped prefetcher,
    // |deferred_launcher|. When |deferred_launcher| is swapped in, the field is
    // set to null.
    raw_ptr<const NoStatePrefetchContents> deferred_launcher;

    // Initially null, |handle| is set once we start this trigger. It is owned
    // by this struct, and must be deleted before destructing this struct.
    std::unique_ptr<NoStatePrefetchHandle> handle;

    // True if this trigger has been abandoned by its launcher.
    bool has_been_abandoned;

    // The unique ID of this trigger. Used for canceling or abandoning
    // prefetching.
    const int link_trigger_id;
  };

  bool IsEmpty() const;

  bool TriggerIsRunningForTesting(LinkTrigger* link_trigger) const;

  // Returns a count of currently running triggers.
  size_t CountRunningTriggers() const;

  // Start any triggers that can be started, respecting concurrency limits for
  // the system and per launcher.
  void StartLinkTriggers();

  LinkTrigger* FindByNoStatePrefetchHandle(
      NoStatePrefetchHandle* no_state_prefetch_handle);
  LinkTrigger* FindByLinkTriggerId(int link_trigger_id);

  // Removes |trigger| from the the prerender link manager. Deletes the
  // NoStatePrefetchHandle as needed.
  void RemoveLinkTrigger(LinkTrigger* trigger);

  // Cancels |trigger| and removes |trigger| from the prerender link
  // manager.
  void CancelLinkTrigger(LinkTrigger* trigger);

  // From KeyedService:
  void Shutdown() override;

  // From NoStatePrefetchHandle::Observer:
  void OnPrefetchStop(NoStatePrefetchHandle* no_state_prefetch_handle) override;

  bool has_shutdown_;

  const raw_ptr<NoStatePrefetchManager> manager_;

  // All triggers known to this NoStatePrefetchLinkManager. Insertions are
  // always made at the back, so the oldest trigger is at the front, and the
  // youngest at the back. Using std::unique_ptr<> here as LinkTrigger is not
  // copyable.
  std::list<std::unique_ptr<LinkTrigger>> triggers_;
};

}  // namespace prerender

#endif  // COMPONENTS_NO_STATE_PREFETCH_BROWSER_NO_STATE_PREFETCH_LINK_MANAGER_H_