1 // Copyright 2013 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_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_ 6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_ 7 8 #include "base/memory/weak_ptr.h" 9 #include "base/time/time.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile_window.h" 12 #include "chrome/browser/ui/host_desktop.h" 13 #include "chrome/browser/ui/webui/options/options_ui.h" 14 #include "google_apis/gaia/google_service_auth_error.h" 15 16 17 namespace base { 18 class DictionaryValue; 19 class ListValue; 20 } 21 22 class ManagedUserRegistrationUtility; 23 24 namespace options { 25 26 // Handler for the 'create profile' overlay. 27 class CreateProfileHandler: public OptionsPageUIHandler { 28 public: 29 CreateProfileHandler(); 30 virtual ~CreateProfileHandler(); 31 32 // OptionsPageUIHandler implementation. 33 virtual void GetLocalizedValues( 34 base::DictionaryValue* localized_strings) OVERRIDE; 35 36 // WebUIMessageHandler implementation. 37 virtual void RegisterMessages() OVERRIDE; 38 39 private: 40 // Represents the final profile creation status. It is used to map 41 // the status to the javascript method to be called. 42 enum ProfileCreationStatus { 43 PROFILE_CREATION_SUCCESS, 44 PROFILE_CREATION_ERROR, 45 }; 46 47 // Represents errors that could occur during a profile creation. 48 // It is used to map error types to messages that will be displayed 49 // to the user. 50 enum ProfileCreationErrorType { 51 REMOTE_ERROR, 52 LOCAL_ERROR, 53 SIGNIN_ERROR 54 }; 55 56 // Represents the type of the in progress profile creation operation. 57 // It is used to map the type of the profile creation operation to the 58 // correct UMA metric name. 59 enum ProfileCreationOperationType { 60 SUPERVISED_PROFILE_CREATION, 61 SUPERVISED_PROFILE_IMPORT, 62 NON_SUPERVISED_PROFILE_CREATION, 63 NO_CREATION_IN_PROGRESS 64 }; 65 66 // Asynchronously creates and initializes a new profile. 67 // The arguments are as follows: 68 // 0: name (string) 69 // 1: icon (string) 70 // 2: a flag stating whether we should create a profile desktop shortcut 71 // (optional, boolean) 72 // 3: a flag stating whether the user should be managed (optional, boolean) 73 // 4: a string representing the managed user ID. 74 void CreateProfile(const base::ListValue* args); 75 76 // If a local error occurs during profile creation, then show an appropriate 77 // error message. However, if profile creation succeeded and the 78 // profile being created/imported is a supervised user profile, 79 // then proceed with the registration step. Otherwise, update the UI 80 // as the final task after a new profile has been created. 81 void OnProfileCreated(bool create_shortcut, 82 chrome::HostDesktopType desktop_type, 83 const std::string& managed_user_id, 84 Profile* profile, 85 Profile::CreateStatus status); 86 87 void HandleProfileCreationSuccess(bool create_shortcut, 88 chrome::HostDesktopType desktop_type, 89 const std::string& managed_user_id, 90 Profile* profile); 91 92 // After a new managed-user profile has been created, registers the user with 93 // the management server. 94 void RegisterManagedUser(bool create_shortcut, 95 chrome::HostDesktopType desktop_type, 96 const std::string& managed_user_id, 97 Profile* new_profile); 98 99 // Called back with the result of the managed user registration. 100 void OnManagedUserRegistered(bool create_shortcut, 101 chrome::HostDesktopType desktop_type, 102 Profile* profile, 103 const GoogleServiceAuthError& error); 104 105 // Creates desktop shortcut and updates the UI to indicate success 106 // when creating a profile. 107 void CreateShortcutAndShowSuccess(bool create_shortcut, 108 chrome::HostDesktopType desktop_type, 109 Profile* profile); 110 111 // Updates the UI to show an error when creating a profile. 112 void ShowProfileCreationError(Profile* profile, const base::string16& error); 113 114 // Updates the UI to show a non-fatal warning when creating a profile. 115 void ShowProfileCreationWarning(const base::string16& warning); 116 117 // Cancels creation of a managed-user profile currently in progress, as 118 // indicated by profile_path_being_created_, removing the object and files 119 // and canceling managed-user registration. This is the handler for the 120 // "cancelCreateProfile" message. |args| is not used. 121 void HandleCancelProfileCreation(const base::ListValue* args); 122 123 // Internal implementation. This may safely be called whether profile creation 124 // or registration is in progress or not. |user_initiated| should be true if 125 // the cancellation was deliberately requested by the user, and false if it 126 // was caused implicitly, e.g. by shutting down the browser. 127 void CancelProfileRegistration(bool user_initiated); 128 129 // Records UMA histograms relevant to profile creation. 130 void RecordProfileCreationMetrics(Profile::CreateStatus status); 131 132 // Records UMA histograms relevant to supervised user profiles 133 // creation and registration. 134 void RecordSupervisedProfileCreationMetrics( 135 GoogleServiceAuthError::State error_state); 136 137 base::string16 GetProfileCreationErrorMessage( 138 ProfileCreationErrorType error) const; 139 std::string GetJavascriptMethodName(ProfileCreationStatus status) const; 140 141 bool IsValidExistingManagedUserId( 142 const std::string& existing_managed_user_id) const; 143 144 // Used to allow cancelling a profile creation (particularly a managed-user 145 // registration) in progress. Set when profile creation is begun, and 146 // cleared when all the callbacks have been run and creation is complete. 147 base::FilePath profile_path_being_created_; 148 149 // Used to track how long profile creation takes. 150 base::TimeTicks profile_creation_start_time_; 151 152 scoped_ptr<ManagedUserRegistrationUtility> managed_user_registration_utility_; 153 154 // Indicates the type of the in progress profile creation operation. 155 // The value is only relevant while we are creating/importing a profile. 156 ProfileCreationOperationType profile_creation_type_; 157 158 base::WeakPtrFactory<CreateProfileHandler> weak_ptr_factory_; 159 160 DISALLOW_COPY_AND_ASSIGN(CreateProfileHandler); 161 }; 162 163 } // namespace options 164 165 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_ 166