Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1616)

Side by Side Diff: content/renderer/fetchers/resource_fetcher_impl.cc

Issue 140823010: ResourceFetcher: Add POST support and the ability to set headers (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Gack Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/time/time.h" 8 #include "base/time/time.h"
9 #include "third_party/WebKit/public/platform/Platform.h" 9 #include "third_party/WebKit/public/platform/Platform.h"
10 #include "third_party/WebKit/public/platform/WebHTTPBody.h"
10 #include "third_party/WebKit/public/platform/WebURL.h" 11 #include "third_party/WebKit/public/platform/WebURL.h"
11 #include "third_party/WebKit/public/platform/WebURLError.h" 12 #include "third_party/WebKit/public/platform/WebURLError.h"
12 #include "third_party/WebKit/public/platform/WebURLLoader.h" 13 #include "third_party/WebKit/public/platform/WebURLLoader.h"
13 #include "third_party/WebKit/public/platform/WebURLRequest.h" 14 #include "third_party/WebKit/public/platform/WebURLRequest.h"
14 #include "third_party/WebKit/public/web/WebDocument.h" 15 #include "third_party/WebKit/public/web/WebDocument.h"
15 #include "third_party/WebKit/public/web/WebFrame.h" 16 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebKit.h" 17 #include "third_party/WebKit/public/web/WebKit.h"
17 18
18 using base::TimeDelta; 19 using base::TimeDelta;
19 using blink::WebFrame; 20 using blink::WebFrame;
21 using blink::WebHTTPBody;
20 using blink::WebURLError; 22 using blink::WebURLError;
21 using blink::WebURLLoader; 23 using blink::WebURLLoader;
22 using blink::WebURLRequest; 24 using blink::WebURLRequest;
23 using blink::WebURLResponse; 25 using blink::WebURLResponse;
24 26
25 namespace content { 27 namespace content {
26 28
27 // static 29 // static
28 ResourceFetcher* ResourceFetcher::Create( 30 ResourceFetcher* ResourceFetcher::Create(const GURL& url) {
29 const GURL& url, WebFrame* frame, WebURLRequest::TargetType target_type, 31 return new ResourceFetcherImpl(url);
30 const Callback& callback) {
31 return new ResourceFetcherImpl(url, frame, target_type, callback);
32 } 32 }
33 33
34 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url, WebFrame* frame, 34 ResourceFetcherImpl::ResourceFetcherImpl(const GURL& url)
35 WebURLRequest::TargetType target_type, 35 : request_(url),
36 const Callback& callback) 36 completed_(false) {
37 : completed_(false),
38 callback_(callback) {
39 // Can't do anything without a frame. However, delegate can be NULL (so we
40 // can do a http request and ignore the results).
41 DCHECK(frame);
42 Start(url, frame, target_type);
43 } 37 }
44 38
45 ResourceFetcherImpl::~ResourceFetcherImpl() { 39 ResourceFetcherImpl::~ResourceFetcherImpl() {
46 if (!completed_ && loader_) 40 if (!completed_ && loader_)
47 loader_->cancel(); 41 loader_->cancel();
48 } 42 }
49 43
44 void ResourceFetcherImpl::SetMethod(const std::string& method) {
45 DCHECK(!request_.isNull());
46 DCHECK(!loader_);
47
48 request_.setHTTPMethod(blink::WebString::fromUTF8(method));
49 }
50
51 void ResourceFetcherImpl::SetBody(const std::string& body) {
52 DCHECK(!request_.isNull());
53 DCHECK(!loader_);
54
55 WebHTTPBody web_http_body;
56 web_http_body.initialize();
57 web_http_body.appendData(blink::WebData(body));
58 request_.setHTTPBody(web_http_body);
59 }
60
61 void ResourceFetcherImpl::SetHeader(const std::string& header,
62 const std::string& value) {
63 DCHECK(!request_.isNull());
64 DCHECK(!loader_);
65
66 request_.setHTTPHeaderField(blink::WebString::fromUTF8(header),
67 blink::WebString::fromUTF8(value));
68 }
69
70 void ResourceFetcherImpl::Start(WebFrame* frame,
71 WebURLRequest::TargetType target_type,
72 const Callback& callback) {
73 DCHECK(!loader_);
74 DCHECK(!request_.isNull());
75 DCHECK(callback_.is_null());
76 DCHECK(!completed_);
77 if (!request_.httpBody().isNull())
78 DCHECK_NE("GET", request_.httpMethod().utf8()) << "GETs can't have bodies.";
79
80 callback_ = callback;
81
82 request_.setTargetType(target_type);
83 request_.setFirstPartyForCookies(frame->document().firstPartyForCookies());
84 frame->dispatchWillSendRequest(request_);
85 loader_.reset(blink::Platform::current()->createURLLoader());
86 loader_->loadAsynchronously(request_, this);
87
88 // No need to hold on to the request.
89 request_.reset();
90 }
91
50 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) { 92 void ResourceFetcherImpl::SetTimeout(const base::TimeDelta& timeout) {
51 DCHECK(loader_); 93 DCHECK(loader_);
52 DCHECK(!completed_); 94 DCHECK(!completed_);
95
53 timeout_timer_.Start(FROM_HERE, timeout, this, 96 timeout_timer_.Start(FROM_HERE, timeout, this,
54 &ResourceFetcherImpl::TimeoutFired); 97 &ResourceFetcherImpl::TimeoutFired);
55 } 98 }
56 99
57 void ResourceFetcherImpl::Start(const GURL& url, WebFrame* frame,
58 WebURLRequest::TargetType target_type) {
59 WebURLRequest request(url);
60 request.setTargetType(target_type);
61 request.setFirstPartyForCookies(frame->document().firstPartyForCookies());
62 frame->dispatchWillSendRequest(request);
63
64 loader_.reset(blink::Platform::current()->createURLLoader());
65 loader_->loadAsynchronously(request, this);
66 }
67
68 void ResourceFetcherImpl::RunCallback(const WebURLResponse& response, 100 void ResourceFetcherImpl::RunCallback(const WebURLResponse& response,
69 const std::string& data) { 101 const std::string& data) {
70 completed_ = true; 102 completed_ = true;
71 timeout_timer_.Stop(); 103 timeout_timer_.Stop();
72 if (callback_.is_null()) 104 if (callback_.is_null())
73 return; 105 return;
74 106
75 // Take a reference to the callback as running the callback may lead to our 107 // Take a reference to the callback as running the callback may lead to our
76 // destruction. 108 // destruction.
77 Callback callback = callback_; 109 Callback callback = callback_;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 161
130 void ResourceFetcherImpl::didFail(WebURLLoader* loader, 162 void ResourceFetcherImpl::didFail(WebURLLoader* loader,
131 const WebURLError& error) { 163 const WebURLError& error) {
132 DCHECK(!completed_); 164 DCHECK(!completed_);
133 165
134 // Go ahead and tell our delegate that we're done. 166 // Go ahead and tell our delegate that we're done.
135 RunCallback(WebURLResponse(), std::string()); 167 RunCallback(WebURLResponse(), std::string());
136 } 168 }
137 169
138 } // namespace content 170 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/fetchers/resource_fetcher_impl.h ('k') | content/renderer/resource_fetcher_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698