Add testing/scripts/OWNERS
[chromium-blink-merge.git] / chrome / renderer / prefetch_helper.cc
blobb64eb19f00898cad4e569bb9e5712268b10be245
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/renderer/prefetch_helper.h"
7 #include "chrome/common/prefetch_messages.h"
8 #include "content/public/renderer/render_frame.h"
9 #include "third_party/WebKit/public/web/WebLocalFrame.h"
10 #include "third_party/WebKit/public/web/WebURLLoaderOptions.h"
12 namespace prefetch {
14 PrefetchHelper::PrefetchHelper(content::RenderFrame* render_frame)
15 : content::RenderFrameObserver(render_frame) {
18 PrefetchHelper::~PrefetchHelper() {
19 STLDeleteElements(&loader_set_);
22 bool PrefetchHelper::OnMessageReceived(
23 const IPC::Message& message) {
24 bool handled = true;
25 IPC_BEGIN_MESSAGE_MAP(PrefetchHelper, message)
26 IPC_MESSAGE_HANDLER(PrefetchMsg_Prefetch, OnPrefetch)
27 IPC_MESSAGE_UNHANDLED(handled = false)
28 IPC_END_MESSAGE_MAP()
30 return handled;
33 void PrefetchHelper::OnPrefetch(const GURL& url) {
34 blink::WebFrame* frame = render_frame()->GetWebFrame();
35 blink::WebURLRequest request(url);
36 request.setRequestContext(blink::WebURLRequest::RequestContextPrefetch);
37 request.setPriority(blink::WebURLRequest::PriorityVeryLow);
38 blink::WebURLLoaderOptions options;
39 options.allowCredentials = true;
40 options.crossOriginRequestPolicy =
41 blink::WebURLLoaderOptions::CrossOriginRequestPolicyAllow;
42 blink::WebURLLoader* loader = frame->createAssociatedURLLoader(options);
43 loader->loadAsynchronously(request, this);
44 loader_set_.insert(loader);
47 void PrefetchHelper::didFinishLoading(blink::WebURLLoader* loader,
48 double finishTime,
49 int64_t totalEncodedDataLength) {
50 loader_set_.erase(loader);
53 void PrefetchHelper::didFail(blink::WebURLLoader* loader,
54 const blink::WebURLError& error) {
55 loader_set_.erase(loader);
58 } // namespace prefetch