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 <[email protected]>
Reviewed-by: Sorin Jianu <[email protected]>
Reviewed-by: Rebekah Potter <[email protected]>
Auto-Submit: Demetrios Papadopoulos <[email protected]>
Cr-Commit-Position: refs/heads/master@{#722633}
diff --git a/chrome/browser/ui/webui/components_handler.h b/chrome/browser/ui/webui/components_handler.h
new file mode 100644
index 0000000..a17d69a
--- /dev/null
+++ b/chrome/browser/ui/webui/components_handler.h
@@ -0,0 +1,54 @@
+// 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_