Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / managed_mode / managed_user_registration_utility.h
bloba568d75dd396e191b7363fd9164fe4ae190dfb22
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_
8 #include <map>
9 #include <string>
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 "chrome/browser/managed_mode/managed_user_sync_service.h"
18 #include "chrome/browser/managed_mode/managed_user_sync_service_observer.h"
19 #include "chrome/browser/managed_mode/managed_users.h"
20 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
22 class GoogleServiceAuthError;
23 class ManagedUserRefreshTokenFetcher;
24 class ManagedUserRegistrationUtilityTest;
25 class PrefService;
26 class Profile;
28 namespace browser_sync {
29 class DeviceInfo;
32 // Structure to store registration information.
33 struct ManagedUserRegistrationInfo {
34 ManagedUserRegistrationInfo(const base::string16& name, int avatar_index);
35 int avatar_index;
36 base::string16 name;
37 std::string master_key;
40 // Holds the state necessary for registering a new managed user with the
41 // management server and associating it with its custodian. Each instance
42 // of this class handles registering a single managed user and should not
43 // be used afterwards.
44 class ManagedUserRegistrationUtility {
45 public:
46 // Callback for Register() below. If registration is successful, |token| will
47 // contain an OAuth2 refresh token for the newly registered managed user,
48 // otherwise |token| will be empty and |error| will contain the authentication
49 // error for the custodian.
50 typedef base::Callback<void(const GoogleServiceAuthError& /* error */,
51 const std::string& /* token */)>
52 RegistrationCallback;
54 virtual ~ManagedUserRegistrationUtility() {}
56 // Creates ManagedUserRegistrationUtility for a given |profile|.
57 static scoped_ptr<ManagedUserRegistrationUtility> Create(Profile* profile);
59 static std::string GenerateNewManagedUserId();
61 // Registers a new managed user with the server. |managed_user_id| is a new
62 // unique ID for the new managed user. If its value is the same as that of
63 // of one of the existing managed users, then the same user will be created
64 // on this machine (and if he has no avatar in sync, his avatar will
65 // be updated). |info| contains necessary information like
66 // the display name of the user and his avatar. |callback| is called
67 // with the result of the registration. We use the info here and not the
68 // profile, because on Chrome OS the profile of the managed user does not
69 // yet exist.
70 virtual void Register(const std::string& managed_user_id,
71 const ManagedUserRegistrationInfo& info,
72 const RegistrationCallback& callback) = 0;
74 protected:
75 ManagedUserRegistrationUtility() {}
77 private:
78 friend class ScopedTestingManagedUserRegistrationUtility;
79 friend class ManagedUserRegistrationUtilityTest;
81 // Creates implementation with explicit dependencies, can be used for testing.
82 static ManagedUserRegistrationUtility* CreateImpl(
83 PrefService* prefs,
84 scoped_ptr<ManagedUserRefreshTokenFetcher> token_fetcher,
85 ManagedUserSyncService* service);
87 // Set the instance of ManagedUserRegistrationUtility that will be returned
88 // by next Create() call. Takes ownership of the |utility|.
89 static void SetUtilityForTests(ManagedUserRegistrationUtility* utility);
92 // Class that sets the instance of ManagedUserRegistrationUtility that will be
93 // returned by next Create() call, and correctly destroys it if Create() was
94 // not called.
95 class ScopedTestingManagedUserRegistrationUtility {
96 public:
97 // Delegates ownership of the |instance| to ManagedUserRegistrationUtility.
98 ScopedTestingManagedUserRegistrationUtility(
99 ManagedUserRegistrationUtility* instance);
101 ~ScopedTestingManagedUserRegistrationUtility();
104 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_