summaryrefslogtreecommitdiff
path: root/chromium/content/browser/preloading/speculation_rules/speculation_host_impl.cc
blob: 691f7a842f9fa99a3028e062da6326b299421f5f (plain)
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
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/preloading/speculation_rules/speculation_host_impl.h"
#include <functional>

#include "base/containers/span.h"
#include "base/ranges/algorithm.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/devtools/network_service_devtools_observer.h"
#include "content/browser/preloading//preloading.h"
#include "content/browser/preloading/prefetch/prefetch_document_manager.h"
#include "content/browser/preloading/prefetch/prefetch_features.h"
#include "content/browser/preloading/preloading_data_impl.h"
#include "content/browser/preloading/prerender/prerender_attributes.h"
#include "content/browser/preloading/prerender/prerender_host_registry.h"
#include "content/browser/renderer_host/render_frame_host_delegate.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "content/public/common/referrer.h"
#include "mojo/public/cpp/bindings/message.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"

namespace content {

namespace {

bool CandidatesAreValid(
    std::vector<blink::mojom::SpeculationCandidatePtr>& candidates) {
  for (const auto& candidate : candidates) {
    // These non-http candidates should be filtered out in Blink and
    // SpeculationHostImpl should not see them. If SpeculationHostImpl receives
    // non-http candidates, it may mean the renderer process has a bug
    // or is compromised.
    if (!candidate->url.SchemeIsHTTPOrHTTPS()) {
      mojo::ReportBadMessage("SH_NON_HTTP");
      return false;
    }
  }
  return true;
}

}  // namespace

struct SpeculationHostImpl::PrerenderInfo {
  GURL url;
  Referrer referrer;
  int prerender_host_id;
};

// static
void SpeculationHostImpl::Bind(
    RenderFrameHost* frame_host,
    mojo::PendingReceiver<blink::mojom::SpeculationHost> receiver) {
  CHECK(frame_host);
  // TODO(crbug.com/1190338): Allow SpeculationHostDelegate to participate in
  // this feature check.
  if (!base::FeatureList::IsEnabled(
          blink::features::kSpeculationRulesPrefetchProxy) &&
      !blink::features::IsPrerender2Enabled()) {
    mojo::ReportBadMessage(
        "Speculation rules must be enabled to bind to "
        "blink.mojom.SpeculationHost in the browser.");
    return;
  }

  // DocumentService will destroy this on pipe closure or frame destruction.
  new SpeculationHostImpl(*frame_host, std::move(receiver));
}

SpeculationHostImpl::SpeculationHostImpl(
    RenderFrameHost& frame_host,
    mojo::PendingReceiver<blink::mojom::SpeculationHost> receiver)
    : DocumentService(frame_host, std::move(receiver)),
      WebContentsObserver(WebContents::FromRenderFrameHost(&frame_host)) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  delegate_ = GetContentClient()->browser()->CreateSpeculationHostDelegate(
      render_frame_host());
  if (blink::features::IsPrerender2Enabled()) {
    auto& rfhi = static_cast<RenderFrameHostImpl&>(frame_host);
    registry_ = rfhi.delegate()->GetPrerenderHostRegistry()->GetWeakPtr();
  }
}

SpeculationHostImpl::~SpeculationHostImpl() {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  CancelStartedPrerenders();
}

void SpeculationHostImpl::PrimaryPageChanged(Page& page) {
  // Listen to the change of the primary page. Since only the primary page can
  // trigger speculationrules, the change of the primary page indicates that the
  // trigger associated with this host is destroyed, so the browser should
  // cancel the prerenders that are initiated by it.
  // We cannot do it in the destructor only, because DocumentService can be
  // deleted asynchronously, but we want to make sure to cancel prerendering
  // before the next primary page swaps in so that the next page can trigger a
  // new prerender without hitting the max number of running prerenders.
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  CancelStartedPrerenders();
}

void SpeculationHostImpl::UpdateSpeculationCandidates(
    std::vector<blink::mojom::SpeculationCandidatePtr> candidates) {
  DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
  if (!CandidatesAreValid(candidates))
    return;

  // Only handle messages from an active main frame.
  if (!render_frame_host().IsActive())
    return;
  if (render_frame_host().GetParent())
    return;

  WebContents* web_contents =
      WebContents::FromRenderFrameHost(&render_frame_host());

  for (const auto& candidate : candidates) {
    // Create new PreloadingPrediction class and pass all fields for all
    // candidates.

    // In case of speculation rules, the confidence is set as 100 as the URL
    // was not predicted and confidence in this case is not defined.
    int64_t confidence = 100;
    PreloadingURLMatchCallback same_url_matcher =
        PreloadingData::GetSameURLMatcher(candidate->url);

    auto* preloading_data =
        PreloadingData::GetOrCreateForWebContents(web_contents);
    // TODO(crbug.com/1341019): Pass the action requested by speculation rules
    // to PreloadingPrediction.
    preloading_data->AddPreloadingPrediction(
        ToPreloadingPredictor(ContentPreloadingPredictor::kSpeculationRules),
        confidence, std::move(same_url_matcher));
  }

  if (base::FeatureList::IsEnabled(features::kPrefetchUseContentRefactor)) {
    PrefetchDocumentManager* prefetch_document_manager =
        PrefetchDocumentManager::GetOrCreateForCurrentDocument(
            &render_frame_host());

    prefetch_document_manager->ProcessCandidates(
        candidates, weak_ptr_factory_.GetWeakPtr());
  }

  // Let `delegate_` process the candidates that it is interested in.
  if (delegate_)
    delegate_->ProcessCandidates(candidates, weak_ptr_factory_.GetWeakPtr());

  ProcessCandidatesForPrerender(candidates);
}

