blob: 05205dc4dcab442f43a3158452b2cbf80461b54f [file] [log] [blame]
Demetrios Papadopouloseec18702019-12-06 21:54:391// Copyright 2019 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Demetrios Papadopoulos4ba6c9a62019-12-07 03:26:015#ifndef CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_
Demetrios Papadopouloseec18702019-12-06 21:54:397
8#include <memory>
9#include <string>
10
Keishi Hattori0e45c022021-11-27 09:25:5211#include "base/memory/raw_ptr.h"
Sigurdur Asgeirssonecfc6da02021-01-07 19:05:4212#include "base/scoped_observation.h"
Xiaohan Wange9439fd2022-01-18 21:00:3113#include "build/build_config.h"
Demetrios Papadopouloseec18702019-12-06 21:54:3914#include "components/component_updater/component_updater_service.h"
15#include "components/update_client/update_client.h"
Demetrios Papadopouloseec18702019-12-06 21:54:3916#include "content/public/browser/web_ui_message_handler.h"
17
18namespace base {
19class ListValue;
20}
21
22// The handler for Javascript messages for the chrome://components/ page.
23class ComponentsHandler : public content::WebUIMessageHandler,
24 public component_updater::ServiceObserver {
25 public:
Dan Beam6599d5b2020-01-06 22:16:4726 ComponentsHandler(
27 component_updater::ComponentUpdateService* component_updater);
Demetrios Papadopouloseec18702019-12-06 21:54:3928 ComponentsHandler(const ComponentsHandler&) = delete;
29 ComponentsHandler& operator=(const ComponentsHandler&) = delete;
30 ~ComponentsHandler() override;
31
32 // WebUIMessageHandler implementation.
33 void RegisterMessages() override;
34 void OnJavascriptAllowed() override;
35 void OnJavascriptDisallowed() override;
36
37 // Callback for the "requestComponentsData" message.
38 void HandleRequestComponentsData(const base::ListValue* args);
39
40 // Callback for the "checkUpdate" message.
41 void HandleCheckUpdate(const base::ListValue* args);
42
43 // ServiceObserver implementation.
44 void OnEvent(Events event, const std::string& id) override;
45
Xiaohan Wange9439fd2022-01-18 21:00:3146#if BUILDFLAG(IS_CHROMEOS)
Stefan Kuhned6672382021-12-08 00:54:2247 // Callback for the "crosUrlComponentsRedirect" message.
48 void HandleCrosUrlComponentsRedirect(const base::ListValue* args);
49#endif
50
Demetrios Papadopouloseec18702019-12-06 21:54:3951 private:
Jan Wilken Dörrie3f97e292021-03-11 18:07:1452 static std::u16string ComponentEventToString(Events event);
53 static std::u16string ServiceStatusToString(
Demetrios Papadopouloseec18702019-12-06 21:54:3954 update_client::ComponentState state);
Dan Beam6599d5b2020-01-06 22:16:4755
56 std::unique_ptr<base::ListValue> LoadComponents();
57 void OnDemandUpdate(const std::string& component_id);
58
59 // Weak pointer; injected for testing.
Keishi Hattori0e45c022021-11-27 09:25:5260 const raw_ptr<component_updater::ComponentUpdateService> component_updater_;
Demetrios Papadopouloseec18702019-12-06 21:54:3961
Sigurdur Asgeirssonecfc6da02021-01-07 19:05:4262 base::ScopedObservation<component_updater::ComponentUpdateService,
63 component_updater::ComponentUpdateService::Observer>
64 observation_{this};
Demetrios Papadopouloseec18702019-12-06 21:54:3965};
66
Demetrios Papadopoulos4ba6c9a62019-12-07 03:26:0167#endif // CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_