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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
// Copyright (c) 2012 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/service/cloud_print/cloud_print_url_fetcher.h"
#include "base/metrics/histogram.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "chrome/common/cloud_print/cloud_print_helpers.h"
#include "chrome/service/cloud_print/cloud_print_service_helpers.h"
#include "chrome/service/cloud_print/cloud_print_token_store.h"
#include "chrome/service/net/service_url_request_context_getter.h"
#include "chrome/service/service_process.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
namespace cloud_print {
namespace {
void ReportRequestTime(CloudPrintURLFetcher::RequestType type,
base::TimeDelta time) {
if (type == CloudPrintURLFetcher::REQUEST_REGISTER) {
UMA_HISTOGRAM_TIMES("CloudPrint.UrlFetcherRequestTime.Register", time);
} else if (type == CloudPrintURLFetcher::REQUEST_UPDATE_PRINTER) {
UMA_HISTOGRAM_TIMES("CloudPrint.UrlFetcherRequestTime.UpdatePrinter", time);
} else if (type == CloudPrintURLFetcher::REQUEST_DATA) {
UMA_HISTOGRAM_TIMES("CloudPrint.UrlFetcherRequestTime.DownloadData", time);
} else {
UMA_HISTOGRAM_TIMES("CloudPrint.UrlFetcherRequestTime.Other", time);
}
}
void ReportRetriesCount(CloudPrintURLFetcher::RequestType type,
int retries) {
if (type == CloudPrintURLFetcher::REQUEST_REGISTER) {
UMA_HISTOGRAM_COUNTS_100("CloudPrint.UrlFetcherRetries.Register", retries);
} else if (type == CloudPrintURLFetcher::REQUEST_UPDATE_PRINTER) {
UMA_HISTOGRAM_COUNTS_100("CloudPrint.UrlFetcherRetries.UpdatePrinter",
retries);
} else if (type == CloudPrintURLFetcher::REQUEST_DATA) {
UMA_HISTOGRAM_COUNTS_100("CloudPrint.UrlFetcherRetries.DownloadData",
retries);
} else {
UMA_HISTOGRAM_COUNTS_100("CloudPrint.UrlFetcherRetries.Other", retries);
}
}
void ReportDownloadSize(CloudPrintURLFetcher::RequestType type, size_t size) {
if (type == CloudPrintURLFetcher::REQUEST_REGISTER) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherDownloadSize.Register", size);
} else if (type == CloudPrintURLFetcher::REQUEST_UPDATE_PRINTER) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherDownloadSize.UpdatePrinter",
size);
} else if (type == CloudPrintURLFetcher::REQUEST_DATA) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherDownloadSize.DownloadData",
size);
} else {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherDownloadSize.Other", size);
}
}
void ReportUploadSize(CloudPrintURLFetcher::RequestType type, size_t size) {
if (type == CloudPrintURLFetcher::REQUEST_REGISTER) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherUploadSize.Register", size);
} else if (type == CloudPrintURLFetcher::REQUEST_UPDATE_PRINTER) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherUploadSize.UpdatePrinter",
size);
} else if (type == CloudPrintURLFetcher::REQUEST_DATA) {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherUploadSize.DownloadData",
size);
} else {
UMA_HISTOGRAM_MEMORY_KB("CloudPrint.UrlFetcherUploadSize.Other", size);
}
}
CloudPrintURLFetcherFactory* g_factory = NULL;
} // namespace
// virtual
CloudPrintURLFetcherFactory::~CloudPrintURLFetcherFactory() {}
// static
CloudPrintURLFetcher* CloudPrintURLFetcher::Create() {
CloudPrintURLFetcherFactory* factory = CloudPrintURLFetcher::factory();
return factory ? factory->CreateCloudPrintURLFetcher() :
new CloudPrintURLFetcher;
}
// static
CloudPrintURLFetcherFactory* CloudPrintURLFetcher::factory() {
return g_factory;
}
// static
void CloudPrintURLFetcher::set_factory(CloudPrintURLFetcherFactory* factory) {
g_factory = factory;
}
CloudPrintURLFetcher::ResponseAction
CloudPrintURLFetcher::Delegate::HandleRawResponse(
const net::URLFetcher* source,
const GURL& url,
const net::URLRequestStatus& status,
int response_code,
const net::ResponseCookies& cookies,
const std::string& data) {
return CONTINUE_PROCESSING;
}
CloudPrintURLFetcher::ResponseAction
CloudPrintURLFetcher::Delegate::HandleRawData(
const net::URLFetcher* source,
const GURL& url,
const std::string& data) {
return CONTINUE_PROCESSING;
}
CloudPrintURLFetcher::ResponseAction
CloudPrintURLFetcher::Delegate::HandleJSONData(
const net::URLFetcher* source,
const GURL& url,
base::DictionaryValue* json_data,
bool succeeded) {
return CONTINUE_PROCESSING;
}
CloudPrintURLFetcher::CloudPrintURLFetcher()
: delegate_(NULL),
num_retries_(0),
type_(REQUEST_MAX) {
}
bool CloudPrintURLFetcher::IsSameRequest(const net::URLFetcher* source) {
return (request_.get() == source);
}
void CloudPrintURLFetcher::StartGetRequest(
RequestType type,
const GURL& url,
Delegate* delegate,
int max_retries,
const std::string& additional_headers) {
StartRequestHelper(type, url, net::URLFetcher::GET, delegate, max_retries,
std::string(), std::string(), additional_headers);
}
void CloudPrintURLFetcher::StartPostRequest(
RequestType type,
const GURL& url,
Delegate* delegate,
int max_retries,
const std::string& post_data_mime_type,
const std::string& post_data,
const std::string& additional_headers) {
StartRequestHelper(type, url, net::URLFetcher::POST, delegate, max_retries,
post_data_mime_type, post_data, additional_headers);
}
void CloudPrintURLFetcher::OnURLFetchComplete(
const net::URLFetcher* source) {
VLOG(1) << "CP_PROXY: OnURLFetchComplete, url: " << source->GetURL()
<< ", response code: " << source->GetResponseCode();
// Make sure we stay alive through the body of this function.
scoped_refptr<CloudPrintURLFetcher> keep_alive(this);
std::string data;
source->GetResponseAsString(&data);
ReportRequestTime(type_, base::Time::Now() - start_time_);
ReportDownloadSize(type_, data.size());
ResponseAction action = delegate_->HandleRawResponse(
source,
source->GetURL(),
source->GetStatus(),
source->GetResponseCode(),
source->GetCookies(),
data);
// If we get auth error, notify delegate and check if it wants to proceed.
if (action == CONTINUE_PROCESSING &&
source->GetResponseCode() == net::HTTP_FORBIDDEN) {
action = delegate_->OnRequestAuthError();
}
if (action == CONTINUE_PROCESSING) {
// We need to retry on all network errors.
if (!source->GetStatus().is_success() || (source->GetResponseCode() != 200))
action = RETRY_REQUEST;
else
action = delegate_->HandleRawData(source, source->GetURL(), data);
if (action == CONTINUE_PROCESSING) {
// If the delegate is not interested in handling the raw response data,
// we assume that a JSON response is expected. If we do not get a JSON
// response, we will retry (to handle the case where we got redirected
// to a non-cloudprint-server URL eg. for authentication).
bool succeeded = false;
scoped_ptr<base::DictionaryValue> response_dict =
ParseResponseJSON(data, &succeeded);
if (response_dict) {
action = delegate_->HandleJSONData(source,
source->GetURL(),
response_dict.get(),
succeeded);
} else {
action = RETRY_REQUEST;
}
}
}
// Retry the request if needed.
if (action == RETRY_REQUEST) {
// Explicitly call ReceivedContentWasMalformed() to ensure the current
// request gets counted as a failure for calculation of the back-off
// period. If it was already a failure by status code, this call will
// be ignored.
request_->ReceivedContentWasMalformed();
// If we receive error code from the server "Media Type Not Supported",
// there is no reason to retry, request will never succeed.
// In that case we should call OnRequestGiveUp() right away.
if (source->GetResponseCode() == net::HTTP_UNSUPPORTED_MEDIA_TYPE)
num_retries_ = source->GetMaxRetriesOn5xx();
++num_retries_;
if ((-1 != source->GetMaxRetriesOn5xx()) &&
(num_retries_ > source->GetMaxRetriesOn5xx())) {
// Retry limit reached. Give up.
delegate_->OnRequestGiveUp();
action = STOP_PROCESSING;
} else {
// Either no retry limit specified or retry limit has not yet been
// reached. Try again. Set up the request headers again because the token
// may have changed.
SetupRequestHeaders();
request_->SetRequestContext(GetRequestContextGetter());
start_time_ = base::Time::Now();
request_->Start();
}
}
if (action != RETRY_REQUEST) {
ReportRetriesCount(type_, num_retries_);
}
}
void CloudPrintURLFetcher::StartRequestHelper(
RequestType type,
const GURL& url,
net::URLFetcher::RequestType request_type,
Delegate* delegate,
int max_retries,
const std::string& post_data_mime_type,
const std::string& post_data,
const std::string& additional_headers) {
DCHECK(delegate);
type_ = type;
UMA_HISTOGRAM_ENUMERATION("CloudPrint.UrlFetcherRequestType", type,
REQUEST_MAX);
// Persist the additional headers in case we need to retry the request.
additional_headers_ = additional_headers;
request_ = net::URLFetcher::Create(0, url, request_type, this);
request_->SetRequestContext(GetRequestContextGetter());
// Since we implement our own retry logic, disable the retry in URLFetcher.
request_->SetAutomaticallyRetryOn5xx(false);
request_->SetMaxRetriesOn5xx(max_retries);
delegate_ = delegate;
SetupRequestHeaders();
request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
net::LOAD_DO_NOT_SAVE_COOKIES);
if (request_type == net::URLFetcher::POST) {
request_->SetUploadData(post_data_mime_type, post_data);
ReportUploadSize(type_, post_data.size());
}
start_time_ = base::Time::Now();
request_->Start();
}
void CloudPrintURLFetcher::SetupRequestHeaders() {
std::string headers = delegate_->GetAuthHeader();
if (!headers.empty())
headers += "\r\n";
headers += kChromeCloudPrintProxyHeader;
if (!additional_headers_.empty()) {
headers += "\r\n";
headers += additional_headers_;
}
request_->SetExtraRequestHeaders(headers);
}
CloudPrintURLFetcher::~CloudPrintURLFetcher() {}
net::URLRequestContextGetter* CloudPrintURLFetcher::GetRequestContextGetter() {
ServiceURLRequestContextGetter* getter =
g_service_process->GetServiceURLRequestContextGetter();
// Now set up the user agent for cloudprint.
std::string user_agent = getter->user_agent();
base::StringAppendF(&user_agent, " %s", kCloudPrintUserAgent);
getter->set_user_agent(user_agent);
return getter;
}
} // namespace cloud_print
|