| Index: chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.cc
|
| diff --git a/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.cc b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..443dd7927de1e2110340086869dfc1f8e5fc75f5
|
| --- /dev/null
|
| +++ b/chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.cc
|
| @@ -0,0 +1,148 @@
|
| +// 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_fetcher.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 {
|
| +
|
| +const char kID[] = "id";
|
| +
|
| +}
|
| +
|
| +namespace {
|
| +
|
| +// URL of the service to get 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: errno " << status.error();
|
| + return GoogleServiceAuthError::FromConnectionError(status.error());
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +
|
| +namespace extensions {
|
| +
|
| +ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher(
|
| + URLRequestContextGetter* context,
|
| + Delegate* delegate,
|
| + const std::string& refreshToken,
|
| + const std::vector<std::string>& scopes)
|
| + : OAuth2ApiCallFlow(context, refreshToken, "", scopes),
|
| + delegate_(delegate) {
|
| +}
|
| +
|
| +ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { }
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& access_token) {
|
| + if (delegate_)
|
| + delegate_->OnObfuscatedGaiaIdFetchSuccess(access_token);
|
| +}
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ReportFailure(
|
| + const GoogleServiceAuthError& error) {
|
| + if (delegate_)
|
| + delegate_->OnObfuscatedGaiaIdFetchFailure(error);
|
| +}
|
| +
|
| +GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() {
|
| + // TODO remove before checkin
|
| + // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::CreateApiCallUrl called ***";
|
| + return GURL(kCWSChannelServiceURL);
|
| +}
|
| +
|
| +std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() {
|
| + // TODO: remove all the debug log statements before checkin
|
| + // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::CreateApiCallBody called ***";
|
| + // Nothing to do here, we don't need a body for this request, the URL
|
| + // encodes all the proper arguments.
|
| + return std::string();
|
| +}
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess(
|
| + const net::URLFetcher* source) {
|
| + // TODO(munjal): Change error code paths in this method to report an
|
| + // internal error.
|
| + // TODO: remove before checkin
|
| + // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess called ***";
|
| + std::string response_body;
|
| + source->GetResponseAsString(&response_body);
|
| +
|
| + std::string channel_id;
|
| + if (ParseResponse(response_body, &channel_id))
|
| + ReportSuccess(channel_id);
|
| + else
|
| + ReportFailure(GoogleServiceAuthError::FromConnectionError(101));
|
| +}
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure(
|
| + const net::URLFetcher* source) {
|
| + // TODO: remove before checkin
|
| + LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ProcessApiCallFailure called ***";
|
| + ReportFailure(CreateAuthError(source->GetStatus()));
|
| +}
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken(
|
| + const std::string& obfuscated_id) {
|
| + // TODO: remove before checkin
|
| + // LOG(INFO) << "*** ObfuscatedGaiaIDFetcher::ProcessNewAccessToken - "
|
| + // << "got new access token ***";
|
| + // "We generate a new access token every time instead of storing the access
|
| + // token since access token expire every hour and we expect to get
|
| + // obfuscated gaia id very infrequently."
|
| +}
|
| +
|
| +void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure(
|
| + const GoogleServiceAuthError& error) {
|
| + // TODO: remove before checkin
|
| + LOG(INFO) << "*** ObfuscatedGaiaIDFetcher::ProcessMintAccessTokenFailure - "
|
| + << "error ***";
|
| + // We failed to generate the token needed to call the API to get
|
| + // the obfuscated user ID, so report failure to the caller
|
| + ReportFailure(error);
|
| +}
|
| +
|
| +// static
|
| +bool ObfuscatedGaiaIdFetcher::ParseResponse(
|
| + const std::string& data, std::string* result) {
|
| + // TODO: remove before checkin
|
| + // LOG(INFO) << "*** ObfuscatedGaiaIdFetcher::ParseResponse called ***";
|
| +
|
| + 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(kID, result);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|