OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/google_apis/base_operations.h" | 5 #include "chrome/browser/google_apis/base_operations.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 39 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
40 data, base::JSON_PARSE_RFC, &error_code, &error_message)); | 40 data, base::JSON_PARSE_RFC, &error_code, &error_message)); |
41 | 41 |
42 if (!value.get()) { | 42 if (!value.get()) { |
43 LOG(ERROR) << "Error while parsing entry response: " << error_message | 43 LOG(ERROR) << "Error while parsing entry response: " << error_message |
44 << ", code: " << error_code << ", data:\n" << data; | 44 << ", code: " << error_code << ", data:\n" << data; |
45 } | 45 } |
46 return value.Pass(); | 46 return value.Pass(); |
47 } | 47 } |
48 | 48 |
| 49 // Returns response headers as a string. Returns a warning message if |
| 50 // |url_fetcher| does not contain a valid response. Used only for debugging. |
| 51 std::string GetResponseHeadersAsString( |
| 52 const URLFetcher* url_fetcher) { |
| 53 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores |
| 54 // all headers in their raw format, i.e each header is null-terminated. |
| 55 // So logging raw_headers() only shows the first header, which is probably |
| 56 // the status line. GetNormalizedHeaders, on the other hand, will show all |
| 57 // the headers, one per line, which is probably what we want. |
| 58 std::string headers; |
| 59 // Check that response code indicates response headers are valid (i.e. not |
| 60 // malformed) before we retrieve the headers. |
| 61 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { |
| 62 headers.assign("Response headers are malformed!!"); |
| 63 } else { |
| 64 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); |
| 65 } |
| 66 return headers; |
| 67 } |
| 68 |
49 } // namespace | 69 } // namespace |
50 | 70 |
51 namespace google_apis { | 71 namespace google_apis { |
52 | 72 |
53 //============================ UrlFetchOperationBase =========================== | 73 //============================ UrlFetchOperationBase =========================== |
54 | 74 |
55 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry) | 75 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry) |
56 : OperationRegistry::Operation(registry), | 76 : OperationRegistry::Operation(registry), |
| 77 save_temp_file_(false), |
57 re_authenticate_count_(0), | 78 re_authenticate_count_(0), |
58 save_temp_file_(false), | |
59 started_(false), | 79 started_(false), |
60 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 80 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
62 } | 82 } |
63 | 83 |
64 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry, | 84 UrlFetchOperationBase::UrlFetchOperationBase(OperationRegistry* registry, |
65 OperationType type, | 85 OperationType type, |
66 const FilePath& path) | 86 const FilePath& path) |
67 : OperationRegistry::Operation(registry, type, path), | 87 : OperationRegistry::Operation(registry, type, path), |
| 88 save_temp_file_(false), |
68 re_authenticate_count_(0), | 89 re_authenticate_count_(0), |
69 save_temp_file_(false), | |
70 started_(false), | 90 started_(false), |
71 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 91 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
73 } | 93 } |
74 | 94 |
75 UrlFetchOperationBase::~UrlFetchOperationBase() {} | 95 UrlFetchOperationBase::~UrlFetchOperationBase() {} |
76 | 96 |
77 void UrlFetchOperationBase::Start(const std::string& auth_token, | 97 void UrlFetchOperationBase::Start(const std::string& auth_token, |
78 const std::string& custom_user_agent) { | 98 const std::string& custom_user_agent) { |
79 DCHECK(!auth_token.empty()); | 99 DCHECK(!auth_token.empty()); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, | 164 bool UrlFetchOperationBase::GetContentData(std::string* upload_content_type, |
145 std::string* upload_content) { | 165 std::string* upload_content) { |
146 return false; | 166 return false; |
147 } | 167 } |
148 | 168 |
149 void UrlFetchOperationBase::DoCancel() { | 169 void UrlFetchOperationBase::DoCancel() { |
150 url_fetcher_.reset(NULL); | 170 url_fetcher_.reset(NULL); |
151 RunCallbackOnPrematureFailure(GDATA_CANCELLED); | 171 RunCallbackOnPrematureFailure(GDATA_CANCELLED); |
152 } | 172 } |
153 | 173 |
154 GDataErrorCode UrlFetchOperationBase::GetErrorCode( | 174 // static |
155 const URLFetcher* source) const { | 175 GDataErrorCode UrlFetchOperationBase::GetErrorCode(const URLFetcher* source) { |
156 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); | 176 GDataErrorCode code = static_cast<GDataErrorCode>(source->GetResponseCode()); |
157 if (code == HTTP_SUCCESS && !source->GetStatus().is_success()) { | 177 if (code == HTTP_SUCCESS && !source->GetStatus().is_success()) { |
158 // If the HTTP response code is SUCCESS yet the URL request failed, it is | 178 // If the HTTP response code is SUCCESS yet the URL request failed, it is |
159 // likely that the failure is due to loss of connection. | 179 // likely that the failure is due to loss of connection. |
160 code = GDATA_NO_CONNECTION; | 180 code = GDATA_NO_CONNECTION; |
161 } | 181 } |
162 return code; | 182 return code; |
163 } | 183 } |
164 | 184 |
165 void UrlFetchOperationBase::OnProcessURLFetchResultsComplete(bool result) { | 185 void UrlFetchOperationBase::OnProcessURLFetchResultsComplete(bool result) { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 if (!started_) | 227 if (!started_) |
208 NotifyStart(); | 228 NotifyStart(); |
209 | 229 |
210 // Note: NotifyFinish() must be invoked at the end, after all other callbacks | 230 // Note: NotifyFinish() must be invoked at the end, after all other callbacks |
211 // and notifications. Once NotifyFinish() is called, the current instance of | 231 // and notifications. Once NotifyFinish() is called, the current instance of |
212 // gdata operation will be deleted from the OperationRegistry and become | 232 // gdata operation will be deleted from the OperationRegistry and become |
213 // invalid. | 233 // invalid. |
214 NotifyFinish(OPERATION_FAILED); | 234 NotifyFinish(OPERATION_FAILED); |
215 } | 235 } |
216 | 236 |
217 std::string UrlFetchOperationBase::GetResponseHeadersAsString( | |
218 const URLFetcher* url_fetcher) { | |
219 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores | |
220 // all headers in their raw format, i.e each header is null-terminated. | |
221 // So logging raw_headers() only shows the first header, which is probably | |
222 // the status line. GetNormalizedHeaders, on the other hand, will show all | |
223 // the headers, one per line, which is probably what we want. | |
224 std::string headers; | |
225 // Check that response code indicates response headers are valid (i.e. not | |
226 // malformed) before we retrieve the headers. | |
227 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { | |
228 headers.assign("Response headers are malformed!!"); | |
229 } else { | |
230 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); | |
231 } | |
232 return headers; | |
233 } | |
234 | |
235 base::WeakPtr<AuthenticatedOperationInterface> | 237 base::WeakPtr<AuthenticatedOperationInterface> |
236 UrlFetchOperationBase::GetWeakPtr() { | 238 UrlFetchOperationBase::GetWeakPtr() { |
237 return weak_ptr_factory_.GetWeakPtr(); | 239 return weak_ptr_factory_.GetWeakPtr(); |
238 } | 240 } |
239 | 241 |
240 | 242 |
241 //============================ EntryActionOperation ============================ | 243 //============================ EntryActionOperation ============================ |
242 | 244 |
243 EntryActionOperation::EntryActionOperation(OperationRegistry* registry, | 245 EntryActionOperation::EntryActionOperation(OperationRegistry* registry, |
244 const EntryActionCallback& callback) | 246 const EntryActionCallback& callback) |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 334 } |
333 | 335 |
334 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | 336 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, |
335 scoped_ptr<base::Value> value) { | 337 scoped_ptr<base::Value> value) { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
337 if (!callback_.is_null()) | 339 if (!callback_.is_null()) |
338 callback_.Run(fetch_error_code, value.Pass()); | 340 callback_.Run(fetch_error_code, value.Pass()); |
339 } | 341 } |
340 | 342 |
341 } // namespace google_apis | 343 } // namespace google_apis |
OLD | NEW |