Commit 3df71088 authored by Daniel Rubery's avatar Daniel Rubery Committed by Commit Bot

Add ClientDownloadResponse to chrome://safe-browsing

Render: https://screenshot.googleplex.com/gTB6H033JFQ

Bug: 854369
Change-Id: I0dea118fe75090aa7bd4b54b31168768777c7d93
Reviewed-on: https://chromium-review.googlesource.com/1112664
Commit-Queue: Daniel Rubery <drubery@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575704}
parent 0e2b3d75
......@@ -259,6 +259,13 @@ void CheckClientDownloadRequest::OnURLLoaderComplete(
}
}
BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::BindOnce(
&WebUIInfoSingleton::AddToClientDownloadResponsesReceived,
base::Unretained(WebUIInfoSingleton::GetInstance()),
std::make_unique<ClientDownloadResponse>(response)));
if (!token.empty())
DownloadProtectionService::SetDownloadPingToken(item_, token);
......
......@@ -39,6 +39,10 @@
<div class="content">
<p id="sent-client-download-requests-list"></p>
</div>
<h2>Download responses (ClientDownloadResponse) received</h2>
<div class="content">
<p id="received-client-download-response-list"></p>
</div>
<h2>PhishGuard Events</h2>
<div class="content">
<p id="pg-event-log"></p>
......
/* 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. */
* 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';
......@@ -11,8 +11,8 @@ cr.define('safe_browsing', function() {
* addPreferences() (below).
*/
function initialize() {
cr.sendWithPromise('getExperiments', []).then((experiments) =>
addExperiments(experiments));
cr.sendWithPromise('getExperiments', [])
.then((experiments) => addExperiments(experiments));
cr.sendWithPromise('getPrefs', []).then((prefs) => addPrefs(prefs));
cr.sendWithPromise('getSavedPasswords', []).then((passwords) =>
addSavedPasswords(passwords));
......@@ -34,6 +34,17 @@ cr.define('safe_browsing', function() {
addSentClientDownloadRequestsInfo(result);
});
cr.sendWithPromise('getReceivedClientDownloadResponses', [])
.then(
(receivedClientDownloadResponses) => {
receivedClientDownloadResponses.forEach(function(cdr) {
addReceivedClientDownloadResponseInfo(cdr);
})});
cr.addWebUIListener(
'received-client-download-responses-update', function(result) {
addReceivedClientDownloadResponseInfo(result);
});
cr.sendWithPromise('getSentCSBRRs', [])
.then((sentCSBRRs) => {sentCSBRRs.forEach(function(csbrr) {
addSentCSBRRsInfo(csbrr);
......@@ -43,9 +54,7 @@ cr.define('safe_browsing', function() {
});
cr.sendWithPromise('getPGEvents', [])
.then(
(pgEvents) => {
pgEvents.forEach(function (pgEvent) {
.then((pgEvents) => {pgEvents.forEach(function(pgEvent) {
addPGEvent(pgEvent);
})});
cr.addWebUIListener('sent-pg-event', function(result) {
......@@ -71,7 +80,7 @@ cr.define('safe_browsing', function() {
function addExperiments(result) {
var resLength = result.length;
var experimentsListFormatted = "";
var experimentsListFormatted = '';
for (var i = 0; i < resLength; i += 2) {
experimentsListFormatted += "<div><b>" + result[i + 1] +
......@@ -128,6 +137,11 @@ cr.define('safe_browsing', function() {
appendChildWithInnerText(logDiv, result);
}
function addReceivedClientDownloadResponseInfo(result) {
var logDiv = $('received-client-download-response-list');
appendChildWithInnerText(logDiv, result);
}
function addSentCSBRRsInfo(result) {
var logDiv = $('sent-csbrrs-list');
appendChildWithInnerText(logDiv, result);
......@@ -183,6 +197,8 @@ cr.define('safe_browsing', function() {
return {
addSentCSBRRsInfo: addSentCSBRRsInfo,
addSentClientDownloadRequestsInfo: addSentClientDownloadRequestsInfo,
addReceivedClientDownloadResponseInfo:
addReceivedClientDownloadResponseInfo,
addPGEvent: addPGEvent,
addPGPing: addPGPing,
addPGResponse: addPGResponse,
......
......@@ -73,6 +73,23 @@ void WebUIInfoSingleton::ClearClientDownloadRequestsSent() {
client_download_requests_sent_);
}
void WebUIInfoSingleton::AddToClientDownloadResponsesReceived(
std::unique_ptr<ClientDownloadResponse> client_download_response) {
if (webui_instances_.empty())
return;
for (auto* webui_listener : webui_instances_)
webui_listener->NotifyClientDownloadResponseJsListener(
client_download_response.get());
client_download_responses_received_.push_back(
std::move(client_download_response));
}
void WebUIInfoSingleton::ClearClientDownloadResponsesReceived() {
std::vector<std::unique_ptr<ClientDownloadResponse>>().swap(
client_download_responses_received_);
}
void WebUIInfoSingleton::AddToCSBRRsSent(
std::unique_ptr<ClientSafeBrowsingReportRequest> csbrr) {
if (webui_instances_.empty())
......@@ -349,6 +366,52 @@ std::string SerializeClientDownloadRequest(const ClientDownloadRequest& cdr) {
return request_serialized;
}
std::string SerializeClientDownloadResponse(const ClientDownloadResponse& cdr) {
base::DictionaryValue dict;
switch (cdr.verdict()) {
case ClientDownloadResponse::SAFE:
dict.SetPath({"verdict"}, base::Value("SAFE"));
break;
case ClientDownloadResponse::DANGEROUS:
dict.SetPath({"verdict"}, base::Value("DANGEROUS"));
break;
case ClientDownloadResponse::UNCOMMON:
dict.SetPath({"verdict"}, base::Value("UNCOMMON"));
break;
case ClientDownloadResponse::POTENTIALLY_UNWANTED:
dict.SetPath({"verdict"}, base::Value("POTENTIALLY_UNWANTED"));
break;
case ClientDownloadResponse::DANGEROUS_HOST:
dict.SetPath({"verdict"}, base::Value("DANGEROUS_HOST"));
break;
case ClientDownloadResponse::UNKNOWN:
dict.SetPath({"verdict"}, base::Value("UNKNOWN"));
break;
}
if (cdr.has_more_info()) {
dict.SetPath({"more_info", "description"},
base::Value(cdr.more_info().description()));
dict.SetPath({"more_info", "url"}, base::Value(cdr.more_info().url()));
}
if (cdr.has_token()) {
dict.SetPath({"token"}, base::Value(cdr.token()));
}
if (cdr.has_upload()) {
dict.SetPath({"upload"}, base::Value(cdr.upload()));
}
base::Value* request_tree = &dict;
std::string request_serialized;
JSONStringValueSerializer serializer(&request_serialized);
serializer.set_pretty_print(true);
serializer.Serialize(*request_tree);
return request_serialized;
}
std::string SerializeCSBRR(const ClientSafeBrowsingReportRequest& report) {
base::DictionaryValue report_request;
if (report.has_type()) {
......@@ -905,6 +968,24 @@ void SafeBrowsingUIHandler::GetSentClientDownloadRequests(
ResolveJavascriptCallback(base::Value(callback_id), cdrs_sent);
}
void SafeBrowsingUIHandler::GetReceivedClientDownloadResponses(
const base::ListValue* args) {
const std::vector<std::unique_ptr<ClientDownloadResponse>>& cdrs =
WebUIInfoSingleton::GetInstance()->client_download_responses_received();
base::ListValue cdrs_received;
for (const auto& cdr : cdrs) {
cdrs_received.GetList().push_back(
base::Value(SerializeClientDownloadResponse(*cdr)));
}
AllowJavascript();
std::string callback_id;
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id), cdrs_received);
}
void SafeBrowsingUIHandler::GetSentCSBRRs(const base::ListValue* args) {
const std::vector<std::unique_ptr<ClientSafeBrowsingReportRequest>>& reports =
WebUIInfoSingleton::GetInstance()->csbrrs_sent();
......@@ -983,6 +1064,14 @@ void SafeBrowsingUIHandler::NotifyClientDownloadRequestJsListener(
base::Value(SerializeClientDownloadRequest(*client_download_request)));
}
void SafeBrowsingUIHandler::NotifyClientDownloadResponseJsListener(
ClientDownloadResponse* client_download_response) {
AllowJavascript();
FireWebUIListener(
"received-client-download-responses-update",
base::Value(SerializeClientDownloadResponse(*client_download_response)));
}
void SafeBrowsingUIHandler::NotifyCSBRRJsListener(
ClientSafeBrowsingReportRequest* csbrr) {
AllowJavascript();
......@@ -1037,6 +1126,11 @@ void SafeBrowsingUIHandler::RegisterMessages() {
"getSentClientDownloadRequests",
base::BindRepeating(&SafeBrowsingUIHandler::GetSentClientDownloadRequests,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getReceivedClientDownloadResponses",
base::BindRepeating(
&SafeBrowsingUIHandler::GetReceivedClientDownloadResponses,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getSentCSBRRs",
base::BindRepeating(&SafeBrowsingUIHandler::GetSentCSBRRs,
......
......@@ -45,6 +45,10 @@ class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
// currently open chrome://safe-browsing tab was opened.
void GetSentClientDownloadRequests(const base::ListValue* args);
// Get the ClientDownloadReponses that have been collected since the oldest
// currently open chrome://safe-browsing tab was opened.
void GetReceivedClientDownloadResponses(const base::ListValue* args);
// Get the ThreatDetails that have been collected since the oldest currently
// open chrome://safe-browsing tab was opened.
void GetSentCSBRRs(const base::ListValue* args);
......@@ -72,6 +76,11 @@ class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
void NotifyClientDownloadRequestJsListener(
ClientDownloadRequest* client_download_request);
// Called when any new ClientDownloadResponse messages are received while one
// or more WebUI tabs are open.
void NotifyClientDownloadResponseJsListener(
ClientDownloadResponse* client_download_response);
// Get the new ThreatDetails messages sent from ThreatDetails when a ping is
// sent, while one or more WebUI tabs are opened.
void NotifyCSBRRJsListener(ClientSafeBrowsingReportRequest* csbrr);
......@@ -123,6 +132,14 @@ class WebUIInfoSingleton {
// Clear the list of the sent ClientDownloadRequest messages.
void ClearClientDownloadRequestsSent();
// Add the new message in |client_download_responses_received_| and send it to
// all the open chrome://safe-browsing tabs.
void AddToClientDownloadResponsesReceived(
std::unique_ptr<ClientDownloadResponse> response);
// Clear the list of the received ClientDownloadResponse messages.
void ClearClientDownloadResponsesReceived();
// Add the new message in |csbrrs_sent_| and send it to all the open
// chrome://safe-browsing tabs.
void AddToCSBRRsSent(std::unique_ptr<ClientSafeBrowsingReportRequest> csbrr);
......@@ -164,6 +181,13 @@ class WebUIInfoSingleton {
return client_download_requests_sent_;
}
// Get the list of the sent ClientDownloadResponses that have been collected
// since the oldest currently open chrome://safe-browsing tab was opened.
const std::vector<std::unique_ptr<ClientDownloadResponse>>&
client_download_responses_received() const {
return client_download_responses_received_;
}
// Get the list of the sent CSBRR reports that have been collected since the
// oldest currently open chrome://safe-browsing tab was opened.
const std::vector<std::unique_ptr<ClientSafeBrowsingReportRequest>>&
......@@ -207,6 +231,13 @@ class WebUIInfoSingleton {
std::vector<std::unique_ptr<ClientDownloadRequest>>
client_download_requests_sent_;
// List of ClientDownloadResponses received since since the oldest currently
// open chrome://safe-browsing tab was opened. "ClientDownloadReponse" cannot
// be const, due to being used by functions that call AllowJavascript(), which
// is not marked const.
std::vector<std::unique_ptr<ClientDownloadResponse>>
client_download_responses_received_;
// List of CSBRRs sent since since the oldest currently open
// chrome://safe-browsing tab was opened.
// "ClientSafeBrowsingReportRequest" cannot be const, due to being used by
......
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