Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / create_profile_handler.h
blob6ee91ba07b6959eade3d60287905f834eb026a34
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"
17 namespace base {
18 class DictionaryValue;
19 class ListValue;
22 #if defined(ENABLE_SUPERVISED_USERS)
23 class SupervisedUserRegistrationUtility;
24 #endif
26 namespace options {
28 // Handler for the 'create profile' overlay.
29 class CreateProfileHandler: public OptionsPageUIHandler {
30 public:
31 CreateProfileHandler();
32 ~CreateProfileHandler() override;
34 // OptionsPageUIHandler implementation.
35 void GetLocalizedValues(base::DictionaryValue* localized_strings) override;
37 // WebUIMessageHandler implementation.
38 void RegisterMessages() override;
40 private:
41 // Represents the final profile creation status. It is used to map
42 // the status to the javascript method to be called.
43 enum ProfileCreationStatus {
44 PROFILE_CREATION_SUCCESS,
45 PROFILE_CREATION_ERROR,
48 // Represents the type of the in progress profile creation operation.
49 // It is used to map the type of the profile creation operation to the
50 // correct UMA metric name.
51 enum ProfileCreationOperationType {
52 #if defined(ENABLE_SUPERVISED_USERS)
53 SUPERVISED_PROFILE_CREATION,
54 SUPERVISED_PROFILE_IMPORT,
55 #endif
56 NON_SUPERVISED_PROFILE_CREATION,
57 NO_CREATION_IN_PROGRESS
60 // Asynchronously creates and initializes a new profile.
61 // The arguments are as follows:
62 // 0: name (string)
63 // 1: icon (string)
64 // 2: a flag stating whether we should create a profile desktop shortcut
65 // (optional, boolean)
66 // 3: a flag stating whether the user should be supervised
67 // (optional, boolean)
68 // 4: a string representing the supervised user ID.
69 void CreateProfile(const base::ListValue* args);
71 // If a local error occurs during profile creation, then show an appropriate
72 // error message. However, if profile creation succeeded and the
73 // profile being created/imported is a supervised user profile,
74 // then proceed with the registration step. Otherwise, update the UI
75 // as the final task after a new profile has been created.
76 void OnProfileCreated(bool create_shortcut,
77 chrome::HostDesktopType desktop_type,
78 const std::string& supervised_user_id,
79 Profile* profile,
80 Profile::CreateStatus status);
82 void HandleProfileCreationSuccess(bool create_shortcut,
83 chrome::HostDesktopType desktop_type,
84 const std::string& supervised_user_id,
85 Profile* profile);
87 // Creates desktop shortcut and updates the UI to indicate success
88 // when creating a profile.
89 void CreateShortcutAndShowSuccess(bool create_shortcut,
90 chrome::HostDesktopType desktop_type,
91 Profile* profile);
93 // Updates the UI to show an error when creating a profile.
94 void ShowProfileCreationError(Profile* profile, const base::string16& error);
96 // Updates the UI to show a non-fatal warning when creating a profile.
97 void ShowProfileCreationWarning(const base::string16& warning);
99 // Records UMA histograms relevant to profile creation.
100 void RecordProfileCreationMetrics(Profile::CreateStatus status);
102 base::string16 GetProfileCreationErrorMessageLocal() const;
103 #if defined(ENABLE_SUPERVISED_USERS)
104 // The following error messages only apply to supervised profiles.
105 base::string16 GetProfileCreationErrorMessageRemote() const;
106 base::string16 GetProfileCreationErrorMessageSignin() const;
107 #endif
109 std::string GetJavascriptMethodName(ProfileCreationStatus status) const;
111 // Used to allow cancelling a profile creation (particularly a supervised-user
112 // registration) in progress. Set when profile creation is begun, and
113 // cleared when all the callbacks have been run and creation is complete.
114 base::FilePath profile_path_being_created_;
116 // Used to track how long profile creation takes.
117 base::TimeTicks profile_creation_start_time_;
119 // Indicates the type of the in progress profile creation operation.
120 // The value is only relevant while we are creating/importing a profile.
121 ProfileCreationOperationType profile_creation_type_;
123 #if defined(ENABLE_SUPERVISED_USERS)
124 // Extracts the supervised user ID from the args passed into CreateProfile,
125 // sets |profile_creation_type_| if necessary, and returns true if the
126 // supervised user id specified in |args| are valid.
127 bool ProcessSupervisedCreateProfileArgs(const base::ListValue* args,
128 std::string* supervised_user_id);
130 // Cancels creation of a supervised-user profile currently in progress, as
131 // indicated by profile_path_being_created_, removing the object and files
132 // and canceling supervised-user registration. This is the handler for the
133 // "cancelCreateProfile" message. |args| is not used.
134 void HandleCancelProfileCreation(const base::ListValue* args);
136 // Internal implementation. This may safely be called whether profile creation
137 // or registration is in progress or not. |user_initiated| should be true if
138 // the cancellation was deliberately requested by the user, and false if it
139 // was caused implicitly, e.g. by shutting down the browser.
140 void CancelProfileRegistration(bool user_initiated);
142 // After a new supervised-user profile has been created, registers the user
143 // with the management server.
144 void RegisterSupervisedUser(bool create_shortcut,
145 chrome::HostDesktopType desktop_type,
146 const std::string& managed_user_id,
147 Profile* new_profile);
149 // Called back with the result of the supervised user registration.
150 void OnSupervisedUserRegistered(bool create_shortcut,
151 chrome::HostDesktopType desktop_type,
152 Profile* profile,
153 const GoogleServiceAuthError& error);
155 // Records UMA histograms relevant to supervised user profiles
156 // creation and registration.
157 void RecordSupervisedProfileCreationMetrics(
158 GoogleServiceAuthError::State error_state);
160 bool IsValidExistingSupervisedUserId(
161 const std::string& existing_supervised_user_id) const;
163 scoped_ptr<SupervisedUserRegistrationUtility>
164 supervised_user_registration_utility_;
165 #endif
167 base::WeakPtrFactory<CreateProfileHandler> weak_ptr_factory_;
169 DISALLOW_COPY_AND_ASSIGN(CreateProfileHandler);
172 } // namespace options
174 #endif // CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_