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" |
| 16 | #include "components/policy/core/browser/policy_error_map.h" |
| 17 | #include "components/policy/core/common/policy_map.h" |
| 18 | #include "components/policy/core/common/policy_namespace.h" |
| 19 | #include "components/policy/core/common/policy_service.h" |
| 20 | #include "components/policy/core/common/schema_registry.h" |
| 21 | #include "content/public/browser/web_ui.h" |
| 22 | #include "content/public/browser/web_ui_data_source.h" |
| 23 | #include "content/public/browser/web_ui_message_handler.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 24 | #include "extensions/buildflags/buildflags.h" |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 25 | #include "ui/shell_dialogs/select_file_dialog.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 26 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 27 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 28 | #include "extensions/browser/extension_registry_observer.h" |
| 29 | #endif |
| 30 | |
tnagel | 8d2791ec | 2016-11-18 10:43:13 | [diff] [blame] | 31 | class PolicyStatusProvider; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 32 | |
| 33 | // The JavaScript message handler for the chrome://policy page. |
| 34 | class PolicyUIHandler : public content::WebUIMessageHandler, |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 35 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 36 | public extensions::ExtensionRegistryObserver, |
| 37 | #endif |
| 38 | public policy::PolicyService::Observer, |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 39 | public policy::SchemaRegistry::Observer, |
| 40 | public ui::SelectFileDialog::Listener { |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 41 | public: |
| 42 | PolicyUIHandler(); |
| 43 | ~PolicyUIHandler() override; |
| 44 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 45 | static void AddCommonLocalizedStringsToSource( |
| 46 | content::WebUIDataSource* source); |
| 47 | |
| 48 | // content::WebUIMessageHandler implementation. |
| 49 | void RegisterMessages() override; |
| 50 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 51 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 52 | // extensions::ExtensionRegistryObserver implementation. |
| 53 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 54 | const extensions::Extension* extension) override; |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 55 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 56 | const extensions::Extension* extension, |
| 57 | extensions::UnloadedExtensionReason reason) override; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | // policy::PolicyService::Observer implementation. |
| 61 | void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 62 | const policy::PolicyMap& previous, |
| 63 | const policy::PolicyMap& current) override; |
| 64 | |
| 65 | // policy::SchemaRegistry::Observer implementation. |
| 66 | void OnSchemaRegistryUpdated(bool has_new_schemas) override; |
| 67 | |
| 68 | protected: |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 69 | // ui::SelectFileDialog::Listener implementation. |
| 70 | void FileSelected(const base::FilePath& path, |
| 71 | int index, |
| 72 | void* params) override; |
| 73 | void FileSelectionCanceled(void* params) override; |
| 74 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 75 | private: |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 76 | base::Value GetPolicyNames() const; |
| 77 | base::Value GetPolicyValues() const; |
| 78 | |
| 79 | void HandleExportPoliciesJson(const base::ListValue* args); |
| 80 | void HandleListenPoliciesUpdates(const base::ListValue* args); |
| 81 | void HandleReloadPolicies(const base::ListValue* args); |
| 82 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 83 | // Send information about the current policy values to the UI. For each policy |
| 84 | // whose value has been set, a dictionary containing the value and additional |
| 85 | // metadata is sent. |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 86 | void SendPolicies(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 87 | |
| 88 | // Send the status of cloud policy to the UI. For each scope that has cloud |
| 89 | // policy enabled (device and/or user), a dictionary containing status |
| 90 | // information is sent. |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 91 | void SendStatus(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 92 | |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 93 | void WritePoliciesToJSONFile(const base::FilePath& path) const; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 94 | |
Yann Dago | b23ff36 | 2019-02-27 13:15:36 | [diff] [blame] | 95 | void OnRefreshPoliciesDone(); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 96 | |
| 97 | policy::PolicyService* GetPolicyService() const; |
| 98 | |
| 99 | std::string device_domain_; |
| 100 | |
Anton Urusov | cd9784a | 2017-08-10 21:00:21 | [diff] [blame] | 101 | scoped_refptr<ui::SelectFileDialog> export_policies_select_file_dialog_; |
| 102 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 103 | // Providers that supply status dictionaries for user and device policy, |
| 104 | // respectively. These are created on initialization time as appropriate for |
| 105 | // the platform (Chrome OS / desktop) and type of policy that is in effect. |
tnagel | 8d2791ec | 2016-11-18 10:43:13 | [diff] [blame] | 106 | std::unique_ptr<PolicyStatusProvider> user_status_provider_; |
| 107 | std::unique_ptr<PolicyStatusProvider> device_status_provider_; |
Tien Mai | 0b22b08 | 2018-09-26 15:10:12 | [diff] [blame] | 108 | std::unique_ptr<PolicyStatusProvider> machine_status_provider_; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 109 | |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame^] | 110 | base::WeakPtrFactory<PolicyUIHandler> weak_factory_{this}; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 111 | |
| 112 | DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler); |
| 113 | }; |
| 114 | |
| 115 | #endif // CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_ |