Commit 84326ff8 authored by Daniel Rubery's avatar Daniel Rubery Committed by Commit Bot

Add timestamps to chrome://safe-browsing/#tab-deep-scan

This CL adds timestamps for the response and request to the Deep Scans
tab on chrome://safe-browsing, for easier debugging purposes.

Change-Id: I3edce358dc09fb9138180c71e1924468bac2c5ad
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2138594
Commit-Queue: Daniel Rubery <drubery@chromium.org>
Reviewed-by: default avatarRoger Tawa <rogerta@chromium.org>
Cr-Commit-Position: refs/heads/master@{#757149}
parent 399251e0
......@@ -132,22 +132,13 @@ cr.define('safe_browsing', function() {
addReportingEvent(reportingEvent);
});
cr.sendWithPromise('getDeepScanRequests', []).then((requests) => {
cr.sendWithPromise('getDeepScans', []).then((requests) => {
requests.forEach(function(request) {
addDeepScanRequest(request);
addDeepScan(request);
});
});
cr.addWebUIListener('deep-scan-request-update', function(result) {
addDeepScanRequest(result);
});
cr.sendWithPromise('getDeepScanResponses', []).then((responses) => {
responses.forEach(function(response) {
addDeepScanResponse(response);
});
});
cr.addWebUIListener('deep-scan-response-update', function(result) {
addDeepScanResponse(result);
addDeepScan(result);
});
$('get-referrer-chain-form').addEventListener('submit', addReferrerChain);
......@@ -265,45 +256,53 @@ cr.define('safe_browsing', function() {
row.insertCell().className = 'content';
}
function addResultToTable(tableId, result, position) {
const token = result[0];
const request = result[1];
function addResultToTable(tableId, token, result, position) {
if ($(tableId + '-' + token) === null) {
insertTokenToTable(tableId, token);
}
const cell = $(tableId + '-' + token).cells[position];
appendChildWithInnerText(cell, request);
cell.innerText = result;
}
function addPGPing(result) {
addResultToTable('pg-ping-list', result, 0);
addResultToTable('pg-ping-list', result[0], result[1], 0);
}
function addPGResponse(result) {
addResultToTable('pg-ping-list', result, 1);
addResultToTable('pg-ping-list', result[0], result[1], 1);
}
function addRTLookupPing(result) {
addResultToTable('rt-lookup-ping-list', result, 0);
addResultToTable('rt-lookup-ping-list', result[0], result[1], 0);
}
function addRTLookupResponse(result) {
addResultToTable('rt-lookup-ping-list', result, 1);
addResultToTable('rt-lookup-ping-list', result[0], result[1], 1);
}
function addDeepScanRequest(result) {
addResultToTable('deep-scan-list', result, 0);
}
function addDeepScan(result) {
if (result['request_time'] != null) {
const requestFormatted = '[' +
(new Date(result['request_time'])).toLocaleString() + ']\n' +
result['request'];
addResultToTable('deep-scan-list', result['token'], requestFormatted, 0);
}
function addDeepScanResponse(result) {
if (result[1] === 'SUCCESS') {
// Display the response instead
addResultToTable('deep-scan-list', [result[0], result[2]], 1);
} else {
// Display the error code
addResultToTable('deep-scan-list', [result[0], result[1]], 1);
if (result['response_time'] != null) {
if (result['response_status'] == 'SUCCESS') {
// Display the response instead
const resultFormatted = '[' +
(new Date(result['response_time'])).toLocaleString() + ']\n' +
result['response'];
addResultToTable('deep-scan-list', result['token'], resultFormatted, 1);
} else {
// Display the error
const resultFormatted = '[' +
(new Date(result['response_time'])).toLocaleString() + ']\n' +
result['response_status'];
addResultToTable('deep-scan-list', result['token'], resultFormatted, 1);
}
}
}
......
......@@ -255,13 +255,18 @@ void WebUIInfoSingleton::AddToDeepScanRequests(
if (!HasListener())
return;
// Only update the request time the first time we see a token.
if (deep_scan_requests_.find(request.request_token()) ==
deep_scan_requests_.end()) {
for (auto* webui_listener : webui_instances_)
webui_listener->NotifyDeepScanRequestJsListener(request);
deep_scan_requests_[request.request_token()].request_time =
base::Time::Now();
}
deep_scan_requests_[request.request_token()] = request;
deep_scan_requests_[request.request_token()].request = request;
for (auto* webui_listener : webui_instances_)
webui_listener->NotifyDeepScanJsListener(
request.request_token(), deep_scan_requests_[request.request_token()]);
}
void WebUIInfoSingleton::AddToDeepScanResponses(
......@@ -271,15 +276,16 @@ void WebUIInfoSingleton::AddToDeepScanResponses(
if (!HasListener())
return;
for (auto* webui_listener : webui_instances_)
webui_listener->NotifyDeepScanResponseJsListener(token, status, response);
deep_scan_requests_[token].response_time = base::Time::Now();
deep_scan_requests_[token].response_status = status;
deep_scan_requests_[token].response = response;
deep_scan_responses_[token] = std::make_pair(status, response);
for (auto* webui_listener : webui_instances_)
webui_listener->NotifyDeepScanJsListener(token, deep_scan_requests_[token]);
}
void WebUIInfoSingleton::ClearDeepScans() {
DeepScanningRequestMap().swap(deep_scan_requests_);
StatusAndDeepScanningResponseMap().swap(deep_scan_responses_);
base::flat_map<std::string, DeepScanDebugData>().swap(deep_scan_requests_);
}
#endif
void WebUIInfoSingleton::RegisterWebUIInstance(SafeBrowsingUIHandler* webui) {
......@@ -337,6 +343,12 @@ void WebUIInfoSingleton::MaybeClearData() {
}
}
#if BUILDFLAG(FULL_SAFE_BROWSING)
DeepScanDebugData::DeepScanDebugData() = default;
DeepScanDebugData::DeepScanDebugData(const DeepScanDebugData&) = default;
DeepScanDebugData::~DeepScanDebugData() = default;
#endif
namespace {
#if BUILDFLAG(SAFE_BROWSING_DB_LOCAL)
......@@ -1442,6 +1454,37 @@ std::string SerializeDeepScanningResponse(
serializer.Serialize(response_dict);
return response_serialized;
}
base::Value SerializeDeepScanDebugData(const std::string& token,
const DeepScanDebugData& data) {
base::DictionaryValue value;
value.SetStringKey("token", token);
if (!data.request_time.is_null()) {
value.SetDoubleKey("request_time", data.request_time.ToJsTime());
}
if (data.request.has_value()) {
value.SetStringKey("request",
SerializeDeepScanningRequest(data.request.value()));
}
if (!data.response_time.is_null()) {
value.SetDoubleKey("response_time", data.response_time.ToJsTime());
}
if (!data.response_status.empty()) {
value.SetStringKey("response_status", data.response_status);
}
if (data.response.has_value()) {
value.SetStringKey("response",
SerializeDeepScanningResponse(data.response.value()));
}
return std::move(value);
}
#endif
} // namespace
......@@ -1821,15 +1864,12 @@ void SafeBrowsingUIHandler::GetLogMessages(const base::ListValue* args) {
}
#if BUILDFLAG(FULL_SAFE_BROWSING)
void SafeBrowsingUIHandler::GetDeepScanRequests(const base::ListValue* args) {
void SafeBrowsingUIHandler::GetDeepScans(const base::ListValue* args) {
base::ListValue pings_sent;
for (const auto& token_and_request :
for (const auto& token_and_data :
WebUIInfoSingleton::GetInstance()->deep_scan_requests()) {
base::ListValue ping_entry;
ping_entry.Append(base::Value(token_and_request.first));
ping_entry.Append(
base::Value(SerializeDeepScanningRequest(token_and_request.second)));
pings_sent.Append(std::move(ping_entry));
pings_sent.Append(SerializeDeepScanDebugData(token_and_data.first,
token_and_data.second));
}
AllowJavascript();
......@@ -1837,31 +1877,8 @@ void SafeBrowsingUIHandler::GetDeepScanRequests(const base::ListValue* args) {
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id), pings_sent);
}
void SafeBrowsingUIHandler::GetDeepScanResponses(const base::ListValue* args) {
const WebUIInfoSingleton::StatusAndDeepScanningResponseMap& responses =
WebUIInfoSingleton::GetInstance()->deep_scan_responses();
base::ListValue responses_sent;
for (const auto& token_and_status_and_response : responses) {
const std::string& token = token_and_status_and_response.first;
const std::string& status = token_and_status_and_response.second.first;
const DeepScanningClientResponse& response =
token_and_status_and_response.second.second;
base::ListValue response_entry;
response_entry.Append(base::Value(token));
response_entry.Append(base::Value(status));
response_entry.Append(base::Value(SerializeDeepScanningResponse(response)));
responses_sent.Append(std::move(response_entry));
}
AllowJavascript();
std::string callback_id;
args->GetString(0, &callback_id);
ResolveJavascriptCallback(base::Value(callback_id), responses_sent);
}
#endif
void SafeBrowsingUIHandler::NotifyClientDownloadRequestJsListener(
ClientDownloadRequest* client_download_request) {
AllowJavascript();
......@@ -1955,27 +1972,12 @@ void SafeBrowsingUIHandler::NotifyReportingEventJsListener(
}
#if BUILDFLAG(FULL_SAFE_BROWSING)
void SafeBrowsingUIHandler::NotifyDeepScanRequestJsListener(
const DeepScanningClientRequest& request) {
base::ListValue request_list;
request_list.Append(base::Value(request.request_token()));
request_list.Append(base::Value(SerializeDeepScanningRequest(request)));
AllowJavascript();
FireWebUIListener("deep-scan-request-update", request_list);
}
void SafeBrowsingUIHandler::NotifyDeepScanResponseJsListener(
void SafeBrowsingUIHandler::NotifyDeepScanJsListener(
const std::string& token,
const std::string& status,
const DeepScanningClientResponse& response) {
base::ListValue response_list;
response_list.Append(base::Value(token));
response_list.Append(base::Value(status));
response_list.Append(base::Value(SerializeDeepScanningResponse(response)));
const DeepScanDebugData& deep_scan_data) {
AllowJavascript();
FireWebUIListener("deep-scan-response-update", response_list);
FireWebUIListener("deep-scan-request-update",
SerializeDeepScanDebugData(token, deep_scan_data));
}
#endif
......@@ -2051,13 +2053,8 @@ void SafeBrowsingUIHandler::RegisterMessages() {
base::Unretained(this)));
#if BUILDFLAG(FULL_SAFE_BROWSING)
web_ui()->RegisterMessageCallback(
"getDeepScanRequests",
base::BindRepeating(&SafeBrowsingUIHandler::GetDeepScanRequests,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"getDeepScanResponses",
base::BindRepeating(&SafeBrowsingUIHandler::GetDeepScanResponses,
base::Unretained(this)));
"getDeepScans", base::BindRepeating(&SafeBrowsingUIHandler::GetDeepScans,
base::Unretained(this)));
#endif
}
......
......@@ -34,6 +34,21 @@ namespace safe_browsing {
class WebUIInfoSingleton;
class ReferrerChainProvider;
#if BUILDFLAG(FULL_SAFE_BROWSING)
struct DeepScanDebugData {
DeepScanDebugData();
DeepScanDebugData(const DeepScanDebugData&);
~DeepScanDebugData();
base::Time request_time;
base::Optional<DeepScanningClientRequest> request;
base::Time response_time;
std::string response_status;
base::Optional<DeepScanningClientResponse> response;
};
#endif
class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
public:
SafeBrowsingUIHandler(content::BrowserContext* context);
......@@ -115,12 +130,9 @@ class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
#if BUILDFLAG(FULL_SAFE_BROWSING)
// Get the deep scanning requests that have been collected since the oldest
// currently open chrome://safe-browsing tab was opened.
void GetDeepScanRequests(const base::ListValue* args);
// Get the deep scanning responses that have been collected since the oldest
// currently open chrome://safe-browsing tab was opened.
void GetDeepScanResponses(const base::ListValue* args);
void GetDeepScans(const base::ListValue* args);
#endif
// Register callbacks for WebUI messages.
void RegisterMessages() override;
......@@ -182,18 +194,12 @@ class SafeBrowsingUIHandler : public content::WebUIMessageHandler {
void NotifyReportingEventJsListener(const base::Value& event);
#if BUILDFLAG(FULL_SAFE_BROWSING)
// Called when any new deep scan requests are sent while one or more WebUI
// Called when any deep scans are updated while one or more WebUI
// tabs are open.
void NotifyDeepScanRequestJsListener(
const DeepScanningClientRequest& request);
// Called when any new PhishGuard responses are received while one or more
// WebUI tabs are open.
void NotifyDeepScanResponseJsListener(
const std::string& token,
const std::string& status,
const DeepScanningClientResponse& response);
void NotifyDeepScanJsListener(const std::string& token,
const DeepScanDebugData& request);
#endif
// Callback when the CookieManager has returned the cookie.
void OnGetCookie(const std::string& callback_id,
const std::vector<net::CanonicalCookie>& cookies);
......@@ -220,13 +226,6 @@ class SafeBrowsingUI : public content::WebUIController {
class WebUIInfoSingleton {
public:
#if BUILDFLAG(FULL_SAFE_BROWSING)
using DeepScanningRequestMap =
std::map<std::string, DeepScanningClientRequest>;
using StatusAndDeepScanningResponseMap =
std::map<std::string, std::pair<std::string, DeepScanningClientResponse>>;
#endif
static WebUIInfoSingleton* GetInstance();
// Returns true when there is a listening chrome://safe-browsing tab.
......@@ -318,7 +317,7 @@ class WebUIInfoSingleton {
// and response.
void AddToDeepScanRequests(const DeepScanningClientRequest& request);
// Add the new response to |deep_scan_responses_| and send it to all the open
// Add the new response to |deep_scan_requests_| and send it to all the open
// chrome://safe-browsing tabs.
void AddToDeepScanResponses(const std::string& token,
const std::string& status,
......@@ -400,15 +399,10 @@ class WebUIInfoSingleton {
// Get the collection of deep scanning requests since the oldest currently
// open chrome://safe-browsing tab was opened. Returns a map from a unique
// token to the request proto.
const DeepScanningRequestMap& deep_scan_requests() const {
const base::flat_map<std::string, DeepScanDebugData>& deep_scan_requests()
const {
return deep_scan_requests_;
}
// Get the list of deep scan responses since the oldest currently open
// chrome://safe-browsing tab was opened.
const StatusAndDeepScanningResponseMap& deep_scan_responses() const {
return deep_scan_responses_;
}
#endif
ReferrerChainProvider* referrer_chain_provider() {
......@@ -507,12 +501,8 @@ class WebUIInfoSingleton {
#if BUILDFLAG(FULL_SAFE_BROWSING)
// Map of deep scan requests sent since the oldest currently open
// chrome://safe-browsing tab was opened. Maps from the unique token per
// request to the request proto.
DeepScanningRequestMap deep_scan_requests_;
// List of deep scan responses received since the oldest currently open
// chrome://safe-browsing tab was opened.
StatusAndDeepScanningResponseMap deep_scan_responses_;
// request to the data about the request.
base::flat_map<std::string, DeepScanDebugData> deep_scan_requests_;
#endif
// The current referrer chain provider, if any. Can be nullptr.
......
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