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) { ...@@ -470,4 +470,24 @@ void UpdatePrefsBeforeSecurityInterstitial(PrefService* prefs) {
true); 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 } // namespace safe_browsing
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_ #define COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/values.h"
class PrefRegistrySimple; class PrefRegistrySimple;
class PrefService; class PrefService;
...@@ -169,6 +170,11 @@ void UpdateMetricsAfterSecurityInterstitial(const PrefService& prefs, ...@@ -169,6 +170,11 @@ void UpdateMetricsAfterSecurityInterstitial(const PrefService& prefs,
// depending on their experiment state. // depending on their experiment state.
void UpdatePrefsBeforeSecurityInterstitial(PrefService* prefs); 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 } // namespace safe_browsing
#endif // COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_ #endif // COMPONENTS_SAFE_BROWSING_COMMON_SAFE_BROWSING_PREFS_H_
...@@ -33,18 +33,17 @@ const base::Feature kThreatDomDetailsTagAndAttributeFeature{ ...@@ -33,18 +33,17 @@ const base::Feature kThreatDomDetailsTagAndAttributeFeature{
namespace { namespace {
// List of experimental features. Boolean value for each list member should be // 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 // set to true if the experiment is currently running at a probability other
// than 1 or 0, or to False otherwise. // than 1 or 0, or to false otherwise.
std::vector<std::pair<const base::Feature*, bool>> constexpr struct {
const base::Feature* feature;
GetExperimentalFeaturesList() { // True if the feature is running at a probability other than 1 or 0.
std::vector<std::pair<const base::Feature*, bool>> experimental_features_list{ bool probabilistically_enabled;
std::make_pair(&kLocalDatabaseManagerEnabled, true), } kExperimentalFeatures[]{
std::make_pair(&kV4OnlyEnabled, true), {&kLocalDatabaseManagerEnabled, true},
std::make_pair(&kThreatDomDetailsTagAndAttributeFeature, false)}; {&kV4OnlyEnabled, true},
{&kThreatDomDetailsTagAndAttributeFeature, false},
return experimental_features_list; };
}
// Adds the name and the enabled/disabled status of a given feature. // Adds the name and the enabled/disabled status of a given feature.
void AddFeatureAndAvailability(const base::Feature* exp_feature, void AddFeatureAndAvailability(const base::Feature* exp_feature,
...@@ -61,16 +60,10 @@ 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, // Returns the list of the experimental features that are enabled or disabled,
// as part of currently running Safe Browsing experiments. // as part of currently running Safe Browsing experiments.
base::ListValue GetFeatureStatusList() { base::ListValue GetFeatureStatusList() {
std::vector<std::pair<const base::Feature*, bool>> features_list =
GetExperimentalFeaturesList();
base::ListValue param_list; base::ListValue param_list;
for (std::vector<std::pair<const base::Feature*, bool>>::iterator it = for (const auto& feature_status : kExperimentalFeatures) {
features_list.begin(); if (feature_status.probabilistically_enabled)
it != features_list.end(); ++it) { AddFeatureAndAvailability(feature_status.feature, &param_list);
if ((*it).second) {
AddFeatureAndAvailability((*it).first, &param_list);
}
} }
return param_list; return param_list;
} }
......
...@@ -14,7 +14,9 @@ static_library("web_ui") { ...@@ -14,7 +14,9 @@ static_library("web_ui") {
"//components/resources:components_resources_grit", "//components/resources:components_resources_grit",
"//components/resources:components_scaled_resources_grit", "//components/resources:components_scaled_resources_grit",
"//components/safe_browsing:features", "//components/safe_browsing:features",
"//components/safe_browsing/common:safe_browsing_prefs",
"//components/strings:components_strings_grit", "//components/strings:components_strings_grit",
"//components/user_prefs:user_prefs",
"//content/public/browser", "//content/public/browser",
"//net", "//net",
"//url", "//url",
......
include_rules = [ include_rules = [
"+components/grit/components_resources.h", "+components/grit/components_resources.h",
"+components/user_prefs",
"+components/strings/grit/components_strings.h", "+components/strings/grit/components_strings.h",
"+components/grit/components_scaled_resources.h" "+components/grit/components_scaled_resources.h"
] ]
/* Copyright 2017 The Chromium Authors. All rights reserved. /* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */ * found in the LICENSE file. */
body { body {
color: rgb(48, 57, 66); color: rgb(48, 57, 66);
margin:15px; margin:15px;
} }
p { p {
white-space: pre-wrap; white-space: pre-wrap;
} }
.sbPageContent {
.experiments {
background-color: #fbfbfb; background-color: #fbfbfb;
border: 1px solid #cecece; border: 1px solid #cecece;
border-radius: 3px; border-radius: 3px;
padding: 19px; padding: 19px;
line-height: 1.5; line-height: 1.5;
} }
#sbTitle { #sbTitle {
font-size: 2em; font-size: 2em;
margin-bottom: 0.8em; margin-bottom: 0.8em;
} }
h1, h2, h3, p { h1, h2, h3, p {
font-weight: normal; font-weight: normal;
line-height: 1; line-height: 1;
} }
<!DOCTYPE HTML> <!DOCTYPE HTML>
<html> <html>
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Safe Browsing</title> <title>Safe Browsing</title>
<link rel="stylesheet" href="safe_browsing.css"> <link rel="stylesheet" href="safe_browsing.css">
<link rel="stylesheet" href="chrome://resources/css/text_defaults.css"> <link rel="stylesheet" href="chrome://resources/css/text_defaults.css">
<script src="chrome://resources/js/cr.js"></script> <script src="chrome://resources/js/promise_resolver.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script> <script src="chrome://resources/js/cr.js"></script>
<script src="chrome://resources/js/util.js"></script> <script src="chrome://resources/js/load_time_data.js"></script>
<script src="safe_browsing.js"></script> <script src="chrome://resources/js/util.js"></script>
</head> <script src="safe_browsing.js"></script>
<body> </head>
<div id="header"> <body>
<h1 id="sbTitle">Safe Browsing</h1> <div id="header">
</div> <h1 id="sbTitle">Safe Browsing</h1>
<p>$i18n{sbUnderConstruction}</p> </div>
<p>$i18n{sbUnderConstruction}</p>
<h2>Experiments</h2> <h2>Experiments</h2>
<div class="experiments"> <div class="sbPageContent">
<p id="experimentsList"></p> <p id="experimentsList"></p>
</div> </div>
<script src="chrome://resources/js/i18n_template.js"></script> <h2>Preferences</h2>
</body> <div class="sbPageContent">
<p id="preferencesList"></p>
</div>
<script src="chrome://resources/js/i18n_template.js"></script>
</body>
</html> </html>
...@@ -2,28 +2,47 @@ ...@@ -2,28 +2,47 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. */ * found in the LICENSE file. */
cr.define('safe_browsing', function() { cr.define('safe_browsing', function() {
'use strict'; '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() { function addExperiments(result) {
chrome.send('expParamList',[]); var resLength = result.length;
} var experimentsListFormatted = "";
function addExperiment(result) { for (var i = 0; i < resLength; i += 2) {
var resLength = result.length; experimentsListFormatted += "<div><b>" + result[i + 1] +
var experimentsListFormatted=""; "</b>: " + result[i] + "</div>";
}
for (var i = 0; i < resLength; i+=2) { $('experimentsList').innerHTML = experimentsListFormatted;
experimentsListFormatted += "<div>" + result[i+1] + " --- " + result [i] +
"</div>";}
$('experimentsList').innerHTML = experimentsListFormatted;
} }
return { function addPrefs(result) {
addExperiment: addExperiment, var resLength = result.length;
initialize: initialize, 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); document.addEventListener('DOMContentLoaded', safe_browsing.initialize);
...@@ -4,20 +4,18 @@ ...@@ -4,20 +4,18 @@
#include "components/safe_browsing/web_ui/safe_browsing_ui.h" #include "components/safe_browsing/web_ui/safe_browsing_ui.h"
#include <algorithm>
#include <utility>
#include "base/macros.h"
#include "base/values.h" #include "base/values.h"
#include "components/grit/components_resources.h" #include "components/grit/components_resources.h"
#include "components/grit/components_scaled_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/features.h"
#include "components/safe_browsing/web_ui/constants.h" #include "components/safe_browsing/web_ui/constants.h"
#include "components/strings/grit/components_strings.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/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 { namespace safe_browsing {
SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui) SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
: content::WebUIController(web_ui) { : content::WebUIController(web_ui) {
// Set up the chrome://safe-browsing source. // Set up the chrome://safe-browsing source.
...@@ -25,9 +23,13 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui) ...@@ -25,9 +23,13 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
content::WebUIDataSource* html_source = content::WebUIDataSource::Create( content::WebUIDataSource* html_source = content::WebUIDataSource::Create(
safe_browsing::kChromeUISafeBrowsingHost); safe_browsing::kChromeUISafeBrowsingHost);
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
// Register callback handler. // Register callback handler.
// Handles messages from JavaScript to C++ via chrome.send(). // 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. // Add localized string resources.
html_source->AddLocalizedString("sbUnderConstruction", html_source->AddLocalizedString("sbUnderConstruction",
...@@ -38,25 +40,38 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui) ...@@ -38,25 +40,38 @@ SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
html_source->AddResourcePath("safe_browsing.js", IDR_SAFE_BROWSING_JS); html_source->AddResourcePath("safe_browsing.js", IDR_SAFE_BROWSING_JS);
html_source->SetDefaultResource(IDR_SAFE_BROWSING_HTML); html_source->SetDefaultResource(IDR_SAFE_BROWSING_HTML);
content::BrowserContext* browser_context =
web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context, html_source); content::WebUIDataSource::Add(browser_context, html_source);
} }
SafeBrowsingUI::~SafeBrowsingUI() {} 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(); 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() { void SafeBrowsingUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"expParamList", "getExperiments", base::Bind(&SafeBrowsingUIHandler::GetExperiments,
base::Bind(&SafeBrowsingUIHandler::ExpParamList, base::Unretained(this))); base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getPrefs",
base::Bind(&SafeBrowsingUIHandler::GetPrefs, base::Unretained(this)));
} }
SafeBrowsingUIHandler::~SafeBrowsingUIHandler() {}
} // namespace safe_browsing } // namespace safe_browsing
...@@ -5,26 +5,27 @@ ...@@ -5,26 +5,27 @@
#ifndef COMPONENTS_SAFE_BROWSING_WEBUI_SAFE_BROWSING_UI_H_ #ifndef COMPONENTS_SAFE_BROWSING_WEBUI_SAFE_BROWSING_UI_H_
#define 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/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_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_data_source.h" #include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h" #include "content/public/browser/web_ui_message_handler.h"
namespace base {
class ListValue;
}
namespace safe_browsing { namespace safe_browsing {
class SafeBrowsingUIHandler : public content::WebUIMessageHandler { class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
public: public:
SafeBrowsingUIHandler(); SafeBrowsingUIHandler(content::BrowserContext*);
~SafeBrowsingUIHandler() override; ~SafeBrowsingUIHandler() override;
void ExpParamList(const base::ListValue* args); void GetExperiments(const base::ListValue* args);
void GetPrefs(const base::ListValue* args);
void RegisterMessages() override; void RegisterMessages() override;
private: private:
content::BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(SafeBrowsingUIHandler); 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