void SpeculationHostImpl::ProcessCandidatesForPrerender(
    const std::vector<blink::mojom::SpeculationCandidatePtr>& candidates) {
  if (!registry_)
    return;
  DCHECK(blink::features::IsPrerender2Enabled());

  // Extract only the candidates which apply to prerender, and sort them by URL
  // so we can efficiently compare them to `started_prerenders_`.
  std::vector<blink::mojom::SpeculationCandidatePtr> prerender_candidates;
  for (const auto& candidate : candidates) {
    if (candidate->action == blink::mojom::SpeculationAction::kPrerender)
      prerender_candidates.push_back(candidate.Clone());
  }
  base::ranges::sort(prerender_candidates, std::less<>(),
                     &blink::mojom::SpeculationCandidate::url);
  std::vector<blink::mojom::SpeculationCandidatePtr> candidates_to_start;

  // Compare the sorted candidate and started prerender lists to one another.
  // Since they are sorted, we process the lexicographically earlier of the two
  // URLs pointed at by the iterators, and compare the range of entries in each
  // that match that URL.
  //
  // URLs which are present in the prerender list but not the candidate list can
  // no longer proceed and are cancelled.
  //
  // URLs which are present in the candidate list but not the prerender list
  // could be started and are gathered in `candidates_to_start`.
  auto candidate_it = prerender_candidates.begin();
  auto started_it = started_prerenders_.begin();
  while (candidate_it != prerender_candidates.end() ||
         started_it != started_prerenders_.end()) {
    // Select the lesser of the two URLs to diff.
    GURL url;
    if (started_it == started_prerenders_.end())
      url = (*candidate_it)->url;
    else if (candidate_it == prerender_candidates.end())
      url = started_it->url;
    else
      url = std::min((*candidate_it)->url, started_it->url);

    // Select the ranges from both that match the URL in question.
    auto equal_prerender_end = base::ranges::find_if(
        started_it, started_prerenders_.end(),
        [&](const auto& started) { return started.url != url; });
    base::span<PrerenderInfo> matching_prerenders(started_it,
                                                  equal_prerender_end);
    auto equal_candidate_end = base::ranges::find_if(
        candidate_it, prerender_candidates.end(),
        [&](const auto& candidate) { return candidate->url != url; });
    base::span<blink::mojom::SpeculationCandidatePtr> matching_candidates(
        candidate_it, equal_candidate_end);

    // Decide what started prerenders to cancel.
    for (PrerenderInfo& prerender : matching_prerenders) {
      if (prerender.prerender_host_id == RenderFrameHost::kNoFrameTreeNodeId)
        continue;
      // TODO(jbroman): This doesn't currently care about other aspects, like
      // the referrer. This doesn't presently matter, but in the future we might
      // want to cancel if there are candidates which match by URL but none of
      // which permit this prerender.
      if (matching_candidates.empty()) {
        registry_->OnTriggerDestroyed(prerender.prerender_host_id);
        prerender.prerender_host_id = RenderFrameHost::kNoFrameTreeNodeId;
      }
    }

    // Decide what new candidates to start.
    // For now, start the first candidate for a URL only if there are no
    // matching prerenders. We could be cleverer in the future.
    if (matching_prerenders.empty()) {
      DCHECK_GT(matching_candidates.size(), 0u);
      candidates_to_start.push_back(std::move(matching_candidates[0]));
    }

    // Advance the iterators past all matching entries.
    candidate_it = equal_candidate_end;
    started_it = equal_prerender_end;
  }

  // Actually start the candidates once the diffing is done.
  auto& rfhi = static_cast<RenderFrameHostImpl&>(render_frame_host());
  WebContents* web_contents =
      WebContents::FromRenderFrameHost(&render_frame_host());
  for (const auto& it : candidates_to_start) {
    DCHECK_EQ(it->action, blink::mojom::SpeculationAction::kPrerender);

    auto* preloading_data =
        PreloadingData::GetOrCreateForWebContents(web_contents);

    // Create new PreloadingAttempt and pass all the values corresponding to
    // this prerendering attempt.
    PreloadingURLMatchCallback same_url_matcher =
        PreloadingData::GetSameURLMatcher(it->url);
    PreloadingAttempt* preloading_attempt =
        preloading_data->AddPreloadingAttempt(
            ToPreloadingPredictor(
                ContentPreloadingPredictor::kSpeculationRules),
            PreloadingType::kPrerender, std::move(same_url_matcher));

    auto [begin, end] = base::ranges::equal_range(
        started_prerenders_.begin(), started_prerenders_.end(), it->url,
        std::less<>(), &PrerenderInfo::url);
    DCHECK(begin == end)
        << "cannot currently start a second prerender with the same URL";

    GetContentClient()->browser()->LogWebFeatureForCurrentPage(
        &rfhi, blink::mojom::WebFeature::kSpeculationRulesPrerender);

    // TODO(crbug.com/1176054): Remove it after supporting cross-origin
    // prerender.
    if (!rfhi.GetLastCommittedOrigin().IsSameOriginWith(it->url)) {
      rfhi.AddMessageToConsole(
          blink::mojom::ConsoleMessageLevel::kWarning,
          base::StringPrintf(
              "The SpeculationRules API does not support cross-origin "
              "prerender yet. (initiator origin: %s, prerender origin: %s). "
              "https://crbug.com/1176054 tracks cross-origin support.",
              rfhi.GetLastCommittedOrigin().Serialize().c_str(),
              url::Origin::Create(it->url).Serialize().c_str()));
    }

    Referrer referrer(*(it->referrer));
    int prerender_host_id = registry_->CreateAndStartHost(
        PrerenderAttributes(it->url, PrerenderTriggerType::kSpeculationRule,
                            /*embedder_histogram_suffix=*/"", referrer,
                            rfhi.GetLastCommittedOrigin(),
                            rfhi.GetLastCommittedURL(),
                            rfhi.GetProcess()->GetID(), rfhi.GetFrameToken(),
                            rfhi.GetFrameTreeNodeId(),
                            rfhi.GetPageUkmSourceId(), ui::PAGE_TRANSITION_LINK,
                            /*url_match_predicate=*/absl::nullopt),
        *web_contents, /*preloading_attempt=*/preloading_attempt);
    started_prerenders_.insert(end, {.url = it->url,
                                     .referrer = referrer,
                                     .prerender_host_id = prerender_host_id});
  }
}

