Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 1 | // 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 Papadopoulos | 4ba6c9a6 | 2019-12-07 03:26:01 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_ |
| 6 | #define CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_ |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Dan Beam | 78c18c3 | 2020-01-04 02:49:14 | [diff] [blame] | 11 | #include "base/scoped_observer.h" |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 12 | #include "base/strings/string16.h" |
| 13 | #include "components/component_updater/component_updater_service.h" |
| 14 | #include "components/update_client/update_client.h" |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 15 | #include "content/public/browser/web_ui_message_handler.h" |
| 16 | |
| 17 | namespace base { |
| 18 | class ListValue; |
| 19 | } |
| 20 | |
| 21 | // The handler for Javascript messages for the chrome://components/ page. |
| 22 | class ComponentsHandler : public content::WebUIMessageHandler, |
| 23 | public component_updater::ServiceObserver { |
| 24 | public: |
Dan Beam | 6599d5b | 2020-01-06 22:16:47 | [diff] [blame] | 25 | ComponentsHandler( |
| 26 | component_updater::ComponentUpdateService* component_updater); |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 27 | ComponentsHandler(const ComponentsHandler&) = delete; |
| 28 | ComponentsHandler& operator=(const ComponentsHandler&) = delete; |
| 29 | ~ComponentsHandler() override; |
| 30 | |
| 31 | // WebUIMessageHandler implementation. |
| 32 | void RegisterMessages() override; |
| 33 | void OnJavascriptAllowed() override; |
| 34 | void OnJavascriptDisallowed() override; |
| 35 | |
| 36 | // Callback for the "requestComponentsData" message. |
| 37 | void HandleRequestComponentsData(const base::ListValue* args); |
| 38 | |
| 39 | // Callback for the "checkUpdate" message. |
| 40 | void HandleCheckUpdate(const base::ListValue* args); |
| 41 | |
| 42 | // ServiceObserver implementation. |
| 43 | void OnEvent(Events event, const std::string& id) override; |
| 44 | |
| 45 | private: |
| 46 | static base::string16 ComponentEventToString(Events event); |
| 47 | static base::string16 ServiceStatusToString( |
| 48 | update_client::ComponentState state); |
Dan Beam | 6599d5b | 2020-01-06 22:16:47 | [diff] [blame] | 49 | |
| 50 | std::unique_ptr<base::ListValue> LoadComponents(); |
| 51 | void OnDemandUpdate(const std::string& component_id); |
| 52 | |
| 53 | // Weak pointer; injected for testing. |
| 54 | component_updater::ComponentUpdateService* const component_updater_; |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 55 | |
Dan Beam | 78c18c3 | 2020-01-04 02:49:14 | [diff] [blame] | 56 | ScopedObserver<component_updater::ComponentUpdateService, |
| 57 | component_updater::ComponentUpdateService::Observer> |
| 58 | observer_{this}; |
Demetrios Papadopoulos | eec1870 | 2019-12-06 21:54:39 | [diff] [blame] | 59 | }; |
| 60 | |
Demetrios Papadopoulos | 4ba6c9a6 | 2019-12-07 03:26:01 | [diff] [blame] | 61 | #endif // CHROME_BROWSER_UI_WEBUI_COMPONENTS_COMPONENTS_HANDLER_H_ |