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. 4 5 #ifndef CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 6 #define CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 7 8 #include <map> 9 #include <string> 10 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" 21 22 class GoogleServiceAuthError; 23 class ManagedUserRefreshTokenFetcher; 24 class ManagedUserRegistrationUtilityTest; 25 class PrefService; 26 class Profile; 27 28 namespace browser_sync { 29 class DeviceInfo; 30 } 31 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; 38 }; 39 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; 53 ~ManagedUserRegistrationUtility()54 virtual ~ManagedUserRegistrationUtility() {} 55 56 // Creates ManagedUserRegistrationUtility for a given |profile|. 57 static scoped_ptr<ManagedUserRegistrationUtility> Create(Profile* profile); 58 59 static std::string GenerateNewManagedUserId(); 60 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; 73 74 protected: ManagedUserRegistrationUtility()75 ManagedUserRegistrationUtility() {} 76 77 private: 78 friend class ScopedTestingManagedUserRegistrationUtility; 79 friend class ManagedUserRegistrationUtilityTest; 80 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); 86 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); 90 }; 91 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); 100 101 ~ScopedTestingManagedUserRegistrationUtility(); 102 }; 103 104 #endif // CHROME_BROWSER_MANAGED_MODE_MANAGED_USER_REGISTRATION_UTILITY_H_ 105