1 // Copyright (c) 2012 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 "content/renderer/fetchers/resource_fetcher_impl.h"
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "base/time/time.h"
10 #include "third_party/WebKit/public/platform/Platform.h"
11 #include "third_party/WebKit/public/platform/WebHTTPBody.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/WebURL.h"
14 #include "third_party/WebKit/public/platform/WebURLLoader.h"
15 #include "third_party/WebKit/public/platform/WebURLRequest.h"
16 #include "third_party/WebKit/public/web/WebDocument.h"
17 #include "third_party/WebKit/public/web/WebFrame.h"
18 #include "third_party/WebKit/public/web/WebKit.h"
19 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
24 ResourceFetcher
* ResourceFetcher::Create(const GURL
& url
) {
25 return new ResourceFetcherImpl(url
);
28 ResourceFetcherImpl::ResourceFetcherImpl(const GURL
& url
)
32 ResourceFetcherImpl::~ResourceFetcherImpl() {
33 if (!completed() && loader_
)
37 void ResourceFetcherImpl::SetMethod(const std::string
& method
) {
38 DCHECK(!request_
.isNull());
41 request_
.setHTTPMethod(blink::WebString::fromUTF8(method
));
44 void ResourceFetcherImpl::SetBody(const std::string
& body
) {
45 DCHECK(!request_
.isNull());
48 blink::WebHTTPBody web_http_body
;
49 web_http_body
.initialize();
50 web_http_body
.appendData(blink::WebData(body
));
51 request_
.setHTTPBody(web_http_body
);
54 void ResourceFetcherImpl::SetHeader(const std::string
& header
,
55 const std::string
& value
) {
56 DCHECK(!request_
.isNull());
59 if (LowerCaseEqualsASCII(header
, "referer")) {
60 blink::WebString referrer
=
61 blink::WebSecurityPolicy::generateReferrerHeader(
62 blink::WebReferrerPolicyDefault
,
64 blink::WebString::fromUTF8(value
));
65 request_
.setHTTPReferrer(referrer
, blink::WebReferrerPolicyDefault
);
67 request_
.setHTTPHeaderField(blink::WebString::fromUTF8(header
),
68 blink::WebString::fromUTF8(value
));
72 void ResourceFetcherImpl::SetSkipServiceWorker(bool skip_service_worker
) {
73 DCHECK(!request_
.isNull());
76 request_
.setSkipServiceWorker(skip_service_worker
);
79 void ResourceFetcherImpl::SetLoaderOptions(
80 const blink::WebURLLoaderOptions
& options
) {
81 DCHECK(!request_
.isNull());
87 void ResourceFetcherImpl::Start(
88 blink::WebFrame
* frame
,
89 blink::WebURLRequest::RequestContext request_context
,
90 blink::WebURLRequest::FrameType frame_type
,
91 LoaderType loader_type
,
92 const Callback
& callback
) {
94 DCHECK(!request_
.isNull());
95 DCHECK(callback_
.is_null());
97 if (!request_
.httpBody().isNull())
98 DCHECK_NE("GET", request_
.httpMethod().utf8()) << "GETs can't have bodies.";
100 callback_
= callback
;
102 request_
.setRequestContext(request_context
);
103 request_
.setFrameType(frame_type
);
104 request_
.setFirstPartyForCookies(frame
->document().firstPartyForCookies());
105 frame
->dispatchWillSendRequest(request_
);
107 switch (loader_type
) {
108 case PLATFORM_LOADER
:
109 loader_
.reset(blink::Platform::current()->createURLLoader());
111 case FRAME_ASSOCIATED_LOADER
:
112 loader_
.reset(frame
->createAssociatedURLLoader(options_
));
115 loader_
->loadAsynchronously(request_
, this);
117 // No need to hold on to the request.
121 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta
& timeout
) {
123 DCHECK(!completed());
125 timeout_timer_
.Start(FROM_HERE
, timeout
, this, &ResourceFetcherImpl::Cancel
);
128 void ResourceFetcherImpl::OnLoadComplete() {
129 timeout_timer_
.Stop();
130 if (callback_
.is_null())
133 // Take a reference to the callback as running the callback may lead to our
135 Callback callback
= callback_
;
136 callback
.Run(status() == LOAD_FAILED
? blink::WebURLResponse() : response(),
137 status() == LOAD_FAILED
? std::string() : data());
140 void ResourceFetcherImpl::Cancel() {
142 WebURLLoaderClientImpl::Cancel();
145 } // namespace content