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 "remoting/host/url_fetcher.h" | 5 #include "remoting/host/url_fetcher.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
12 #include "net/url_request/url_request_context_getter.h" | 12 #include "net/url_request/url_request_context_getter.h" |
13 | 13 |
14 namespace remoting { | 14 namespace remoting { |
15 | 15 |
16 const int kBufferSize = 4096; | 16 const int kBufferSize = 4096; |
17 | 17 |
18 class UrlFetcher::Core : public base::RefCountedThreadSafe<Core>, | 18 class UrlFetcher::Core : public base::RefCountedThreadSafe<Core>, |
19 public net::URLRequest::Delegate { | 19 public net::URLRequest::Delegate { |
20 public: | 20 public: |
21 Core(const GURL& url, Method method); | 21 Core(const GURL& url, Method method); |
22 | 22 |
23 void SetRequestContext( | 23 void SetRequestContext( |
24 net::URLRequestContextGetter* request_context_getter); | 24 net::URLRequestContextGetter* request_context_getter); |
25 void SetUploadData(const std::string& upload_content_type, | 25 void SetUploadData(const std::string& upload_content_type, |
26 const std::string& upload_content); | 26 const std::string& upload_content); |
| 27 void SetHeader(const std::string& key, const std::string& value); |
27 void Start(const UrlFetcher::DoneCallback& done_callback); | 28 void Start(const UrlFetcher::DoneCallback& done_callback); |
28 | 29 |
29 void Detach(); | 30 void Detach(); |
30 | 31 |
31 // net::UrlRequest::Delegate interface. | 32 // net::UrlRequest::Delegate interface. |
32 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; | 33 virtual void OnResponseStarted(net::URLRequest* request) OVERRIDE; |
33 virtual void OnReadCompleted(net::URLRequest* request, | 34 virtual void OnReadCompleted(net::URLRequest* request, |
34 int bytes_read) OVERRIDE; | 35 int bytes_read) OVERRIDE; |
35 | 36 |
36 private: | 37 private: |
(...skipping 13 matching lines...) Expand all Loading... |
50 Method method_; | 51 Method method_; |
51 | 52 |
52 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; | 53 scoped_refptr<base::MessageLoopProxy> delegate_message_loop_; |
53 scoped_refptr<base::MessageLoopProxy> io_message_loop_; | 54 scoped_refptr<base::MessageLoopProxy> io_message_loop_; |
54 | 55 |
55 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 56 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
56 | 57 |
57 std::string upload_content_; | 58 std::string upload_content_; |
58 std::string upload_content_type_; | 59 std::string upload_content_type_; |
59 | 60 |
| 61 net::HttpRequestHeaders request_headers_; |
| 62 |
60 scoped_ptr<net::URLRequest> request_; | 63 scoped_ptr<net::URLRequest> request_; |
61 | 64 |
62 scoped_refptr<net::IOBuffer> buffer_; | 65 scoped_refptr<net::IOBuffer> buffer_; |
63 std::string data_; | 66 std::string data_; |
64 | 67 |
65 UrlFetcher::DoneCallback done_callback_; | 68 UrlFetcher::DoneCallback done_callback_; |
66 | 69 |
67 DISALLOW_COPY_AND_ASSIGN(Core); | 70 DISALLOW_COPY_AND_ASSIGN(Core); |
68 }; | 71 }; |
69 | 72 |
(...skipping 12 matching lines...) Expand all Loading... |
82 DCHECK(!request_context_getter_); | 85 DCHECK(!request_context_getter_); |
83 request_context_getter_ = request_context_getter; | 86 request_context_getter_ = request_context_getter; |
84 } | 87 } |
85 | 88 |
86 void UrlFetcher::Core::SetUploadData(const std::string& upload_content_type, | 89 void UrlFetcher::Core::SetUploadData(const std::string& upload_content_type, |
87 const std::string& upload_content) { | 90 const std::string& upload_content) { |
88 upload_content_type_ = upload_content_type; | 91 upload_content_type_ = upload_content_type; |
89 upload_content_ = upload_content; | 92 upload_content_ = upload_content; |
90 } | 93 } |
91 | 94 |
| 95 void UrlFetcher::Core::SetHeader(const std::string& key, |
| 96 const std::string& value) { |
| 97 request_headers_.SetHeader(key, value); |
| 98 } |
| 99 |
92 void UrlFetcher::Core::Start(const UrlFetcher::DoneCallback& done_callback) { | 100 void UrlFetcher::Core::Start(const UrlFetcher::DoneCallback& done_callback) { |
93 done_callback_ = done_callback; | 101 done_callback_ = done_callback; |
94 io_message_loop_ = request_context_getter_->GetIOMessageLoopProxy(); | 102 io_message_loop_ = request_context_getter_->GetIOMessageLoopProxy(); |
95 DCHECK(io_message_loop_); | 103 DCHECK(io_message_loop_); |
96 io_message_loop_->PostTask(FROM_HERE, base::Bind( | 104 io_message_loop_->PostTask(FROM_HERE, base::Bind( |
97 &UrlFetcher::Core::DoStart, this)); | 105 &UrlFetcher::Core::DoStart, this)); |
98 } | 106 } |
99 | 107 |
100 void UrlFetcher::Core::Detach() { | 108 void UrlFetcher::Core::Detach() { |
101 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); | 109 DCHECK(delegate_message_loop_->BelongsToCurrentThread()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 147 |
140 switch (method_) { | 148 switch (method_) { |
141 case GET: | 149 case GET: |
142 break; | 150 break; |
143 | 151 |
144 case POST: | 152 case POST: |
145 DCHECK(!upload_content_.empty()); | 153 DCHECK(!upload_content_.empty()); |
146 DCHECK(!upload_content_type_.empty()); | 154 DCHECK(!upload_content_type_.empty()); |
147 request_->set_method("POST"); | 155 request_->set_method("POST"); |
148 | 156 |
149 net::HttpRequestHeaders headers; | 157 request_headers_.SetHeader(net::HttpRequestHeaders::kContentType, |
150 headers.SetHeader(net::HttpRequestHeaders::kContentType, | 158 upload_content_type_); |
151 upload_content_type_); | |
152 request_->SetExtraRequestHeaders(headers); | |
153 | 159 |
154 request_->AppendBytesToUpload( | 160 request_->AppendBytesToUpload( |
155 upload_content_.data(), static_cast<int>(upload_content_.length())); | 161 upload_content_.data(), static_cast<int>(upload_content_.length())); |
156 break; | 162 break; |
157 } | 163 } |
158 | 164 |
| 165 request_->SetExtraRequestHeaders(request_headers_); |
| 166 |
159 request_->Start(); | 167 request_->Start(); |
160 } | 168 } |
161 | 169 |
162 void UrlFetcher::Core::ReadResponse() { | 170 void UrlFetcher::Core::ReadResponse() { |
163 int bytes_read = 0; | 171 int bytes_read = 0; |
164 if (request_->status().is_success()) { | 172 if (request_->status().is_success()) { |
165 request_->Read(buffer_, kBufferSize, &bytes_read); | 173 request_->Read(buffer_, kBufferSize, &bytes_read); |
166 } | 174 } |
167 OnReadCompleted(request_.get(), bytes_read); | 175 OnReadCompleted(request_.get(), bytes_read); |
168 } | 176 } |
(...skipping 24 matching lines...) Expand all Loading... |
193 void UrlFetcher::SetRequestContext( | 201 void UrlFetcher::SetRequestContext( |
194 net::URLRequestContextGetter* request_context_getter) { | 202 net::URLRequestContextGetter* request_context_getter) { |
195 core_->SetRequestContext(request_context_getter); | 203 core_->SetRequestContext(request_context_getter); |
196 } | 204 } |
197 | 205 |
198 void UrlFetcher::SetUploadData(const std::string& upload_content_type, | 206 void UrlFetcher::SetUploadData(const std::string& upload_content_type, |
199 const std::string& upload_content) { | 207 const std::string& upload_content) { |
200 core_->SetUploadData(upload_content_type, upload_content); | 208 core_->SetUploadData(upload_content_type, upload_content); |
201 } | 209 } |
202 | 210 |
| 211 void UrlFetcher::SetHeader(const std::string& key, const std::string& value) { |
| 212 core_->SetHeader(key, value); |
| 213 } |
| 214 |
203 void UrlFetcher::Start(const DoneCallback& done_callback) { | 215 void UrlFetcher::Start(const DoneCallback& done_callback) { |
204 core_->Start(done_callback); | 216 core_->Start(done_callback); |
205 } | 217 } |
206 | 218 |
207 } // namespace remoting | 219 } // namespace remoting |
OLD | NEW |