Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / fetchers / resource_fetcher_impl.cc
blobb48ad21d89df43dcfd6cf1429e5e6f6aa8ee8d37
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"
21 namespace content {
23 // static
24 ResourceFetcher* ResourceFetcher::Create(const GURL& url) {
25 return new ResourceFetcherImpl(url);
28 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url)
29 : request_(url) {
32 ResourceFetcherImpl::~ResourceFetcherImpl() {
33 if (!completed() && loader_)
34 loader_->cancel();
37 void ResourceFetcherImpl::SetMethod(const std::string& method) {
38 DCHECK(!request_.isNull());
39 DCHECK(!loader_);
41 request_.setHTTPMethod(blink::WebString::fromUTF8(method));
44 void ResourceFetcherImpl::SetBody(const std::string& body) {
45 DCHECK(!request_.isNull());
46 DCHECK(!loader_);
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());
57 DCHECK(!loader_);
59 if (base::LowerCaseEqualsASCII(header, "referer")) {
60 blink::WebString referrer =
61 blink::WebSecurityPolicy::generateReferrerHeader(
62 blink::WebReferrerPolicyDefault,
63 request_.url(),
64 blink::WebString::fromUTF8(value));
65 request_.setHTTPReferrer(referrer, blink::WebReferrerPolicyDefault);
66 } else {
67 request_.setHTTPHeaderField(blink::WebString::fromUTF8(header),
68 blink::WebString::fromUTF8(value));
72 void ResourceFetcherImpl::SetSkipServiceWorker(bool skip_service_worker) {
73 DCHECK(!request_.isNull());
74 DCHECK(!loader_);
76 request_.setSkipServiceWorker(skip_service_worker);
79 void ResourceFetcherImpl::SetCachePolicy(
80 blink::WebURLRequest::CachePolicy policy) {
81 DCHECK(!request_.isNull());
82 DCHECK(!loader_);
84 request_.setCachePolicy(policy);
87 void ResourceFetcherImpl::SetLoaderOptions(
88 const blink::WebURLLoaderOptions& options) {
89 DCHECK(!request_.isNull());
90 DCHECK(!loader_);
92 options_ = options;
95 void ResourceFetcherImpl::Start(
96 blink::WebFrame* frame,
97 blink::WebURLRequest::RequestContext request_context,
98 blink::WebURLRequest::FrameType frame_type,
99 LoaderType loader_type,
100 const Callback& callback) {
101 DCHECK(!loader_);
102 DCHECK(!request_.isNull());
103 DCHECK(callback_.is_null());
104 DCHECK(!completed());
105 if (!request_.httpBody().isNull())
106 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies.";
108 callback_ = callback;
110 request_.setRequestContext(request_context);
111 request_.setFrameType(frame_type);
112 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies());
113 frame->dispatchWillSendRequest(request_);
115 switch (loader_type) {
116 case PLATFORM_LOADER:
117 loader_.reset(blink::Platform::current()->createURLLoader());
118 break;
119 case FRAME_ASSOCIATED_LOADER:
120 loader_.reset(frame->createAssociatedURLLoader(options_));
121 break;
123 loader_->loadAsynchronously(request_, this);
125 // No need to hold on to the request.
126 request_.reset();
129 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) {
130 DCHECK(loader_);
131 DCHECK(!completed());
133 timeout_timer_.Start(FROM_HERE, timeout, this, &ResourceFetcherImpl::Cancel);
136 void ResourceFetcherImpl::OnLoadComplete() {
137 timeout_timer_.Stop();
138 if (callback_.is_null())
139 return;
141 // Take a reference to the callback as running the callback may lead to our
142 // destruction.
143 Callback callback = callback_;
144 callback.Run(status() == LOAD_FAILED ? blink::WebURLResponse() : response(),
145 status() == LOAD_FAILED ? std::string() : data());
148 void ResourceFetcherImpl::Cancel() {
149 loader_->cancel();
150 WebURLLoaderClientImpl::Cancel();
153 } // namespace content