OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/fetchers/resource_fetcher_impl.h" | 5 #include "content/renderer/fetchers/resource_fetcher_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "third_party/WebKit/public/platform/Platform.h" | 10 #include "third_party/WebKit/public/platform/Platform.h" |
11 #include "third_party/WebKit/public/platform/WebHTTPBody.h" | 11 #include "third_party/WebKit/public/platform/WebHTTPBody.h" |
| 12 #include "third_party/WebKit/public/platform/WebString.h" |
12 #include "third_party/WebKit/public/platform/WebURL.h" | 13 #include "third_party/WebKit/public/platform/WebURL.h" |
13 #include "third_party/WebKit/public/platform/WebURLError.h" | |
14 #include "third_party/WebKit/public/platform/WebURLLoader.h" | 14 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
15 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 15 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
16 #include "third_party/WebKit/public/web/WebDocument.h" | 16 #include "third_party/WebKit/public/web/WebDocument.h" |
17 #include "third_party/WebKit/public/web/WebFrame.h" | 17 #include "third_party/WebKit/public/web/WebFrame.h" |
18 #include "third_party/WebKit/public/web/WebKit.h" | 18 #include "third_party/WebKit/public/web/WebKit.h" |
19 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" | 19 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" |
20 | 20 |
21 using base::TimeDelta; | |
22 using blink::WebFrame; | |
23 using blink::WebHTTPBody; | |
24 using blink::WebSecurityPolicy; | |
25 using blink::WebURLError; | |
26 using blink::WebURLLoader; | |
27 using blink::WebURLRequest; | |
28 using blink::WebURLResponse; | |
29 | |
30 namespace content { | 21 namespace content { |
31 | 22 |
32 // static | 23 // static |
33 ResourceFetcher* ResourceFetcher::Create(const GURL& url) { | 24 ResourceFetcher* ResourceFetcher::Create(const GURL& url) { |
34 return new ResourceFetcherImpl(url); | 25 return new ResourceFetcherImpl(url); |
35 } | 26 } |
36 | 27 |
37 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url) | 28 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url) |
38 : request_(url) { | 29 : request_(url) { |
39 } | 30 } |
40 | 31 |
41 ResourceFetcherImpl::~ResourceFetcherImpl() { | 32 ResourceFetcherImpl::~ResourceFetcherImpl() { |
42 if (!completed() && loader_) | 33 if (!completed() && loader_) |
43 loader_->cancel(); | 34 loader_->cancel(); |
44 } | 35 } |
45 | 36 |
46 void ResourceFetcherImpl::SetMethod(const std::string& method) { | 37 void ResourceFetcherImpl::SetMethod(const std::string& method) { |
47 DCHECK(!request_.isNull()); | 38 DCHECK(!request_.isNull()); |
48 DCHECK(!loader_); | 39 DCHECK(!loader_); |
49 | 40 |
50 request_.setHTTPMethod(blink::WebString::fromUTF8(method)); | 41 request_.setHTTPMethod(blink::WebString::fromUTF8(method)); |
51 } | 42 } |
52 | 43 |
53 void ResourceFetcherImpl::SetBody(const std::string& body) { | 44 void ResourceFetcherImpl::SetBody(const std::string& body) { |
54 DCHECK(!request_.isNull()); | 45 DCHECK(!request_.isNull()); |
55 DCHECK(!loader_); | 46 DCHECK(!loader_); |
56 | 47 |
57 WebHTTPBody web_http_body; | 48 blink::WebHTTPBody web_http_body; |
58 web_http_body.initialize(); | 49 web_http_body.initialize(); |
59 web_http_body.appendData(blink::WebData(body)); | 50 web_http_body.appendData(blink::WebData(body)); |
60 request_.setHTTPBody(web_http_body); | 51 request_.setHTTPBody(web_http_body); |
61 } | 52 } |
62 | 53 |
63 void ResourceFetcherImpl::SetHeader(const std::string& header, | 54 void ResourceFetcherImpl::SetHeader(const std::string& header, |
64 const std::string& value) { | 55 const std::string& value) { |
65 DCHECK(!request_.isNull()); | 56 DCHECK(!request_.isNull()); |
66 DCHECK(!loader_); | 57 DCHECK(!loader_); |
67 | 58 |
68 if (LowerCaseEqualsASCII(header, "referer")) { | 59 if (LowerCaseEqualsASCII(header, "referer")) { |
69 blink::WebString referrer = WebSecurityPolicy::generateReferrerHeader( | 60 blink::WebString referrer = |
70 blink::WebReferrerPolicyDefault, | 61 blink::WebSecurityPolicy::generateReferrerHeader( |
71 request_.url(), | 62 blink::WebReferrerPolicyDefault, |
72 blink::WebString::fromUTF8(value)); | 63 request_.url(), |
| 64 blink::WebString::fromUTF8(value)); |
73 request_.setHTTPReferrer(referrer, blink::WebReferrerPolicyDefault); | 65 request_.setHTTPReferrer(referrer, blink::WebReferrerPolicyDefault); |
74 } else { | 66 } else { |
75 request_.setHTTPHeaderField(blink::WebString::fromUTF8(header), | 67 request_.setHTTPHeaderField(blink::WebString::fromUTF8(header), |
76 blink::WebString::fromUTF8(value)); | 68 blink::WebString::fromUTF8(value)); |
77 } | 69 } |
78 } | 70 } |
79 | 71 |
80 void ResourceFetcherImpl::Start(WebFrame* frame, | 72 void ResourceFetcherImpl::SetSkipServiceWorker(bool skip_service_worker) { |
81 WebURLRequest::RequestContext request_context, | 73 DCHECK(!request_.isNull()); |
82 WebURLRequest::FrameType frame_type, | 74 DCHECK(!loader_); |
83 LoaderType loader_type, | 75 |
84 const Callback& callback) { | 76 request_.setSkipServiceWorker(skip_service_worker); |
| 77 } |
| 78 |
| 79 void ResourceFetcherImpl::SetLoaderOptions( |
| 80 const blink::WebURLLoaderOptions& options) { |
| 81 DCHECK(!request_.isNull()); |
| 82 DCHECK(!loader_); |
| 83 |
| 84 options_ = options; |
| 85 } |
| 86 |
| 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) { |
85 DCHECK(!loader_); | 93 DCHECK(!loader_); |
86 DCHECK(!request_.isNull()); | 94 DCHECK(!request_.isNull()); |
87 DCHECK(callback_.is_null()); | 95 DCHECK(callback_.is_null()); |
88 DCHECK(!completed()); | 96 DCHECK(!completed()); |
89 if (!request_.httpBody().isNull()) | 97 if (!request_.httpBody().isNull()) |
90 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies."; | 98 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies."; |
91 | 99 |
92 callback_ = callback; | 100 callback_ = callback; |
93 | 101 |
94 request_.setRequestContext(request_context); | 102 request_.setRequestContext(request_context); |
95 request_.setFrameType(frame_type); | 103 request_.setFrameType(frame_type); |
96 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies()); | 104 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies()); |
97 frame->dispatchWillSendRequest(request_); | 105 frame->dispatchWillSendRequest(request_); |
98 | 106 |
99 switch (loader_type) { | 107 switch (loader_type) { |
100 case PLATFORM_LOADER: | 108 case PLATFORM_LOADER: |
101 loader_.reset(blink::Platform::current()->createURLLoader()); | 109 loader_.reset(blink::Platform::current()->createURLLoader()); |
102 break; | 110 break; |
103 case FRAME_ASSOCIATED_LOADER: | 111 case FRAME_ASSOCIATED_LOADER: |
104 loader_.reset(frame->createAssociatedURLLoader()); | 112 loader_.reset(frame->createAssociatedURLLoader(options_)); |
105 break; | 113 break; |
106 } | 114 } |
107 loader_->loadAsynchronously(request_, this); | 115 loader_->loadAsynchronously(request_, this); |
108 | 116 |
109 // No need to hold on to the request. | 117 // No need to hold on to the request. |
110 request_.reset(); | 118 request_.reset(); |
111 } | 119 } |
112 | 120 |
113 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) { | 121 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) { |
114 DCHECK(loader_); | 122 DCHECK(loader_); |
(...skipping 13 matching lines...) Expand all Loading... |
128 callback.Run(status() == LOAD_FAILED ? blink::WebURLResponse() : response(), | 136 callback.Run(status() == LOAD_FAILED ? blink::WebURLResponse() : response(), |
129 status() == LOAD_FAILED ? std::string() : data()); | 137 status() == LOAD_FAILED ? std::string() : data()); |
130 } | 138 } |
131 | 139 |
132 void ResourceFetcherImpl::Cancel() { | 140 void ResourceFetcherImpl::Cancel() { |
133 loader_->cancel(); | 141 loader_->cancel(); |
134 WebURLLoaderClientImpl::Cancel(); | 142 WebURLLoaderClientImpl::Cancel(); |
135 } | 143 } |
136 | 144 |
137 } // namespace content | 145 } // namespace content |
OLD | NEW |