OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/net/http_pipelining_compatibility_client.h" | |
6 | |
7 #include "base/metrics/histogram.h" | |
8 #include "base/stringprintf.h" | |
9 #include "net/base/load_flags.h" | |
10 #include "net/disk_cache/histogram_macros.h" | |
11 | |
12 namespace chrome_browser_net { | |
13 | |
14 HttpPipeliningCompatibilityClient::HttpPipeliningCompatibilityClient() | |
15 : num_finished_(0) { | |
16 } | |
17 | |
18 HttpPipeliningCompatibilityClient::~HttpPipeliningCompatibilityClient() { | |
19 } | |
20 | |
21 void HttpPipeliningCompatibilityClient::Start( | |
22 const std::string& base_url, | |
23 std::vector<RequestInfo>& requests, | |
24 const net::CompletionCallback& callback, | |
25 net::URLRequestContext* url_request_context) { | |
26 finished_callback_ = callback; | |
27 for (size_t i = 0; i < requests.size(); ++i) { | |
28 requests_.push_back(new Request(i, base_url, requests[i], this, | |
29 url_request_context)); | |
30 } | |
31 } | |
32 | |
33 void HttpPipeliningCompatibilityClient::OnRequestFinished(int request_id, | |
34 Status status) { | |
35 // The CACHE_HISTOGRAM_* macros are used, because they allow dynamic metric | |
36 // names. | |
37 CACHE_HISTOGRAM_ENUMERATION(GetMetricName(request_id, "Status"), | |
38 status, STATUS_MAX); | |
39 ++num_finished_; | |
40 if (num_finished_ == requests_.size()) { | |
41 finished_callback_.Run(0); | |
42 } | |
43 } | |
44 | |
45 void HttpPipeliningCompatibilityClient::ReportNetworkError(int request_id, | |
46 int error_code) { | |
47 CACHE_HISTOGRAM_ENUMERATION(GetMetricName(request_id, "NetworkError"), | |
48 -error_code, 900); | |
49 } | |
50 | |
51 void HttpPipeliningCompatibilityClient::ReportResponseCode(int request_id, | |
52 int response_code) { | |
53 CACHE_HISTOGRAM_ENUMERATION(GetMetricName(request_id, "ResponseCode"), | |
54 response_code, 600); | |
55 } | |
56 | |
57 std::string HttpPipeliningCompatibilityClient::GetMetricName( | |
58 int request_id, const char* description) { | |
59 return base::StringPrintf("NetConnectivity.Pipeline.%d.%s", | |
60 request_id, description); | |
61 } | |
62 | |
63 HttpPipeliningCompatibilityClient::Request::Request( | |
64 int request_id, | |
65 const std::string& base_url, | |
66 const RequestInfo& info, | |
67 HttpPipeliningCompatibilityClient* client, | |
68 net::URLRequestContext* url_request_context) | |
69 : request_id_(request_id), | |
70 request_(GURL(base_url + info.filename), this), | |
71 info_(info), | |
72 client_(client), | |
73 finished_(false) { | |
74 request_.set_context(url_request_context); | |
75 // TODO(simonjam): Force pipelining. | |
76 request_.set_load_flags(net::LOAD_BYPASS_CACHE | | |
77 net::LOAD_DISABLE_CACHE | | |
78 net::LOAD_DO_NOT_SAVE_COOKIES | | |
79 net::LOAD_DO_NOT_SEND_COOKIES | | |
80 net::LOAD_DO_NOT_PROMPT_FOR_LOGIN | | |
81 net::LOAD_DO_NOT_SEND_AUTH_DATA); | |
82 request_.Start(); | |
83 } | |
84 | |
85 HttpPipeliningCompatibilityClient::Request::~Request() { | |
86 } | |
87 | |
88 void HttpPipeliningCompatibilityClient::Request::OnReceivedRedirect( | |
89 net::URLRequest* request, | |
90 const GURL& new_url, | |
91 bool* defer_redirect) { | |
92 *defer_redirect = true; | |
93 request->Cancel(); | |
94 Finished(REDIRECTED); | |
95 } | |
96 | |
97 void HttpPipeliningCompatibilityClient::Request::OnSSLCertificateError( | |
98 net::URLRequest* request, | |
99 const net::SSLInfo& ssl_info, | |
100 bool fatal) { | |
101 Finished(CERT_ERROR); | |
102 } | |
103 | |
104 void HttpPipeliningCompatibilityClient::Request::OnResponseStarted( | |
105 net::URLRequest* request) { | |
106 if (finished_) { | |
107 return; | |
108 } | |
109 int response_code = request->GetResponseCode(); | |
110 if (response_code > 0) { | |
111 client_->ReportResponseCode(request_id_, response_code); | |
112 } | |
113 if (response_code == 200) { | |
114 read_buffer_ = new net::IOBuffer(info_.expected_response.length()); | |
115 DoRead(); | |
116 } else { | |
117 Finished(BAD_RESPONSE_CODE); | |
118 } | |
119 } | |
120 | |
121 void HttpPipeliningCompatibilityClient::Request::OnReadCompleted( | |
122 net::URLRequest* request, | |
123 int bytes_read) { | |
124 if (bytes_read == 0) { | |
125 DoReadFinished(); | |
126 } else if (bytes_read < 0) { | |
127 Finished(NETWORK_ERROR); | |
128 } else { | |
129 response_.append(read_buffer_->data(), bytes_read); | |
130 if (response_.length() <= info_.expected_response.length()) { | |
131 DoRead(); | |
132 } else if (response_.find(info_.expected_response) == 0) { | |
133 Finished(TOO_LARGE); | |
134 } else { | |
135 Finished(CONTENT_MISMATCH); | |
136 } | |
137 } | |
138 } | |
139 | |
140 void HttpPipeliningCompatibilityClient::Request::DoRead() { | |
141 int bytes_read = 0; | |
142 if (request_.Read(read_buffer_.get(), info_.expected_response.length(), | |
143 &bytes_read)) { | |
144 OnReadCompleted(&request_, bytes_read); | |
145 } | |
146 } | |
147 | |
148 void HttpPipeliningCompatibilityClient::Request::DoReadFinished() { | |
149 if (response_.length() != info_.expected_response.length()) { | |
150 if (info_.expected_response.find(response_) == 0) { | |
151 Finished(TOO_SMALL); | |
mmenke
2012/02/07 15:17:39
Think it would be a good idea to document these, e
James Simonsen
2012/02/07 22:40:40
Done.
| |
152 } else { | |
153 Finished(CONTENT_MISMATCH); | |
154 } | |
155 } else if (response_ == info_.expected_response) { | |
156 Finished(SUCCESS); | |
157 } else { | |
158 Finished(CONTENT_MISMATCH); | |
159 } | |
160 } | |
161 | |
162 void HttpPipeliningCompatibilityClient::Request::Finished(Status result) { | |
163 if (finished_) { | |
164 return; | |
165 } | |
166 finished_ = true; | |
167 const net::URLRequestStatus& status = request_.status(); | |
168 if (status.status() == net::URLRequestStatus::FAILED) { | |
169 client_->ReportNetworkError(request_id_, status.error()); | |
170 client_->OnRequestFinished(request_id_, NETWORK_ERROR); | |
171 return; | |
172 } | |
173 client_->OnRequestFinished(request_id_, result); | |
174 } | |
175 | |
176 } // namespace chrome_browser_net | |
OLD | NEW |