| 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 <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/values.h" |
| 12 #include "chrome/common/net/gaia/google_service_auth_error.h" |
| 13 #include "net/url_request/url_fetcher.h" |
| 14 #include "net/url_request/url_request_status.h" |
| 15 |
| 16 using net::URLFetcher; |
| 17 using net::URLRequestContextGetter; |
| 18 using net::URLRequestStatus; |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kID[] = "id"; |
| 23 |
| 24 } |
| 25 |
| 26 namespace { |
| 27 |
| 28 // URL of the service to get obfuscated GAIA ID (here misnamed channel ID). |
| 29 static const char kCWSChannelServiceURL[] = |
| 30 "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; |
| 31 |
| 32 // TODO: How should we handle an error while calling the API? We can just |
| 33 // leave the GAIA ID as a null string, and allow the client to try again later. |
| 34 // We should probably log an error. Is it useful to return an error to the |
| 35 // client, or should we just leave the GAIA ID null, and let the client check |
| 36 // it for validity when they try to use it? |
| 37 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { |
| 38 if (status.status() == URLRequestStatus::CANCELED) { |
| 39 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); |
| 40 } else { |
| 41 // TODO(munjal): Improve error handling. Currently we return connection |
| 42 // error for even application level errors. We need to either expand the |
| 43 // GoogleServiceAuthError enum or create a new one to report better |
| 44 // errors. |
| 45 DLOG(WARNING) << "Server returned error: errno " << status.error(); |
| 46 return GoogleServiceAuthError::FromConnectionError(status.error()); |
| 47 } |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 |
| 53 namespace extensions { |
| 54 |
| 55 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher( |
| 56 URLRequestContextGetter* context, |
| 57 Delegate* delegate, |
| 58 const std::string& refreshToken, |
| 59 const std::vector<std::string>& scopes) |
| 60 : OAuth2ApiCallFlow(context, refreshToken, "", scopes), |
| 61 delegate_(delegate) { |
| 62 } |
| 63 |
| 64 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } |
| 65 |
| 66 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& access_token) { |
| 67 if (delegate_) |
| 68 delegate_->OnObfuscatedGaiaIdFetchSuccess(access_token); |
| 69 } |
| 70 |
| 71 void ObfuscatedGaiaIdFetcher::ReportFailure( |
| 72 const GoogleServiceAuthError& error) { |
| 73 if (delegate_) |
| 74 delegate_->OnObfuscatedGaiaIdFetchFailure(error); |
| 75 } |
| 76 |
| 77 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { |
| 78 // TODO remove before checkin |
| 79 // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::CreateApiCallUrl called ***"; |
| 80 return GURL(kCWSChannelServiceURL); |
| 81 } |
| 82 |
| 83 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { |
| 84 // TODO: remove all the debug log statements before checkin |
| 85 // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::CreateApiCallBody called ***"; |
| 86 // Nothing to do here, we don't need a body for this request, the URL |
| 87 // encodes all the proper arguments. |
| 88 return std::string(); |
| 89 } |
| 90 |
| 91 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( |
| 92 const net::URLFetcher* source) { |
| 93 // TODO(munjal): Change error code paths in this method to report an |
| 94 // internal error. |
| 95 // TODO: remove before checkin |
| 96 // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess called ***
"; |
| 97 std::string response_body; |
| 98 source->GetResponseAsString(&response_body); |
| 99 |
| 100 std::string channel_id; |
| 101 if (ParseResponse(response_body, &channel_id)) |
| 102 ReportSuccess(channel_id); |
| 103 else |
| 104 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 105 } |
| 106 |
| 107 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure( |
| 108 const net::URLFetcher* source) { |
| 109 // TODO: remove before checkin |
| 110 LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ProcessApiCallFailure called ***"; |
| 111 ReportFailure(CreateAuthError(source->GetStatus())); |
| 112 } |
| 113 |
| 114 void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken( |
| 115 const std::string& obfuscated_id) { |
| 116 // TODO: remove before checkin |
| 117 // LOG(INFO) << "*** ObfuscatedGaiaIDFetcher::ProcessNewAccessToken - " |
| 118 // << "got new access token ***"; |
| 119 // "We generate a new access token every time instead of storing the access |
| 120 // token since access token expire every hour and we expect to get |
| 121 // obfuscated gaia id very infrequently." |
| 122 } |
| 123 |
| 124 void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure( |
| 125 const GoogleServiceAuthError& error) { |
| 126 // TODO: remove before checkin |
| 127 LOG(INFO) << "*** ObfuscatedGaiaIDFetcher::ProcessMintAccessTokenFailure - " |
| 128 << "error ***"; |
| 129 // We failed to generate the token needed to call the API to get |
| 130 // the obfuscated user ID, so report failure to the caller |
| 131 ReportFailure(error); |
| 132 } |
| 133 |
| 134 // static |
| 135 bool ObfuscatedGaiaIdFetcher::ParseResponse( |
| 136 const std::string& data, std::string* result) { |
| 137 // TODO: remove before checkin |
| 138 // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ParseResponse called ***"; |
| 139 |
| 140 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
| 141 if (!value.get() || value->GetType() != base::Value::TYPE_DICTIONARY) |
| 142 return false; |
| 143 |
| 144 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
| 145 return dict->GetString(kID, result); |
| 146 } |
| 147 |
| 148 } // namespace extensions |
| OLD | NEW |