1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/url_request_slow_download_job.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "googleurl/src/gurl.h"
#include "net/base/io_buffer.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_filter.h"
const int kFirstDownloadSize = 1024 * 35;
const int kSecondDownloadSize = 1024 * 10;
const char URLRequestSlowDownloadJob::kUnknownSizeUrl[] =
"http://url.handled.by.slow.download/download-unknown-size";
const char URLRequestSlowDownloadJob::kKnownSizeUrl[] =
"http://url.handled.by.slow.download/download-known-size";
const char URLRequestSlowDownloadJob::kFinishDownloadUrl[] =
"http://url.handled.by.slow.download/download-finish";
std::vector<URLRequestSlowDownloadJob*>
URLRequestSlowDownloadJob::kPendingRequests;
void URLRequestSlowDownloadJob::Start() {
MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(this,
&URLRequestSlowDownloadJob::StartAsync));
}
/* static */
void URLRequestSlowDownloadJob::AddUrlHandler() {
URLRequestFilter* filter = URLRequestFilter::GetInstance();
filter->AddUrlHandler(GURL(kUnknownSizeUrl),
&URLRequestSlowDownloadJob::Factory);
filter->AddUrlHandler(GURL(kKnownSizeUrl),
&URLRequestSlowDownloadJob::Factory);
filter->AddUrlHandler(GURL(kFinishDownloadUrl),
&URLRequestSlowDownloadJob::Factory);
}
/*static */
URLRequestJob* URLRequestSlowDownloadJob::Factory(URLRequest* request,
const std::string& scheme) {
URLRequestSlowDownloadJob* job = new URLRequestSlowDownloadJob(request);
if (request->url().spec() != kFinishDownloadUrl)
URLRequestSlowDownloadJob::kPendingRequests.push_back(job);
return job;
}
/* static */
void URLRequestSlowDownloadJob::FinishPendingRequests() {
typedef std::vector<URLRequestSlowDownloadJob*> JobList;
for (JobList::iterator it = kPendingRequests.begin(); it !=
kPendingRequests.end(); ++it) {
(*it)->set_should_finish_download();
}
kPendingRequests.clear();
}
URLRequestSlowDownloadJob::URLRequestSlowDownloadJob(URLRequest* request)
: URLRequestJob(request),
first_download_size_remaining_(kFirstDownloadSize),
should_finish_download_(false),
should_send_second_chunk_(false) {
}
void URLRequestSlowDownloadJob::StartAsync() {
if (LowerCaseEqualsASCII(kFinishDownloadUrl, request_->url().spec().c_str()))
URLRequestSlowDownloadJob::FinishPendingRequests();
NotifyHeadersComplete();
}
bool URLRequestSlowDownloadJob::ReadRawData(net::IOBuffer* buf, int buf_size,
int *bytes_read) {
if (LowerCaseEqualsASCII(kFinishDownloadUrl,
request_->url().spec().c_str())) {
*bytes_read = 0;
return true;
}
if (should_send_second_chunk_) {
DCHECK(buf_size > kSecondDownloadSize);
for (int i = 0; i < kSecondDownloadSize; ++i) {
buf->data()[i] = '*';
}
*bytes_read = kSecondDownloadSize;
should_send_second_chunk_ = false;
return true;
}
if (first_download_size_remaining_ > 0) {
int send_size = std::min(first_download_size_remaining_, buf_size);
for (int i = 0; i < send_size; ++i) {
buf->data()[i] = '*';
}
*bytes_read = send_size;
first_download_size_remaining_ -= send_size;
DCHECK(!is_done());
return true;
}
if (should_finish_download_) {
*bytes_read = 0;
return true;
}
// If we make it here, the first chunk has been sent and we need to wait
// until a request is made for kFinishDownloadUrl.
SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
this, &URLRequestSlowDownloadJob::CheckDoneStatus), 100);
AddRef();
// Return false to signal there is pending data.
return false;
}
void URLRequestSlowDownloadJob::CheckDoneStatus() {
if (should_finish_download_) {
should_send_second_chunk_ = true;
SetStatus(URLRequestStatus());
NotifyReadComplete(kSecondDownloadSize);
Release();
} else {
MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableMethod(
this, &URLRequestSlowDownloadJob::CheckDoneStatus), 100);
}
}
// Public virtual version.
void URLRequestSlowDownloadJob::GetResponseInfo(net::HttpResponseInfo* info) {
// Forward to private const version.
GetResponseInfoConst(info);
}
// Private const version.
void URLRequestSlowDownloadJob::GetResponseInfoConst(
net::HttpResponseInfo* info) const {
// Send back mock headers.
std::string raw_headers;
if (LowerCaseEqualsASCII(kFinishDownloadUrl,
request_->url().spec().c_str())) {
raw_headers.append(
"HTTP/1.1 200 OK\n"
"Content-type: text/plain\n");
} else {
raw_headers.append(
"HTTP/1.1 200 OK\n"
"Content-type: application/octet-stream\n"
"Cache-Control: max-age=0\n");
if (LowerCaseEqualsASCII(kKnownSizeUrl, request_->url().spec().c_str())) {
raw_headers.append(StringPrintf("Content-Length: %d\n",
kFirstDownloadSize + kSecondDownloadSize));
}
}
// ParseRawHeaders expects \0 to end each header line.
ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1));
info->headers = new net::HttpResponseHeaders(raw_headers);
}
bool URLRequestSlowDownloadJob::GetMimeType(std::string* mime_type) const {
net::HttpResponseInfo info;
GetResponseInfoConst(&info);
return info.headers && info.headers->GetMimeType(mime_type);
}
|