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