Index: chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetch.cc |
diff --git a/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetch.cc b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetch.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f7993076a4ea5eb8c538160cbca325dd80ac094b |
--- /dev/null |
+++ b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetch.cc |
@@ -0,0 +1,149 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetch.h" |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/json/json_reader.h" |
+#include "base/values.h" |
+#include "chrome/common/net/gaia/google_service_auth_error.h" |
+#include "net/url_request/url_fetcher.h" |
+#include "net/url_request/url_request_status.h" |
+ |
+using net::URLFetcher; |
+using net::URLRequestContextGetter; |
+using net::URLRequestStatus; |
+ |
+namespace { |
+// this is the URL of the service where we get the obfuscated GAIA ID |
+// (here misnamed channel ID) |
+static const char kCWSChannelServiceURL[] = |
+ "https://www.googleapis.com/chromewebstore/v1.1/channels/id"; |
+ |
+// TODO: how should we handle an error while calling the API? We can |
+// just leave the GAIA ID as a null string, and allow the client to |
+// try again later. We should probably log an error. Is it useful |
+// to return an error to the client, or should we just leave the GAIA |
+// ID null, and let the client check it for validity when they try |
+// to use it? |
+static GoogleServiceAuthError |
+ CreateAuthError(URLRequestStatus status) { |
+ |
+ if (status.status() == URLRequestStatus::CANCELED) { |
+ return GoogleServiceAuthError( |
+ GoogleServiceAuthError::REQUEST_CANCELED); |
+ } else { |
+ // TODO(munjal): Improve error handling. Currently we return |
+ // connection error for even application level errors. We need |
+ // to either expand the GoogleServiceAuthError enum or create a |
+ // new one to report better errors. |
+ DLOG(WARNING) << "Server returned error: err " << status.error(); |
+ return |
+ GoogleServiceAuthError::FromConnectionError(status.error()); |
+ } |
+} |
+ |
+} // namespace |
+ |
+ |
+namespace extension { |
+ |
+// parameter structure for creating a fetch object |
+ObfuscatedGaiaIdFetch::Parameters::Parameters() {} |
+ |
+ObfuscatedGaiaIdFetch::Parameters::Parameters( |
+ const std::string& rt, |
+ const std::string& eid, |
+ const std::vector<std::string>& scopes_arg) |
+ : login_refresh_token(rt), |
+ extension_id(eid), |
+ scopes(scopes_arg) { |
+} |
+ |
+ObfuscatedGaiaIdFetch::Parameters::~Parameters() {} |
+ |
+ |
+// constructor for the fetch object |
+ObfuscatedGaiaIdFetch::ObfuscatedGaiaIdFetch( |
+ URLRequestContextGetter* context, |
+ Delegate* delegate, |
+ const Parameters& parameters) |
+ : OAuth2ApiCallFlow( |
+ context, parameters.login_refresh_token, |
+ "", std::vector<std::string>()), |
+ context_(context), |
+ delegate_(delegate), |
+ parameters_(parameters), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
+} |
+ |
+ObfuscatedGaiaIdFetch::~ObfuscatedGaiaIdFetch() { } |
+ |
+// on success, notify the delegate set in the ctor, if any |
+void ObfuscatedGaiaIdFetch::ReportSuccess( |
+ const std::string& access_token) { |
+ |
+ if (delegate_) |
+ delegate_->OnObfuscatedGaiaIdFetchSuccess(access_token); |
+} |
+ |
+// on failure, notify the delegate set in the ctor, if any |
+void ObfuscatedGaiaIdFetch::ReportFailure( |
+ const GoogleServiceAuthError& error) { |
+ if (delegate_) |
+ delegate_->OnObfuscatedGaiaIdFetchFailure(error); |
+} |
+ |
+// this is the URL we will try to get an access token from |
+// On the first call, we want to get the token issuing URL, |
+// On the second call, we want to get the CWS ChannelID |
+GURL ObfuscatedGaiaIdFetch::CreateApiCallUrl() { |
+ return GURL(kCWSChannelServiceURL); |
+} |
+ |
+// Nothing to do here, we don't need a body for this request, the URL |
+// encodes all the proper arguments |
+std::string ObfuscatedGaiaIdFetch::CreateApiCallBody() { |
+ return ""; |
+} |
+ |
+// This is a callback from the oauth call flow in case of success |
+// on the first call, we want to process the token issue-ing response |
+// on subsequent calls, we want to process the ChannelID |
+void ObfuscatedGaiaIdFetch::ProcessApiCallSuccess( |
+ const net::URLFetcher* source) { |
+ // TODO(munjal): Change error code paths in this method to report an |
+ // internal error. |
+ std::string response_body; |
+ source->GetResponseAsString(&response_body); |
+ |
+ std::string channel_id; |
+ if (ParseObfuscatedGaiaIdFetchResponse(response_body, &channel_id)) |
+ ReportSuccess(channel_id); |
+ else |
+ ReportFailure(GoogleServiceAuthError::FromConnectionError(101)); |
+} |
+ |
+// this is a callback from the oauth call flow in case of failure |
+void ObfuscatedGaiaIdFetch::ProcessApiCallFailure( |
+ const net::URLFetcher* source) { |
+ ReportFailure(CreateAuthError(source->GetStatus())); |
+} |
+ |
+// static |
+bool ObfuscatedGaiaIdFetch::ParseObfuscatedGaiaIdFetchResponse( |
+ const std::string& data, std::string* result) { |
+ |
+ scoped_ptr<base::Value> value(base::JSONReader::Read(data)); |
+ if (!value.get() |
+ || value->GetType() != base::Value::TYPE_DICTIONARY) |
+ return false; |
+ |
+ DictionaryValue* dict = static_cast<DictionaryValue*>(value.get()); |
+ return dict->GetString("id", result); |
+} |
+ |
+} |