Chromium Code Reviews| 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> | |
|
dcheng
2012/08/20 21:04:13
This is already included in the header, so it's OK
Pete Williamson
2012/08/20 22:26:45
Done.
| |
| 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 // URL of the service to get obfuscated GAIA ID (here misnamed channel ID). | |
| 23 static const char kCWSChannelServiceURL[] = | |
| 24 "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; | |
| 25 | |
| 26 static GoogleServiceAuthError CreateAuthError(URLRequestStatus status) { | |
| 27 if (status.status() == URLRequestStatus::CANCELED) { | |
| 28 return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED); | |
| 29 } else { | |
| 30 // TODO(munjal): Improve error handling. Currently we return connection | |
| 31 // error for even application level errors. We need to either expand the | |
| 32 // GoogleServiceAuthError enum or create a new one to report better | |
| 33 // errors. | |
| 34 DLOG(WARNING) << "Server returned error: errno " << status.error(); | |
| 35 return GoogleServiceAuthError::FromConnectionError(status.error()); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 // Returns a vector of scopes needed to call the API to get obfuscated gaia id. | |
| 40 std::vector<std::string> GetScopes() { | |
| 41 std::vector<std::string> scopes; | |
| 42 scopes.push_back( | |
| 43 "https://www.googleapis.com/auth/chromewebstore.notification"); | |
| 44 return scopes; | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 | |
| 50 namespace extensions { | |
| 51 | |
| 52 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher( | |
| 53 URLRequestContextGetter* context, | |
| 54 Delegate* delegate, | |
| 55 const std::string& refresh_token) | |
| 56 : OAuth2ApiCallFlow(context, refresh_token, std::string(), GetScopes()), | |
| 57 delegate_(delegate) { | |
| 58 } | |
| 59 | |
| 60 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } | |
| 61 | |
| 62 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) { | |
| 63 if (delegate_ != NULL) | |
|
dcheng
2012/08/20 21:04:13
Do we need to allow NULL delegates? It doesn't loo
Pete Williamson
2012/08/20 22:26:45
Done.
| |
| 64 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id); | |
| 65 } | |
| 66 | |
| 67 void ObfuscatedGaiaIdFetcher::ReportFailure( | |
| 68 const GoogleServiceAuthError& error) { | |
| 69 if (delegate_ != NULL) | |
| 70 delegate_->OnObfuscatedGaiaIdFetchFailure(error); | |
| 71 } | |
| 72 | |
| 73 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { | |
| 74 return GURL(kCWSChannelServiceURL); | |
| 75 } | |
| 76 | |
| 77 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { | |
| 78 // Nothing to do here, we don't need a body for this request, the URL | |
| 79 // encodes all the proper arguments. | |
| 80 return std::string(); | |
| 81 } | |
| 82 | |
| 83 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( | |
| 84 const net::URLFetcher* source) { | |
| 85 // TODO(munjal): Change error code paths in this method to report an | |
| 86 // internal error. | |
| 87 std::string response_body; | |
| 88 CHECK(source->GetResponseAsString(&response_body)); | |
| 89 | |
| 90 std::string channel_id; | |
| 91 if (ParseResponse(response_body, &channel_id)) | |
| 92 ReportSuccess(channel_id); | |
| 93 else | |
| 94 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | |
|
dcheng
2012/08/20 21:04:13
What does 101 mean? Is this code defined somewhere
Pete Williamson
2012/08/20 22:26:45
This came from code that I copied from oauth2_mint
| |
| 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 token 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 user 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 |