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"
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
) {
25 IPC_BEGIN_MESSAGE_MAP(PrefetchHelper
, message
)
26 IPC_MESSAGE_HANDLER(PrefetchMsg_Prefetch
, OnPrefetch
)
27 IPC_MESSAGE_UNHANDLED(handled
= false)
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
,
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