Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / supervised_user_create_confirm_handler.cc
blob9c889a9b5a4d33438bb7c89b53393b246a61e4c8
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"
7 #include "base/bind.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"
30 namespace options {
32 // ProfileUpdateObserver------------------------------------------------------
34 class SupervisedUserCreateConfirmHandler::ProfileUpdateObserver
35 : public ProfileInfoCacheObserver {
36 public:
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_);
47 private:
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",
57 *profile_path_value);
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)
65 return;
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",
72 *profile_path_value,
73 base::StringValue(new_profile_name));
76 // Weak.
77 ProfileInfoCache* profile_info_cache_;
79 // Weak; owns us.
80 SupervisedUserCreateConfirmHandler* create_confirm_handler_;
82 // Manages any sources we're observing, ensuring that they're all removed
83 // on destruction.
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()));
115 if (signin) {
116 localized_strings->SetString("custodianEmail",
117 signin->GetAuthenticatedAccountInfo().email);
118 } else {
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
127 // by the page's JS.
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) {
146 DCHECK(args);
147 const base::Value* file_path_value;
148 if (!args->Get(0, &file_path_value))
149 return;
151 base::FilePath profile_file_path;
152 if (!base::GetValueAsFilePath(*file_path_value, &profile_file_path))
153 return;
155 Profile* profile = g_browser_process->profile_manager()->
156 GetProfileByPath(profile_file_path);
157 DCHECK(profile);
159 Browser* browser =
160 chrome::FindBrowserWithWebContents(web_ui()->GetWebContents());
161 chrome::HostDesktopType desktop_type = chrome::HOST_DESKTOP_TYPE_NATIVE;
162 if (browser)
163 desktop_type = browser->host_desktop_type();
165 profiles::FindOrCreateNewWindowForProfile(
166 profile,
167 chrome::startup::IS_PROCESS_STARTUP,
168 chrome::startup::IS_FIRST_RUN,
169 desktop_type,
170 false);
173 } // namespace options