Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / policy / core / common / cloud / user_info_fetcher.cc
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 "components/policy/core/common/cloud/user_info_fetcher.h"
6
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 "google_apis/gaia/gaia_urls.h"
12 #include "google_apis/gaia/google_service_auth_error.h"
13 #include "net/base/load_flags.h"
14 #include "net/http/http_status_code.h"
15 #include "net/url_request/url_fetcher.h"
16 #include "net/url_request/url_request_status.h"
17 #include "url/gurl.h"
18
19 namespace policy {
20
21 UserInfoFetcher::UserInfoFetcher(Delegate* delegate,
22                                  net::URLRequestContextGetter* context)
23     : delegate_(delegate), gaia_client_(context) {
24   DCHECK(delegate);
25 }
26
27 UserInfoFetcher::~UserInfoFetcher() {
28 }
29
30 void UserInfoFetcher::Start(const std::string& access_token) {
31   // Create a URLFetcher and start it.
32   gaia_client_.GetUserInfo(access_token, 0, &delegate_);
33 }
34
35 UserInfoFetcher::GaiaDelegate::GaiaDelegate(UserInfoFetcher::Delegate* delegate)
36     : delegate_(delegate) {
37 }
38
39 void UserInfoFetcher::GaiaDelegate::OnGetUserInfoResponse(
40     scoped_ptr<base::DictionaryValue> user_info) {
41   delegate_->OnGetUserInfoSuccess(user_info.get());
42 }
43
44 void UserInfoFetcher::GaiaDelegate::OnOAuthError() {
45   GoogleServiceAuthError error =
46       GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS);
47   delegate_->OnGetUserInfoFailure(error);
48 }
49
50 void UserInfoFetcher::GaiaDelegate::OnNetworkError(int response_code) {
51   GoogleServiceAuthError error =
52       GoogleServiceAuthError::FromConnectionError(response_code);
53   delegate_->OnGetUserInfoFailure(error);
54 }
55
56 };  // namespace policy