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.
5 #ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_
6 #define CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_
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"
18 class DictionaryValue;
22 class SupervisedUserRegistrationUtility;
26 // Handler for the 'create profile' overlay.
27 class CreateProfileHandler: public OptionsPageUIHandler {
29 CreateProfileHandler();
30 virtual ~CreateProfileHandler();
32 // OptionsPageUIHandler implementation.
33 virtual void GetLocalizedValues(
34 base::DictionaryValue* localized_strings) OVERRIDE;
36 // WebUIMessageHandler implementation.
37 virtual void RegisterMessages() OVERRIDE;
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,
47 // Represents errors that could occur during a profile creation.
48 // It is used to map error types to messages that will be displayed
50 enum ProfileCreationErrorType {
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
66 // Asynchronously creates and initializes a new profile.
67 // The arguments are as follows:
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 supervised
73 // (optional, boolean)
74 // 4: a string representing the supervised user ID.
75 void CreateProfile(const base::ListValue* args);
77 // If a local error occurs during profile creation, then show an appropriate
78 // error message. However, if profile creation succeeded and the
79 // profile being created/imported is a supervised user profile,
80 // then proceed with the registration step. Otherwise, update the UI
81 // as the final task after a new profile has been created.
82 void OnProfileCreated(bool create_shortcut,
83 chrome::HostDesktopType desktop_type,
84 const std::string& supervised_user_id,
86 Profile::CreateStatus status);
88 void HandleProfileCreationSuccess(bool create_shortcut,
89 chrome::HostDesktopType desktop_type,
90 const std::string& supervised_user_id,
93 // After a new supervised-user profile has been created, registers the user
94 // with the management server.
95 void RegisterSupervisedUser(bool create_shortcut,
96 chrome::HostDesktopType desktop_type,
97 const std::string& supervised_user_id,
98 Profile* new_profile);
100 // Called back with the result of the supervised user registration.
101 void OnSupervisedUserRegistered(bool create_shortcut,
102 chrome::HostDesktopType desktop_type,
104 const GoogleServiceAuthError& error);
106 // Creates desktop shortcut and updates the UI to indicate success
107 // when creating a profile.
108 void CreateShortcutAndShowSuccess(bool create_shortcut,
109 chrome::HostDesktopType desktop_type,
112 // Updates the UI to show an error when creating a profile.
113 void ShowProfileCreationError(Profile* profile, const base::string16& error);
115 // Updates the UI to show a non-fatal warning when creating a profile.
116 void ShowProfileCreationWarning(const base::string16& warning);
118 // Cancels creation of a supervised-user profile currently in progress, as
119 // indicated by profile_path_being_created_, removing the object and files
120 // and canceling supervised-user registration. This is the handler for the
121 // "cancelCreateProfile" message. |args| is not used.
122 void HandleCancelProfileCreation(const base::ListValue* args);
124 // Internal implementation. This may safely be called whether profile creation
125 // or registration is in progress or not. |user_initiated| should be true if
126 // the cancellation was deliberately requested by the user, and false if it
127 // was caused implicitly, e.g. by shutting down the browser.
128 void CancelProfileRegistration(bool user_initiated);
130 // Records UMA histograms relevant to profile creation.
131 void RecordProfileCreationMetrics(Profile::CreateStatus status);
133 // Records UMA histograms relevant to supervised user profiles
134 // creation and registration.
135 void RecordSupervisedProfileCreationMetrics(
136 GoogleServiceAuthError::State error_state);
138 base::string16 GetProfileCreationErrorMessage(
139 ProfileCreationErrorType error) const;
140 std::string GetJavascriptMethodName(ProfileCreationStatus status) const;
142 bool IsValidExistingSupervisedUserId(
143 const std::string& existing_supervised_user_id) const;
145 // Used to allow cancelling a profile creation (particularly a supervised-user
146 // registration) in progress. Set when profile creation is begun, and
147 // cleared when all the callbacks have been run and creation is complete.
148 base::FilePath profile_path_being_created_;
150 // Used to track how long profile creation takes.
151 base::TimeTicks profile_creation_start_time_;
153 scoped_ptr<SupervisedUserRegistrationUtility>
154 supervised_user_registration_utility_;
156 // Indicates the type of the in progress profile creation operation.
157 // The value is only relevant while we are creating/importing a profile.
158 ProfileCreationOperationType profile_creation_type_;
160 base::WeakPtrFactory<CreateProfileHandler> weak_ptr_factory_;
162 DISALLOW_COPY_AND_ASSIGN(CreateProfileHandler);
165 } // namespace options
167 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_