OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ |
6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ | 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/prefs/pref_change_registrar.h" | 13 #include "base/prefs/pref_change_registrar.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
16 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 16 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
17 #include "sync/api/syncable_service.h" | 17 #include "sync/api/syncable_service.h" |
18 | 18 |
19 class GoogleServiceAuthError; | 19 class GoogleServiceAuthError; |
| 20 class ManagedUserRefreshTokenFetcher; |
20 class PrefService; | 21 class PrefService; |
21 | 22 |
| 23 namespace browser_sync { |
| 24 class DeviceInfo; |
| 25 } |
| 26 |
22 namespace user_prefs { | 27 namespace user_prefs { |
23 class PrefRegistrySyncable; | 28 class PrefRegistrySyncable; |
24 } | 29 } |
25 | 30 |
26 // Holds the state necessary for registering a new managed user with the | 31 // Holds the state necessary for registering a new managed user with the |
27 // management server and associating it with its custodian. It is owned by the | 32 // management server and associating it with its custodian. It is owned by the |
28 // custodian's profile. | 33 // custodian's profile. |
29 class ManagedUserRegistrationService : public BrowserContextKeyedService, | 34 class ManagedUserRegistrationService : public BrowserContextKeyedService, |
30 public syncer::SyncableService { | 35 public syncer::SyncableService { |
31 public: | 36 public: |
32 // Callback for Register() below. If registration is successful, |token| will | 37 // Callback for Register() below. If registration is successful, |token| will |
33 // contain an OAuth2 refresh token for the newly registered managed user, | 38 // contain an OAuth2 refresh token for the newly registered managed user, |
34 // otherwise |token| will be empty and |error| will contain the authentication | 39 // otherwise |token| will be empty and |error| will contain the authentication |
35 // error for the custodian. | 40 // error for the custodian. |
36 typedef base::Callback<void(const GoogleServiceAuthError& /* error */, | 41 typedef base::Callback<void(const GoogleServiceAuthError& /* error */, |
37 const std::string& /* token */)> | 42 const std::string& /* token */)> |
38 RegistrationCallback; | 43 RegistrationCallback; |
39 | 44 |
40 explicit ManagedUserRegistrationService(PrefService* prefs); | 45 ManagedUserRegistrationService( |
| 46 PrefService* prefs, |
| 47 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher); |
41 virtual ~ManagedUserRegistrationService(); | 48 virtual ~ManagedUserRegistrationService(); |
42 | 49 |
43 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry); | 50 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry); |
44 | 51 |
45 // Registers a new managed user with the server. |name| is the display name of | 52 // Registers a new managed user with the server. |name| is the display name of |
46 // the user. |callback| is called with the result of the registration. | 53 // the user. |callback| is called with the result of the registration. |
| 54 // TODO(bauerb): There should be a way to cancel a pending managed user |
| 55 // registration. |
47 void Register(const string16& name, const RegistrationCallback& callback); | 56 void Register(const string16& name, const RegistrationCallback& callback); |
48 | 57 |
49 // Convenience method that registers a new managed user with the server and | 58 // Convenience method that registers a new managed user with the server and |
50 // initializes it locally. The callback allows it to be run after a new | 59 // initializes it locally. The callback allows it to be run after a new |
51 // profile has been created: | 60 // profile has been created: |
52 // ProfileManager::CreateMultiProfileAsync( | 61 // ProfileManager::CreateMultiProfileAsync( |
53 // name, icon, | 62 // name, icon, |
54 // managed_user_registration_service->GetRegistrationAndInitCallback(), | 63 // managed_user_registration_service->GetRegistrationAndInitCallback(), |
55 // managed_user); | 64 // managed_user); |
56 ProfileManager::CreateCallback GetRegistrationAndInitCallback(); | 65 ProfileManager::CreateCallback GetRegistrationAndInitCallback(); |
(...skipping 13 matching lines...) Expand all Loading... |
70 virtual syncer::SyncError ProcessSyncChanges( | 79 virtual syncer::SyncError ProcessSyncChanges( |
71 const tracked_objects::Location& from_here, | 80 const tracked_objects::Location& from_here, |
72 const syncer::SyncChangeList& change_list) OVERRIDE; | 81 const syncer::SyncChangeList& change_list) OVERRIDE; |
73 | 82 |
74 private: | 83 private: |
75 void OnLastSignedInUsernameChange(); | 84 void OnLastSignedInUsernameChange(); |
76 | 85 |
77 // Called when the Sync server has acknowledged a newly created managed user. | 86 // Called when the Sync server has acknowledged a newly created managed user. |
78 void OnManagedUserAcknowledged(const std::string& managed_user_id); | 87 void OnManagedUserAcknowledged(const std::string& managed_user_id); |
79 | 88 |
| 89 // Fetches the managed user token when we have the device info. |
| 90 void FetchToken(const string16& name, |
| 91 const browser_sync::DeviceInfo& device_info); |
| 92 |
80 // Called when we have received a token for the managed user. | 93 // Called when we have received a token for the managed user. |
81 void OnReceivedToken(const std::string& token); | 94 void OnReceivedToken(const GoogleServiceAuthError& error, |
| 95 const std::string& token); |
82 | 96 |
83 // Dispatches the callback if all the conditions have been met. | 97 // Dispatches the callback if all the conditions have been met. |
84 void DispatchCallbackIfReady(); | 98 void DispatchCallbackIfReady(); |
85 | 99 |
86 void CancelPendingRegistration(); | 100 void CancelPendingRegistration(); |
87 | 101 |
88 // Dispatches the callback with the saved token (which may be empty) and the | 102 // Dispatches the callback with the saved token (which may be empty) and the |
89 // given |error|. | 103 // given |error|. |
90 void DispatchCallback(const GoogleServiceAuthError& error); | 104 void DispatchCallback(const GoogleServiceAuthError& error); |
91 | 105 |
92 void OnProfileCreated(Profile* profile, Profile::CreateStatus status); | 106 void OnProfileCreated(Profile* profile, Profile::CreateStatus status); |
93 | 107 |
94 base::WeakPtrFactory<ManagedUserRegistrationService> weak_ptr_factory_; | 108 base::WeakPtrFactory<ManagedUserRegistrationService> weak_ptr_factory_; |
95 PrefService* prefs_; | 109 PrefService* prefs_; |
96 PrefChangeRegistrar pref_change_registrar_; | 110 PrefChangeRegistrar pref_change_registrar_; |
| 111 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher_; |
97 | 112 |
98 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; | 113 scoped_ptr<syncer::SyncChangeProcessor> sync_processor_; |
99 scoped_ptr<syncer::SyncErrorFactory> error_handler_; | 114 scoped_ptr<syncer::SyncErrorFactory> error_handler_; |
100 | 115 |
101 std::string pending_managed_user_id_; | 116 std::string pending_managed_user_id_; |
102 std::string pending_managed_user_token_; | 117 std::string pending_managed_user_token_; |
103 bool pending_managed_user_acknowledged_; | 118 bool pending_managed_user_acknowledged_; |
104 RegistrationCallback callback_; | 119 RegistrationCallback callback_; |
105 | 120 |
106 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationService); | 121 DISALLOW_COPY_AND_ASSIGN(ManagedUserRegistrationService); |
107 }; | 122 }; |
108 | 123 |
109 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ | 124 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_SERVICE_H_ |
OLD | NEW |