blob: 1201ce0de845ff4cbc2c3eeb24eb806dd8b1ba60 [file] [log] [blame]
fhorschig07c5e0622015-12-03 16:48:571// 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
avi0f233432015-12-25 02:23:388#include <stddef.h>
fhorschig07c5e0622015-12-03 16:48:579#include <string.h>
10
dcheng9603ab92016-04-08 04:17:3211#include <memory>
12
fhorschig07c5e0622015-12-03 16:48:5713#include "base/macros.h"
fhorschig07c5e0622015-12-03 16:48:5714#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"
brettw00899e62016-11-12 02:10:1723#include "extensions/features/features.h"
fhorschig07c5e0622015-12-03 16:48:5724
brettw00899e62016-11-12 02:10:1725#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5726#include "extensions/browser/extension_registry_observer.h"
27#endif
28
29struct PolicyStringMap {
30 const char* key;
31 int string_id;
32};
33
tnagel8d2791ec2016-11-18 10:43:1334class PolicyStatusProvider;
fhorschig07c5e0622015-12-03 16:48:5735
36// The JavaScript message handler for the chrome://policy page.
37class PolicyUIHandler : public content::WebUIMessageHandler,
brettw00899e62016-11-12 02:10:1738#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5739 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
brettw00899e62016-11-12 02:10:1758#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5759 // extensions::ExtensionRegistryObserver implementation.
60 void OnExtensionLoaded(content::BrowserContext* browser_context,
61 const extensions::Extension* extension) override;
limasdf0deef2042017-05-03 19:17:1762 void OnExtensionUnloaded(content::BrowserContext* browser_context,
63 const extensions::Extension* extension,
64 extensions::UnloadedExtensionReason reason) override;
fhorschig07c5e0622015-12-03 16:48:5765#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.
tnagel8d2791ec2016-11-18 10:43:13113 std::unique_ptr<PolicyStatusProvider> user_status_provider_;
114 std::unique_ptr<PolicyStatusProvider> device_status_provider_;
fhorschig07c5e0622015-12-03 16:48:57115
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_