create_profile_handler.h revision 116680a4aac90f2aa7413d9095a592090648e557
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
17namespace base {
18class DictionaryValue;
19class ListValue;
20}
21
22class SupervisedUserRegistrationUtility;
23
24namespace options {
25
26// Handler for the 'create profile' overlay.
27class 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 supervised
73  //      (optional, boolean)
74  //   4: a string representing the supervised user ID.
75  void CreateProfile(const base::ListValue* args);
76
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,
85                        Profile* profile,
86                        Profile::CreateStatus status);
87
88  void HandleProfileCreationSuccess(bool create_shortcut,
89                                    chrome::HostDesktopType desktop_type,
90                                    const std::string& supervised_user_id,
91                                    Profile* profile);
92
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);
99
100  // Called back with the result of the supervised user registration.
101  void OnSupervisedUserRegistered(bool create_shortcut,
102                                  chrome::HostDesktopType desktop_type,
103                                  Profile* profile,
104                                  const GoogleServiceAuthError& error);
105
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,
110                                    Profile* profile);
111
112  // Updates the UI to show an error when creating a profile.
113  void ShowProfileCreationError(Profile* profile, const base::string16& error);
114
115  // Updates the UI to show a non-fatal warning when creating a profile.
116  void ShowProfileCreationWarning(const base::string16& warning);
117
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);
123
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);
129
130  // Records UMA histograms relevant to profile creation.
131  void RecordProfileCreationMetrics(Profile::CreateStatus status);
132
133  // Records UMA histograms relevant to supervised user profiles
134  // creation and registration.
135  void RecordSupervisedProfileCreationMetrics(
136      GoogleServiceAuthError::State error_state);
137
138  base::string16 GetProfileCreationErrorMessage(
139      ProfileCreationErrorType error) const;
140  std::string GetJavascriptMethodName(ProfileCreationStatus status) const;
141
142  bool IsValidExistingSupervisedUserId(
143      const std::string& existing_supervised_user_id) const;
144
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_;
149
150  // Used to track how long profile creation takes.
151  base::TimeTicks profile_creation_start_time_;
152
153  scoped_ptr<SupervisedUserRegistrationUtility>
154      supervised_user_registration_utility_;
155
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_;
159
160  base::WeakPtrFactory<CreateProfileHandler> weak_ptr_factory_;
161
162  DISALLOW_COPY_AND_ASSIGN(CreateProfileHandler);
163};
164
165}  // namespace options
166
167#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CREATE_PROFILE_HANDLER_H_
168