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 CHROME_BROWSER_GOOGLE_APIS_AUTH_SERVICE_H_
6 #define CHROME_BROWSER_GOOGLE_APIS_AUTH_SERVICE_H_
11 #include "base/memory/weak_ptr.h"
12 #include "base/observer_list.h"
13 #include "chrome/browser/google_apis/auth_service_interface.h"
14 #include "chrome/browser/google_apis/gdata_errorcode.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
21 class URLRequestContextGetter
;
24 namespace google_apis
{
26 class OperationRegistry
;
27 class AuthServiceObserver
;
29 // This class provides authentication for Google services.
30 // It integrates specific service integration with OAuth2 stack
31 // (TokenService) and provides OAuth2 token refresh infrastructure.
32 // All public functions must be called on UI thread.
33 class AuthService
: public AuthServiceInterface
,
34 public content::NotificationObserver
{
36 // |url_request_context_getter| is used to perform authentication with
39 // |scopes| specifies OAuth2 scopes.
40 AuthService(net::URLRequestContextGetter
* url_request_context_getter
,
41 const std::vector
<std::string
>& scopes
);
42 virtual ~AuthService();
44 // Overriden from AuthServiceInterface:
45 virtual void AddObserver(AuthServiceObserver
* observer
) OVERRIDE
;
46 virtual void RemoveObserver(AuthServiceObserver
* observer
) OVERRIDE
;
47 virtual void Initialize(Profile
* profile
) OVERRIDE
;
48 virtual void StartAuthentication(OperationRegistry
* registry
,
49 const AuthStatusCallback
& callback
) OVERRIDE
;
50 virtual bool HasAccessToken() const OVERRIDE
;
51 virtual bool HasRefreshToken() const OVERRIDE
;
52 virtual const std::string
& access_token() const OVERRIDE
;
53 virtual void ClearAccessToken() OVERRIDE
;
54 virtual void ClearRefreshToken() OVERRIDE
;
56 // Overridden from content::NotificationObserver:
57 virtual void Observe(int type
,
58 const content::NotificationSource
& source
,
59 const content::NotificationDetails
& details
) OVERRIDE
;
61 // Sets the access_token as specified. This should be used only for testing.
62 void set_access_token_for_testing(const std::string
& token
) {
63 access_token_
= token
;
66 // Returns true if authentication can be done using the class for the given
67 // profile. For instance, this function returns false if the profile is
68 // used for the incognito mode.
69 static bool CanAuthenticate(Profile
* profile
);
72 // Callback for AuthOperation (InternalAuthStatusCallback).
73 void OnAuthCompleted(const AuthStatusCallback
& callback
,
75 const std::string
& access_token
);
78 net::URLRequestContextGetter
* url_request_context_getter_
; // Not owned.
79 std::string refresh_token_
;
80 std::string access_token_
;
81 std::vector
<std::string
> scopes_
;
82 ObserverList
<AuthServiceObserver
> observers_
;
84 content::NotificationRegistrar registrar_
;
86 // Note: This should remain the last member so it'll be destroyed and
87 // invalidate its weak pointers before any other members are destroyed.
88 base::WeakPtrFactory
<AuthService
> weak_ptr_factory_
;
90 DISALLOW_COPY_AND_ASSIGN(AuthService
);
93 } // namespace google_apis
95 #endif // CHROME_BROWSER_GOOGLE_APIS_AUTH_SERVICE_H_