void SpeculationHostImpl::CancelStartedPrerenders() {
  if (registry_) {
    for (const auto& prerender : started_prerenders_) {
      int host_id = prerender.prerender_host_id;
      if (host_id != RenderFrameHost::kNoFrameTreeNodeId)
        registry_->OnTriggerDestroyed(host_id);
    }
    started_prerenders_.clear();
  }
}

void SpeculationHostImpl::OnStartSinglePrefetch(
    const std::string& request_id,
    const network::ResourceRequest& request) {
  auto* ftn = static_cast<RenderFrameHostImpl*>(&render_frame_host())
                  ->frame_tree_node();
  devtools_instrumentation::OnPrefetchRequestWillBeSent(
      ftn, request_id, render_frame_host().GetLastCommittedURL(), request);
}

void SpeculationHostImpl::OnPrefetchResponseReceived(
    const GURL& url,
    const std::string& request_id,
    const network::mojom::URLResponseHead& response) {
  auto* ftn = static_cast<RenderFrameHostImpl*>(&render_frame_host())
                  ->frame_tree_node();
  devtools_instrumentation::OnPrefetchResponseReceived(ftn, request_id, url,
                                                       response);
}

void SpeculationHostImpl::OnPrefetchRequestComplete(
    const std::string& request_id,
    const network::URLLoaderCompletionStatus& status) {
  auto* ftn = static_cast<RenderFrameHostImpl*>(&render_frame_host())
                  ->frame_tree_node();
  devtools_instrumentation::OnPrefetchRequestComplete(ftn, request_id, status);
}

void SpeculationHostImpl::OnPrefetchBodyDataReceived(
    const std::string& request_id,
    const std::string& body,
    bool is_base64_encoded) {
  auto* ftn = static_cast<RenderFrameHostImpl*>(&render_frame_host())
                  ->frame_tree_node();
  devtools_instrumentation::OnPrefetchBodyDataReceived(ftn, request_id, body,
                                                       is_base64_encoded);
}

mojo::PendingRemote<network::mojom::DevToolsObserver>
SpeculationHostImpl::MakeSelfOwnedNetworkServiceDevToolsObserver() {
  auto* ftn = static_cast<RenderFrameHostImpl*>(&render_frame_host())
                  ->frame_tree_node();
  return NetworkServiceDevToolsObserver::MakeSelfOwned(ftn);
}

}  // namespace content