1 // Copyright (c) 2012 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_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_ 6 #define CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_ 7 8 #include <string> 9 10 #include "base/files/file_path.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/strings/string16.h" 14 #include "base/timer/timer.h" 15 #include "chrome/browser/chromeos/login/managed/managed_user_authenticator.h" 16 #include "chrome/browser/managed_mode/managed_user_registration_utility.h" 17 18 class Profile; 19 20 namespace chromeos { 21 22 // LocallyManagedUserCreationController is used to locally managed user 23 // creation. 24 // LMU Creation process: 25 // 0. Manager is logged in 26 // 1. Generate ID for new LMU 27 // 2. Start "transaction" in Local State. 28 // 3. Create local cryptohome (errors could arise) 29 // 4. Create user in cloud (errors could arise) 30 // 5. Store cloud token in cryptohome (actually, error could arise). 31 // 6. Mark "transaction" as completed. 32 // 7. End manager session. 33 34 class LocallyManagedUserCreationController 35 : public ManagedUserAuthenticator::AuthStatusConsumer { 36 public: 37 enum ErrorCode { 38 NO_ERROR, 39 CRYPTOHOME_NO_MOUNT, 40 CRYPTOHOME_FAILED_MOUNT, 41 CRYPTOHOME_FAILED_TPM, 42 CLOUD_SERVER_ERROR, 43 TOKEN_WRITE_FAILED, 44 }; 45 46 class StatusConsumer { 47 public: 48 virtual ~StatusConsumer(); 49 50 virtual void OnCreationError(ErrorCode code) = 0; 51 virtual void OnCreationTimeout() = 0; 52 virtual void OnCreationSuccess() = 0; 53 }; 54 55 // All UI initialization is deferred till Init() call. 56 // |Consumer| is not owned by controller, and it is expected that it wouldn't 57 // be deleted before LocallyManagedUserCreationController. 58 LocallyManagedUserCreationController(StatusConsumer* consumer, 59 const std::string& manager_id); 60 virtual ~LocallyManagedUserCreationController(); 61 62 // Returns the current locally managed user controller if it has been created. 63 static LocallyManagedUserCreationController* current_controller() { 64 return current_controller_; 65 } 66 67 void SetUpCreation(string16 display_name, std::string password); 68 void SetManagerProfile(Profile* manager_profile); 69 void StartCreation(); 70 void CancelCreation(); 71 void FinishCreation(); 72 std::string GetManagedUserId(); 73 74 private: 75 // Contains information necessary for new user creation. 76 struct UserCreationContext { 77 UserCreationContext(); 78 ~UserCreationContext(); 79 80 string16 display_name; 81 std::string manager_id; 82 std::string local_user_id; // Used to identify cryptohome. 83 std::string sync_user_id; // Used to identify user in manager's sync data. 84 std::string password; 85 std::string mount_hash; 86 std::string master_key; 87 bool token_acquired; 88 std::string token; 89 bool token_succesfully_written; 90 Profile* manager_profile; 91 scoped_ptr<ManagedUserRegistrationUtility> registration_utility; 92 }; 93 94 // ManagedUserAuthenticator::StatusConsumer overrides. 95 virtual void OnAuthenticationFailure( 96 ManagedUserAuthenticator::AuthState error) OVERRIDE; 97 virtual void OnMountSuccess(const std::string& mount_hash) OVERRIDE; 98 virtual void OnAddKeySuccess() OVERRIDE; 99 100 void CreationTimedOut(); 101 void RegistrationCallback(const GoogleServiceAuthError& error, 102 const std::string& token); 103 104 void TokenFetched(const std::string& token); 105 106 // Completion callback for StoreManagedUserFiles method. 107 // Called on the UI thread. 108 void OnManagedUserFilesStored(bool success); 109 110 // Pointer to the current instance of the controller to be used by 111 // automation tests. 112 static LocallyManagedUserCreationController* current_controller_; 113 114 StatusConsumer* consumer_; 115 116 scoped_refptr<ManagedUserAuthenticator> authenticator_; 117 118 // Creation context. Not null while creating new LMU. 119 scoped_ptr<UserCreationContext> creation_context_; 120 121 // Timer for showing warning if creation process takes too long. 122 base::OneShotTimer<LocallyManagedUserCreationController> timeout_timer_; 123 124 // Factory of callbacks. 125 base::WeakPtrFactory<LocallyManagedUserCreationController> weak_factory_; 126 127 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUserCreationController); 128 }; 129 130 } // namespace chromeos 131 132 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_ 133