| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ |
| 6 #define CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ | 6 #define CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/weak_ptr.h" |
| 10 #include "chrome/browser/policy/cloud_policy_service.h" | 11 #include "chrome/browser/policy/cloud_policy_service.h" |
| 11 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 12 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 14 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 15 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 15 | 16 |
| 16 class OAuth2AccessTokenFetcher; | 17 class OAuth2AccessTokenFetcher; |
| 17 class Profile; | 18 class Profile; |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 class Time; | 21 class Time; |
| 21 } | 22 } |
| 22 | 23 |
| 23 namespace policy { | 24 namespace policy { |
| 24 | 25 |
| 25 class UserCloudPolicyManager; | 26 class UserCloudPolicyManager; |
| 26 | 27 |
| 27 // The UserPolicySigninService tracks when user signin/signout actions occur and | 28 // The UserPolicySigninService is responsible for interacting with the policy |
| 28 // initializes/shuts down the UserCloudPolicyManager as required. This class is | 29 // infrastructure (mainly UserCloudPolicyManager) to load policy for the signed |
| 29 // not used on ChromeOS because UserCloudPolicyManager initialization is handled | 30 // in user. |
| 30 // via LoginUtils, since it must happen before profile creation. | 31 // |
| 32 // At signin time, this class initializes the UCPM and loads policy before any |
| 33 // other signed in services are initialized. After each restart, this class |
| 34 // ensures that the CloudPolicyClient is registered (in case the policy server |
| 35 // was offline during the initial policy fetch) and if not it initiates a fresh |
| 36 // registration process. |
| 37 // |
| 38 // Finally, if the user signs out, this class is responsible for shutting down |
| 39 // the policy infrastructure to ensure that any cached policy is cleared. |
| 31 class UserPolicySigninService | 40 class UserPolicySigninService |
| 32 : public ProfileKeyedService, | 41 : public ProfileKeyedService, |
| 33 public OAuth2AccessTokenConsumer, | 42 public OAuth2AccessTokenConsumer, |
| 34 public CloudPolicyService::Observer, | 43 public CloudPolicyService::Observer, |
| 44 public CloudPolicyClient::Observer, |
| 35 public content::NotificationObserver { | 45 public content::NotificationObserver { |
| 36 public: | 46 public: |
| 47 // The callback invoked once policy fetch is complete. Passed boolean |
| 48 // parameter is set to true if the policy fetch succeeded. |
| 49 typedef base::Callback<void(bool)> PolicyFetchCallback; |
| 50 |
| 37 // Creates a UserPolicySigninService associated with the passed |profile|. | 51 // Creates a UserPolicySigninService associated with the passed |profile|. |
| 38 explicit UserPolicySigninService(Profile* profile); | 52 explicit UserPolicySigninService(Profile* profile); |
| 39 virtual ~UserPolicySigninService(); | 53 virtual ~UserPolicySigninService(); |
| 40 | 54 |
| 55 // Initiates a policy fetch as part of user signin. The |oauth2_access_token| |
| 56 // is explicitly passed because TokenService does not have the token yet |
| 57 // (to prevent services from using it until after we've fetched policy). |
| 58 // |callback| is invoked once the policy fetch is complete, passing true if |
| 59 // the policy fetch succeeded. |
| 60 void FetchPolicyForSignedInUser(const std::string& oauth2_access_token, |
| 61 const PolicyFetchCallback& callback); |
| 62 |
| 41 // content::NotificationObserver implementation. | 63 // content::NotificationObserver implementation. |
| 42 virtual void Observe(int type, | 64 virtual void Observe(int type, |
| 43 const content::NotificationSource& source, | 65 const content::NotificationSource& source, |
| 44 const content::NotificationDetails& details) OVERRIDE; | 66 const content::NotificationDetails& details) OVERRIDE; |
| 45 | 67 |
| 46 // CloudPolicyService::Observer implementation. | 68 // CloudPolicyService::Observer implementation. |
| 47 virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE; | 69 virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE; |
| 48 | 70 |
| 71 // CloudPolicyClient::Observer implementation. |
| 72 virtual void OnClientError(CloudPolicyClient* client) OVERRIDE; |
| 73 virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE; |
| 74 virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE; |
| 75 |
| 49 // OAuth2AccessTokenConsumer implementation. | 76 // OAuth2AccessTokenConsumer implementation. |
| 50 virtual void OnGetTokenSuccess(const std::string& access_token, | 77 virtual void OnGetTokenSuccess(const std::string& access_token, |
| 51 const base::Time& expiration_time) OVERRIDE; | 78 const base::Time& expiration_time) OVERRIDE; |
| 52 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 79 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 53 | 80 |
| 54 // ProfileKeyedService implementation: | 81 // ProfileKeyedService implementation: |
| 55 virtual void Shutdown() OVERRIDE; | 82 virtual void Shutdown() OVERRIDE; |
| 56 | 83 |
| 57 private: | 84 private: |
| 58 // Initializes the UserCloudPolicyManager to reflect the currently-signed-in | 85 // Initializes the UserCloudPolicyManager to reflect the currently-signed-in |
| 59 // user. | 86 // user. |
| 60 void ConfigureUserCloudPolicyManager(); | 87 void InitializeUserCloudPolicyManager(); |
| 61 | 88 |
| 62 // Fetches an OAuth token to allow the cloud policy service to register with | 89 // Fetches an OAuth token to allow the cloud policy service to register with |
| 63 // the cloud policy server. | 90 // the cloud policy server. |oauth_login_token| should contain an OAuth login |
| 64 void RegisterCloudPolicyService(); | 91 // refresh token that can be downscoped to get an access token for the |
| 92 // device_management service. |
| 93 void RegisterCloudPolicyService(std::string oauth_login_token); |
| 65 | 94 |
| 66 // Helper routine to unregister for CloudPolicyService notifications. | 95 // Helper routines to (un)register for CloudPolicyService and |
| 96 // CloudPolicyClient notifications. |
| 97 void StartObserving(); |
| 67 void StopObserving(); | 98 void StopObserving(); |
| 68 | 99 |
| 100 // If a policy fetch was requested, invokes the callback passing through the |
| 101 // |success| flag. |
| 102 void NotifyPendingFetchCallback(bool success); |
| 103 |
| 104 // Shuts down the UserCloudPolicyManager (for example, after the user signs |
| 105 // out) and deletes any cached policy. |
| 106 void ShutdownUserCloudPolicyManager(); |
| 107 |
| 69 // Convenience helper to get the UserCloudPolicyManager for |profile_|. | 108 // Convenience helper to get the UserCloudPolicyManager for |profile_|. |
| 70 UserCloudPolicyManager* GetManager(); | 109 UserCloudPolicyManager* GetManager(); |
| 71 | 110 |
| 111 // WeakPtrFactory used to create callbacks for loading policy. |
| 112 base::WeakPtrFactory<UserPolicySigninService> weak_factory_; |
| 113 |
| 72 // Weak pointer to the profile this service is associated with. | 114 // Weak pointer to the profile this service is associated with. |
| 73 Profile* profile_; | 115 Profile* profile_; |
| 74 | 116 |
| 117 // If true, we have a pending fetch so notify the callback the next time |
| 118 // the appropriate notification is delivered from CloudPolicyService/Client. |
| 119 bool pending_fetch_; |
| 120 |
| 121 // The callback to invoke when the pending policy fetch is completed. |
| 122 PolicyFetchCallback pending_fetch_callback_; |
| 123 |
| 75 content::NotificationRegistrar registrar_; | 124 content::NotificationRegistrar registrar_; |
| 125 |
| 126 // Fetcher used while obtaining an OAuth token for client registration. |
| 76 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | 127 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; |
| 77 | 128 |
| 78 DISALLOW_COPY_AND_ASSIGN(UserPolicySigninService); | 129 DISALLOW_COPY_AND_ASSIGN(UserPolicySigninService); |
| 79 }; | 130 }; |
| 80 | 131 |
| 81 } // namespace policy | 132 } // namespace policy |
| 82 | 133 |
| 83 #endif // CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ | 134 #endif // CHROME_BROWSER_POLICY_USER_POLICY_SIGNIN_SERVICE_H_ |
| OLD | NEW |