Chromium Code Reviews| 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/operations_base.h" | 5 #include "chrome/browser/google_apis/operations_base.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
| 13 #include "chrome/common/chrome_version_info.h" | |
| 13 #include "chrome/common/net/url_util.h" | 14 #include "chrome/common/net/url_util.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "google_apis/gaia/gaia_urls.h" | 16 #include "google_apis/gaia/gaia_urls.h" |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | 17 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "google_apis/gaia/oauth2_access_token_fetcher.h" | 18 #include "google_apis/gaia/oauth2_access_token_fetcher.h" |
| 18 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
| 19 #include "net/http/http_util.h" | 20 #include "net/http/http_util.h" |
| 20 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
| 21 #include "net/url_request/url_request_status.h" | 22 #include "net/url_request/url_request_status.h" |
| 23 #include "webkit/user_agent/user_agent_util.h" | |
| 22 | 24 |
| 23 using content::BrowserThread; | 25 using content::BrowserThread; |
| 24 using net::URLFetcher; | 26 using net::URLFetcher; |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 // Used for success ratio histograms. 0 for failure, 1 for success, | 30 // Used for success ratio histograms. 0 for failure, 1 for success, |
| 29 // 2 for no connection (likely offline). | 31 // 2 for no connection (likely offline). |
| 30 const int kSuccessRatioHistogramFailure = 0; | 32 const int kSuccessRatioHistogramFailure = 0; |
| 31 const int kSuccessRatioHistogramSuccess = 1; | 33 const int kSuccessRatioHistogramSuccess = 1; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 55 if (!value->get()) { | 57 if (!value->get()) { |
| 56 LOG(ERROR) << "Error while parsing entry response: " | 58 LOG(ERROR) << "Error while parsing entry response: " |
| 57 << error_message | 59 << error_message |
| 58 << ", code: " | 60 << ", code: " |
| 59 << error_code | 61 << error_code |
| 60 << ", data:\n" | 62 << ", data:\n" |
| 61 << data; | 63 << data; |
| 62 } | 64 } |
| 63 } | 65 } |
| 64 | 66 |
| 67 // Returns a user agent string used for communicating with the Drive backend, | |
| 68 // both WAPI and Drive API. The user agent looks like: | |
| 69 // | |
| 70 // chromedrive-<VERSION> chrome-cc/none (<OS_CPU_INFO>) | |
| 71 // chromedrive-24.0.1274.0 chrome-cc/none (CrOS x86_64 0.4.0) | |
| 72 // | |
| 73 // TODO(satorux): Move this function to somewhere else: crbug.com/151605 | |
| 74 std::string GetDriveUserAgent() { | |
| 75 const char kDriveClientName[] = "chromedrive"; | |
| 76 | |
| 77 chrome::VersionInfo version_info; | |
| 78 const std::string version = (version_info.is_valid() ? | |
| 79 version_info.Version() : | |
| 80 std::string("unknown")); | |
| 81 | |
| 82 // This part is <client_name>/<version>. | |
| 83 const std::string library = "chrome-cc/none"; | |
|
Daniel Erat
2012/09/21 21:11:12
nit: use const char kLibraryName[] here?
satorux1
2012/09/21 22:41:22
Done.
| |
| 84 | |
| 85 const std::string os_cpu = webkit_glue::BuildOSCpuInfo(); | |
| 86 | |
| 87 return base::StringPrintf("%s-%s %s (%s)", | |
| 88 kDriveClientName, | |
| 89 version.c_str(), | |
| 90 library.c_str(), | |
| 91 os_cpu.c_str()); | |
| 92 } | |
| 93 | |
| 65 } // namespace | 94 } // namespace |
| 66 | 95 |
| 67 namespace gdata { | 96 namespace gdata { |
| 68 | 97 |
| 69 //================================ AuthOperation =============================== | 98 //================================ AuthOperation =============================== |
| 70 | 99 |
| 71 AuthOperation::AuthOperation(OperationRegistry* registry, | 100 AuthOperation::AuthOperation(OperationRegistry* registry, |
| 72 const AuthStatusCallback& callback, | 101 const AuthStatusCallback& callback, |
| 73 const std::vector<std::string>& scopes, | 102 const std::vector<std::string>& scopes, |
| 74 const std::string& refresh_token) | 103 const std::string& refresh_token) |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 204 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 176 } else if (!output_file_path_.empty()) { | 205 } else if (!output_file_path_.empty()) { |
| 177 url_fetcher_->SaveResponseToFileAtPath(output_file_path_, | 206 url_fetcher_->SaveResponseToFileAtPath(output_file_path_, |
| 178 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 207 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 179 } | 208 } |
| 180 | 209 |
| 181 // Add request headers. | 210 // Add request headers. |
| 182 // Note that SetExtraRequestHeaders clears the current headers and sets it | 211 // Note that SetExtraRequestHeaders clears the current headers and sets it |
| 183 // to the passed-in headers, so calling it for each header will result in | 212 // to the passed-in headers, so calling it for each header will result in |
| 184 // only the last header being set in request headers. | 213 // only the last header being set in request headers. |
| 214 // | |
| 215 // TODO(satorux): The custom user-agent should be set only for Drive | |
| 216 // operations. crbug.com/151605 | |
| 217 url_fetcher_->AddExtraRequestHeader("User-Agent: " + GetDriveUserAgent()); | |
| 185 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); | 218 url_fetcher_->AddExtraRequestHeader(kGDataVersionHeader); |
| 186 url_fetcher_->AddExtraRequestHeader( | 219 url_fetcher_->AddExtraRequestHeader( |
| 187 base::StringPrintf(kAuthorizationHeaderFormat, auth_token.data())); | 220 base::StringPrintf(kAuthorizationHeaderFormat, auth_token.data())); |
| 188 std::vector<std::string> headers = GetExtraRequestHeaders(); | 221 std::vector<std::string> headers = GetExtraRequestHeaders(); |
| 189 for (size_t i = 0; i < headers.size(); ++i) { | 222 for (size_t i = 0; i < headers.size(); ++i) { |
| 190 url_fetcher_->AddExtraRequestHeader(headers[i]); | 223 url_fetcher_->AddExtraRequestHeader(headers[i]); |
| 191 DVLOG(1) << "Extra header: " << headers[i]; | 224 DVLOG(1) << "Extra header: " << headers[i]; |
| 192 } | 225 } |
| 193 | 226 |
| 194 // Set upload data if available. | 227 // Set upload data if available. |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 } | 450 } |
| 418 | 451 |
| 419 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, | 452 void GetDataOperation::RunCallback(GDataErrorCode fetch_error_code, |
| 420 scoped_ptr<base::Value> value) { | 453 scoped_ptr<base::Value> value) { |
| 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 454 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 422 if (!callback_.is_null()) | 455 if (!callback_.is_null()) |
| 423 callback_.Run(fetch_error_code, value.Pass()); | 456 callback_.Run(fetch_error_code, value.Pass()); |
| 424 } | 457 } |
| 425 | 458 |
| 426 } // namespace gdata | 459 } // namespace gdata |
| OLD | NEW |