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