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_fetch.
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 // this is the URL of the service where we get the obfuscated GAIA ID |
| 22 // (here misnamed channel ID) |
| 23 static const char kCWSChannelServiceURL[] = |
| 24 "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; |
| 25 |
| 26 // TODO: how should we handle an error while calling the API? We can |
| 27 // just leave the GAIA ID as a null string, and allow the client to |
| 28 // try again later. We should probably log an error. Is it useful |
| 29 // to return an error to the client, or should we just leave the GAIA |
| 30 // ID null, and let the client check it for validity when they try |
| 31 // to use it? |
| 32 static GoogleServiceAuthError |
| 33 CreateAuthError(URLRequestStatus status) { |
| 34 |
| 35 if (status.status() == URLRequestStatus::CANCELED) { |
| 36 return GoogleServiceAuthError( |
| 37 GoogleServiceAuthError::REQUEST_CANCELED); |
| 38 } else { |
| 39 // TODO(munjal): Improve error handling. Currently we return |
| 40 // connection error for even application level errors. We need |
| 41 // to either expand the GoogleServiceAuthError enum or create a |
| 42 // new one to report better errors. |
| 43 DLOG(WARNING) << "Server returned error: err " << status.error(); |
| 44 return |
| 45 GoogleServiceAuthError::FromConnectionError(status.error()); |
| 46 } |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 |
| 52 namespace extension { |
| 53 |
| 54 // parameter structure for creating a fetch object |
| 55 ObfuscatedGaiaIdFetch::Parameters::Parameters() {} |
| 56 |
| 57 ObfuscatedGaiaIdFetch::Parameters::Parameters( |
| 58 const std::string& rt, |
| 59 const std::string& eid, |
| 60 const std::vector<std::string>& scopes_arg) |
| 61 : login_refresh_token(rt), |
| 62 extension_id(eid), |
| 63 scopes(scopes_arg) { |
| 64 } |
| 65 |
| 66 ObfuscatedGaiaIdFetch::Parameters::~Parameters() {} |
| 67 |
| 68 |
| 69 // constructor for the fetch object |
| 70 ObfuscatedGaiaIdFetch::ObfuscatedGaiaIdFetch( |
| 71 URLRequestContextGetter* context, |
| 72 Delegate* delegate, |
| 73 const Parameters& parameters) |
| 74 : OAuth2ApiCallFlow( |
| 75 context, parameters.login_refresh_token, |
| 76 "", std::vector<std::string>()), |
| 77 context_(context), |
| 78 delegate_(delegate), |
| 79 parameters_(parameters), |
| 80 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 81 } |
| 82 |
| 83 ObfuscatedGaiaIdFetch::~ObfuscatedGaiaIdFetch() { } |
| 84 |
| 85 // on success, notify the delegate set in the ctor, if any |
| 86 void ObfuscatedGaiaIdFetch::ReportSuccess( |
| 87 const std::string& access_token) { |
| 88 |
| 89 if (delegate_) |
| 90 delegate_->OnObfuscatedGaiaIdFetchSuccess(access_token); |
| 91 } |
| 92 |
| 93 // on failure, notify the delegate set in the ctor, if any |
| 94 void ObfuscatedGaiaIdFetch::ReportFailure( |
| 95 const GoogleServiceAuthError& error) { |
| 96 if (delegate_) |
| 97 delegate_->OnObfuscatedGaiaIdFetchFailure(error); |
| 98 } |
| 99 |
| 100 // this is the URL we will try to get an access token from |
| 101 // On the first call, we want to get the token issuing URL, |
| 102 // On the second call, we want to get the CWS ChannelID |
| 103 GURL ObfuscatedGaiaIdFetch::CreateApiCallUrl() { |
| 104 return GURL(kCWSChannelServiceURL); |
| 105 } |
| 106 |
| 107 // Nothing to do here, we don't need a body for this request, the URL |
| 108 // encodes all the proper arguments |
| 109 std::string ObfuscatedGaiaIdFetch::CreateApiCallBody() { |
| 110 return ""; |
| 111 } |
| 112 |
| 113 // This is a callback from the oauth call flow in case of success |
| 114 // on the first call, we want to process the token issue-ing response |
| 115 // on subsequent calls, we want to process the ChannelID |
| 116 void ObfuscatedGaiaIdFetch::ProcessApiCallSuccess( |
| 117 const net::URLFetcher* source) { |
| 118 // TODO(munjal): Change error code paths in this method to report an |
| 119 // internal error. |
| 120 std::string response_body; |
| 121 source->GetResponseAsString(&response_body); |
| 122 |
| 123 std::string channel_id; |
| 124 if (ParseObfuscatedGaiaIdFetchResponse(response_body, &channel_id)) |
| 125 ReportSuccess(channel_id); |
| 126 else |
| 127 ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
| 128 } |
| 129 |
| 130 // this is a callback from the oauth call flow in case of failure |
| 131 void ObfuscatedGaiaIdFetch::ProcessApiCallFailure( |
| 132 const net::URLFetcher* source) { |
| 133 ReportFailure(CreateAuthError(source->GetStatus())); |
| 134 } |
| 135 |
| 136 // static |
| 137 bool ObfuscatedGaiaIdFetch::ParseObfuscatedGaiaIdFetchResponse( |
| 138 const std::string& data, std::string* result) { |
| 139 |
| 140 scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
| 141 if (!value.get() |
| 142 || value->GetType() != base::Value::TYPE_DICTIONARY) |
| 143 return false; |
| 144 |
| 145 DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
| 146 return dict->GetString("id", result); |
| 147 } |
| 148 |
| 149 } |
OLD | NEW |