Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / webui / flags_ui.cc
blob4d503ddc70b429c40c84c823b89d8ee3f838813a
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 "chrome/grit/chromium_strings.h"
25 #include "chrome/grit/generated_resources.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_ui.h"
28 #include "content/public/browser/web_ui_data_source.h"
29 #include "content/public/browser/web_ui_message_handler.h"
30 #include "grit/browser_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/ownership/owner_settings_service_chromeos.h"
38 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_factory.h"
39 #include "chrome/browser/chromeos/settings/cros_settings.h"
40 #include "chrome/browser/chromeos/settings/owner_flags_storage.h"
41 #include "chromeos/dbus/dbus_thread_manager.h"
42 #include "chromeos/dbus/session_manager_client.h"
43 #include "components/pref_registry/pref_registry_syncable.h"
44 #include "components/user_manager/user_manager.h"
45 #endif
47 using content::WebContents;
48 using content::WebUIMessageHandler;
50 namespace {
52 content::WebUIDataSource* CreateFlagsUIHTMLSource() {
53 content::WebUIDataSource* source =
54 content::WebUIDataSource::Create(chrome::kChromeUIFlagsHost);
56 source->AddLocalizedString("flagsLongTitle", IDS_FLAGS_LONG_TITLE);
57 source->AddLocalizedString("flagsTableTitle", IDS_FLAGS_TABLE_TITLE);
58 source->AddLocalizedString("flagsNoExperimentsAvailable",
59 IDS_FLAGS_NO_EXPERIMENTS_AVAILABLE);
60 source->AddLocalizedString("flagsWarningHeader", IDS_FLAGS_WARNING_HEADER);
61 source->AddLocalizedString("flagsBlurb", IDS_FLAGS_WARNING_TEXT);
62 source->AddLocalizedString("channelPromoBeta",
63 IDS_FLAGS_PROMOTE_BETA_CHANNEL);
64 source->AddLocalizedString("channelPromoDev", IDS_FLAGS_PROMOTE_DEV_CHANNEL);
65 source->AddLocalizedString("flagsUnsupportedTableTitle",
66 IDS_FLAGS_UNSUPPORTED_TABLE_TITLE);
67 source->AddLocalizedString("flagsNoUnsupportedExperiments",
68 IDS_FLAGS_NO_UNSUPPORTED_EXPERIMENTS);
69 source->AddLocalizedString("flagsNotSupported", IDS_FLAGS_NOT_AVAILABLE);
70 source->AddLocalizedString("flagsRestartNotice", IDS_FLAGS_RELAUNCH_NOTICE);
71 source->AddLocalizedString("flagsRestartButton", IDS_FLAGS_RELAUNCH_BUTTON);
72 source->AddLocalizedString("resetAllButton", IDS_FLAGS_RESET_ALL_BUTTON);
73 source->AddLocalizedString("disable", IDS_FLAGS_DISABLE);
74 source->AddLocalizedString("enable", IDS_FLAGS_ENABLE);
76 #if defined(OS_CHROMEOS)
77 if (!user_manager::UserManager::Get()->IsCurrentUserOwner() &&
78 base::SysInfo::IsRunningOnChromeOS()) {
79 // Set the strings to show which user can actually change the flags.
80 std::string owner;
81 chromeos::CrosSettings::Get()->GetString(chromeos::kDeviceOwner, &owner);
82 source->AddString("ownerWarning",
83 l10n_util::GetStringFUTF16(IDS_SYSTEM_FLAGS_OWNER_ONLY,
84 base::UTF8ToUTF16(owner)));
85 } else {
86 // The warning will be only shown on ChromeOS, when the current user is not
87 // the owner.
88 source->AddString("ownerWarning", base::string16());
90 #endif
92 source->SetJsonPath("strings.js");
93 source->AddResourcePath("flags.js", IDR_FLAGS_JS);
94 source->SetDefaultResource(IDR_FLAGS_HTML);
95 return source;
98 ////////////////////////////////////////////////////////////////////////////////
100 // FlagsDOMHandler
102 ////////////////////////////////////////////////////////////////////////////////
104 // The handler for Javascript messages for the about:flags page.
105 class FlagsDOMHandler : public WebUIMessageHandler {
106 public:
107 FlagsDOMHandler() : access_(about_flags::kGeneralAccessFlagsOnly),
108 flags_experiments_requested_(false) {
110 ~FlagsDOMHandler() override {}
112 // Initializes the DOM handler with the provided flags storage and flags
113 // access. If there were flags experiments requested from javascript before
114 // this was called, it calls |HandleRequestFlagsExperiments| again.
115 void Init(about_flags::FlagsStorage* flags_storage,
116 about_flags::FlagAccess access);
118 // WebUIMessageHandler implementation.
119 void RegisterMessages() override;
121 // Callback for the "requestFlagsExperiments" message.
122 void HandleRequestFlagsExperiments(const base::ListValue* args);
124 // Callback for the "enableFlagsExperiment" message.
125 void HandleEnableFlagsExperimentMessage(const base::ListValue* args);
127 // Callback for the "restartBrowser" message. Restores all tabs on restart.
128 void HandleRestartBrowser(const base::ListValue* args);
130 // Callback for the "resetAllFlags" message.
131 void HandleResetAllFlags(const base::ListValue* args);
133 private:
134 scoped_ptr<about_flags::FlagsStorage> flags_storage_;
135 about_flags::FlagAccess access_;
136 bool flags_experiments_requested_;
138 DISALLOW_COPY_AND_ASSIGN(FlagsDOMHandler);
141 void FlagsDOMHandler::RegisterMessages() {
142 web_ui()->RegisterMessageCallback("requestFlagsExperiments",
143 base::Bind(&FlagsDOMHandler::HandleRequestFlagsExperiments,
144 base::Unretained(this)));
145 web_ui()->RegisterMessageCallback("enableFlagsExperiment",
146 base::Bind(&FlagsDOMHandler::HandleEnableFlagsExperimentMessage,
147 base::Unretained(this)));
148 web_ui()->RegisterMessageCallback("restartBrowser",
149 base::Bind(&FlagsDOMHandler::HandleRestartBrowser,
150 base::Unretained(this)));
151 web_ui()->RegisterMessageCallback("resetAllFlags",
152 base::Bind(&FlagsDOMHandler::HandleResetAllFlags,
153 base::Unretained(this)));
156 void FlagsDOMHandler::Init(about_flags::FlagsStorage* flags_storage,
157 about_flags::FlagAccess access) {
158 flags_storage_.reset(flags_storage);
159 access_ = access;
161 if (flags_experiments_requested_)
162 HandleRequestFlagsExperiments(NULL);
165 void FlagsDOMHandler::HandleRequestFlagsExperiments(
166 const base::ListValue* args) {
167 flags_experiments_requested_ = true;
168 // Bail out if the handler hasn't been initialized yet. The request will be
169 // handled after the initialization.
170 if (!flags_storage_)
171 return;
173 base::DictionaryValue results;
175 scoped_ptr<base::ListValue> supported_experiments(new base::ListValue);
176 scoped_ptr<base::ListValue> unsupported_experiments(new base::ListValue);
177 about_flags::GetFlagsExperimentsData(flags_storage_.get(),
178 access_,
179 supported_experiments.get(),
180 unsupported_experiments.get());
181 results.Set("supportedExperiments", supported_experiments.release());
182 results.Set("unsupportedExperiments", unsupported_experiments.release());
183 results.SetBoolean("needsRestart",
184 about_flags::IsRestartNeededToCommitChanges());
185 results.SetBoolean("showOwnerWarning",
186 access_ == about_flags::kGeneralAccessFlagsOnly);
188 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
189 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
190 results.SetBoolean("showBetaChannelPromotion",
191 channel == chrome::VersionInfo::CHANNEL_STABLE);
192 results.SetBoolean("showDevChannelPromotion",
193 channel == chrome::VersionInfo::CHANNEL_BETA);
194 #else
195 results.SetBoolean("showBetaChannelPromotion", false);
196 results.SetBoolean("showDevChannelPromotion", false);
197 #endif
198 web_ui()->CallJavascriptFunction("returnFlagsExperiments", results);
201 void FlagsDOMHandler::HandleEnableFlagsExperimentMessage(
202 const base::ListValue* args) {
203 DCHECK(flags_storage_);
204 DCHECK_EQ(2u, args->GetSize());
205 if (args->GetSize() != 2)
206 return;
208 std::string experiment_internal_name;
209 std::string enable_str;
210 if (!args->GetString(0, &experiment_internal_name) ||
211 !args->GetString(1, &enable_str))
212 return;
214 about_flags::SetExperimentEnabled(
215 flags_storage_.get(),
216 experiment_internal_name,
217 enable_str == "true");
220 void FlagsDOMHandler::HandleRestartBrowser(const base::ListValue* args) {
221 DCHECK(flags_storage_);
222 #if defined(OS_CHROMEOS)
223 // On ChromeOS be less intrusive and restart inside the user session after
224 // we apply the newly selected flags.
225 base::CommandLine user_flags(base::CommandLine::NO_PROGRAM);
226 about_flags::ConvertFlagsToSwitches(flags_storage_.get(),
227 &user_flags,
228 about_flags::kAddSentinels);
229 base::CommandLine::StringVector flags;
230 // argv[0] is the program name |base::CommandLine::NO_PROGRAM|.
231 flags.assign(user_flags.argv().begin() + 1, user_flags.argv().end());
232 VLOG(1) << "Restarting to apply per-session flags...";
233 chromeos::DBusThreadManager::Get()
234 ->GetSessionManagerClient()
235 ->SetFlagsForUser(
236 user_manager::UserManager::Get()->GetActiveUser()->email(), flags);
237 #endif
238 chrome::AttemptRestart();
241 void FlagsDOMHandler::HandleResetAllFlags(const base::ListValue* args) {
242 DCHECK(flags_storage_);
243 about_flags::ResetAllFlags(flags_storage_.get());
247 #if defined(OS_CHROMEOS)
248 // On ChromeOS verifying if the owner is signed in is async operation and only
249 // after finishing it the UI can be properly populated. This function is the
250 // callback for whether the owner is signed in. It will respectively pick the
251 // proper PrefService for the flags interface.
252 void FinishInitialization(base::WeakPtr<FlagsUI> flags_ui,
253 Profile* profile,
254 FlagsDOMHandler* dom_handler,
255 bool current_user_is_owner) {
256 // If the flags_ui has gone away, there's nothing to do.
257 if (!flags_ui)
258 return;
260 // On Chrome OS the owner can set system wide flags and other users can only
261 // set flags for their own session.
262 // Note that |dom_handler| is owned by the web ui that owns |flags_ui|, so
263 // it is still alive if |flags_ui| is.
264 if (current_user_is_owner) {
265 chromeos::OwnerSettingsServiceChromeOS* service =
266 chromeos::OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
267 profile);
268 dom_handler->Init(new chromeos::about_flags::OwnerFlagsStorage(
269 profile->GetPrefs(), service),
270 about_flags::kOwnerAccessToFlags);
271 } else {
272 dom_handler->Init(
273 new about_flags::PrefServiceFlagsStorage(profile->GetPrefs()),
274 about_flags::kGeneralAccessFlagsOnly);
277 #endif
279 } // namespace
281 ///////////////////////////////////////////////////////////////////////////////
283 // FlagsUI
285 ///////////////////////////////////////////////////////////////////////////////
287 FlagsUI::FlagsUI(content::WebUI* web_ui)
288 : WebUIController(web_ui),
289 weak_factory_(this) {
290 Profile* profile = Profile::FromWebUI(web_ui);
292 FlagsDOMHandler* handler = new FlagsDOMHandler();
293 web_ui->AddMessageHandler(handler);
295 #if defined(OS_CHROMEOS)
296 if (base::SysInfo::IsRunningOnChromeOS() &&
297 chromeos::OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
298 profile)) {
299 chromeos::OwnerSettingsServiceChromeOS* service =
300 chromeos::OwnerSettingsServiceChromeOSFactory::GetForBrowserContext(
301 profile);
302 service->IsOwnerAsync(base::Bind(
303 &FinishInitialization, weak_factory_.GetWeakPtr(), profile, handler));
304 } else {
305 FinishInitialization(weak_factory_.GetWeakPtr(), profile, handler,
306 false /* current_user_is_owner */);
308 #else
309 handler->Init(new about_flags::PrefServiceFlagsStorage(
310 g_browser_process->local_state()),
311 about_flags::kOwnerAccessToFlags);
312 #endif
314 // Set up the about:flags source.
315 content::WebUIDataSource::Add(profile, CreateFlagsUIHTMLSource());
318 FlagsUI::~FlagsUI() {
321 // static
322 base::RefCountedMemory* FlagsUI::GetFaviconResourceBytes(
323 ui::ScaleFactor scale_factor) {
324 return ResourceBundle::GetSharedInstance().
325 LoadDataResourceBytesForScale(IDR_FLAGS_FAVICON, scale_factor);
328 // static
329 void FlagsUI::RegisterPrefs(PrefRegistrySimple* registry) {
330 registry->RegisterListPref(prefs::kEnabledLabsExperiments);
333 #if defined(OS_CHROMEOS)
334 // static
335 void FlagsUI::RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
336 registry->RegisterListPref(prefs::kEnabledLabsExperiments,
337 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
340 #endif