Componentize SigninManager.
[chromium-blink-merge.git] / chrome / browser / ui / webui / flags_ui.cc
blobaca6d1b182e79c9094da60492c2aab9fdf985e78
1 // Copyright (c) 2012 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/flags_ui.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/about_flags.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/pref_service_flags_storage.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
26 #include "content/public/browser/web_ui_data_source.h"
27 #include "content/public/browser/web_ui_message_handler.h"
28 #include "grit/browser_resources.h"
29 #include "grit/chromium_strings.h"
30 #include "grit/generated_resources.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/resource/resource_bundle.h"
35 #if defined(OS_CHROMEOS)
36 #include "base/sys_info.h"
37 #include "chrome/browser/chromeos/login/user_manager.h"
38 #include "chrome/browser/chromeos/settings/cros_settings.h"
39 #include "chrome/browser/chromeos/settings/owner_flags_storage.h"
40 #include "chromeos/dbus/dbus_thread_manager.h"
41 #include "chromeos/dbus/session_manager_client.h"
42 #include "components/user_prefs/pref_registry_syncable.h"
43 #endif
45 using content::WebContents;
46 using content::WebUIMessageHandler;
48 namespace {
50 content::WebUIDataSource* CreateFlagsUIHTMLSource() {
51 content::WebUIDataSource* source =
52 content::WebUIDataSource::Create(chrome::kChromeUIFlagsHost);
54 source->SetUseJsonJSFormatV2();
55 source->AddLocalizedString("flagsLongTitle", IDS_FLAGS_LONG_TITLE);
56 source->AddLocalizedString("flagsTableTitle", IDS_FLAGS_TABLE_TITLE);
57 source->AddLocalizedString("flagsNoExperimentsAvailable",
58 IDS_FLAGS_NO_EXPERIMENTS_AVAILABLE);
59 source->AddLocalizedString("flagsWarningHeader", IDS_FLAGS_WARNING_HEADER);
60 source->AddLocalizedString("flagsBlurb", IDS_FLAGS_WARNING_TEXT);
61 source->AddLocalizedString("channelPromoBeta",
62 IDS_FLAGS_PROMOTE_BETA_CHANNEL);
63 source->AddLocalizedString("channelPromoDev", IDS_FLAGS_PROMOTE_DEV_CHANNEL);
64 source->AddLocalizedString("flagsUnsupportedTableTitle",
65 IDS_FLAGS_UNSUPPORTED_TABLE_TITLE);
66 source->AddLocalizedString("flagsNoUnsupportedExperiments",
67 IDS_FLAGS_NO_UNSUPPORTED_EXPERIMENTS);
68 source->AddLocalizedString("flagsNotSupported", IDS_FLAGS_NOT_AVAILABLE);
69 source->AddLocalizedString("flagsRestartNotice", IDS_FLAGS_RELAUNCH_NOTICE);
70 source->AddLocalizedString("flagsRestartButton", IDS_FLAGS_RELAUNCH_BUTTON);
71 source->AddLocalizedString("resetAllButton", IDS_FLAGS_RESET_ALL_BUTTON);
72 source->AddLocalizedString("disable", IDS_FLAGS_DISABLE);
73 source->AddLocalizedString("enable", IDS_FLAGS_ENABLE);
75 #if defined(OS_CHROMEOS)
76 if (!chromeos::UserManager::Get()->IsCurrentUserOwner() &&
77 base::SysInfo::IsRunningOnChromeOS()) {
78 // Set the strings to show which user can actually change the flags.
79 std::string owner;
80 chromeos::CrosSettings::Get()->GetString(chromeos::kDeviceOwner, &owner);
81 source->AddString("ownerWarning",
82 l10n_util::GetStringFUTF16(IDS_SYSTEM_FLAGS_OWNER_ONLY,
83 base::UTF8ToUTF16(owner)));
84 } else {
85 // The warning will be only shown on ChromeOS, when the current user is not
86 // the owner.
87 source->AddString("ownerWarning", base::string16());
89 #endif
91 source->SetJsonPath("strings.js");
92 source->AddResourcePath("flags.js", IDR_FLAGS_JS);
93 source->SetDefaultResource(IDR_FLAGS_HTML);
94 return source;
97 ////////////////////////////////////////////////////////////////////////////////
99 // FlagsDOMHandler
101 ////////////////////////////////////////////////////////////////////////////////
103 // The handler for Javascript messages for the about:flags page.
104 class FlagsDOMHandler : public WebUIMessageHandler {
105 public:
106 FlagsDOMHandler() : access_(about_flags::kGeneralAccessFlagsOnly),
107 flags_experiments_requested_(false) {
109 virtual ~FlagsDOMHandler() {}
111 // Initializes the DOM handler with the provided flags storage and flags
112 // access. If there were flags experiments requested from javascript before
113 // this was called, it calls |HandleRequestFlagsExperiments| again.
114 void Init(about_flags::FlagsStorage* flags_storage,
115 about_flags::FlagAccess access);
117 // WebUIMessageHandler implementation.
118 virtual void RegisterMessages() OVERRIDE;
120 // Callback for the "requestFlagsExperiments" message.
121 void HandleRequestFlagsExperiments(const base::ListValue* args);
123 // Callback for the "enableFlagsExperiment" message.
124 void HandleEnableFlagsExperimentMessage(const base::ListValue* args);
126 // Callback for the "restartBrowser" message. Restores all tabs on restart.
127 void HandleRestartBrowser(const base::ListValue* args);
129 // Callback for the "resetAllFlags" message.
130 void HandleResetAllFlags(const base::ListValue* args);
132 private:
133 scoped_ptr<about_flags::FlagsStorage> flags_storage_;
134 about_flags::FlagAccess access_;
135 bool flags_experiments_requested_;
137 DISALLOW_COPY_AND_ASSIGN(FlagsDOMHandler);
140 void FlagsDOMHandler::RegisterMessages() {
141 web_ui()->RegisterMessageCallback("requestFlagsExperiments",
142 base::Bind(&FlagsDOMHandler::HandleRequestFlagsExperiments,
143 base::Unretained(this)));
144 web_ui()->RegisterMessageCallback("enableFlagsExperiment",
145 base::Bind(&FlagsDOMHandler::HandleEnableFlagsExperimentMessage,
146 base::Unretained(this)));
147 web_ui()->RegisterMessageCallback("restartBrowser",
148 base::Bind(&FlagsDOMHandler::HandleRestartBrowser,
149 base::Unretained(this)));
150 web_ui()->RegisterMessageCallback("resetAllFlags",
151 base::Bind(&FlagsDOMHandler::HandleResetAllFlags,
152 base::Unretained(this)));
155 void FlagsDOMHandler::Init(about_flags::FlagsStorage* flags_storage,
156 about_flags::FlagAccess access) {
157 flags_storage_.reset(flags_storage);
158 access_ = access;
160 if (flags_experiments_requested_)
161 HandleRequestFlagsExperiments(NULL);
164 void FlagsDOMHandler::HandleRequestFlagsExperiments(
165 const base::ListValue* args) {
166 flags_experiments_requested_ = true;
167 // Bail out if the handler hasn't been initialized yet. The request will be
168 // handled after the initialization.
169 if (!flags_storage_)
170 return;
172 base::DictionaryValue results;
174 scoped_ptr<base::ListValue> supported_experiments(new base::ListValue);
175 scoped_ptr<base::ListValue> unsupported_experiments(new base::ListValue);
176 about_flags::GetFlagsExperimentsData(flags_storage_.get(),
177 access_,
178 supported_experiments.get(),
179 unsupported_experiments.get());
180 results.Set("supportedExperiments", supported_experiments.release());
181 results.Set("unsupportedExperiments", unsupported_experiments.release());
182 results.SetBoolean("needsRestart",
183 about_flags::IsRestartNeededToCommitChanges());
184 results.SetBoolean("showOwnerWarning",
185 access_ == about_flags::kGeneralAccessFlagsOnly);
187 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
188 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
189 results.SetBoolean("showBetaChannelPromotion",
190 channel == chrome::VersionInfo::CHANNEL_STABLE);
191 results.SetBoolean("showDevChannelPromotion",
192 channel == chrome::VersionInfo::CHANNEL_BETA);
193 #else
194 results.SetBoolean("showBetaChannelPromotion", false);
195 results.SetBoolean("showDevChannelPromotion", false);
196 #endif
197 web_ui()->CallJavascriptFunction("returnFlagsExperiments", results);
200 void FlagsDOMHandler::HandleEnableFlagsExperimentMessage(
201 const base::ListValue* args) {
202 DCHECK(flags_storage_);
203 DCHECK_EQ(2u, args->GetSize());
204 if (args->GetSize() != 2)
205 return;
207 std::string experiment_internal_name;
208 std::string enable_str;
209 if (!args->GetString(0, &experiment_internal_name) ||
210 !args->GetString(1, &enable_str))
211 return;
213 about_flags::SetExperimentEnabled(
214 flags_storage_.get(),
215 experiment_internal_name,
216 enable_str == "true");
219 void FlagsDOMHandler::HandleRestartBrowser(const base::ListValue* args) {
220 DCHECK(flags_storage_);
221 #if defined(OS_CHROMEOS)
222 // On ChromeOS be less intrusive and restart inside the user session after
223 // we apply the newly selected flags.
224 CommandLine user_flags(CommandLine::NO_PROGRAM);
225 about_flags::ConvertFlagsToSwitches(flags_storage_.get(),
226 &user_flags,
227 about_flags::kAddSentinels);
228 CommandLine::StringVector flags;
229 // argv[0] is the program name |CommandLine::NO_PROGRAM|.
230 flags.assign(user_flags.argv().begin() + 1, user_flags.argv().end());
231 VLOG(1) << "Restarting to apply per-session flags...";
232 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
233 SetFlagsForUser(chromeos::UserManager::Get()->GetActiveUser()->email(),
234 flags);
235 #endif
236 chrome::AttemptRestart();
239 void FlagsDOMHandler::HandleResetAllFlags(const base::ListValue* args) {
240 DCHECK(flags_storage_);
241 about_flags::ResetAllFlags(flags_storage_.get());
245 #if defined(OS_CHROMEOS)
246 // On ChromeOS verifying if the owner is signed in is async operation and only
247 // after finishing it the UI can be properly populated. This function is the
248 // callback for whether the owner is signed in. It will respectively pick the
249 // proper PrefService for the flags interface.
250 void FinishInitialization(base::WeakPtr<FlagsUI> flags_ui,
251 Profile* profile,
252 FlagsDOMHandler* dom_handler,
253 bool current_user_is_owner) {
254 // If the flags_ui has gone away, there's nothing to do.
255 if (!flags_ui)
256 return;
258 // On Chrome OS the owner can set system wide flags and other users can only
259 // set flags for their own session.
260 // Note that |dom_handler| is owned by the web ui that owns |flags_ui|, so
261 // it is still alive if |flags_ui| is.
262 if (current_user_is_owner) {
263 dom_handler->Init(new chromeos::about_flags::OwnerFlagsStorage(
264 profile->GetPrefs(),
265 chromeos::CrosSettings::Get()),
266 about_flags::kOwnerAccessToFlags);
267 } else {
268 dom_handler->Init(
269 new about_flags::PrefServiceFlagsStorage(profile->GetPrefs()),
270 about_flags::kGeneralAccessFlagsOnly);
273 #endif
275 } // namespace
277 ///////////////////////////////////////////////////////////////////////////////
279 // FlagsUI
281 ///////////////////////////////////////////////////////////////////////////////
283 FlagsUI::FlagsUI(content::WebUI* web_ui)
284 : WebUIController(web_ui),
285 weak_factory_(this) {
286 Profile* profile = Profile::FromWebUI(web_ui);
288 FlagsDOMHandler* handler = new FlagsDOMHandler();
289 web_ui->AddMessageHandler(handler);
291 #if defined(OS_CHROMEOS)
292 chromeos::DeviceSettingsService::Get()->IsCurrentUserOwnerAsync(
293 base::Bind(&FinishInitialization,
294 weak_factory_.GetWeakPtr(), profile, handler));
295 #else
296 handler->Init(new about_flags::PrefServiceFlagsStorage(
297 g_browser_process->local_state()),
298 about_flags::kOwnerAccessToFlags);
299 #endif
301 // Set up the about:flags source.
302 content::WebUIDataSource::Add(profile, CreateFlagsUIHTMLSource());
305 FlagsUI::~FlagsUI() {
308 // static
309 base::RefCountedMemory* FlagsUI::GetFaviconResourceBytes(
310 ui::ScaleFactor scale_factor) {
311 return ResourceBundle::GetSharedInstance().
312 LoadDataResourceBytesForScale(IDR_FLAGS_FAVICON, scale_factor);
315 // static
316 void FlagsUI::RegisterPrefs(PrefRegistrySimple* registry) {
317 registry->RegisterListPref(prefs::kEnabledLabsExperiments);
320 #if defined(OS_CHROMEOS)
321 // static
322 void FlagsUI::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
323 registry->RegisterListPref(prefs::kEnabledLabsExperiments,
324 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
327 #endif