Commit c983b345 authored by hkamila's avatar hkamila Committed by Commit Bot

Adding WebUI functions passing and processing the WebUI proto

Added the functions in the WebUI side which are going to process the proto information
for the Safe Browsing database and show it in the page.

Bug: 734667
Change-Id: I10d8c89318a02563b2ce803110339191fe88aac6
Reviewed-on: https://chromium-review.googlesource.com/585230
Commit-Queue: Kamila Hasanbega <hkamila@google.com>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarJialiu Lin <jialiul@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491045}
parent 16329088
......@@ -18,6 +18,7 @@ static_library("web_ui") {
"//components/resources:components_scaled_resources_grit",
"//components/safe_browsing:features",
"//components/safe_browsing/common:safe_browsing_prefs",
"//components/safe_browsing_db:v4_local_database_manager",
"//components/strings:components_strings_grit",
"//components/user_prefs:user_prefs",
"//content/public/browser",
......
......@@ -2,5 +2,6 @@ include_rules = [
"+components/grit/components_resources.h",
"+components/user_prefs",
"+components/strings/grit/components_strings.h",
"+components/grit/components_scaled_resources.h"
"+components/grit/components_scaled_resources.h",
"+components/safe_browsing_db",
]
......@@ -9,18 +9,18 @@ body {
p {
white-space: pre-wrap;
}
.sbPageContent {
.content {
background-color: #fbfbfb;
border: 1px solid #cecece;
border-radius: 3px;
padding: 19px;
line-height: 1.5;
}
#sbTitle {
#sb-title {
font-size: 2em;
margin-bottom: 0.8em;
}
h1, h2, h3, p {
font-weight: normal;
line-height: 1;
line-height: 1.5;
}
......@@ -13,16 +13,20 @@
</head>
<body>
<div id="header">
<h1 id="sbTitle">Safe Browsing</h1>
<h1 id="sb-title">Safe Browsing</h1>
</div>
<p>$i18n{sbUnderConstruction}</p>
<h2>Experiments</h2>
<div class="sbPageContent">
<p id="experimentsList"></p>
<div class="content">
<p id="experiments-list"></p>
</div>
<h2>Preferences</h2>
<div class="sbPageContent">
<p id="preferencesList"></p>
<div class="content">
<p id="preferences-list"></p>
</div>
<h2>Database Manager</h2>
<div class="content">
<p id="database-info-list"></p>
</div>
<script src="chrome://resources/js/i18n_template.js"></script>
</body>
......
......@@ -13,8 +13,9 @@ cr.define('safe_browsing', function() {
function initialize() {
cr.sendWithPromise('getExperiments', []).then((experiments) =>
addExperiments(experiments));
cr.sendWithPromise('getPrefs', []).then(
prefs=>addPrefs(prefs));
cr.sendWithPromise('getPrefs', []).then((prefs) => addPrefs(prefs));
cr.sendWithPromise('getDatabaseManagerInfo', []).then(
(databaseState) => addDatabaseInfo(databaseState));
}
function addExperiments(result) {
......@@ -26,7 +27,7 @@ cr.define('safe_browsing', function() {
"</b>: " + result[i] + "</div>";
}
$('experimentsList').innerHTML = experimentsListFormatted;
$('experiments-list').innerHTML = experimentsListFormatted;
}
function addPrefs(result) {
......@@ -37,7 +38,19 @@ cr.define('safe_browsing', function() {
preferencesListFormatted += "<div><b>" + result[i + 1] + "</b>: " +
result[i] + "</div>";
}
$('preferencesList').innerHTML = preferencesListFormatted;
$('preferences-list').innerHTML = preferencesListFormatted;
}
function addDatabaseInfo(result) {
var resLength = result.length;
var preferencesListFormatted = "";
for (var i = 0; i < resLength; i += 2) {
preferencesListFormatted += "<div><b>" + result[i] + "</b>: " +
result[i+1] + "</div>";
}
$('database-info-list').innerHTML = preferencesListFormatted;
}
return {
......
......@@ -4,6 +4,14 @@
#include "components/safe_browsing/web_ui/safe_browsing_ui.h"
#include <stddef.h>
#include <algorithm>
#include <utility>
#include <vector>
#include "base/i18n/time_formatting.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/grit/components_resources.h"
#include "components/grit/components_scaled_resources.h"
......@@ -14,7 +22,67 @@
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#if SAFE_BROWSING_DB_LOCAL
#include "components/safe_browsing_db/v4_local_database_manager.h"
#endif
using base::Time;
namespace safe_browsing {
namespace {
#if SAFE_BROWSING_DB_LOCAL
void AddStoreInfo(const DatabaseManagerInfo::DatabaseInfo::StoreInfo store_info,
base::ListValue* database_info_list) {
if (store_info.has_file_size_bytes() && store_info.has_file_name()) {
database_info_list->GetList().push_back(
base::Value(store_info.file_name()));
database_info_list->GetList().push_back(
base::Value(static_cast<double>(store_info.file_size_bytes())));
}
if (store_info.has_update_status()) {
database_info_list->GetList().push_back(base::Value("Store update status"));
database_info_list->GetList().push_back(
base::Value(store_info.update_status()));
}
}
void AddDatabaseInfo(const DatabaseManagerInfo::DatabaseInfo database_info,
base::ListValue* database_info_list) {
if (database_info.has_database_size_bytes()) {
database_info_list->GetList().push_back(
base::Value("Database size in bytes"));
database_info_list->GetList().push_back(
base::Value(static_cast<double>(database_info.database_size_bytes())));
}
// Add the information specific to each store.
for (int i = 0; i < database_info.store_info_size(); i++) {
AddStoreInfo(database_info.store_info(i), database_info_list);
}
}
void AddUpdateInfo(const DatabaseManagerInfo::UpdateInfo update_info,
base::ListValue* database_info_list) {
if (update_info.has_network_status_code()) {
// Network status of the last GetUpdate().
database_info_list->GetList().push_back(
base::Value("Last update network status code"));
database_info_list->GetList().push_back(
base::Value(update_info.network_status_code()));
}
if (update_info.has_last_update_time_millis()) {
database_info_list->GetList().push_back(base::Value("Last update time"));
// Converts time to Base::Time
base::Time last_update =
base::Time::UnixEpoch() + base::TimeDelta::FromMicroseconds(
update_info.last_update_time_millis());
database_info_list->GetList().push_back(base::Value(
base::UTF16ToASCII(base::TimeFormatShortDateAndTime(last_update))));
}
}
#endif
} // namespace
SafeBrowsingUI::SafeBrowsingUI(content::WebUI* web_ui)
: content::WebUIController(web_ui) {
......@@ -48,6 +116,8 @@ SafeBrowsingUI::~SafeBrowsingUI() {}
SafeBrowsingUIHandler::SafeBrowsingUIHandler(content::BrowserContext* context)
: browser_context_(context) {}
SafeBrowsingUIHandler::~SafeBrowsingUIHandler() = default;
void SafeBrowsingUIHandler::GetExperiments(const base::ListValue* args) {
AllowJavascript();
std::string callback_id;
......@@ -63,7 +133,38 @@ void SafeBrowsingUIHandler::GetPrefs(const base::ListValue* args) {
safe_browsing::GetSafeBrowsingPreferencesList(
user_prefs::UserPrefs::Get(browser_context_)));
}
SafeBrowsingUIHandler::~SafeBrowsingUIHandler() {}
void SafeBrowsingUIHandler::GetDatabaseManagerInfo(
const base::ListValue* args) {
base::ListValue database_manager_info;
#if SAFE_BROWSING_DB_LOCAL
const V4LocalDatabaseManager* local_database_manager_instance =
V4LocalDatabaseManager::current_local_database_manager();
if (local_database_manager_instance) {
DatabaseManagerInfo database_manager_info_proto;
local_database_manager_instance->CollectDatabaseManagerInfo(
&database_manager_info_proto);
if (database_manager_info_proto.has_update_info()) {
AddUpdateInfo(database_manager_info_proto.update_info(),
&database_manager_info);
}
if (database_manager_info_proto.has_database_info()) {
AddDatabaseInfo(database_manager_info_proto.database_info(),
&database_manager_info);
}
}
#endif
AllowJavascript();
std::string callback_id;
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id), database_manager_info);
}
void SafeBrowsingUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
......@@ -72,6 +173,10 @@ void SafeBrowsingUIHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"getPrefs",
base::Bind(&SafeBrowsingUIHandler::GetPrefs, base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getDatabaseManagerInfo",
base::Bind(&SafeBrowsingUIHandler::GetDatabaseManagerInfo,
base::Unretained(this)));
}
} // namespace safe_browsing
......@@ -6,6 +6,7 @@
#define COMPONENTS_SAFE_BROWSING_WEBUI_SAFE_BROWSING_UI_H_
#include "base/macros.h"
#include "components/safe_browsing/web_ui/webui.pb.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_data_source.h"
......@@ -20,8 +21,11 @@ class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
public:
SafeBrowsingUIHandler(content::BrowserContext*);
~SafeBrowsingUIHandler() override;
void GetExperiments(const base::ListValue* args);
void GetPrefs(const base::ListValue* args);
void GetDatabaseManagerInfo(const base::ListValue* args);
void RegisterMessages() override;
private:
......
......@@ -433,8 +433,11 @@ void V4UpdateProtocolManager::GetUpdateUrlAndHeaders(
void V4UpdateProtocolManager::CollectUpdateInfo(
DatabaseManagerInfo::UpdateInfo* update_info) {
update_info->set_network_status_code(last_response_code_);
update_info->set_last_update_time_millis(last_response_time_.ToJavaTime());
if (last_response_code_)
update_info->set_network_status_code(last_response_code_);
if (last_response_time_.ToJavaTime())
update_info->set_last_update_time_millis(last_response_time_.ToJavaTime());
}
} // namespace safe_browsing
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