Home | History | Annotate | Download | only in managed
      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   // 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;
     41 
     42   enum ErrorCode {
     43     NO_ERROR,
     44     CRYPTOHOME_NO_MOUNT,
     45     CRYPTOHOME_FAILED_MOUNT,
     46     CRYPTOHOME_FAILED_TPM,
     47     CLOUD_SERVER_ERROR,
     48     TOKEN_WRITE_FAILED,
     49   };
     50 
     51   class StatusConsumer {
     52    public:
     53     virtual ~StatusConsumer();
     54 
     55     virtual void OnCreationError(ErrorCode code) = 0;
     56     virtual void OnLongCreationWarning() = 0;
     57     virtual void OnCreationTimeout() = 0;
     58     virtual void OnCreationSuccess() = 0;
     59   };
     60 
     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();
     67 
     68   // Returns the current locally managed user controller if it has been created.
     69   static LocallyManagedUserCreationController* current_controller() {
     70     return current_controller_;
     71   }
     72 
     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 base::string16& display_name,
     77                      const std::string& password,
     78                      int avatar_index);
     79 
     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 base::string16& display_name,
     84                    const std::string& password,
     85                    int avatar_index,
     86                    const std::string& sync_id,
     87                    const std::string& master_key);
     88   void SetManagerProfile(Profile* manager_profile);
     89   void StartCreation();
     90   void CancelCreation();
     91   void FinishCreation();
     92   std::string GetManagedUserId();
     93 
     94  private:
     95   // Indicates if we create new user, or import an existing one.
     96   enum CreationType {
     97     NEW_USER,
     98     USER_IMPORT,
     99   };
    100 
    101   // Contains information necessary for new user creation.
    102   struct UserCreationContext {
    103     UserCreationContext();
    104     ~UserCreationContext();
    105 
    106     base::string16 display_name;
    107     int avatar_index;
    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;
    114     bool token_acquired;
    115     std::string token;
    116     bool token_succesfully_written;
    117     CreationType creation_type;
    118     Profile* manager_profile;
    119     scoped_ptr<ManagedUserRegistrationUtility> registration_utility;
    120   };
    121 
    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;
    127 
    128   void CreationTimedOut();
    129   void RegistrationCallback(const GoogleServiceAuthError& error,
    130                             const std::string& token);
    131 
    132   void TokenFetched(const std::string& token);
    133 
    134   // Completion callback for StoreManagedUserFiles method.
    135   // Called on the UI thread.
    136   void OnManagedUserFilesStored(bool success);
    137 
    138   // Pointer to the current instance of the controller to be used by
    139   // automation tests.
    140   static LocallyManagedUserCreationController* current_controller_;
    141 
    142   StatusConsumer* consumer_;
    143 
    144   scoped_refptr<ManagedUserAuthenticator> authenticator_;
    145 
    146   // Creation context. Not null while creating new LMU.
    147   scoped_ptr<UserCreationContext> creation_context_;
    148 
    149   // Timer for showing warning if creation process takes too long.
    150   base::OneShotTimer<LocallyManagedUserCreationController> timeout_timer_;
    151 
    152   // Factory of callbacks.
    153   base::WeakPtrFactory<LocallyManagedUserCreationController> weak_factory_;
    154 
    155   DISALLOW_COPY_AND_ASSIGN(LocallyManagedUserCreationController);
    156 };
    157 
    158 }  // namespace chromeos
    159 
    160 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_MANAGED_LOCALLY_MANAGED_USER_CREATION_CONTROLLER_H_
    161