| OLD | NEW |
| (Empty) |
| 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 CHROME_BROWSER_CHROMEOS_GDATA_AUTH_SERVICE_H_ | |
| 6 #define CHROME_BROWSER_CHROMEOS_GDATA_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 "chrome/browser/chromeos/gdata/gdata_errorcode.h" | |
| 14 #include "chrome/browser/chromeos/gdata/operations_base.h" | |
| 15 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
| 16 #include "content/public/browser/notification_observer.h" | |
| 17 #include "content/public/browser/notification_registrar.h" | |
| 18 | |
| 19 class Profile; | |
| 20 | |
| 21 namespace gdata { | |
| 22 | |
| 23 class OperationRegistry; | |
| 24 | |
| 25 // This class provides authentication for Google services. | |
| 26 // It integrates specific service integration with OAuth2 stack | |
| 27 // (TokenService) and provides OAuth2 token refresh infrastructure. | |
| 28 // All public functions must be called on UI thread. | |
| 29 class AuthService : public content::NotificationObserver { | |
| 30 public: | |
| 31 class Observer { | |
| 32 public: | |
| 33 // Triggered when a new OAuth2 refresh token is received from TokenService. | |
| 34 virtual void OnOAuth2RefreshTokenChanged() = 0; | |
| 35 | |
| 36 protected: | |
| 37 virtual ~Observer() {} | |
| 38 }; | |
| 39 | |
| 40 explicit AuthService(const std::vector<std::string>& scopes); | |
| 41 virtual ~AuthService(); | |
| 42 | |
| 43 // Adds and removes the observer. AddObserver() should be called before | |
| 44 // Initialize() as it can change the refresh token. | |
| 45 void AddObserver(Observer* observer); | |
| 46 void RemoveObserver(Observer* observer); | |
| 47 | |
| 48 // Initializes the auth service. Starts TokenService to retrieve the | |
| 49 // refresh token. | |
| 50 void Initialize(Profile* profile); | |
| 51 | |
| 52 // Starts fetching OAuth2 auth token from the refresh token for |scopes_|. | |
| 53 void StartAuthentication(OperationRegistry* registry, | |
| 54 const AuthStatusCallback& callback); | |
| 55 | |
| 56 // True if an OAuth2 access token is retrieved and believed to be fresh. | |
| 57 // The access token is used to access the gdata server. | |
| 58 bool HasAccessToken() const { return !access_token_.empty(); } | |
| 59 | |
| 60 // True if an OAuth2 refresh token is present. Its absence means that user | |
| 61 // is not properly authenticated. | |
| 62 // The refresh token is used to get the access token. | |
| 63 bool HasRefreshToken() const { return !refresh_token_.empty(); } | |
| 64 | |
| 65 // Returns OAuth2 access token. | |
| 66 const std::string& access_token() const { return access_token_; } | |
| 67 | |
| 68 // Clears OAuth2 access token. | |
| 69 void ClearAccessToken() { access_token_.clear(); } | |
| 70 | |
| 71 // Overridden from content::NotificationObserver: | |
| 72 virtual void Observe(int type, | |
| 73 const content::NotificationSource& source, | |
| 74 const content::NotificationDetails& details) OVERRIDE; | |
| 75 | |
| 76 // Sets the access_token as specified. This should be used only for testing. | |
| 77 void set_access_token_for_testing(const std::string& token) { | |
| 78 access_token_ = token; | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 // Helper function for StartAuthentication() call. | |
| 83 void StartAuthenticationOnUIThread(OperationRegistry* registry, | |
| 84 const AuthStatusCallback& callback); | |
| 85 | |
| 86 // Callback for AuthOperation (InternalAuthStatusCallback). | |
| 87 void OnAuthCompleted(const AuthStatusCallback& callback, | |
| 88 GDataErrorCode error, | |
| 89 const std::string& access_token); | |
| 90 | |
| 91 Profile* profile_; | |
| 92 std::string refresh_token_; | |
| 93 std::string access_token_; | |
| 94 std::vector<std::string> scopes_; | |
| 95 ObserverList<Observer> observers_; | |
| 96 | |
| 97 content::NotificationRegistrar registrar_; | |
| 98 | |
| 99 // Note: This should remain the last member so it'll be destroyed and | |
| 100 // invalidate its weak pointers before any other members are destroyed. | |
| 101 base::WeakPtrFactory<AuthService> weak_ptr_factory_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(AuthService); | |
| 104 }; | |
| 105 | |
| 106 } // namespace gdata | |
| 107 | |
| 108 #endif // CHROME_BROWSER_CHROMEOS_GDATA_AUTH_SERVICE_H_ | |
| OLD | NEW |