File: chrome_speculation_host_delegate.cc

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 (86 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download | duplicates (5)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/preloading/prefetch/no_state_prefetch/chrome_speculation_host_delegate.h"

#include <algorithm>
#include <vector>

#include "chrome/browser/preloading/prefetch/no_state_prefetch/no_state_prefetch_manager_factory.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_handle.h"
#include "components/no_state_prefetch/browser/no_state_prefetch_manager.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/session_storage_namespace.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/geometry/size.h"
#include "url/gurl.h"
#include "url/origin.h"

ChromeSpeculationHostDelegate::ChromeSpeculationHostDelegate(
    content::RenderFrameHost& render_frame_host)
    : render_frame_host_(render_frame_host) {}

ChromeSpeculationHostDelegate::~ChromeSpeculationHostDelegate() {
  for (auto& prefetch : same_origin_no_state_prefetches_) {
    prefetch->OnNavigateAway();
  }
}

void ChromeSpeculationHostDelegate::ProcessCandidates(
    std::vector<blink::mojom::SpeculationCandidatePtr>& candidates) {
  auto* web_contents =
      content::WebContents::FromRenderFrameHost(&*render_frame_host_);

  // Same origin prefetches with subresources are handled by NSP.
  std::vector<GURL> same_origin_prefetches_with_subresources;

  const url::Origin& origin = render_frame_host_->GetLastCommittedOrigin();

  // Returns true if the given entry is processed. Being processed means this
  // delegate has a corresponding strategy to process the candidate, so it
  // extracts the candidate's URL.
  auto should_process_entry =
      [&](const blink::mojom::SpeculationCandidatePtr& candidate) {
        bool is_same_origin = origin.IsSameOriginWith(candidate->url);

        if (!is_same_origin) {
          return false;
        }

        if (candidate->action !=
            blink::mojom::SpeculationAction::kPrefetchWithSubresources) {
          return false;
        }

        same_origin_prefetches_with_subresources.push_back(candidate->url);
        return true;
      };

  // Remove the entries that are to be processed by this delegate.
  std::erase_if(candidates, should_process_entry);

  if (same_origin_prefetches_with_subresources.size() > 0) {
    prerender::NoStatePrefetchManager* no_state_prefetch_manager =
        prerender::NoStatePrefetchManagerFactory::GetForBrowserContext(
            render_frame_host_->GetBrowserContext());
    if (!no_state_prefetch_manager) {
      return;
    }
    content::SessionStorageNamespace* session_storage_namespace =
        web_contents->GetController().GetDefaultSessionStorageNamespace();
    gfx::Size size = web_contents->GetContainerBounds().size();
    // The chrome implementation almost certainly only allows one NSP to start
    // (500 ms limit), but treat all requests of this class as handled by
    // chrome.
    for (const auto& url : same_origin_prefetches_with_subresources) {
      std::unique_ptr<prerender::NoStatePrefetchHandle> handle =
          no_state_prefetch_manager->AddSameOriginSpeculation(
              url, session_storage_namespace, size, origin);
      if (handle) {
        same_origin_no_state_prefetches_.push_back(std::move(handle));
      }
    }
  }
}