| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/net/url_request_slow_download_job.h" | 5 #include "content/browser/net/url_request_slow_download_job.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] = | 21 const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] = |
| 22 "http://url.handled.by.slow.download/download-unknown-size"; | 22 "http://url.handled.by.slow.download/download-unknown-size"; |
| 23 const char URLRequestSlowDownloadJob::kKnownSizeUrl[] = | 23 const char URLRequestSlowDownloadJob::kKnownSizeUrl[] = |
| 24 "http://url.handled.by.slow.download/download-known-size"; | 24 "http://url.handled.by.slow.download/download-known-size"; |
| 25 const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] = | 25 const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] = |
| 26 "http://url.handled.by.slow.download/download-finish"; | 26 "http://url.handled.by.slow.download/download-finish"; |
| 27 | 27 |
| 28 const int URLRequestSlowDownloadJob::kFirstDownloadSize = 1024 * 35; | 28 const int URLRequestSlowDownloadJob::kFirstDownloadSize = 1024 * 35; |
| 29 const int URLRequestSlowDownloadJob::kSecondDownloadSize = 1024 * 10; | 29 const int URLRequestSlowDownloadJob::kSecondDownloadSize = 1024 * 10; |
| 30 | 30 |
| 31 std::set<URLRequestSlowDownloadJob*> | 31 // static |
| 32 URLRequestSlowDownloadJob::pending_requests_; | 32 base::LazyInstance< |
| 33 URLRequestSlowDownloadJob::SlowJobsSet, |
| 34 base::LeakyLazyInstanceTraits<URLRequestSlowDownloadJob::SlowJobsSet> > |
| 35 URLRequestSlowDownloadJob::pending_requests_(base::LINKER_INITIALIZED); |
| 33 | 36 |
| 34 void URLRequestSlowDownloadJob::Start() { | 37 void URLRequestSlowDownloadJob::Start() { |
| 35 MessageLoop::current()->PostTask( | 38 MessageLoop::current()->PostTask( |
| 36 FROM_HERE, | 39 FROM_HERE, |
| 37 method_factory_.NewRunnableMethod( | 40 method_factory_.NewRunnableMethod( |
| 38 &URLRequestSlowDownloadJob::StartAsync)); | 41 &URLRequestSlowDownloadJob::StartAsync)); |
| 39 } | 42 } |
| 40 | 43 |
| 41 // static | 44 // static |
| 42 void URLRequestSlowDownloadJob::AddUrlHandler() { | 45 void URLRequestSlowDownloadJob::AddUrlHandler() { |
| 43 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 46 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 44 filter->AddUrlHandler(GURL(kUnknownSizeUrl), | 47 filter->AddUrlHandler(GURL(kUnknownSizeUrl), |
| 45 &URLRequestSlowDownloadJob::Factory); | 48 &URLRequestSlowDownloadJob::Factory); |
| 46 filter->AddUrlHandler(GURL(kKnownSizeUrl), | 49 filter->AddUrlHandler(GURL(kKnownSizeUrl), |
| 47 &URLRequestSlowDownloadJob::Factory); | 50 &URLRequestSlowDownloadJob::Factory); |
| 48 filter->AddUrlHandler(GURL(kFinishDownloadUrl), | 51 filter->AddUrlHandler(GURL(kFinishDownloadUrl), |
| 49 &URLRequestSlowDownloadJob::Factory); | 52 &URLRequestSlowDownloadJob::Factory); |
| 50 } | 53 } |
| 51 | 54 |
| 52 // static | 55 // static |
| 53 net::URLRequestJob* URLRequestSlowDownloadJob::Factory( | 56 net::URLRequestJob* URLRequestSlowDownloadJob::Factory( |
| 54 net::URLRequest* request, | 57 net::URLRequest* request, |
| 55 const std::string& scheme) { | 58 const std::string& scheme) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 57 URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request); | 60 URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request); |
| 58 if (request->url().spec() != kFinishDownloadUrl) | 61 if (request->url().spec() != kFinishDownloadUrl) |
| 59 pending_requests_.insert(job); | 62 pending_requests_.Get().insert(job); |
| 60 return job; | 63 return job; |
| 61 } | 64 } |
| 62 | 65 |
| 63 // static | 66 // static |
| 64 size_t URLRequestSlowDownloadJob::NumberOutstandingRequests() { | 67 size_t URLRequestSlowDownloadJob::NumberOutstandingRequests() { |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 66 return pending_requests_.size(); | 69 return pending_requests_.Get().size(); |
| 67 } | 70 } |
| 68 | 71 |
| 69 // static | 72 // static |
| 70 void URLRequestSlowDownloadJob::FinishPendingRequests() { | 73 void URLRequestSlowDownloadJob::FinishPendingRequests() { |
| 71 typedef std::set<URLRequestSlowDownloadJob*> JobList; | 74 typedef std::set<URLRequestSlowDownloadJob*> JobList; |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 73 for (JobList::iterator it = pending_requests_.begin(); it != | 76 for (JobList::iterator it = pending_requests_.Get().begin(); it != |
| 74 pending_requests_.end(); ++it) { | 77 pending_requests_.Get().end(); ++it) { |
| 75 (*it)->set_should_finish_download(); | 78 (*it)->set_should_finish_download(); |
| 76 } | 79 } |
| 77 } | 80 } |
| 78 | 81 |
| 79 URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(net::URLRequest* request) | 82 URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(net::URLRequest* request) |
| 80 : net::URLRequestJob(request), | 83 : net::URLRequestJob(request), |
| 81 first_download_size_remaining_(kFirstDownloadSize), | 84 first_download_size_remaining_(kFirstDownloadSize), |
| 82 should_finish_download_(false), | 85 should_finish_download_(false), |
| 83 buffer_size_(0), | 86 buffer_size_(0), |
| 84 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 87 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 154 } |
| 152 | 155 |
| 153 // Public virtual version. | 156 // Public virtual version. |
| 154 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { | 157 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 155 // Forward to private const version. | 158 // Forward to private const version. |
| 156 GetResponseInfoConst(info); | 159 GetResponseInfoConst(info); |
| 157 } | 160 } |
| 158 | 161 |
| 159 URLRequestSlowDownloadJob::~URLRequestSlowDownloadJob() { | 162 URLRequestSlowDownloadJob::~URLRequestSlowDownloadJob() { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 161 pending_requests_.erase(this); | 164 pending_requests_.Get().erase(this); |
| 162 } | 165 } |
| 163 | 166 |
| 164 // Private const version. | 167 // Private const version. |
| 165 void URLRequestSlowDownloadJob::GetResponseInfoConst( | 168 void URLRequestSlowDownloadJob::GetResponseInfoConst( |
| 166 net::HttpResponseInfo* info) const { | 169 net::HttpResponseInfo* info) const { |
| 167 // Send back mock headers. | 170 // Send back mock headers. |
| 168 std::string raw_headers; | 171 std::string raw_headers; |
| 169 if (LowerCaseEqualsASCII(kFinishDownloadUrl, | 172 if (LowerCaseEqualsASCII(kFinishDownloadUrl, |
| 170 request_->url().spec().c_str())) { | 173 request_->url().spec().c_str())) { |
| 171 raw_headers.append( | 174 raw_headers.append( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 187 // ParseRawHeaders expects \0 to end each header line. | 190 // ParseRawHeaders expects \0 to end each header line. |
| 188 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 191 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
| 189 info->headers = new net::HttpResponseHeaders(raw_headers); | 192 info->headers = new net::HttpResponseHeaders(raw_headers); |
| 190 } | 193 } |
| 191 | 194 |
| 192 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { | 195 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { |
| 193 net::HttpResponseInfo info; | 196 net::HttpResponseInfo info; |
| 194 GetResponseInfoConst(&info); | 197 GetResponseInfoConst(&info); |
| 195 return info.headers && info.headers->GetMimeType(mime_type); | 198 return info.headers && info.headers->GetMimeType(mime_type); |
| 196 } | 199 } |
| OLD | NEW |