Commit 641f208d authored by dpapad's avatar dpapad Committed by Commit Bot

Simplify C++ -> JS messaging in chrome://quota-internals.

This is in preparation of migrating chrome://quota-internals to use
JS modules.

 - Replace calls to CallJavascriptFunctionUnsafe with FireWebUIListener.
 - Replace event handlers on unnecessary custom EventTarget instances
   with cr.addWebUIListener() calls.

Bug: 1028829
Change-Id: Id5a97d5e81a4e920dfe116a4b93cbacbf5ce55fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2478847Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Commit-Queue: dpapad <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#818397}
parent 53526e65
...@@ -245,20 +245,14 @@ function getOriginObject(type, host, origin) { ...@@ -245,20 +245,14 @@ function getOriginObject(type, host, origin) {
return originObject; return originObject;
} }
/** /** @param {number} space Total available disk space. */
* Event Handler for |cr.quota.onAvailableSpaceUpdated|. function handleAvailableSpace(space) {
* |event.detail| contains |availableSpace|. availableSpace = space;
* |availableSpace| represents total available disk space.
* @param {!CustomEvent<number>} event AvailableSpaceUpdated event.
*/
function handleAvailableSpace(event) {
availableSpace = event.detail;
$('diskspace-entry').textContent = numBytesToText_(availableSpace); $('diskspace-entry').textContent = numBytesToText_(availableSpace);
} }
/** /**
* Event Handler for |cr.quota.onGlobalInfoUpdated|. * |data| contains a record which has:
* |event.detail| contains a record which has:
* |type|: * |type|:
* Storage type, that is either 'temporary' or 'persistent'. * Storage type, that is either 'temporary' or 'persistent'.
* |usage|: * |usage|:
...@@ -270,15 +264,14 @@ function handleAvailableSpace(event) { ...@@ -270,15 +264,14 @@ function handleAvailableSpace(event) {
* *
* |usage|, |unlimitedUsage| and |quota| can be missing, * |usage|, |unlimitedUsage| and |quota| can be missing,
* and some additional fields can be included. * and some additional fields can be included.
* @param {!CustomEvent<!{ * @param {!{
* type: string, * type: string,
* usage: ?number, * usage: ?number,
* unlimitedUsage: ?number, * unlimitedUsage: ?number,
* quota: ?string * quota: ?string
* }>} event GlobalInfoUpdated event. * }} data
*/ */
function handleGlobalInfo(event) { function handleGlobalInfo(data) {
const data = event.detail;
const storageObject = getStorageObject(data.type); const storageObject = getStorageObject(data.type);
copyAttributes_(data, storageObject.detail.payload); copyAttributes_(data, storageObject.detail.payload);
storageObject.reveal(); storageObject.reveal();
...@@ -288,8 +281,7 @@ function handleGlobalInfo(event) { ...@@ -288,8 +281,7 @@ function handleGlobalInfo(event) {
} }
/** /**
* Event Handler for |cr.quota.onPerHostInfoUpdated|. * |dataArray| contains records which have:
* |event.detail| contains records which have:
* |host|: * |host|:
* Hostname of the entry. (e.g. 'example.com') * Hostname of the entry. (e.g. 'example.com')
* |type|: * |type|:
...@@ -301,16 +293,14 @@ function handleGlobalInfo(event) { ...@@ -301,16 +293,14 @@ function handleGlobalInfo(event) {
* *
* |usage| and |quota| can be missing, * |usage| and |quota| can be missing,
* and some additional fields can be included. * and some additional fields can be included.
* @param {!CustomEvent<!Array<{ * @param {!Array<{
* host: string, * host: string,
* type: string, * type: string,
* usage: ?number, * usage: ?number,
* quota: ?number * quota: ?number
* }>>} event PerHostInfoUpdated event. * }>} dataArray
*/ */
function handlePerHostInfo(event) { function handlePerHostInfo(dataArray) {
const dataArray = event.detail;
for (let i = 0; i < dataArray.length; ++i) { for (let i = 0; i < dataArray.length; ++i) {
const data = dataArray[i]; const data = dataArray[i];
const hostObject = getHostObject(data.type, data.host); const hostObject = getHostObject(data.type, data.host);
...@@ -323,8 +313,7 @@ function handlePerHostInfo(event) { ...@@ -323,8 +313,7 @@ function handlePerHostInfo(event) {
} }
/** /**
* Event Handler for |cr.quota.onPerOriginInfoUpdated|. * |dataArray| contains records which have:
* |event.detail| contains records which have:
* |origin|: * |origin|:
* Origin URL of the entry. * Origin URL of the entry.
* |type|: * |type|:
...@@ -344,7 +333,7 @@ function handlePerHostInfo(event) { ...@@ -344,7 +333,7 @@ function handlePerHostInfo(event) {
* *
* |inUse|, |usedCount|, |lastAccessTime| and |lastModifiedTime| can be missing, * |inUse|, |usedCount|, |lastAccessTime| and |lastModifiedTime| can be missing,
* and some additional fields can be included. * and some additional fields can be included.
* @param {!CustomEvent<!Array<!{ * @param {!Array<!{
* origin: string, * origin: string,
* type: string, * type: string,
* host: string, * host: string,
...@@ -352,11 +341,9 @@ function handlePerHostInfo(event) { ...@@ -352,11 +341,9 @@ function handlePerHostInfo(event) {
* usedCount: ?number, * usedCount: ?number,
* lastAccessTime: ?number, * lastAccessTime: ?number,
* lastModifiedTime: ?number * lastModifiedTime: ?number
* }>>} event PerOriginInfoUpdated event. * }>} dataArray
*/ */
function handlePerOriginInfo(event) { function handlePerOriginInfo(dataArray) {
const dataArray = event.detail;
for (let i = 0; i < dataArray.length; ++i) { for (let i = 0; i < dataArray.length; ++i) {
const data = dataArray[i]; const data = dataArray[i];
const originObject = getOriginObject(data.type, data.host, data.origin); const originObject = getOriginObject(data.type, data.host, data.origin);
...@@ -369,12 +356,10 @@ function handlePerOriginInfo(event) { ...@@ -369,12 +356,10 @@ function handlePerOriginInfo(event) {
} }
/** /**
* Event Handler for |cr.quota.onStatisticsUpdated|. * |data| contains misc statistics data as dictionary.
* |event.detail| contains misc statistics data as dictionary. * @param {!Object} data
* @param {!CustomEvent<!Object>} event StatisticsUpdated event.
*/ */
function handleStatistics(event) { function handleStatistics(data) {
const data = event.detail;
for (const key in data) { for (const key in data) {
let entry = statistics[key]; let entry = statistics[key];
if (!entry) { if (!entry) {
...@@ -391,13 +376,10 @@ function handleStatistics(event) { ...@@ -391,13 +376,10 @@ function handleStatistics(event) {
} }
/** /**
* Event Handler for |cr.quota.onStoragePressureFlagUpdated|. * @param {!{isStoragePressureEnabled: boolean}} data Contains a boolean
* |event.detail| contains a boolean representing whether or not to show * representing whether or not to show the storage pressure UI.
* the storage pressure UI.
* @param {!CustomEvent<!Object>} event StoragePressureFlagUpdated event.
*/ */
function handleStoragePressureFlagInfo(event) { function handleStoragePressureFlagInfo(data) {
const data = event.detail;
$('storage-pressure-loading').hidden = true; $('storage-pressure-loading').hidden = true;
if (data.isStoragePressureEnabled) { if (data.isStoragePressureEnabled) {
$('storage-pressure-outer').hidden = false; $('storage-pressure-outer').hidden = false;
...@@ -499,15 +481,14 @@ function dump() { ...@@ -499,15 +481,14 @@ function dump() {
function onLoad() { function onLoad() {
cr.ui.decorate('tabbox', cr.ui.TabBox); cr.ui.decorate('tabbox', cr.ui.TabBox);
cr.quota.onAvailableSpaceUpdated.addEventListener( cr.addWebUIListener('AvailableSpaceUpdated', handleAvailableSpace);
'update', handleAvailableSpace); cr.addWebUIListener('GlobalInfoUpdated', handleGlobalInfo);
cr.quota.onGlobalInfoUpdated.addEventListener('update', handleGlobalInfo); cr.addWebUIListener('PerHostInfoUpdated', handlePerHostInfo);
cr.quota.onPerHostInfoUpdated.addEventListener('update', handlePerHostInfo); cr.addWebUIListener('PerOriginInfoUpdated', handlePerOriginInfo);
cr.quota.onPerOriginInfoUpdated.addEventListener( cr.addWebUIListener('StatisticsUpdated', handleStatistics);
'update', handlePerOriginInfo); cr.addWebUIListener(
cr.quota.onStatisticsUpdated.addEventListener('update', handleStatistics); 'StoragePressureFlagUpdated', handleStoragePressureFlagInfo);
cr.quota.onStoragePressureFlagUpdated.addEventListener(
'update', handleStoragePressureFlagInfo);
cr.quota.requestInfo(); cr.quota.requestInfo();
$('refresh-button').addEventListener('click', cr.quota.requestInfo, false); $('refresh-button').addEventListener('click', cr.quota.requestInfo, false);
......
...@@ -3,14 +3,10 @@ ...@@ -3,14 +3,10 @@
// found in the LICENSE file. // found in the LICENSE file.
// require cr.js // require cr.js
// require cr/event_target.js
// require cr/util.js
/** /**
* Bridge between the browser and the page. * Bridge between the browser and the page.
* In this file: * In this file:
* * define EventTargets to receive message from the browser,
* * dispatch browser messages to EventTarget,
* * define interface to request data to the browser. * * define interface to request data to the browser.
*/ */
...@@ -31,66 +27,10 @@ cr.define('cr.quota', function() { ...@@ -31,66 +27,10 @@ cr.define('cr.quota', function() {
chrome.send('triggerStoragePressure', [origin]); chrome.send('triggerStoragePressure', [origin]);
} }
/**
* Callback entry point from Browser.
* Messages are Dispatched as Event to:
* * onAvailableSpaceUpdated,
* * onGlobalInfoUpdated,
* * onPerHostInfoUpdated,
* * onPerOriginInfoUpdated,
* * onStatisticsUpdated,
* * onStoragePressureFlagUpdated.
* @param {string} message Message label. Possible Values are:
* * 'AvailableSpaceUpdated',
* * 'GlobalInfoUpdated',
* * 'PerHostInfoUpdated',
* * 'PerOriginInfoUpdated',
* * 'StatisticsUpdated',
* * 'StoragePressureFlagUpdated'.
* @param {Object} detail Message specific additional data.
*/
function messageHandler(message, detail) {
let target = null;
switch (message) {
case 'AvailableSpaceUpdated':
target = cr.quota.onAvailableSpaceUpdated;
break;
case 'GlobalInfoUpdated':
target = cr.quota.onGlobalInfoUpdated;
break;
case 'PerHostInfoUpdated':
target = cr.quota.onPerHostInfoUpdated;
break;
case 'PerOriginInfoUpdated':
target = cr.quota.onPerOriginInfoUpdated;
break;
case 'StatisticsUpdated':
target = cr.quota.onStatisticsUpdated;
break;
case 'StoragePressureFlagUpdated':
target = cr.quota.onStoragePressureFlagUpdated;
break;
default:
console.error('Unknown Message');
break;
}
if (target) {
const event = document.createEvent('CustomEvent');
event.initCustomEvent('update', false, false, detail);
target.dispatchEvent(event);
}
}
return {
onAvailableSpaceUpdated: new cr.EventTarget(),
onGlobalInfoUpdated: new cr.EventTarget(),
onPerHostInfoUpdated: new cr.EventTarget(),
onPerOriginInfoUpdated: new cr.EventTarget(),
onStatisticsUpdated: new cr.EventTarget(),
onStoragePressureFlagUpdated: new cr.EventTarget(),
return {
requestInfo: requestInfo, requestInfo: requestInfo,
triggerStoragePressure: triggerStoragePressure, triggerStoragePressure: triggerStoragePressure,
messageHandler: messageHandler
}; };
}); });
...@@ -51,13 +51,13 @@ void QuotaInternalsHandler::RegisterMessages() { ...@@ -51,13 +51,13 @@ void QuotaInternalsHandler::RegisterMessages() {
} }
void QuotaInternalsHandler::ReportAvailableSpace(int64_t available_space) { void QuotaInternalsHandler::ReportAvailableSpace(int64_t available_space) {
SendMessage("AvailableSpaceUpdated", FireWebUIListener("AvailableSpaceUpdated",
base::Value(static_cast<double>(available_space))); base::Value(static_cast<double>(available_space)));
} }
void QuotaInternalsHandler::ReportGlobalInfo(const GlobalStorageInfo& data) { void QuotaInternalsHandler::ReportGlobalInfo(const GlobalStorageInfo& data) {
std::unique_ptr<base::Value> value(data.NewValue()); std::unique_ptr<base::Value> value(data.NewValue());
SendMessage("GlobalInfoUpdated", *value); FireWebUIListener("GlobalInfoUpdated", *value);
} }
void QuotaInternalsHandler::ReportPerHostInfo( void QuotaInternalsHandler::ReportPerHostInfo(
...@@ -67,7 +67,7 @@ void QuotaInternalsHandler::ReportPerHostInfo( ...@@ -67,7 +67,7 @@ void QuotaInternalsHandler::ReportPerHostInfo(
values.Append(itr->NewValue()); values.Append(itr->NewValue());
} }
SendMessage("PerHostInfoUpdated", values); FireWebUIListener("PerHostInfoUpdated", values);
} }
void QuotaInternalsHandler::ReportPerOriginInfo( void QuotaInternalsHandler::ReportPerOriginInfo(
...@@ -77,7 +77,7 @@ void QuotaInternalsHandler::ReportPerOriginInfo( ...@@ -77,7 +77,7 @@ void QuotaInternalsHandler::ReportPerOriginInfo(
origins_value.Append(itr->NewValue()); origins_value.Append(itr->NewValue());
} }
SendMessage("PerOriginInfoUpdated", origins_value); FireWebUIListener("PerOriginInfoUpdated", origins_value);
} }
void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) { void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) {
...@@ -86,23 +86,18 @@ void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) { ...@@ -86,23 +86,18 @@ void QuotaInternalsHandler::ReportStatistics(const Statistics& stats) {
dict.SetString(itr->first, itr->second); dict.SetString(itr->first, itr->second);
} }
SendMessage("StatisticsUpdated", dict); FireWebUIListener("StatisticsUpdated", dict);
} }
void QuotaInternalsHandler::ReportStoragePressureFlag() { void QuotaInternalsHandler::ReportStoragePressureFlag() {
base::DictionaryValue flag_enabled; base::DictionaryValue flag_enabled;
flag_enabled.SetBoolean("isStoragePressureEnabled", flag_enabled.SetBoolean("isStoragePressureEnabled",
IsStoragePressureEnabled()); IsStoragePressureEnabled());
SendMessage("StoragePressureFlagUpdated", flag_enabled); FireWebUIListener("StoragePressureFlagUpdated", flag_enabled);
}
void QuotaInternalsHandler::SendMessage(const std::string& message,
const base::Value& value) {
web_ui()->CallJavascriptFunctionUnsafe("cr.quota.messageHandler",
base::Value(message), value);
} }
void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) { void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) {
AllowJavascript();
if (!proxy_.get()) if (!proxy_.get())
proxy_ = new QuotaInternalsProxy(this); proxy_ = new QuotaInternalsProxy(this);
ReportStoragePressureFlag(); ReportStoragePressureFlag();
...@@ -113,6 +108,7 @@ void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) { ...@@ -113,6 +108,7 @@ void QuotaInternalsHandler::OnRequestInfo(const base::ListValue*) {
void QuotaInternalsHandler::OnTriggerStoragePressure( void QuotaInternalsHandler::OnTriggerStoragePressure(
const base::ListValue* args) { const base::ListValue* args) {
AllowJavascript();
CHECK_EQ(1U, args->GetSize()); CHECK_EQ(1U, args->GetSize());
std::string origin_string; std::string origin_string;
CHECK(args->GetString(0, &origin_string)); CHECK(args->GetString(0, &origin_string));
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include "content/public/browser/web_ui_message_handler.h" #include "content/public/browser/web_ui_message_handler.h"
namespace base { namespace base {
class Value;
class ListValue; class ListValue;
} }
...@@ -47,7 +46,6 @@ class QuotaInternalsHandler : public content::WebUIMessageHandler { ...@@ -47,7 +46,6 @@ class QuotaInternalsHandler : public content::WebUIMessageHandler {
private: private:
void OnRequestInfo(const base::ListValue*); void OnRequestInfo(const base::ListValue*);
void OnTriggerStoragePressure(const base::ListValue*); void OnTriggerStoragePressure(const base::ListValue*);
void SendMessage(const std::string& message, const base::Value& value);
scoped_refptr<QuotaInternalsProxy> proxy_; scoped_refptr<QuotaInternalsProxy> proxy_;
......
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