Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / cloud / user_info_fetcher.cc
blob0212bb99de0ada6b1ebabafd96c43e3d7ab2d22b
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.
5 #include "components/policy/core/common/cloud/user_info_fetcher.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/values.h"
11 #include "components/data_use_measurement/core/data_use_user_data.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "net/base/load_flags.h"
15 #include "net/http/http_status_code.h"
16 #include "net/url_request/url_fetcher.h"
17 #include "net/url_request/url_request_status.h"
18 #include "url/gurl.h"
20 namespace {
22 static const char kAuthorizationHeaderFormat[] =
23 "Authorization: Bearer %s";
25 static std::string MakeAuthorizationHeader(const std::string& auth_token) {
26 return base::StringPrintf(kAuthorizationHeaderFormat, auth_token.c_str());
29 } // namespace
31 namespace policy {
33 UserInfoFetcher::UserInfoFetcher(Delegate* delegate,
34 net::URLRequestContextGetter* context)
35 : delegate_(delegate),
36 context_(context) {
37 DCHECK(delegate);
40 UserInfoFetcher::~UserInfoFetcher() {
43 void UserInfoFetcher::Start(const std::string& access_token) {
44 // Create a URLFetcher and start it.
45 url_fetcher_ =
46 net::URLFetcher::Create(0, GaiaUrls::GetInstance()->oauth_user_info_url(),
47 net::URLFetcher::GET, this);
48 data_use_measurement::DataUseUserData::AttachToFetcher(
49 url_fetcher_.get(), data_use_measurement::DataUseUserData::POLICY);
50 url_fetcher_->SetRequestContext(context_);
51 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
52 net::LOAD_DO_NOT_SAVE_COOKIES);
53 url_fetcher_->AddExtraRequestHeader(MakeAuthorizationHeader(access_token));
54 url_fetcher_->Start(); // Results in a call to OnURLFetchComplete().
57 void UserInfoFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
58 net::URLRequestStatus status = source->GetStatus();
59 GoogleServiceAuthError error = GoogleServiceAuthError::AuthErrorNone();
60 if (!status.is_success()) {
61 if (status.status() == net::URLRequestStatus::CANCELED)
62 error = GoogleServiceAuthError(GoogleServiceAuthError::REQUEST_CANCELED);
63 else
64 error = GoogleServiceAuthError::FromConnectionError(status.error());
65 } else if (source->GetResponseCode() != net::HTTP_OK) {
66 DLOG(WARNING) << "UserInfo request failed with HTTP code: "
67 << source->GetResponseCode();
68 error = GoogleServiceAuthError(
69 GoogleServiceAuthError::CONNECTION_FAILED);
71 if (error.state() != GoogleServiceAuthError::NONE) {
72 delegate_->OnGetUserInfoFailure(error);
73 return;
76 // Successfully fetched userinfo from the server - parse it and hand it off
77 // to the delegate.
78 std::string unparsed_data;
79 source->GetResponseAsString(&unparsed_data);
80 DVLOG(1) << "Received UserInfo response: " << unparsed_data;
81 scoped_ptr<base::Value> parsed_value = base::JSONReader::Read(unparsed_data);
82 base::DictionaryValue* dict;
83 if (parsed_value.get() && parsed_value->GetAsDictionary(&dict)) {
84 delegate_->OnGetUserInfoSuccess(dict);
85 } else {
86 NOTREACHED() << "Could not parse userinfo response from server";
87 delegate_->OnGetUserInfoFailure(GoogleServiceAuthError(
88 GoogleServiceAuthError::CONNECTION_FAILED));
92 }; // namespace policy