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/bind.h" |
7 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
10 #include "base/string_util.h" | 11 #include "base/string_util.h" |
11 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
13 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
15 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
16 #include "net/url_request/url_request.h" | 17 #include "net/url_request/url_request.h" |
(...skipping 13 matching lines...) Expand all Loading... |
30 | 31 |
31 // static | 32 // static |
32 base::LazyInstance< | 33 base::LazyInstance< |
33 URLRequestSlowDownloadJob::SlowJobsSet, | 34 URLRequestSlowDownloadJob::SlowJobsSet, |
34 base::LeakyLazyInstanceTraits<URLRequestSlowDownloadJob::SlowJobsSet> > | 35 base::LeakyLazyInstanceTraits<URLRequestSlowDownloadJob::SlowJobsSet> > |
35 URLRequestSlowDownloadJob::pending_requests_(base::LINKER_INITIALIZED); | 36 URLRequestSlowDownloadJob::pending_requests_(base::LINKER_INITIALIZED); |
36 | 37 |
37 void URLRequestSlowDownloadJob::Start() { | 38 void URLRequestSlowDownloadJob::Start() { |
38 MessageLoop::current()->PostTask( | 39 MessageLoop::current()->PostTask( |
39 FROM_HERE, | 40 FROM_HERE, |
40 method_factory_.NewRunnableMethod( | 41 base::Bind(&URLRequestSlowDownloadJob::StartAsync, |
41 &URLRequestSlowDownloadJob::StartAsync)); | 42 weak_factory_.GetWeakPtr())); |
42 } | 43 } |
43 | 44 |
44 // static | 45 // static |
45 void URLRequestSlowDownloadJob::AddUrlHandler() { | 46 void URLRequestSlowDownloadJob::AddUrlHandler() { |
46 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 47 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
47 filter->AddUrlHandler(GURL(kUnknownSizeUrl), | 48 filter->AddUrlHandler(GURL(kUnknownSizeUrl), |
48 &URLRequestSlowDownloadJob::Factory); | 49 &URLRequestSlowDownloadJob::Factory); |
49 filter->AddUrlHandler(GURL(kKnownSizeUrl), | 50 filter->AddUrlHandler(GURL(kKnownSizeUrl), |
50 &URLRequestSlowDownloadJob::Factory); | 51 &URLRequestSlowDownloadJob::Factory); |
51 filter->AddUrlHandler(GURL(kFinishDownloadUrl), | 52 filter->AddUrlHandler(GURL(kFinishDownloadUrl), |
(...skipping 25 matching lines...) Expand all Loading... |
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(net::URLRequest* request) |
83 : net::URLRequestJob(request), | 84 : net::URLRequestJob(request), |
84 first_download_size_remaining_(kFirstDownloadSize), | 85 first_download_size_remaining_(kFirstDownloadSize), |
85 should_finish_download_(false), | 86 should_finish_download_(false), |
86 buffer_size_(0), | 87 buffer_size_(0), |
87 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 88 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
88 } | 89 } |
89 | 90 |
90 void URLRequestSlowDownloadJob::StartAsync() { | 91 void URLRequestSlowDownloadJob::StartAsync() { |
91 if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str())) | 92 if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str())) |
92 URLRequestSlowDownloadJob::FinishPendingRequests(); | 93 URLRequestSlowDownloadJob::FinishPendingRequests(); |
93 | 94 |
94 NotifyHeadersComplete(); | 95 NotifyHeadersComplete(); |
95 } | 96 } |
96 | 97 |
97 bool URLRequestSlowDownloadJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 98 bool URLRequestSlowDownloadJob::ReadRawData(net::IOBuffer* buf, int buf_size, |
(...skipping 21 matching lines...) Expand all Loading... |
119 return true; | 120 return true; |
120 } | 121 } |
121 | 122 |
122 // If we make it here, the first chunk has been sent and we need to wait | 123 // If we make it here, the first chunk has been sent and we need to wait |
123 // until a request is made for kFinishDownloadUrl. | 124 // until a request is made for kFinishDownloadUrl. |
124 buffer_ = buf; | 125 buffer_ = buf; |
125 buffer_size_ = buf_size; | 126 buffer_size_ = buf_size; |
126 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 127 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
127 MessageLoop::current()->PostDelayedTask( | 128 MessageLoop::current()->PostDelayedTask( |
128 FROM_HERE, | 129 FROM_HERE, |
129 method_factory_.NewRunnableMethod( | 130 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, |
130 &URLRequestSlowDownloadJob::CheckDoneStatus), | 131 weak_factory_.GetWeakPtr()), |
131 100); | 132 100); |
132 | 133 |
133 // Return false to signal there is pending data. | 134 // Return false to signal there is pending data. |
134 return false; | 135 return false; |
135 } | 136 } |
136 | 137 |
137 void URLRequestSlowDownloadJob::CheckDoneStatus() { | 138 void URLRequestSlowDownloadJob::CheckDoneStatus() { |
138 if (should_finish_download_) { | 139 if (should_finish_download_) { |
139 DCHECK(NULL != buffer_); | 140 DCHECK(NULL != buffer_); |
140 // Send the rest of the data. | 141 // Send the rest of the data. |
141 DCHECK_GT(buffer_size_, kSecondDownloadSize); | 142 DCHECK_GT(buffer_size_, kSecondDownloadSize); |
142 for (int i = 0; i < kSecondDownloadSize; ++i) { | 143 for (int i = 0; i < kSecondDownloadSize; ++i) { |
143 buffer_->data()[i] = '*'; | 144 buffer_->data()[i] = '*'; |
144 } | 145 } |
145 SetStatus(net::URLRequestStatus()); | 146 SetStatus(net::URLRequestStatus()); |
146 NotifyReadComplete(kSecondDownloadSize); // Deletes this. | 147 NotifyReadComplete(kSecondDownloadSize); // Deletes this. |
147 } else { | 148 } else { |
148 MessageLoop::current()->PostDelayedTask( | 149 MessageLoop::current()->PostDelayedTask( |
149 FROM_HERE, | 150 FROM_HERE, |
150 method_factory_.NewRunnableMethod( | 151 base::Bind(&URLRequestSlowDownloadJob::CheckDoneStatus, |
151 &URLRequestSlowDownloadJob::CheckDoneStatus), | 152 weak_factory_.GetWeakPtr()), |
152 100); | 153 100); |
153 } | 154 } |
154 } | 155 } |
155 | 156 |
156 // Public virtual version. | 157 // Public virtual version. |
157 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { | 158 void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) { |
158 // Forward to private const version. | 159 // Forward to private const version. |
159 GetResponseInfoConst(info); | 160 GetResponseInfoConst(info); |
160 } | 161 } |
161 | 162 |
(...skipping 28 matching lines...) Expand all Loading... |
190 // ParseRawHeaders expects \0 to end each header line. | 191 // ParseRawHeaders expects \0 to end each header line. |
191 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 192 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); |
192 info->headers = new net::HttpResponseHeaders(raw_headers); | 193 info->headers = new net::HttpResponseHeaders(raw_headers); |
193 } | 194 } |
194 | 195 |
195 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { | 196 bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const { |
196 net::HttpResponseInfo info; | 197 net::HttpResponseInfo info; |
197 GetResponseInfoConst(&info); | 198 GetResponseInfoConst(&info); |
198 return info.headers && info.headers->GetMimeType(mime_type); | 199 return info.headers && info.headers->GetMimeType(mime_type); |
199 } | 200 } |
OLD | NEW |