| 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/extensions/api/push_messaging/obfuscated_gaia_id_fetche
r.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/json/json_reader.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 12 #include "net/url_request/url_fetcher.h" |
| 13 #include "net/url_request/url_request_status.h" |
| 14 |
| 15 using net::URLFetcher; |
| 16 using net::URLRequestContextGetter; |
| 17 using net::URLRequestStatus; |
| 18 |
| 19 namespace { |
| 20 |
| 21 // URL of the service to get obfuscated Gaia ID (here misnamed channel ID). |
| 22 static const char kCWSChannelServiceURL[] = |
| 23 "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; |
| 24 |
| 25 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { |
| 26 if (status.status() == URLRequestStatus::CANCELED) { |
| 27 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 28 } else { |
| 29 // TODO(munjal): Improve error handling. Currently we return connection |
| 30 // error for even application level errors. We need to either expand the |
| 31 // GoogleServiceAuthError enum or create a new one to report better |
| 32 // errors. |
| 33 DLOG(WARNING) << "Server returned error: errno " << status.error(); |
| 34 return GoogleServiceAuthError::FromConnectionError(status.error()); |
| 35 } |
| 36 } |
| 37 |
| 38 // Returns a vector of scopes needed to call the API to get obfuscated Gaia ID. |
| 39 std::vector<std::string> GetScopes() { |
| 40 std::vector<std::string> scopes; |
| 41 scopes.push_back( |
| 42 "https://www.googleapis.com/auth/chromewebstore.notification"); |
| 43 return scopes; |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 namespace extensions { |
| 49 |
| 50 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher( |
| 51 URLRequestContextGetter* context, |
| 52 Delegate* delegate, |
| 53 const std::string& refresh_token) |
| 54 : OAuth2ApiCallFlow(context, refresh_token, std::string(), GetScopes()), |
| 55 delegate_(delegate) { |
| 56 DCHECK(delegate); |
| 57 } |
| 58 |
| 59 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } |
| 60 |
| 61 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) { |
| 62 if (delegate_) |
| 63 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id); |
| 64 } |
| 65 |
| 66 void ObfuscatedGaiaIdFetcher::ReportFailure( |
| 67 const GoogleServiceAuthError& error) { |
| 68 if (delegate_) |
| 69 delegate_->OnObfuscatedGaiaIdFetchFailure(error); |
| 70 } |
| 71 |
| 72 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { |
| 73 return GURL(kCWSChannelServiceURL); |
| 74 } |
| 75 |
| 76 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { |
| 77 // Nothing to do here, we don't need a body for this request, the URL |
| 78 // encodes all the proper arguments. |
| 79 return std::string(); |
| 80 } |
| 81 |
| 82 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( |
| 83 const net::URLFetcher* source) { |
| 84 // TODO(munjal): Change error code paths in this method to report an |
| 85 // internal error. |
| 86 std::string response_body; |
| 87 CHECK(source->GetResponseAsString(&response_body)); |
| 88 |
| 89 std::string obfuscated_id; |
| 90 if (ParseResponse(response_body, &obfuscated_id)) |
| 91 ReportSuccess(obfuscated_id); |
| 92 else |
| 93 // we picked 101 arbitrarily to help us correlate the error with this code. |
| 94 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 95 } |
| 96 |
| 97 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure( |
| 98 const net::URLFetcher* source) { |
| 99 ReportFailure(CreateAuthError(source->GetStatus())); |
| 100 } |
| 101 |
| 102 void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken( |
| 103 const std::string& obfuscated_id) { |
| 104 // We generate a new access token every time instead of storing the access |
| 105 // token since access tokens expire every hour and we expect to get |
| 106 // obfuscated Gaia ID very infrequently. |
| 107 } |
| 108 |
| 109 void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure( |
| 110 const GoogleServiceAuthError& error) { |
| 111 // We failed to generate the token needed to call the API to get |
| 112 // the obfuscated Gaia ID, so report failure to the caller. |
| 113 ReportFailure(error); |
| 114 } |
| 115 |
| 116 // static |
| 117 bool ObfuscatedGaiaIdFetcher::ParseResponse( |
| 118 const std::string& data, std::string* result) { |
| 119 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
| 120 |
| 121 if (!value.get()) |
| 122 return false; |
| 123 |
| 124 DictionaryValue* dict = NULL; |
| 125 if (!value->GetAsDictionary(&dict)) |
| 126 return false; |
| 127 |
| 128 return dict->GetString("id", result); |
| 129 } |
| 130 |
| 131 } // namespace extensions |
| OLD | NEW |