Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / signin / core / browser / account_service_flag_fetcher.h
1 // Copyright 2014 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 #ifndef COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
6 #define COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/callback.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "google_apis/gaia/gaia_auth_consumer.h"
14 #include "google_apis/gaia/gaia_auth_fetcher.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
16
17 class ProfileOAuth2TokenService;
18
19 namespace net {
20 class URLRequestContextGetter;
21 }
22
23 // Downloads an account's list of Gaia service flags.
24 // On construction, the download starts immediately and calls the given callback
25 // when either the download is successful or an error is detected. It is valid
26 // to destruct the object before the callback is called; this will cancel the
27 // pending request.
28 class AccountServiceFlagFetcher : public GaiaAuthConsumer,
29                                   public OAuth2TokenService::Observer,
30                                   public OAuth2TokenService::Consumer {
31  public:
32   enum ResultCode {
33     SUCCESS,
34     TOKEN_ERROR,    // Failed to get OAuth2 token.
35     SERVICE_ERROR,  // Service returned an error or malformed reply.
36   };
37
38   // If the flag download is successful, this will return the list of service
39   // flags that are set for the given account.
40   typedef base::Callback<void(ResultCode /* result */,
41                               const std::vector<std::string>& /* flags */)>
42       ResultCallback;
43
44   // Immediately starts fetching the flags.
45   AccountServiceFlagFetcher(const std::string& account_id,
46                             ProfileOAuth2TokenService* token_service,
47                             net::URLRequestContextGetter* request_context,
48                             const ResultCallback& callback);
49
50   // Destructing the object before the callback is called cancels the request.
51   ~AccountServiceFlagFetcher() override;
52
53  private:
54   void Start();
55   void StartFetchingOAuth2AccessToken();
56
57   // Overridden from OAuth2TokenService::Observer:
58   void OnRefreshTokenAvailable(const std::string& account_id) override;
59   void OnRefreshTokensLoaded() override;
60
61   // Overridden from OAuth2TokenService::Consumer:
62   void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
63                          const std::string& access_token,
64                          const base::Time& expiration_time) override;
65   void OnGetTokenFailure(const OAuth2TokenService::Request* request,
66                          const GoogleServiceAuthError& error) override;
67
68   // Overridden from GaiaAuthConsumer:
69   void OnClientLoginSuccess(const ClientLoginResult& result) override;
70   void OnClientLoginFailure(const GoogleServiceAuthError& error) override;
71   void OnGetUserInfoSuccess(const UserInfoMap& data) override;
72   void OnGetUserInfoFailure(const GoogleServiceAuthError& error) override;
73
74   const std::string account_id_;
75   ProfileOAuth2TokenService* token_service_;
76   GaiaAuthFetcher gaia_auth_fetcher_;
77
78   ResultCallback callback_;
79
80   scoped_ptr<OAuth2TokenService::Request> oauth2_access_token_request_;
81
82   DISALLOW_COPY_AND_ASSIGN(AccountServiceFlagFetcher);
83 };
84
85 #endif  // COMPONENTS_SIGNIN_CORE_BROWSER_ACCOUNT_SERVICE_FLAG_FETCHER_H_