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.
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_
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"
22 // LocallyManagedUserCreationController is used to locally managed user
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.
34 class LocallyManagedUserCreationController
35 : public ManagedUserAuthenticator::AuthStatusConsumer {
37 // This constant is used to indicate that user does not have one of default
38 // avatars: either he has no chromeos avatar at all, or has an external
39 // image as an avatar.
40 static const int kDummyAvatarIndex;
45 CRYPTOHOME_FAILED_MOUNT,
46 CRYPTOHOME_FAILED_TPM,
51 class StatusConsumer {
53 virtual ~StatusConsumer();
55 virtual void OnCreationError(ErrorCode code) = 0;
56 virtual void OnLongCreationWarning() = 0;
57 virtual void OnCreationTimeout() = 0;
58 virtual void OnCreationSuccess() = 0;
61 // All UI initialization is deferred till Init() call.
62 // |Consumer| is not owned by controller, and it is expected that it wouldn't
63 // be deleted before LocallyManagedUserCreationController.
64 LocallyManagedUserCreationController(StatusConsumer* consumer,
65 const std::string& manager_id);
66 virtual ~LocallyManagedUserCreationController();
68 // Returns the current locally managed user controller if it has been created.
69 static LocallyManagedUserCreationController* current_controller() {
70 return current_controller_;
73 // Set up controller for creating new supervised user with |display_name|,
74 // |password| and avatar indexed by |avatar_index|. StartCreation() have to
75 // be called to actually start creating user.
76 void SetUpCreation(const string16& display_name,
77 const std::string& password,
80 // Configures and initiates importing existing supervised user to this device.
81 // Existing user is identified by |sync_id|, has |display_name|, |password|,
82 // |avatar_index|. The master key for cryptohome is a |master_key|.
83 void StartImport(const string16& display_name,
84 const std::string& password,
86 const std::string& sync_id,
87 const std::string& master_key);
88 void SetManagerProfile(Profile* manager_profile);
90 void CancelCreation();
91 void FinishCreation();
92 std::string GetManagedUserId();
95 // Indicates if we create new user, or import an existing one.
101 // Contains information necessary for new user creation.
102 struct UserCreationContext {
103 UserCreationContext();
104 ~UserCreationContext();
106 string16 display_name;
108 std::string manager_id;
109 std::string local_user_id; // Used to identify cryptohome.
110 std::string sync_user_id; // Used to identify user in manager's sync data.
111 std::string password;
112 std::string mount_hash;
113 std::string master_key;
116 bool token_succesfully_written;
117 CreationType creation_type;
118 Profile* manager_profile;
119 scoped_ptr<ManagedUserRegistrationUtility> registration_utility;
122 // ManagedUserAuthenticator::StatusConsumer overrides.
123 virtual void OnAuthenticationFailure(
124 ManagedUserAuthenticator::AuthState error) OVERRIDE;
125 virtual void OnMountSuccess(const std::string& mount_hash) OVERRIDE;
126 virtual void OnAddKeySuccess() OVERRIDE;
128 void CreationTimedOut();
129 void RegistrationCallback(const GoogleServiceAuthError& error,
130 const std::string& token);
132 void TokenFetched(const std::string& token);
134 // Completion callback for StoreManagedUserFiles method.
135 // Called on the UI thread.
136 void OnManagedUserFilesStored(bool success);
138 // Pointer to the current instance of the controller to be used by
140 static LocallyManagedUserCreationController* current_controller_;
142 StatusConsumer* consumer_;
144 scoped_refptr<ManagedUserAuthenticator> authenticator_;
146 // Creation context. Not null while creating new LMU.
147 scoped_ptr<UserCreationContext> creation_context_;
149 // Timer for showing warning if creation process takes too long.
150 base::OneShotTimer<LocallyManagedUserCreationController> timeout_timer_;
152 // Factory of callbacks.
153 base::WeakPtrFactory<LocallyManagedUserCreationController> weak_factory_;
155 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUserCreationController);
158 } // namespace chromeos
160 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_