1 // Copyright 2014 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_SUPERVISED_USER_SUPERVISED_USER_REGISTRATION_UTILITY_H_ 6 #define CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_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 "base/values.h" 18 #include "chrome/browser/supervised_user/supervised_user_sync_service.h" 19 #include "chrome/browser/supervised_user/supervised_user_sync_service_observer.h" 20 #include "chrome/browser/supervised_user/supervised_users.h" 21 #include "components/keyed_service/core/keyed_service.h" 22 23 class GoogleServiceAuthError; 24 class PrefService; 25 class Profile; 26 class SupervisedUserRefreshTokenFetcher; 27 class SupervisedUserRegistrationUtilityTest; 28 class SupervisedUserSharedSettingsService; 29 30 namespace browser_sync { 31 class DeviceInfo; 32 } 33 34 // Structure to store registration information. 35 struct SupervisedUserRegistrationInfo { 36 SupervisedUserRegistrationInfo(const base::string16& name, int avatar_index); 37 ~SupervisedUserRegistrationInfo(); 38 int avatar_index; 39 base::string16 name; 40 std::string master_key; 41 std::string password_signature_key; 42 std::string password_encryption_key; 43 base::DictionaryValue password_data; 44 }; 45 46 // Holds the state necessary for registering a new supervised user with the 47 // management server and associating it with its custodian. Each instance 48 // of this class handles registering a single supervised user and should not 49 // be used afterwards. 50 class SupervisedUserRegistrationUtility { 51 public: 52 // Callback for Register() below. If registration is successful, |token| will 53 // contain an OAuth2 refresh token for the newly registered supervised 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 */)> 58 RegistrationCallback; 59 60 virtual ~SupervisedUserRegistrationUtility() {} 61 62 // Creates SupervisedUserRegistrationUtility for a given |profile|. 63 static scoped_ptr<SupervisedUserRegistrationUtility> Create(Profile* profile); 64 65 static std::string GenerateNewSupervisedUserId(); 66 67 // Registers a new supervised user with the server. |supervised_user_id| is a 68 // new unique ID for the new supervised user. If its value is the same as that 69 // of one of the existing supervised users, then the same user will be created 70 // on this machine (and if he has no avatar in sync, his avatar will be 71 // updated). |info| contains necessary information like the display name of 72 // the user and his avatar. |callback| is called with the result of the 73 // registration. We use the info here and not the profile, because on Chrome 74 // OS the profile of the supervised user does not yet exist. 75 virtual void Register(const std::string& supervised_user_id, 76 const SupervisedUserRegistrationInfo& info, 77 const RegistrationCallback& callback) = 0; 78 79 protected: 80 SupervisedUserRegistrationUtility() {} 81 82 private: 83 friend class ScopedTestingSupervisedUserRegistrationUtility; 84 friend class SupervisedUserRegistrationUtilityTest; 85 86 // Creates implementation with explicit dependencies, can be used for testing. 87 static SupervisedUserRegistrationUtility* CreateImpl( 88 PrefService* prefs, 89 scoped_ptr<SupervisedUserRefreshTokenFetcher> token_fetcher, 90 SupervisedUserSyncService* service, 91 SupervisedUserSharedSettingsService* shared_settings_service); 92 93 // Set the instance of SupervisedUserRegistrationUtility that will be returned 94 // by next Create() call. Takes ownership of the |utility|. 95 static void SetUtilityForTests(SupervisedUserRegistrationUtility* utility); 96 }; 97 98 // Class that sets the instance of SupervisedUserRegistrationUtility that will 99 // be returned by next Create() call, and correctly destroys it if Create() was 100 // not called. 101 class ScopedTestingSupervisedUserRegistrationUtility { 102 public: 103 // Delegates ownership of the |instance| to SupervisedUserRegistrationUtility. 104 ScopedTestingSupervisedUserRegistrationUtility( 105 SupervisedUserRegistrationUtility* instance); 106 107 ~ScopedTestingSupervisedUserRegistrationUtility(); 108 }; 109 110 #endif // CHROME_BROWSER_SUPERVISED_USER_SUPERVISED_USER_REGISTRATION_UTILITY_H_ 111