fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #ifndef CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_ |
| 6 | #define CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_ |
| 7 | |
avi | 0f23343 | 2015-12-25 02:23:38 | [diff] [blame] | 8 | #include <stddef.h> |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 9 | #include <string.h> |
| 10 | |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 11 | #include <memory> |
Lei Zhang | 776ed071 | 2019-05-23 14:13:09 | [diff] [blame] | 12 | #include <string> |
dcheng | 9603ab9 | 2016-04-08 04:17:32 | [diff] [blame] | 13 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 14 | #include "base/macros.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
Alexander Hendrich | 02ab52e | 2019-12-04 15:47:43 | [diff] [blame] | 16 | #include "base/values.h" |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 17 | #include "build/branding_buildflags.h" |
| 18 | #include "build/build_config.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 19 | #include "components/policy/core/browser/policy_error_map.h" |
| 20 | #include "components/policy/core/common/policy_map.h" |
| 21 | #include "components/policy/core/common/policy_namespace.h" |
| 22 | #include "components/policy/core/common/policy_service.h" |
| 23 | #include "components/policy/core/common/schema_registry.h" |
| 24 | #include "content/public/browser/web_ui.h" |
| 25 | #include "content/public/browser/web_ui_data_source.h" |
| 26 | #include "content/public/browser/web_ui_message_handler.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 27 | #include "extensions/buildflags/buildflags.h" |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 28 | #include "ui/shell_dialogs/select_file_dialog.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 29 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 30 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 31 | #include "extensions/browser/extension_registry_observer.h" |
| 32 | #endif |
| 33 | |
tnagel | 8d2791ec | 2016-11-18 10:43:13 | [diff] [blame] | 34 | class PolicyStatusProvider; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 35 | |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 36 | namespace policy { |
| 37 | class PolicyMap; |
| 38 | } |
| 39 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 40 | // The JavaScript message handler for the chrome://policy page. |
| 41 | class PolicyUIHandler : public content::WebUIMessageHandler, |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 42 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 43 | public extensions::ExtensionRegistryObserver, |
| 44 | #endif |
| 45 | public policy::PolicyService::Observer, |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 46 | public policy::SchemaRegistry::Observer, |
| 47 | public ui::SelectFileDialog::Listener { |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 48 | public: |
| 49 | PolicyUIHandler(); |
| 50 | ~PolicyUIHandler() override; |
| 51 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 52 | static void AddCommonLocalizedStringsToSource( |
| 53 | content::WebUIDataSource* source); |
| 54 | |
| 55 | // content::WebUIMessageHandler implementation. |
| 56 | void RegisterMessages() override; |
| 57 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 58 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 59 | // extensions::ExtensionRegistryObserver implementation. |
| 60 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 61 | const extensions::Extension* extension) override; |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 62 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 63 | const extensions::Extension* extension, |
| 64 | extensions::UnloadedExtensionReason reason) override; |
Alexander Hendrich | 02ab52e | 2019-12-04 15:47:43 | [diff] [blame] | 65 | #endif // BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 66 | |
| 67 | // policy::PolicyService::Observer implementation. |
| 68 | void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 69 | const policy::PolicyMap& previous, |
| 70 | const policy::PolicyMap& current) override; |
| 71 | |
| 72 | // policy::SchemaRegistry::Observer implementation. |
| 73 | void OnSchemaRegistryUpdated(bool has_new_schemas) override; |
| 74 | |
| 75 | protected: |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 76 | // ui::SelectFileDialog::Listener implementation. |
| 77 | void FileSelected(const base::FilePath& path, |
| 78 | int index, |
| 79 | void* params) override; |
| 80 | void FileSelectionCanceled(void* params) override; |
| 81 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 82 | private: |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 83 | base::Value GetPolicyNames() const; |
| 84 | base::Value GetPolicyValues() const; |
| 85 | |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 86 | void AddExtensionPolicyNames(base::Value* names, |
Alexander Hendrich | 02ab52e | 2019-12-04 15:47:43 | [diff] [blame] | 87 | policy::PolicyDomain policy_domain) const; |
| 88 | |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 89 | void HandleExportPoliciesJson(const base::ListValue* args); |
| 90 | void HandleListenPoliciesUpdates(const base::ListValue* args); |
| 91 | void HandleReloadPolicies(const base::ListValue* args); |
Jérôme Gingras | 4b90f6fc | 2020-05-28 18:48:57 | [diff] [blame] | 92 | void HandleCopyPoliciesJson(const base::ListValue* args); |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 93 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 94 | // Send information about the current policy values to the UI. For each policy |
| 95 | // whose value has been set, a dictionary containing the value and additional |
| 96 | // metadata is sent. |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 97 | void SendPolicies(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 98 | |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 99 | #if defined(OS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 100 | // Sets |updater_policies_| in this instance and refreshes the UI via |
| 101 | // SendPolicies. |
| 102 | void SetUpdaterPolicies(std::unique_ptr<policy::PolicyMap> updater_policies); |
Yann Dago | a26cfab7 | 2020-08-31 17:09:35 | [diff] [blame] | 103 | |
| 104 | void ReloadUpdaterPolicies(); |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 105 | #endif // defined(OS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 106 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 107 | // Send the status of cloud policy to the UI. For each scope that has cloud |
| 108 | // policy enabled (device and/or user), a dictionary containing status |
| 109 | // information is sent. |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 110 | void SendStatus(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 111 | |
Jérôme Gingras | 4b90f6fc | 2020-05-28 18:48:57 | [diff] [blame] | 112 | // Build a JSON string of all the policies. |
| 113 | std::string GetPoliciesAsJson() const; |
| 114 | |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 115 | void WritePoliciesToJSONFile(const base::FilePath& path) const; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 116 | |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 117 | void OnRefreshPoliciesDone(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 118 | |
| 119 | policy::PolicyService* GetPolicyService() const; |
| 120 | |
| 121 | std::string device_domain_; |
| 122 | |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 123 | scoped_refptr<ui::SelectFileDialog> export_policies_select_file_dialog_; |
| 124 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 125 | // Providers that supply status dictionaries for user and device policy, |
| 126 | // respectively. These are created on initialization time as appropriate for |
| 127 | // the platform (Chrome OS / desktop) and type of policy that is in effect. |
tnagel | 8d2791ec | 2016-11-18 10:43:13 | [diff] [blame] | 128 | std::unique_ptr<PolicyStatusProvider> user_status_provider_; |
| 129 | std::unique_ptr<PolicyStatusProvider> device_status_provider_; |
Tien Mai | 0b22b08 | 2018-09-26 15:10:12 | [diff] [blame] | 130 | std::unique_ptr<PolicyStatusProvider> machine_status_provider_; |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 131 | std::unique_ptr<PolicyStatusProvider> updater_status_provider_; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 132 | |
Yann Dago | 10433939 | 2020-02-25 17:40:07 | [diff] [blame] | 133 | #if defined(OS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 134 | std::unique_ptr<policy::PolicyMap> updater_policies_; |
| 135 | #endif // defined(OS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING) |
| 136 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 137 | base::WeakPtrFactory<PolicyUIHandler> weak_factory_{this}; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler); |
| 140 | }; |
| 141 | |
| 142 | #endif // CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_ |