This source file includes following definitions.
- CreateAuthError
- delegate_
- GetScopes
- ReportSuccess
- ReportFailure
- CreateApiCallUrl
- CreateApiCallBody
- ProcessApiCallSuccess
- ProcessApiCallFailure
- ProcessNewAccessToken
- ProcessMintAccessTokenFailure
- ParseResponse
#include "chrome/browser/extensions/api/push_messaging/obfuscated_gaia_id_fetcher.h"
#include <vector>
#include "base/json/json_reader.h"
#include "base/values.h"
#include "google_apis/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 {
static const char kCWSChannelServiceURL[] =
"https://www.googleapis.com/gcm_for_chrome/v1/channels/id";
GoogleServiceAuthError CreateAuthError(const URLFetcher* source) {
if (source->GetStatus().status() == URLRequestStatus::CANCELED) {
return GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
} else {
if (source->GetStatus().is_success()) {
DLOG(WARNING) << "Remote server returned " << source->GetResponseCode();
return GoogleServiceAuthError::FromConnectionError(
source->GetResponseCode());
} else {
DLOG(WARNING) << "URLFetcher failed: " << source->GetStatus().error();
return GoogleServiceAuthError::FromConnectionError(
source->GetStatus().error());
}
}
}
}
namespace extensions {
ObfuscatedGaiaIdFetcher::ObfuscatedGaiaIdFetcher(
URLRequestContextGetter* context,
Delegate* delegate,
const std::string& access_token)
: OAuth2ApiCallFlow(context, std::string(), access_token, GetScopes()),
delegate_(delegate) {
DCHECK(delegate);
}
ObfuscatedGaiaIdFetcher::~ObfuscatedGaiaIdFetcher() { }
std::vector<std::string> ObfuscatedGaiaIdFetcher::GetScopes() {
std::vector<std::string> scopes;
scopes.push_back("https://www.googleapis.com/auth/gcm_for_chrome.readonly");
return scopes;
}
void ObfuscatedGaiaIdFetcher::ReportSuccess(const std::string& obfuscated_id) {
if (delegate_)
delegate_->OnObfuscatedGaiaIdFetchSuccess(obfuscated_id);
}
void ObfuscatedGaiaIdFetcher::ReportFailure(
const GoogleServiceAuthError& error) {
if (delegate_)
delegate_->OnObfuscatedGaiaIdFetchFailure(error);
}
GURL ObfuscatedGaiaIdFetcher::CreateApiCallUrl() {
return GURL(kCWSChannelServiceURL);
}
std::string ObfuscatedGaiaIdFetcher::CreateApiCallBody() {
return std::string();
}
void ObfuscatedGaiaIdFetcher::ProcessApiCallSuccess(
const net::URLFetcher* source) {
std::string response_body;
CHECK(source->GetResponseAsString(&response_body));
std::string obfuscated_id;
if (ParseResponse(response_body, &obfuscated_id))
ReportSuccess(obfuscated_id);
else
ReportFailure(GoogleServiceAuthError::FromConnectionError(101));
}
void ObfuscatedGaiaIdFetcher::ProcessApiCallFailure(
const net::URLFetcher* source) {
ReportFailure(CreateAuthError(source));
}
void ObfuscatedGaiaIdFetcher::ProcessNewAccessToken(
const std::string& obfuscated_id) {
}
void ObfuscatedGaiaIdFetcher::ProcessMintAccessTokenFailure(
const GoogleServiceAuthError& error) {
ReportFailure(error);
}
bool ObfuscatedGaiaIdFetcher::ParseResponse(
const std::string& data, std::string* result) {
scoped_ptr<base::Value> value(base::JSONReader::Read(data));
if (!value.get())
return false;
base::DictionaryValue* dict = NULL;
if (!value->GetAsDictionary(&dict))
return false;
return dict->GetString("id", result);
}
}