Home | History | Annotate | Download | only in drive
      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 #ifndef GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_
      6 #define GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     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"
     16 
     17 namespace net {
     18 class URLRequestContextGetter;
     19 }
     20 
     21 namespace google_apis {
     22 
     23 class AuthServiceObserver;
     24 
     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.
     34   //
     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   virtual ~AuthService();
     41 
     42   // Overriden from AuthServiceInterface:
     43   virtual void AddObserver(AuthServiceObserver* observer) OVERRIDE;
     44   virtual void RemoveObserver(AuthServiceObserver* observer) OVERRIDE;
     45   virtual void StartAuthentication(const AuthStatusCallback& callback) OVERRIDE;
     46   virtual bool HasAccessToken() const OVERRIDE;
     47   virtual bool HasRefreshToken() const OVERRIDE;
     48   virtual const std::string& access_token() const OVERRIDE;
     49   virtual void ClearAccessToken() OVERRIDE;
     50   virtual void ClearRefreshToken() OVERRIDE;
     51 
     52   // Overridden from OAuth2TokenService::Observer:
     53   virtual void OnRefreshTokenAvailable(const std::string& account_id) OVERRIDE;
     54   virtual void OnRefreshTokenRevoked(const std::string& account_id) OVERRIDE;
     55 
     56  private:
     57   // Called when the state of the refresh token changes.
     58   void OnHandleRefreshToken(bool has_refresh_token);
     59 
     60   // Called when authentication request from StartAuthentication() is
     61   // completed.
     62   void OnAuthCompleted(const AuthStatusCallback& callback,
     63                        GDataErrorCode error,
     64                        const std::string& access_token);
     65 
     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_;
     74 
     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_;
     78 
     79   DISALLOW_COPY_AND_ASSIGN(AuthService);
     80 };
     81 
     82 }  // namespace google_apis
     83 
     84 #endif  // GOOGLE_APIS_DRIVE_AUTH_SERVICE_H_
     85