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> |
| 12 | |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 13 | #include "base/macros.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
| 15 | #include "components/policy/core/browser/policy_error_map.h" |
| 16 | #include "components/policy/core/common/policy_map.h" |
| 17 | #include "components/policy/core/common/policy_namespace.h" |
| 18 | #include "components/policy/core/common/policy_service.h" |
| 19 | #include "components/policy/core/common/schema_registry.h" |
| 20 | #include "content/public/browser/web_ui.h" |
| 21 | #include "content/public/browser/web_ui_data_source.h" |
| 22 | #include "content/public/browser/web_ui_message_handler.h" |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 23 | #include "extensions/features/features.h" |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 24 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 25 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 26 | #include "extensions/browser/extension_registry_observer.h" |
| 27 | #endif |
| 28 | |
| 29 | struct PolicyStringMap { |
| 30 | const char* key; |
| 31 | int string_id; |
| 32 | }; |
| 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 | |
| 36 | // The JavaScript message handler for the chrome://policy page. |
| 37 | class PolicyUIHandler : public content::WebUIMessageHandler, |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 38 | #if BUILDFLAG(ENABLE_EXTENSIONS) |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 39 | public extensions::ExtensionRegistryObserver, |
| 40 | #endif |
| 41 | public policy::PolicyService::Observer, |
| 42 | public policy::SchemaRegistry::Observer { |
| 43 | public: |
| 44 | PolicyUIHandler(); |
| 45 | ~PolicyUIHandler() override; |
| 46 | |
| 47 | |
| 48 | static void AddLocalizedPolicyStrings(content::WebUIDataSource* source, |
| 49 | const PolicyStringMap* strings, |
| 50 | size_t count); |
| 51 | |
| 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; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 65 | #endif |
| 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: |
| 76 | virtual void AddPolicyName(const std::string& name, |
| 77 | base::DictionaryValue* names) const; |
| 78 | |
| 79 | // Send a dictionary containing the names of all known policies to the UI. |
| 80 | virtual void SendPolicyNames() const; |
| 81 | |
| 82 | private: |
| 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. |
| 86 | void SendPolicyValues() const; |
| 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. |
| 91 | void SendStatus() const; |
| 92 | |
| 93 | // Inserts a description of each policy in |policy_map| into |values|, using |
| 94 | // the optional errors in |errors| to determine the status of each policy. |
| 95 | void GetPolicyValues(const policy::PolicyMap& policy_map, |
| 96 | policy::PolicyErrorMap* errors, |
| 97 | base::DictionaryValue* values) const; |
| 98 | |
| 99 | void GetChromePolicyValues(base::DictionaryValue* values) const; |
| 100 | |
| 101 | void HandleInitialized(const base::ListValue* args); |
| 102 | void HandleReloadPolicies(const base::ListValue* args); |
| 103 | |
| 104 | void OnRefreshPoliciesDone() const; |
| 105 | |
| 106 | policy::PolicyService* GetPolicyService() const; |
| 107 | |
| 108 | std::string device_domain_; |
| 109 | |
| 110 | // Providers that supply status dictionaries for user and device policy, |
| 111 | // respectively. These are created on initialization time as appropriate for |
| 112 | // the platform (Chrome OS / desktop) and type of policy that is in effect. |
tnagel | 8d2791ec | 2016-11-18 10:43:13 | [diff] [blame] | 113 | std::unique_ptr<PolicyStatusProvider> user_status_provider_; |
| 114 | std::unique_ptr<PolicyStatusProvider> device_status_provider_; |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 115 | |
| 116 | base::WeakPtrFactory<PolicyUIHandler> weak_factory_; |
| 117 | |
| 118 | DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler); |
| 119 | }; |
| 120 | |
| 121 | #endif // CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_ |