Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / managed / locally_managed_user_creation_controller.h
blobd0c227cd417178ad12d3a2480d3db6added27635
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_
8 #include <string>
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 "base/values.h"
16 #include "chrome/browser/chromeos/login/managed/managed_user_authenticator.h"
17 #include "chrome/browser/managed_mode/managed_user_registration_utility.h"
19 class Profile;
21 namespace chromeos {
23 // LocallyManagedUserCreationController is used to locally managed user
24 // creation.
25 // LMU Creation process:
26 // 0. Manager is logged in
27 // 1. Generate ID for new LMU
28 // 2. Start "transaction" in Local State.
29 // 3. Create local cryptohome (errors could arise)
30 // 4. Create user in cloud (errors could arise)
31 // 5. Store cloud token in cryptohome (actually, error could arise).
32 // 6. Mark "transaction" as completed.
33 // 7. End manager session.
35 class LocallyManagedUserCreationController
36 : public ManagedUserAuthenticator::AuthStatusConsumer {
37 public:
38 // This constant is used to indicate that user does not have one of default
39 // avatars: either he has no chromeos avatar at all, or has an external
40 // image as an avatar.
41 static const int kDummyAvatarIndex;
43 enum ErrorCode {
44 NO_ERROR,
45 CRYPTOHOME_NO_MOUNT,
46 CRYPTOHOME_FAILED_MOUNT,
47 CRYPTOHOME_FAILED_TPM,
48 CLOUD_SERVER_ERROR,
49 TOKEN_WRITE_FAILED,
52 class StatusConsumer {
53 public:
54 virtual ~StatusConsumer();
56 virtual void OnCreationError(ErrorCode code) = 0;
57 virtual void OnLongCreationWarning() = 0;
58 virtual void OnCreationTimeout() = 0;
59 virtual void OnCreationSuccess() = 0;
62 // All UI initialization is deferred till Init() call.
63 // |Consumer| is not owned by controller, and it is expected that it wouldn't
64 // be deleted before LocallyManagedUserCreationController.
65 LocallyManagedUserCreationController(StatusConsumer* consumer,
66 const std::string& manager_id);
67 virtual ~LocallyManagedUserCreationController();
69 // Returns the current locally managed user controller if it has been created.
70 static LocallyManagedUserCreationController* current_controller() {
71 return current_controller_;
74 // Set up controller for creating new supervised user with |display_name|,
75 // |password| and avatar indexed by |avatar_index|. StartCreation() have to
76 // be called to actually start creating user.
77 void SetUpCreation(const base::string16& display_name,
78 const std::string& password,
79 int avatar_index);
81 // Configures and initiates importing existing supervised user to this device.
82 // Existing user is identified by |sync_id|, has |display_name|, |password|,
83 // |avatar_index|. The master key for cryptohome is a |master_key|.
84 void StartImport(const base::string16& display_name,
85 const std::string& password,
86 int avatar_index,
87 const std::string& sync_id,
88 const std::string& master_key);
89 void SetManagerProfile(Profile* manager_profile);
90 void StartCreation();
91 void CancelCreation();
92 void FinishCreation();
93 std::string GetManagedUserId();
95 private:
96 // Indicates if we create new user, or import an existing one.
97 enum CreationType {
98 NEW_USER,
99 USER_IMPORT,
102 // Contains information necessary for new user creation.
103 struct UserCreationContext {
104 UserCreationContext();
105 ~UserCreationContext();
107 base::string16 display_name;
108 int avatar_index;
109 std::string manager_id;
110 std::string local_user_id; // Used to identify cryptohome.
111 std::string sync_user_id; // Used to identify user in manager's sync data.
112 std::string password;
113 std::string mount_hash;
114 std::string master_key;
115 bool token_acquired;
116 std::string token;
117 bool token_succesfully_written;
118 CreationType creation_type;
119 base::DictionaryValue password_data;
120 Profile* manager_profile;
121 scoped_ptr<ManagedUserRegistrationUtility> registration_utility;
124 // ManagedUserAuthenticator::StatusConsumer overrides.
125 virtual void OnAuthenticationFailure(
126 ManagedUserAuthenticator::AuthState error) OVERRIDE;
127 virtual void OnMountSuccess(const std::string& mount_hash) OVERRIDE;
128 virtual void OnAddKeySuccess() OVERRIDE;
130 void CreationTimedOut();
131 void RegistrationCallback(const GoogleServiceAuthError& error,
132 const std::string& token);
134 void TokenFetched(const std::string& token);
136 // Completion callback for StoreManagedUserFiles method.
137 // Called on the UI thread.
138 void OnManagedUserFilesStored(bool success);
140 // Pointer to the current instance of the controller to be used by
141 // automation tests.
142 static LocallyManagedUserCreationController* current_controller_;
144 StatusConsumer* consumer_;
146 scoped_refptr<ManagedUserAuthenticator> authenticator_;
148 // Creation context. Not null while creating new LMU.
149 scoped_ptr<UserCreationContext> creation_context_;
151 // Timer for showing warning if creation process takes too long.
152 base::OneShotTimer<LocallyManagedUserCreationController> timeout_timer_;
154 // Factory of callbacks.
155 base::WeakPtrFactory<LocallyManagedUserCreationController> weak_factory_;
157 DISALLOW_COPY_AND_ASSIGN(LocallyManagedUserCreationController);
160 } // namespace chromeos
162 #endif // CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_