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/test/net/url_request_slow_download_job.h" | 5 #include "content/test/net/url_request_slow_download_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
18 #include "net/url_request/url_request_context.h" | |
19 #include "net/url_request/url_request_filter.h" | 18 #include "net/url_request/url_request_filter.h" |
20 | 19 |
21 using content::BrowserThread; | 20 using content::BrowserThread; |
22 | 21 |
23 const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] = | 22 const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] = |
24 "http://url.handled.by.slow.download/download-unknown-size"; | 23 "http://url.handled.by.slow.download/download-unknown-size"; |
25 const char URLRequestSlowDownloadJob::kKnownSizeUrl[] = | 24 const char URLRequestSlowDownloadJob::kKnownSizeUrl[] = |
26 "http://url.handled.by.slow.download/download-known-size"; | 25 "http://url.handled.by.slow.download/download-known-size"; |
27 const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] = | 26 const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] = |
28 "http://url.handled.by.slow.download/download-finish"; | 27 "http://url.handled.by.slow.download/download-finish"; |
(...skipping 19 matching lines...) Expand all Loading... |
48 &URLRequestSlowDownloadJob::Factory); | 47 &URLRequestSlowDownloadJob::Factory); |
49 filter->AddUrlHandler(GURL(kKnownSizeUrl), | 48 filter->AddUrlHandler(GURL(kKnownSizeUrl), |
50 &URLRequestSlowDownloadJob::Factory); | 49 &URLRequestSlowDownloadJob::Factory); |
51 filter->AddUrlHandler(GURL(kFinishDownloadUrl), | 50 filter->AddUrlHandler(GURL(kFinishDownloadUrl), |
52 &URLRequestSlowDownloadJob::Factory); | 51 &URLRequestSlowDownloadJob::Factory); |
53 } | 52 } |
54 | 53 |
55 // static | 54 // static |
56 net::URLRequestJob* URLRequestSlowDownloadJob::Factory( | 55 net::URLRequestJob* URLRequestSlowDownloadJob::Factory( |
57 net::URLRequest* request, | 56 net::URLRequest* request, |
| 57 net::NetworkDelegate* network_delegate, |
58 const std::string& scheme) { | 58 const std::string& scheme) { |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
60 URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request); | 60 URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob( |
| 61 request, network_delegate); |
61 if (request->url().spec() != kFinishDownloadUrl) | 62 if (request->url().spec() != kFinishDownloadUrl) |
62 pending_requests_.Get().insert(job); | 63 pending_requests_.Get().insert(job); |
63 return job; | 64 return job; |
64 } | 65 } |
65 | 66 |
66 // static | 67 // static |
67 size_t URLRequestSlowDownloadJob::NumberOutstandingRequests() { | 68 size_t URLRequestSlowDownloadJob::NumberOutstandingRequests() { |
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
69 return pending_requests_.Get().size(); | 70 return pending_requests_.Get().size(); |
70 } | 71 } |
71 | 72 |
72 // static | 73 // static |
73 void URLRequestSlowDownloadJob::FinishPendingRequests() { | 74 void URLRequestSlowDownloadJob::FinishPendingRequests() { |
74 typedef std::set<URLRequestSlowDownloadJob*> JobList; | 75 typedef std::set<URLRequestSlowDownloadJob*> JobList; |
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
76 for (JobList::iterator it = pending_requests_.Get().begin(); it != | 77 for (JobList::iterator it = pending_requests_.Get().begin(); it != |
77 pending_requests_.Get().end(); ++it) { | 78 pending_requests_.Get().end(); ++it) { |
78 (*it)->set_should_finish_download(); | 79 (*it)->set_should_finish_download(); |
79 } | 80 } |
80 } | 81 } |
81 | 82 |
82 URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(net::URLRequest* request) | 83 URLRequestSlowDownloadJob::URLRequestSlowDownloadJob( |
83 : net::URLRequestJob(request, request->context()->network_delegate()), | 84 net::URLRequest* request, net::NetworkDelegate* network_delegate) |
| 85 : net::URLRequestJob(request, network_delegate), |
84 bytes_already_sent_(0), | 86 bytes_already_sent_(0), |
85 should_finish_download_(false), | 87 should_finish_download_(false), |
86 buffer_size_(0), | 88 buffer_size_(0), |
87 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 89 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
88 } | 90 } |
89 | 91 |
90 void URLRequestSlowDownloadJob::StartAsync() { | 92 void URLRequestSlowDownloadJob::StartAsync() { |
91 if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str())) | 93 if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str())) |
92 URLRequestSlowDownloadJob::FinishPendingRequests(); | 94 URLRequestSlowDownloadJob::FinishPendingRequests(); |
93 | 95 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 // ParseRawHeaders expects \0 to end each header line. | 236 // ParseRawHeaders expects \0 to end each header line. |
235 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 237 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
236 info->headers = new net::HttpResponseHeaders(raw_headers); | 238 info->headers = new net::HttpResponseHeaders(raw_headers); |
237 } | 239 } |
238 | 240 |
239 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { | 241 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { |
240 net::HttpResponseInfo info; | 242 net::HttpResponseInfo info; |
241 GetResponseInfoConst(&info); | 243 GetResponseInfoConst(&info); |
242 return info.headers && info.headers->GetMimeType(mime_type); | 244 return info.headers && info.headers->GetMimeType(mime_type); |
243 } | 245 } |
OLD | NEW |