1 // Copyright 2014 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 #include "chrome/browser/ui/webui/options/supervised_user_create_confirm_handler.h"
8 #include "base/files/file_path.h"
9 #include "base/scoped_observer.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/value_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_info_cache.h"
16 #include "chrome/browser/profiles/profile_info_cache_observer.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/profiles/profile_window.h"
19 #include "chrome/browser/signin/signin_manager_factory.h"
20 #include "chrome/browser/ui/browser_finder.h"
21 #include "chrome/browser/ui/host_desktop.h"
22 #include "chrome/browser/ui/startup/startup_types.h"
23 #include "chrome/common/url_constants.h"
24 #include "chrome/grit/generated_resources.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "components/signin/core/browser/signin_manager_base.h"
27 #include "content/public/browser/web_ui.h"
28 #include "ui/base/l10n/l10n_util.h"
32 // ProfileUpdateObserver------------------------------------------------------
34 class SupervisedUserCreateConfirmHandler::ProfileUpdateObserver
35 : public ProfileInfoCacheObserver
{
37 ProfileUpdateObserver(ProfileInfoCache
* profile_info_cache
,
38 SupervisedUserCreateConfirmHandler
* handler
)
39 : profile_info_cache_(profile_info_cache
),
40 create_confirm_handler_(handler
),
41 scoped_observer_(this) {
42 DCHECK(profile_info_cache_
);
43 DCHECK(create_confirm_handler_
);
44 scoped_observer_
.Add(profile_info_cache_
);
48 // ProfileInfoCacheObserver implementation:
49 // Forward possibly relevant changes to the dialog, which will check the
50 // affected profile and update or close as needed.
51 void OnProfileWasRemoved(const base::FilePath
& profile_path
,
52 const base::string16
& profile_name
) override
{
53 scoped_ptr
<base::StringValue
> profile_path_value(
54 base::CreateFilePathValue(profile_path
));
55 create_confirm_handler_
->web_ui()->CallJavascriptFunction(
56 "SupervisedUserCreateConfirmOverlay.onDeletedProfile",
60 void OnProfileNameChanged(const base::FilePath
& profile_path
,
61 const base::string16
& old_profile_name
) override
{
62 size_t profile_index
=
63 profile_info_cache_
->GetIndexOfProfileWithPath(profile_path
);
64 if (profile_index
== std::string::npos
)
66 base::string16 new_profile_name
=
67 profile_info_cache_
->GetNameOfProfileAtIndex(profile_index
);
68 scoped_ptr
<base::StringValue
> profile_path_value(
69 base::CreateFilePathValue(profile_path
));
70 create_confirm_handler_
->web_ui()->CallJavascriptFunction(
71 "SupervisedUserCreateConfirmOverlay.onUpdatedProfileName",
73 base::StringValue(new_profile_name
));
77 ProfileInfoCache
* profile_info_cache_
;
80 SupervisedUserCreateConfirmHandler
* create_confirm_handler_
;
82 // Manages any sources we're observing, ensuring that they're all removed
84 ScopedObserver
<ProfileInfoCache
, ProfileUpdateObserver
> scoped_observer_
;
86 DISALLOW_COPY_AND_ASSIGN(ProfileUpdateObserver
);
90 // SupervisedUserCreateConfirmHandler-----------------------------------------
92 SupervisedUserCreateConfirmHandler::SupervisedUserCreateConfirmHandler() {
93 profile_info_cache_observer_
.reset(
94 new SupervisedUserCreateConfirmHandler::ProfileUpdateObserver(
95 &g_browser_process
->profile_manager()->GetProfileInfoCache(), this));
98 SupervisedUserCreateConfirmHandler::~SupervisedUserCreateConfirmHandler() {
101 void SupervisedUserCreateConfirmHandler::GetLocalizedValues(
102 base::DictionaryValue
* localized_strings
) {
103 DCHECK(localized_strings
);
105 static OptionsStringResource resources
[] = {
106 { "supervisedUserCreatedTitle", IDS_LEGACY_SUPERVISED_USER_CREATED_TITLE
},
107 { "supervisedUserCreatedDone",
108 IDS_LEGACY_SUPERVISED_USER_CREATED_DONE_BUTTON
},
109 { "supervisedUserCreatedSwitch",
110 IDS_LEGACY_SUPERVISED_USER_CREATED_SWITCH_BUTTON
},
113 SigninManagerBase
* signin
=
114 SigninManagerFactory::GetForProfile(Profile::FromWebUI(web_ui()));
116 localized_strings
->SetString("custodianEmail",
117 signin
->GetAuthenticatedAccountInfo().email
);
119 localized_strings
->SetString("custodianEmail", std::string());
122 base::string16 supervised_user_dashboard_url
=
123 base::ASCIIToUTF16(chrome::kLegacySupervisedUserManagementURL
);
124 base::string16 supervised_user_dashboard_display
=
125 base::ASCIIToUTF16(chrome::kLegacySupervisedUserManagementDisplayURL
);
126 // The first two substitution parameters need to remain; they will be filled
128 localized_strings
->SetString("supervisedUserCreatedText",
129 l10n_util::GetStringFUTF16(IDS_LEGACY_SUPERVISED_USER_CREATED_TEXT
,
130 base::ASCIIToUTF16("$1"),
131 base::ASCIIToUTF16("$2"),
132 supervised_user_dashboard_url
,
133 supervised_user_dashboard_display
));
135 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
138 void SupervisedUserCreateConfirmHandler::RegisterMessages() {
139 web_ui()->RegisterMessageCallback("switchToProfile",
140 base::Bind(&SupervisedUserCreateConfirmHandler::SwitchToProfile
,
141 base::Unretained(this)));
144 void SupervisedUserCreateConfirmHandler::SwitchToProfile(
145 const base::ListValue
* args
) {
147 const base::Value
* file_path_value
;
148 if (!args
->Get(0, &file_path_value
))
151 base::FilePath profile_file_path
;
152 if (!base::GetValueAsFilePath(*file_path_value
, &profile_file_path
))
155 Profile
* profile
= g_browser_process
->profile_manager()->
156 GetProfileByPath(profile_file_path
);
160 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
161 chrome::HostDesktopType desktop_type
= chrome::HOST_DESKTOP_TYPE_NATIVE
;
163 desktop_type
= browser
->host_desktop_type();
165 profiles::FindOrCreateNewWindowForProfile(
167 chrome::startup::IS_PROCESS_STARTUP
,
168 chrome::startup::IS_FIRST_RUN
,
173 } // namespace options