Commit eec18700 authored by Demetrios Papadopoulos's avatar Demetrios Papadopoulos Committed by Commit Bot

Eliminate calls to CallJavascriptFunctionUnsafe in chrome://components.

 - Moving ComponentsDomHandler to its own .h/cc files,
   and renaming to ComponentsHandler.
 - Moving some logic from ComponentsUI to ComponentsHandler so that proper
   lifetime methods (OnJavascriptAllowed/Disallowed) can be leveraged.

This is in preparation of migrating this page to JS modules.

Bug: 1028829
Change-Id: I530c439b3e659f3b1b5e5dc3ef05f2a3df88559e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1939893
Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org>
Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Auto-Submit: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#722633}
parent 4cb9d73d
......@@ -67,6 +67,7 @@
</div>
</div>
<script src="chrome://resources/js/util.js"></script>
<script src="chrome://resources/js/promise_resolver.js"></script>
<script src="chrome://resources/js/cr.js"></script>
<script src="chrome://components/components.js"></script>
<script src="chrome://resources/js/load_time_data.js"></script>
......
......@@ -31,10 +31,9 @@ function renderTemplate(componentsData) {
/**
* Asks the C++ ComponentsDOMHandler to get details about the installed
* components.
* The ComponentsDOMHandler should reply to returnComponentsData() (below).
*/
function requestComponentsData() {
chrome.send('requestComponentsData');
cr.sendWithPromise('requestComponentsData').then(returnComponentsData);
}
/**
......@@ -93,8 +92,7 @@ function returnComponentsData(componentsData) {
}
/**
* This event function is called from component UI indicating changed state
* of component updater service.
* Listener called when state of component updater service changes.
* @param {Object} eventArgs Contains event and component ID. Component ID is
* optional.
*/
......@@ -135,4 +133,7 @@ function handleCheckUpdate(node) {
}
// Get data and have it displayed upon loading.
document.addEventListener('DOMContentLoaded', requestComponentsData);
document.addEventListener('DOMContentLoaded', function() {
cr.addWebUIListener('component-event', onComponentEvent);
requestComponentsData();
});
......@@ -210,6 +210,8 @@ jumbo_static_library("ui") {
"webui/autofill_and_password_manager_internals/password_manager_internals_ui.h",
"webui/chrome_web_ui_controller_factory.cc",
"webui/chrome_web_ui_controller_factory.h",
"webui/components_handler.cc",
"webui/components_handler.h",
"webui/components_ui.cc",
"webui/components_ui.h",
"webui/constrained_web_dialog_ui.cc",
......
// Copyright 2019 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.
#include "chrome/browser/ui/webui/components_handler.h"
#include "base/bind.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/grit/generated_resources.h"
#include "components/update_client/crx_update_item.h"
#include "ui/base/l10n/l10n_util.h"
ComponentsHandler::ComponentsHandler() = default;
ComponentsHandler::~ComponentsHandler() = default;
void ComponentsHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"requestComponentsData",
base::BindRepeating(&ComponentsHandler::HandleRequestComponentsData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"checkUpdate", base::BindRepeating(&ComponentsHandler::HandleCheckUpdate,
base::Unretained(this)));
}
void ComponentsHandler::OnJavascriptAllowed() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
cus->AddObserver(this);
}
void ComponentsHandler::OnJavascriptDisallowed() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
if (cus)
cus->RemoveObserver(this);
}
void ComponentsHandler::HandleRequestComponentsData(
const base::ListValue* args) {
AllowJavascript();
const base::Value& callback_id = args->GetList()[0];
base::DictionaryValue result;
result.Set("components", LoadComponents());
ResolveJavascriptCallback(callback_id, result);
}
// This function is called when user presses button from html UI.
// TODO(shrikant): We need to make this button available based on current
// state e.g. If component state is currently updating then we need to disable
// button. (https://code.google.com/p/chromium/issues/detail?id=272540)
void ComponentsHandler::HandleCheckUpdate(const base::ListValue* args) {
if (args->GetSize() != 1) {
NOTREACHED();
return;
}
std::string component_id;
if (!args->GetString(0, &component_id)) {
NOTREACHED();
return;
}
OnDemandUpdate(component_id);
}
void ComponentsHandler::OnEvent(Events event, const std::string& id) {
base::DictionaryValue parameters;
parameters.SetString("event", ComponentEventToString(event));
if (!id.empty()) {
if (event == Events::COMPONENT_UPDATED) {
auto* component_updater = g_browser_process->component_updater();
update_client::CrxUpdateItem item;
if (component_updater->GetComponentDetails(id, &item) && item.component)
parameters.SetString("version", item.component->version.GetString());
}
parameters.SetString("id", id);
}
FireWebUIListener("component-event", parameters);
}
base::string16 ComponentsHandler::ComponentEventToString(Events event) {
switch (event) {
case Events::COMPONENT_CHECKING_FOR_UPDATES:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED);
case Events::COMPONENT_WAIT:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING);
case Events::COMPONENT_UPDATE_FOUND:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND);
case Events::COMPONENT_UPDATE_READY:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY);
case Events::COMPONENT_UPDATED:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED);
case Events::COMPONENT_NOT_UPDATED:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED);
case Events::COMPONENT_UPDATE_ERROR:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATE_ERROR);
case Events::COMPONENT_UPDATE_DOWNLOADING:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_DOWNLOADING);
}
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
base::string16 ComponentsHandler::ServiceStatusToString(
update_client::ComponentState state) {
// TODO(sorin): handle kDownloaded. For now, just handle it as kUpdating.
switch (state) {
case update_client::ComponentState::kNew:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW);
case update_client::ComponentState::kChecking:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING);
case update_client::ComponentState::kCanUpdate:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE);
case update_client::ComponentState::kDownloadingDiff:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF);
case update_client::ComponentState::kDownloading:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL);
case update_client::ComponentState::kUpdatingDiff:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF);
case update_client::ComponentState::kUpdating:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING);
case update_client::ComponentState::kDownloaded:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DOWNLOADED);
case update_client::ComponentState::kUpdated:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED);
case update_client::ComponentState::kUpToDate:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE);
case update_client::ComponentState::kUpdateError:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE_ERROR);
case update_client::ComponentState::kUninstalled: // Fall through.
case update_client::ComponentState::kRun:
case update_client::ComponentState::kLastStatus:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
// static
void ComponentsHandler::OnDemandUpdate(const std::string& component_id) {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
if (cus) {
cus->GetOnDemandUpdater().OnDemandUpdate(
component_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
component_updater::Callback());
}
}
// static
std::unique_ptr<base::ListValue> ComponentsHandler::LoadComponents() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
std::vector<std::string> component_ids;
component_ids = cus->GetComponentIDs();
// Construct DictionaryValues to return to UI.
auto component_list = std::make_unique<base::ListValue>();
for (size_t j = 0; j < component_ids.size(); ++j) {
update_client::CrxUpdateItem item;
if (cus->GetComponentDetails(component_ids[j], &item)) {
auto component_entry = std::make_unique<base::DictionaryValue>();
component_entry->SetString("id", component_ids[j]);
component_entry->SetString("status", ServiceStatusToString(item.state));
if (item.component) {
component_entry->SetString("name", item.component->name);
component_entry->SetString("version",
item.component->version.GetString());
}
component_list->Append(std::move(component_entry));
}
}
return component_list;
}
// Copyright 2019 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.
#ifndef CHROME_BROWSER_UI_WEBUI_COMPONENTS_HANDLER_H_
#define CHROME_BROWSER_UI_WEBUI_COMPONENTS_HANDLER_H_
#include <memory>
#include <string>
#include "base/strings/string16.h"
#include "components/component_updater/component_updater_service.h"
#include "components/update_client/update_client.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_ui_message_handler.h"
namespace base {
class ListValue;
}
// The handler for Javascript messages for the chrome://components/ page.
class ComponentsHandler : public content::WebUIMessageHandler,
public component_updater::ServiceObserver {
public:
ComponentsHandler();
ComponentsHandler(const ComponentsHandler&) = delete;
ComponentsHandler& operator=(const ComponentsHandler&) = delete;
~ComponentsHandler() override;
// WebUIMessageHandler implementation.
void RegisterMessages() override;
void OnJavascriptAllowed() override;
void OnJavascriptDisallowed() override;
// Callback for the "requestComponentsData" message.
void HandleRequestComponentsData(const base::ListValue* args);
// Callback for the "checkUpdate" message.
void HandleCheckUpdate(const base::ListValue* args);
// ServiceObserver implementation.
void OnEvent(Events event, const std::string& id) override;
private:
static base::string16 ComponentEventToString(Events event);
static base::string16 ServiceStatusToString(
update_client::ComponentState state);
static std::unique_ptr<base::ListValue> LoadComponents();
static void OnDemandUpdate(const std::string& component_id);
content::NotificationRegistrar registrar_;
};
#endif // CHROME_BROWSER_UI_WEBUI_COMPONENTS_HANDLER_H_
......@@ -18,26 +18,21 @@
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/components_handler.h"
#include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/browser_resources.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/update_client/crx_update_item.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#if defined(OS_CHROMEOS)
#include "components/user_manager/user_manager.h"
#endif
using content::WebUIMessageHandler;
namespace {
content::WebUIDataSource* CreateComponentsUIHTMLSource(Profile* profile) {
......@@ -73,74 +68,6 @@ content::WebUIDataSource* CreateComponentsUIHTMLSource(Profile* profile) {
return source;
}
////////////////////////////////////////////////////////////////////////////////
//
// ComponentsDOMHandler
//
////////////////////////////////////////////////////////////////////////////////
// The handler for Javascript messages for the chrome://components/ page.
class ComponentsDOMHandler : public WebUIMessageHandler {
public:
ComponentsDOMHandler();
~ComponentsDOMHandler() override {}
// WebUIMessageHandler implementation.
void RegisterMessages() override;
// Callback for the "requestComponentsData" message.
void HandleRequestComponentsData(const base::ListValue* args);
// Callback for the "checkUpdate" message.
void HandleCheckUpdate(const base::ListValue* args);
private:
content::NotificationRegistrar registrar_;
DISALLOW_COPY_AND_ASSIGN(ComponentsDOMHandler);
};
ComponentsDOMHandler::ComponentsDOMHandler() {
}
void ComponentsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"requestComponentsData",
base::BindRepeating(&ComponentsDOMHandler::HandleRequestComponentsData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"checkUpdate",
base::BindRepeating(&ComponentsDOMHandler::HandleCheckUpdate,
base::Unretained(this)));
}
void ComponentsDOMHandler::HandleRequestComponentsData(
const base::ListValue* args) {
base::DictionaryValue result;
result.Set("components", ComponentsUI::LoadComponents());
web_ui()->CallJavascriptFunctionUnsafe("returnComponentsData", result);
}
// This function is called when user presses button from html UI.
// TODO(shrikant): We need to make this button available based on current
// state e.g. If component state is currently updating then we need to disable
// button. (https://code.google.com/p/chromium/issues/detail?id=272540)
void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) {
if (args->GetSize() != 1) {
NOTREACHED();
return;
}
std::string component_id;
if (!args->GetString(0, &component_id)) {
NOTREACHED();
return;
}
ComponentsUI::OnDemandUpdate(component_id);
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
......@@ -150,58 +77,16 @@ void ComponentsDOMHandler::HandleCheckUpdate(const base::ListValue* args) {
///////////////////////////////////////////////////////////////////////////////
ComponentsUI::ComponentsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
web_ui->AddMessageHandler(std::make_unique<ComponentsDOMHandler>());
web_ui->AddMessageHandler(std::make_unique<ComponentsHandler>());
// Set up the chrome://components/ source.
Profile* profile = Profile::FromWebUI(web_ui);
content::WebUIDataSource::Add(profile, CreateComponentsUIHTMLSource(profile));
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
cus->AddObserver(this);
}
ComponentsUI::~ComponentsUI() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
if (cus)
cus->RemoveObserver(this);
}
// static
void ComponentsUI::OnDemandUpdate(const std::string& component_id) {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
cus->GetOnDemandUpdater().OnDemandUpdate(
component_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
component_updater::Callback());
}
// static
std::unique_ptr<base::ListValue> ComponentsUI::LoadComponents() {
component_updater::ComponentUpdateService* cus =
g_browser_process->component_updater();
std::vector<std::string> component_ids;
component_ids = cus->GetComponentIDs();
// Construct DictionaryValues to return to UI.
auto component_list = std::make_unique<base::ListValue>();
for (size_t j = 0; j < component_ids.size(); ++j) {
update_client::CrxUpdateItem item;
if (cus->GetComponentDetails(component_ids[j], &item)) {
auto component_entry = std::make_unique<base::DictionaryValue>();
component_entry->SetString("id", component_ids[j]);
component_entry->SetString("status", ServiceStatusToString(item.state));
if (item.component) {
component_entry->SetString("name", item.component->name);
component_entry->SetString("version",
item.component->version.GetString());
}
component_list->Append(std::move(component_entry));
}
}
return component_list;
}
// static
base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes(
......@@ -210,73 +95,3 @@ base::RefCountedMemory* ComponentsUI::GetFaviconResourceBytes(
IDR_PLUGINS_FAVICON, scale_factor);
}
base::string16 ComponentsUI::ComponentEventToString(Events event) {
switch (event) {
case Events::COMPONENT_CHECKING_FOR_UPDATES:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_STARTED);
case Events::COMPONENT_WAIT:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_SLEEPING);
case Events::COMPONENT_UPDATE_FOUND:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_FOUND);
case Events::COMPONENT_UPDATE_READY:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_READY);
case Events::COMPONENT_UPDATED:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATED);
case Events::COMPONENT_NOT_UPDATED:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_NOTUPDATED);
case Events::COMPONENT_UPDATE_ERROR:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_UPDATE_ERROR);
case Events::COMPONENT_UPDATE_DOWNLOADING:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_EVT_STATUS_DOWNLOADING);
}
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
base::string16 ComponentsUI::ServiceStatusToString(
update_client::ComponentState state) {
// TODO(sorin): handle kDownloaded. For now, just handle it as kUpdating.
switch (state) {
case update_client::ComponentState::kNew:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_NEW);
case update_client::ComponentState::kChecking:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_CHECKING);
case update_client::ComponentState::kCanUpdate:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE);
case update_client::ComponentState::kDownloadingDiff:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL_DIFF);
case update_client::ComponentState::kDownloading:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DNL);
case update_client::ComponentState::kUpdatingDiff:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDT_DIFF);
case update_client::ComponentState::kUpdating:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATING);
case update_client::ComponentState::kDownloaded:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_DOWNLOADED);
case update_client::ComponentState::kUpdated:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATED);
case update_client::ComponentState::kUpToDate:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPTODATE);
case update_client::ComponentState::kUpdateError:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_SVC_STATUS_UPDATE_ERROR);
case update_client::ComponentState::kUninstalled: // Fall through.
case update_client::ComponentState::kRun:
case update_client::ComponentState::kLastStatus:
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
return l10n_util::GetStringUTF16(IDS_COMPONENTS_UNKNOWN);
}
void ComponentsUI::OnEvent(Events event, const std::string& id) {
base::DictionaryValue parameters;
parameters.SetString("event", ComponentEventToString(event));
if (!id.empty()) {
if (event == Events::COMPONENT_UPDATED) {
auto* component_updater = g_browser_process->component_updater();
update_client::CrxUpdateItem item;
if (component_updater->GetComponentDetails(id, &item) && item.component)
parameters.SetString("version", item.component->version.GetString());
}
parameters.SetString("id", id);
}
web_ui()->CallJavascriptFunctionUnsafe("onComponentEvent", parameters);
}
......@@ -6,39 +6,24 @@
#define CHROME_BROWSER_UI_WEBUI_COMPONENTS_UI_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "components/component_updater/component_updater_service.h"
#include "components/update_client/update_client.h"
#include "content/public/browser/web_ui_controller.h"
#include "ui/base/layout.h"
namespace base {
class ListValue;
class RefCountedMemory;
}
class ComponentsUI : public content::WebUIController,
public component_updater::ServiceObserver {
class ComponentsUI : public content::WebUIController {
public:
explicit ComponentsUI(content::WebUI* web_ui);
~ComponentsUI() override;
static void OnDemandUpdate(const std::string& component_id);
static std::unique_ptr<base::ListValue> LoadComponents();
static base::RefCountedMemory* GetFaviconResourceBytes(
ui::ScaleFactor scale_factor);
// ServiceObserver implementation.
void OnEvent(Events event, const std::string& id) override;
private:
static base::string16 ComponentEventToString(Events event);
static base::string16 ServiceStatusToString(
update_client::ComponentState state);
DISALLOW_COPY_AND_ASSIGN(ComponentsUI);
};
......
......@@ -19,7 +19,7 @@
#include "components/update_client/update_client.h"
#include "url/gurl.h"
class ComponentsUI;
class ComponentsHandler;
class PluginObserver;
namespace policy {
......@@ -143,7 +143,7 @@ class ComponentUpdateService {
virtual bool GetComponentDetails(const std::string& id,
CrxUpdateItem* item) const = 0;
friend class ::ComponentsUI;
friend class ::ComponentsHandler;
FRIEND_TEST_ALL_PREFIXES(ComponentInstallerTest, RegisterComponent);
};
......@@ -162,7 +162,7 @@ class OnDemandUpdater {
friend class OnDemandTester;
friend class policy::ComponentUpdaterPolicyTest;
friend class SupervisedUserWhitelistInstaller;
friend class ::ComponentsUI;
friend class ::ComponentsHandler;
friend class ::PluginObserver;
friend class SwReporterOnDemandFetcher;
#if defined(OS_CHROMEOS)
......
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