Convert cr-settings-main to Polymer 0.8
[chromium-blink-merge.git] / google_apis / drive / auth_service.h
blob50f3b9fa76275df4ebab32b57cd2d8b7fa57f439
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 #ifndef GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_
6 #define GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/threading/thread_checker.h"
14 #include "google_apis/drive/auth_service_interface.h"
15 #include "google_apis/gaia/oauth2_token_service.h"
17 namespace net {
18 class URLRequestContextGetter;
21 namespace google_apis {
23 class AuthServiceObserver;
25 // This class provides authentication for Google services.
26 // It integrates specific service integration with OAuth2 stack
27 // (OAuth2TokenService) and provides OAuth2 token refresh infrastructure.
28 // All public functions must be called on UI thread.
29 class AuthService : public AuthServiceInterface,
30 public OAuth2TokenService::Observer {
31 public:
32 // |url_request_context_getter| is used to perform authentication with
33 // URLFetcher.
35 // |scopes| specifies OAuth2 scopes.
36 AuthService(OAuth2TokenService* oauth2_token_service,
37 const std::string& account_id,
38 net::URLRequestContextGetter* url_request_context_getter,
39 const std::vector<std::string>& scopes);
40 ~AuthService() override;
42 // Overriden from AuthServiceInterface:
43 void AddObserver(AuthServiceObserver* observer) override;
44 void RemoveObserver(AuthServiceObserver* observer) override;
45 void StartAuthentication(const AuthStatusCallback& callback) override;
46 bool HasAccessToken() const override;
47 bool HasRefreshToken() const override;
48 const std::string& access_token() const override;
49 void ClearAccessToken() override;
50 void ClearRefreshToken() override;
52 // Overridden from OAuth2TokenService::Observer:
53 void OnRefreshTokenAvailable(const std::string& account_id) override;
54 void OnRefreshTokenRevoked(const std::string& account_id) override;
56 private:
57 // Called when the state of the refresh token changes.
58 void OnHandleRefreshToken(bool has_refresh_token);
60 // Called when authentication request from StartAuthentication() is
61 // completed.
62 void OnAuthCompleted(const AuthStatusCallback& callback,
63 DriveApiErrorCode error,
64 const std::string& access_token);
66 OAuth2TokenService* oauth2_token_service_;
67 std::string account_id_;
68 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
69 bool has_refresh_token_;
70 std::string access_token_;
71 std::vector<std::string> scopes_;
72 ObserverList<AuthServiceObserver> observers_;
73 base::ThreadChecker thread_checker_;
75 // Note: This should remain the last member so it'll be destroyed and
76 // invalidate its weak pointers before any other members are destroyed.
77 base::WeakPtrFactory<AuthService> weak_ptr_factory_;
79 DISALLOW_COPY_AND_ASSIGN(AuthService);
82 } // namespace google_apis
84 #endif // GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_