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 12 matching lines...) Expand all Loading... |
23 | 23 |
24 // Template for optional OAuth2 authorization HTTP header. | 24 // Template for optional OAuth2 authorization HTTP header. |
25 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; | 25 const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s"; |
26 // Template for GData API version HTTP header. | 26 // Template for GData API version HTTP header. |
27 const char kGDataVersionHeader[] = "GData-Version: 3.0"; | 27 const char kGDataVersionHeader[] = "GData-Version: 3.0"; |
28 | 28 |
29 // Maximum number of attempts for re-authentication per operation. | 29 // Maximum number of attempts for re-authentication per operation. |
30 const int kMaxReAuthenticateAttemptsPerOperation = 1; | 30 const int kMaxReAuthenticateAttemptsPerOperation = 1; |
31 | 31 |
32 // Parse JSON string to base::Value object. | 32 // Parse JSON string to base::Value object. |
33 scoped_ptr<base::Value> ParseJsonOnBlockingPool(const std::string& data) { | 33 scoped_ptr<base::Value> ParseJsonOnBlockingPool(const std::string& json) { |
34 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); |
35 | 35 |
36 int error_code = -1; | 36 int error_code = -1; |
37 std::string error_message; | 37 std::string error_message; |
38 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 38 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
39 data, base::JSON_PARSE_RFC, &error_code, &error_message)); | 39 json, base::JSON_PARSE_RFC, &error_code, &error_message)); |
40 | 40 |
41 if (!value.get()) { | 41 if (!value.get()) { |
42 LOG(ERROR) << "Error while parsing entry response: " << error_message | 42 LOG(ERROR) << "Error while parsing entry response: " << error_message |
43 << ", code: " << error_code << ", data:\n" << data; | 43 << ", code: " << error_code << ", json:\n" << json; |
44 } | 44 } |
45 return value.Pass(); | 45 return value.Pass(); |
46 } | 46 } |
47 | 47 |
48 // Returns response headers as a string. Returns a warning message if | 48 // Returns response headers as a string. Returns a warning message if |
49 // |url_fetcher| does not contain a valid response. Used only for debugging. | 49 // |url_fetcher| does not contain a valid response. Used only for debugging. |
50 std::string GetResponseHeadersAsString( | 50 std::string GetResponseHeadersAsString( |
51 const URLFetcher* url_fetcher) { | 51 const URLFetcher* url_fetcher) { |
52 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores | 52 // net::HttpResponseHeaders::raw_headers(), as the name implies, stores |
53 // all headers in their raw format, i.e each header is null-terminated. | 53 // all headers in their raw format, i.e each header is null-terminated. |
54 // So logging raw_headers() only shows the first header, which is probably | 54 // So logging raw_headers() only shows the first header, which is probably |
55 // the status line. GetNormalizedHeaders, on the other hand, will show all | 55 // the status line. GetNormalizedHeaders, on the other hand, will show all |
56 // the headers, one per line, which is probably what we want. | 56 // the headers, one per line, which is probably what we want. |
57 std::string headers; | 57 std::string headers; |
58 // Check that response code indicates response headers are valid (i.e. not | 58 // Check that response code indicates response headers are valid (i.e. not |
59 // malformed) before we retrieve the headers. | 59 // malformed) before we retrieve the headers. |
60 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { | 60 if (url_fetcher->GetResponseCode() == URLFetcher::RESPONSE_CODE_INVALID) { |
61 headers.assign("Response headers are malformed!!"); | 61 headers.assign("Response headers are malformed!!"); |
62 } else { | 62 } else { |
63 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); | 63 url_fetcher->GetResponseHeaders()->GetNormalizedHeaders(&headers); |
64 } | 64 } |
65 return headers; | 65 return headers; |
66 } | 66 } |
67 | 67 |
68 } // namespace | 68 } // namespace |
69 | 69 |
70 namespace google_apis { | 70 namespace google_apis { |
71 | 71 |
| 72 void ParseJson(const std::string& json, const ParseJsonCallback& callback) { |
| 73 base::PostTaskAndReplyWithResult( |
| 74 BrowserThread::GetBlockingPool(), |
| 75 FROM_HERE, |
| 76 base::Bind(&ParseJsonOnBlockingPool, json), |
| 77 callback); |
| 78 } |
| 79 |
72 //============================ UrlFetchOperationBase =========================== | 80 //============================ UrlFetchOperationBase =========================== |
73 | 81 |
74 UrlFetchOperationBase::UrlFetchOperationBase( | 82 UrlFetchOperationBase::UrlFetchOperationBase( |
75 OperationRegistry* registry, | 83 OperationRegistry* registry, |
76 net::URLRequestContextGetter* url_request_context_getter) | 84 net::URLRequestContextGetter* url_request_context_getter) |
77 : OperationRegistry::Operation(registry), | 85 : OperationRegistry::Operation(registry), |
78 url_request_context_getter_(url_request_context_getter), | 86 url_request_context_getter_(url_request_context_getter), |
79 re_authenticate_count_(0), | 87 re_authenticate_count_(0), |
80 started_(false), | 88 started_(false), |
81 save_temp_file_(false), | 89 save_temp_file_(false), |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 290 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
283 DCHECK(!callback_.is_null()); | 291 DCHECK(!callback_.is_null()); |
284 } | 292 } |
285 | 293 |
286 GetDataOperation::~GetDataOperation() {} | 294 GetDataOperation::~GetDataOperation() {} |
287 | 295 |
288 void GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code, | 296 void GetDataOperation::ParseResponse(GDataErrorCode fetch_error_code, |
289 const std::string& data) { | 297 const std::string& data) { |
290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 298 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
291 | 299 |
292 base::PostTaskAndReplyWithResult( | 300 ParseJson(data, |
293 BrowserThread::GetBlockingPool(), | 301 base::Bind(&GetDataOperation::OnDataParsed, |
294 FROM_HERE, | 302 weak_ptr_factory_.GetWeakPtr(), |
295 base::Bind(&ParseJsonOnBlockingPool, data), | 303 fetch_error_code)); |
296 base::Bind(&GetDataOperation::OnDataParsed, | |
297 weak_ptr_factory_.GetWeakPtr(), | |
298 fetch_error_code)); | |
299 } | 304 } |
300 | 305 |
301 void GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { | 306 void GetDataOperation::ProcessURLFetchResults(const URLFetcher* source) { |
302 std::string data; | 307 std::string data; |
303 source->GetResponseAsString(&data); | 308 source->GetResponseAsString(&data); |
304 scoped_ptr<base::Value> root_value; | 309 scoped_ptr<base::Value> root_value; |
305 GDataErrorCode fetch_error_code = GetErrorCode(source); | 310 GDataErrorCode fetch_error_code = GetErrorCode(source); |
306 | 311 |
307 switch (fetch_error_code) { | 312 switch (fetch_error_code) { |
308 case HTTP_SUCCESS: | 313 case HTTP_SUCCESS: |
(...skipping 30 matching lines...) Expand all Loading... |
339 OnProcessURLFetchResultsComplete(success); | 344 OnProcessURLFetchResultsComplete(success); |
340 } | 345 } |
341 | 346 |
342 void GetDataOperation::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, | 347 void GetDataOperation::RunCallbackOnSuccess(GDataErrorCode fetch_error_code, |
343 scoped_ptr<base::Value> value) { | 348 scoped_ptr<base::Value> value) { |
344 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
345 callback_.Run(fetch_error_code, value.Pass()); | 350 callback_.Run(fetch_error_code, value.Pass()); |
346 } | 351 } |
347 | 352 |
348 } // namespace google_apis | 353 } // namespace google_apis |
OLD | NEW |