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> | |
| 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 | |
|
dcheng
2012/08/20 19:10:01
Generally we don't use double newlines to separate
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 39 | |
| 40 // We use this static function to encapsulate the scopes needed at ctor time. | |
|
Munjal (Google)
2012/08/20 20:01:25
Nit: may be just say "Returns a vector of scopes n
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 41 std::vector<std::string> GetScopes() { | |
| 42 std::vector<std::string> scopes; | |
| 43 scopes.push_back( | |
| 44 "https://www.googleapis.com/auth/chromewebstore.notification"); | |
|
Munjal (Google)
2012/08/20 20:01:25
NIt: style issue: indent 4 spaces
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 45 return scopes; | |
| 46 } | |
| 47 | |
| 48 } // namespace | |
| 49 | |
| 50 | |
| 51 namespace extensions { | |
| 52 | |
| 53 ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher( | |
| 54 URLRequestContextGetter* context, | |
| 55 Delegate* delegate, | |
| 56 const std::string& refresh_token) | |
| 57 : OAuth2ApiCallFlow(context, refresh_token, std::string(), GetScopes()), | |
| 58 delegate_(delegate) { | |
| 59 } | |
| 60 | |
| 61 ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { } | |
| 62 | |
| 63 void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) { | |
| 64 if (delegate_ != NULL) | |
| 65 delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id); | |
| 66 } | |
| 67 | |
| 68 void ObfuscatedGaiaIdFetcher::ReportFailure( | |
| 69 const GoogleServiceAuthError& error) { | |
| 70 if (delegate_ != NULL) | |
| 71 delegate_->OnObfuscatedGaiaIdFetchFailure(error); | |
| 72 } | |
| 73 | |
| 74 GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() { | |
| 75 return GURL(kCWSChannelServiceURL); | |
| 76 } | |
| 77 | |
| 78 std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() { | |
| 79 // Nothing to do here, we don't need a body for this request, the URL | |
| 80 // encodes all the proper arguments. | |
| 81 return std::string(); | |
| 82 } | |
| 83 | |
| 84 void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess( | |
| 85 const net::URLFetcher* source) { | |
| 86 // TODO(munjal): Change error code paths in this method to report an | |
|
Munjal (Google)
2012/08/20 20:01:25
Nit: I am ok with my name in the TODO. But just ma
Pete Williamson
2012/08/20 20:53:05
This was a copy/ paste artifact, but I think it is
| |
| 87 // internal error. | |
| 88 std::string response_body; | |
| 89 CHECK(source->GetResponseAsString(&response_body)); | |
| 90 | |
| 91 std::string channel_id; | |
| 92 if (ParseResponse(response_body, &channel_id)) | |
| 93 ReportSuccess(channel_id); | |
| 94 else | |
| 95 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); | |
| 96 } | |
| 97 | |
| 98 void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure( | |
| 99 const net::URLFetcher* source) { | |
| 100 ReportFailure(CreateAuthError(source->GetStatus())); | |
| 101 } | |
| 102 | |
| 103 void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken( | |
| 104 const std::string& obfuscated_id) { | |
| 105 // We generate a new access token every time instead of storing the access | |
| 106 // token since access token expire every hour and we expect to get | |
| 107 // obfuscated gaia id very infrequently. | |
| 108 } | |
| 109 | |
| 110 void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure( | |
| 111 const GoogleServiceAuthError& error) { | |
| 112 // We failed to generate the token needed to call the API to get | |
| 113 // the obfuscated user ID, so report failure to the caller. | |
| 114 ReportFailure(error); | |
| 115 } | |
| 116 | |
| 117 // static | |
| 118 bool ObfuscatedGaiaIdFetcher::ParseResponse( | |
| 119 const std::string& data, std::string* result) { | |
| 120 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); | |
| 121 | |
| 122 if (!value.get()) | |
| 123 return false; | |
| 124 | |
| 125 DictionaryValue* dict = NULL; | |
| 126 bool rv = value->GetAsDictionary(&dict); | |
| 127 if (!rv || NULL == dict) | |
|
dcheng
2012/08/20 19:10:01
It is unnecessary to check that dict is NULL. So y
Pete Williamson
2012/08/20 20:53:05
Done.
| |
| 128 return false; | |
| 129 | |
| 130 return dict->GetString("id", result); | |
| 131 } | |
| 132 | |
| 133 } // namespace extensions | |
| OLD | NEW |