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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_
6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "base/strings/string16.h"
17 #include "base/values.h"
18 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
19 #include "chrome/browser/managed_mode/managed_user_sync_service_observer.h"
20 #include "chrome/browser/managed_mode/managed_users.h"
21 #include "components/keyed_service/core/keyed_service.h"
23 class GoogleServiceAuthError
;
24 class ManagedUserRefreshTokenFetcher
;
25 class ManagedUserRegistrationUtilityTest
;
26 class ManagedUserSharedSettingsService
;
30 namespace browser_sync
{
34 // Structure to store registration information.
35 struct ManagedUserRegistrationInfo
{
36 ManagedUserRegistrationInfo(const base::string16
& name
, int avatar_index
);
37 ~ManagedUserRegistrationInfo();
40 std::string master_key
;
41 std::string password_signature_key
;
42 std::string password_encryption_key
;
43 base::DictionaryValue password_data
;
46 // Holds the state necessary for registering a new managed user with the
47 // management server and associating it with its custodian. Each instance
48 // of this class handles registering a single managed user and should not
49 // be used afterwards.
50 class ManagedUserRegistrationUtility
{
52 // Callback for Register() below. If registration is successful, |token| will
53 // contain an OAuth2 refresh token for the newly registered managed user,
54 // otherwise |token| will be empty and |error| will contain the authentication
55 // error for the custodian.
56 typedef base::Callback
<void(const GoogleServiceAuthError
& /* error */,
57 const std::string
& /* token */)>
60 virtual ~ManagedUserRegistrationUtility() {}
62 // Creates ManagedUserRegistrationUtility for a given |profile|.
63 static scoped_ptr
<ManagedUserRegistrationUtility
> Create(Profile
* profile
);
65 static std::string
GenerateNewManagedUserId();
67 // Registers a new managed user with the server. |managed_user_id| is a new
68 // unique ID for the new managed user. If its value is the same as that of
69 // of one of the existing managed users, then the same user will be created
70 // on this machine (and if he has no avatar in sync, his avatar will
71 // be updated). |info| contains necessary information like
72 // the display name of the user and his avatar. |callback| is called
73 // with the result of the registration. We use the info here and not the
74 // profile, because on Chrome OS the profile of the managed user does not
76 virtual void Register(const std::string
& managed_user_id
,
77 const ManagedUserRegistrationInfo
& info
,
78 const RegistrationCallback
& callback
) = 0;
81 ManagedUserRegistrationUtility() {}
84 friend class ScopedTestingManagedUserRegistrationUtility
;
85 friend class ManagedUserRegistrationUtilityTest
;
87 // Creates implementation with explicit dependencies, can be used for testing.
88 static ManagedUserRegistrationUtility
* CreateImpl(
90 scoped_ptr
<ManagedUserRefreshTokenFetcher
> token_fetcher
,
91 ManagedUserSyncService
* service
,
92 ManagedUserSharedSettingsService
* shared_settings_service
);
94 // Set the instance of ManagedUserRegistrationUtility that will be returned
95 // by next Create() call. Takes ownership of the |utility|.
96 static void SetUtilityForTests(ManagedUserRegistrationUtility
* utility
);
99 // Class that sets the instance of ManagedUserRegistrationUtility that will be
100 // returned by next Create() call, and correctly destroys it if Create() was
102 class ScopedTestingManagedUserRegistrationUtility
{
104 // Delegates ownership of the |instance| to ManagedUserRegistrationUtility.
105 ScopedTestingManagedUserRegistrationUtility(
106 ManagedUserRegistrationUtility
* instance
);
108 ~ScopedTestingManagedUserRegistrationUtility();
111 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_