Commit 3f8a63c8 authored by hkamila's avatar hkamila Committed by Commit Bot

Added preferences as part of chrome://safe-browsing

Added the Safe Browsing Preferences as part of the chrome://safe-browsing page.
Added the respective webui messages/functions.

Bug: 734667
Change-Id: I481a9c930ed1859c2a93ff32670e009fb148ae02
Reviewed-on: https://chromium-review.googlesource.com/566383
Commit-Queue: Kamila Hasanbega <hkamila@google.com>
Reviewed-by: default avatarJialiu Lin <jialiul@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/master@{#487134}
parent a55d2792
......@@ -470,4 +470,24 @@ void UpdatePrefsBeforeSecurityInterstitial(PrefService* prefs) {
true);
}
base::ListValue GetSafeBrowsingPreferencesList(PrefService* prefs) {
base::ListValue preferences_list;
const char* safe_browsing_preferences[] = {
prefs::kSafeBrowsingEnabled,
prefs::kSafeBrowsingExtendedReportingOptInAllowed,
prefs::kSafeBrowsingExtendedReportingEnabled,
prefs::kSafeBrowsingScoutReportingEnabled};
// Add the status of the preferences if they are Enabled or Disabled for the
// user.
for (const char* preference : safe_browsing_preferences) {
preferences_list.GetList().push_back(base::Value(preference));
bool enabled = prefs->GetBoolean(preference);
preferences_list.GetList().push_back(
base::Value(enabled ? "Enabled" : "Disabled"));
}
return preferences_list;
}
} // namespace safe_browsing
......@@ -8,6 +8,7 @@
#define COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_
#include "base/feature_list.h"
#include "base/values.h"
class PrefRegistrySimple;
class PrefService;
......@@ -169,6 +170,11 @@ void UpdateMetricsAfterSecurityInterstitial(const PrefService& prefs,
// depending on their experiment state.
void UpdatePrefsBeforeSecurityInterstitial(PrefService* prefs);
// Returns a list of preferences to be shown in chrome://safe-browsing. The
// preferences are passed as an alternating sequence of preference names and
// values represented as strings.
base::ListValue GetSafeBrowsingPreferencesList(PrefService* prefs);
} // namespace safe_browsing
#endif // COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_
......@@ -33,18 +33,17 @@ const base::Feature kThreatDomDetailsTagAndAttributeFeature{
namespace {
// List of experimental features. Boolean value for each list member should be
// set to True if the experiment is currently running at a probability other
// than 1 or 0, or to False otherwise.
std::vector<std::pair<const base::Feature*, bool>>
GetExperimentalFeaturesList() {
std::vector<std::pair<const base::Feature*, bool>> experimental_features_list{
std::make_pair(&kLocalDatabaseManagerEnabled, true),
std::make_pair(&kV4OnlyEnabled, true),
std::make_pair(&kThreatDomDetailsTagAndAttributeFeature, false)};
return experimental_features_list;
}
// set to true if the experiment is currently running at a probability other
// than 1 or 0, or to false otherwise.
constexpr struct {
const base::Feature* feature;
// True if the feature is running at a probability other than 1 or 0.
bool probabilistically_enabled;
} kExperimentalFeatures[]{
{&kLocalDatabaseManagerEnabled, true},
{&kV4OnlyEnabled, true},
{&kThreatDomDetailsTagAndAttributeFeature, false},
};
// Adds the name and the enabled/disabled status of a given feature.
void AddFeatureAndAvailability(const base::Feature* exp_feature,
......@@ -61,16 +60,10 @@ void AddFeatureAndAvailability(const base::Feature* exp_feature,
// Returns the list of the experimental features that are enabled or disabled,
// as part of currently running Safe Browsing experiments.
base::ListValue GetFeatureStatusList() {
std::vector<std::pair<const base::Feature*, bool>> features_list =
GetExperimentalFeaturesList();
base::ListValue param_list;
for (std::vector<std::pair<const base::Feature*, bool>>::iterator it =
features_list.begin();
it != features_list.end(); ++it) {
if ((*it).second) {
AddFeatureAndAvailability((*it).first, &param_list);
}
for (const auto& feature_status : kExperimentalFeatures) {
if (feature_status.probabilistically_enabled)
AddFeatureAndAvailability(feature_status.feature, &param_list);
}
return param_list;
}
......
......@@ -14,7 +14,9 @@ static_library("web_ui") {
"//components/resources:components_resources_grit",
"//components/resources:components_scaled_resources_grit",
"//components/safe_browsing:features",
"//components/safe_browsing/common:safe_browsing_prefs",
"//components/strings:components_strings_grit",
"//components/user_prefs:user_prefs",
"//content/public/browser",
"//net",
"//url",
......
include_rules = [
"+components/grit/components_resources.h",
"+components/user_prefs",
"+components/strings/grit/components_strings.h",
"+components/grit/components_scaled_resources.h"
]
/* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */
body {
color: rgb(48, 57, 66);
margin:15px;
color: rgb(48, 57, 66);
margin:15px;
}
p {
p {
white-space: pre-wrap;
}
.experiments {
.sbPageContent {
background-color: #fbfbfb;
border: 1px solid #cecece;
border-radius: 3px;
padding: 19px;
line-height: 1.5;
border: 1px solid #cecece;
border-radius: 3px;
padding: 19px;
line-height: 1.5;
}
#sbTitle {
font-size: 2em;
margin-bottom: 0.8em;
font-size: 2em;
margin-bottom: 0.8em;
}
h1, h2, h3, p {
font-weight: normal;
line-height: 1;
}
font-weight: normal;
line-height: 1;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Safe Browsing</title>
<link rel="stylesheet" href="safe_browsing.css">
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script>
<script src="chrome://resources/js/util.js"></script>
<script src="safe_browsing.js"></script>
</head>
<body>
<div id="header">
<h1 id="sbTitle">Safe Browsing</h1>
</div>
<p>$i18n{sbUnderConstruction}</p>
<head>
<meta charset="utf-8">
<title>Safe Browsing</title>
<link rel="stylesheet" href="safe_browsing.css">
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<script src="chrome://resources/js/promise_resolver.js"></script>
<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script>
<script src="chrome://resources/js/util.js"></script>
<script src="safe_browsing.js"></script>
</head>
<body>
<div id="header">
<h1 id="sbTitle">Safe Browsing</h1>
</div>
<p>$i18n{sbUnderConstruction}</p>
<h2>Experiments</h2>
<div class="experiments">
<p id="experimentsList"></p>
</div>
<script src="chrome://resources/js/i18n_template.js"></script>
</body>
<div class="sbPageContent">
<p id="experimentsList"></p>
</div>
<h2>Preferences</h2>
<div class="sbPageContent">
<p id="preferencesList"></p>
</div>
<script src="chrome://resources/js/i18n_template.js"></script>
</body>
</html>
......@@ -2,28 +2,47 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */
cr.define('safe_browsing', function() {
'use strict';
cr.define('safe_browsing', function() {
'use strict';
/**
* Asks the C++ SafeBrowsingUIHandler to get the lists of Safe Browsing
* ongoing experiments and preferences.
* The SafeBrowsingUIHandler should reply to addExperiment() and
* addPreferences() (below).
*/
function initialize() {
cr.sendWithPromise('getExperiments', []).then((experiments) =>
addExperiments(experiments));
cr.sendWithPromise('getPrefs', []).then(
prefs=>addPrefs(prefs));
}
function initialize() {
chrome.send('expParamList',[]);
}
function addExperiments(result) {
var resLength = result.length;
var experimentsListFormatted = "";
function addExperiment(result) {
var resLength = result.length;
var experimentsListFormatted="";
for (var i = 0; i < resLength; i += 2) {
experimentsListFormatted += "<div><b>" + result[i + 1] +
"</b>: " + result[i] + "</div>";
}
for (var i = 0; i < resLength; i+=2) {
experimentsListFormatted += "<div>" + result[i+1] + " --- " + result [i] +
"</div>";}
$('experimentsList').innerHTML = experimentsListFormatted;
$('experimentsList').innerHTML = experimentsListFormatted;
}
return {
addExperiment: addExperiment,
initialize: initialize,
};
function addPrefs(result) {
var resLength = result.length;
var preferencesListFormatted = "";
for (var i = 0; i < resLength; i += 2) {
preferencesListFormatted += "<div><b>" + result[i + 1] + "</b>: " +
result[i] + "</div>";
}
$('preferencesList').innerHTML = preferencesListFormatted;
}
return {
initialize: initialize,
};
});
document.addEventListener('DOMContentLoaded', safe_browsing.initialize);
......@@ -4,20 +4,18 @@
#include "components/safe_browsing/web_ui/safe_browsing_ui.h"
#include <algorithm>
#include <utility>
#include "base/macros.h"
#include "base/values.h"
#include "components/grit/components_resources.h"
#include "components/grit/components_scaled_resources.h"
#include "components/safe_browsing/common/safe_browsing_prefs.h"
#include "components/safe_browsing/features.h"
#include "components/safe_browsing/web_ui/constants.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace safe_browsing {
SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
: content::WebUIController(web_ui) {
// Set up the chrome://safe-browsing source.
......@@ -25,9 +23,13 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
content::WebUIDataSource* html_source = content::WebUIDataSource::Create(
safe_browsing::kChromeUISafeBrowsingHost);
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
// Register callback handler.
// Handles messages from JavaScript to C++ via chrome.send().
web_ui->AddMessageHandler(base::MakeUnique<SafeBrowsingUIHandler>());
web_ui->AddMessageHandler(
base::MakeUnique<SafeBrowsingUIHandler>(browser_context));
// Add localized string resources.
html_source->AddLocalizedString("sbUnderConstruction",
......@@ -38,25 +40,38 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
html_source->AddResourcePath("safe_browsing.js", IDR_SAFE_BROWSING_JS);
html_source->SetDefaultResource(IDR_SAFE_BROWSING_HTML);
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context, html_source);
}
SafeBrowsingUI::~SafeBrowsingUI() {}
SafeBrowsingUIHandler::SafeBrowsingUIHandler(){};
SafeBrowsingUIHandler::SafeBrowsingUIHandler(content::BrowserContext* context)
: browser_context_(context) {}
void SafeBrowsingUIHandler::ExpParamList(const base::ListValue* unused) {
void SafeBrowsingUIHandler::GetExperiments(const base::ListValue* args) {
AllowJavascript();
CallJavascriptFunction("safe_browsing.addExperiment", GetFeatureStatusList());
std::string callback_id;
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id), GetFeatureStatusList());
}
void SafeBrowsingUIHandler::GetPrefs(const base::ListValue* args) {
AllowJavascript();
std::string callback_id;
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id),
safe_browsing::GetSafeBrowsingPreferencesList(
user_prefs::UserPrefs::Get(browser_context_)));
}
SafeBrowsingUIHandler::~SafeBrowsingUIHandler() {}
void SafeBrowsingUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"expParamList",
base::Bind(&SafeBrowsingUIHandler::ExpParamList, base::Unretained(this)));
"getExperiments", base::Bind(&SafeBrowsingUIHandler::GetExperiments,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getPrefs",
base::Bind(&SafeBrowsingUIHandler::GetPrefs, base::Unretained(this)));
}
SafeBrowsingUIHandler::~SafeBrowsingUIHandler() {}
} // namespace safe_browsing
......@@ -5,26 +5,27 @@
#ifndef COMPONENTS_SAFE_BROWSING_WEBUI_SAFE_BROWSING_UI_H_
#define COMPONENTS_SAFE_BROWSING_WEBUI_SAFE_BROWSING_UI_H_
#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/values.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace base {
class ListValue;
}
namespace safe_browsing {
class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
public:
SafeBrowsingUIHandler();
SafeBrowsingUIHandler(content::BrowserContext*);
~SafeBrowsingUIHandler() override;
void ExpParamList(const base::ListValue* args);
void GetExperiments(const base::ListValue* args);
void GetPrefs(const base::ListValue* args);
void RegisterMessages() override;
private:
content::BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIHandler);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment