blob: 4967821b5acc70a6f570c3e44edb20d0c1c16fb4 [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>
Lei Zhang776ed0712019-05-23 14:13:0912#include <string>
dcheng9603ab92016-04-08 04:17:3213
fhorschig07c5e0622015-12-03 16:48:5714#include "base/macros.h"
fhorschig07c5e0622015-12-03 16:48:5715#include "base/memory/weak_ptr.h"
Alexander Hendrich02ab52e2019-12-04 15:47:4316#include "base/values.h"
Yann Dago104339392020-02-25 17:40:0717#include "build/branding_buildflags.h"
18#include "build/build_config.h"
fhorschig07c5e0622015-12-03 16:48:5719#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 Violetc8240b02018-03-08 22:03:5927#include "extensions/buildflags/buildflags.h"
Anton Urusovcd9784a2017-08-10 21:00:2128#include "ui/shell_dialogs/select_file_dialog.h"
fhorschig07c5e0622015-12-03 16:48:5729
brettw00899e62016-11-12 02:10:1730#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5731#include "extensions/browser/extension_registry_observer.h"
32#endif
33
tnagel8d2791ec2016-11-18 10:43:1334class PolicyStatusProvider;
fhorschig07c5e0622015-12-03 16:48:5735
Yann Dago104339392020-02-25 17:40:0736namespace policy {
37class PolicyMap;
38}
39
fhorschig07c5e0622015-12-03 16:48:5740// The JavaScript message handler for the chrome://policy page.
41class PolicyUIHandler : public content::WebUIMessageHandler,
brettw00899e62016-11-12 02:10:1742#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5743 public extensions::ExtensionRegistryObserver,
44#endif
45 public policy::PolicyService::Observer,
Anton Urusovcd9784a2017-08-10 21:00:2146 public policy::SchemaRegistry::Observer,
47 public ui::SelectFileDialog::Listener {
fhorschig07c5e0622015-12-03 16:48:5748 public:
49 PolicyUIHandler();
50 ~PolicyUIHandler() override;
51
fhorschig07c5e0622015-12-03 16:48:5752 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;
Alexander Hendrich02ab52e2019-12-04 15:47:4365#endif // BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5766
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 Urusovcd9784a2017-08-10 21:00:2176 // 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
fhorschig07c5e0622015-12-03 16:48:5782 private:
Yann Dagob23ff362019-02-27 13:15:3683 base::Value GetPolicyNames() const;
84 base::Value GetPolicyValues() const;
85
Yann Dago104339392020-02-25 17:40:0786 void AddExtensionPolicyNames(base::Value* names,
Alexander Hendrich02ab52e2019-12-04 15:47:4387 policy::PolicyDomain policy_domain) const;
88
Yann Dagob23ff362019-02-27 13:15:3689 void HandleExportPoliciesJson(const base::ListValue* args);
90 void HandleListenPoliciesUpdates(const base::ListValue* args);
91 void HandleReloadPolicies(const base::ListValue* args);
Jérôme Gingras4b90f6fc2020-05-28 18:48:5792 void HandleCopyPoliciesJson(const base::ListValue* args);
Yann Dagob23ff362019-02-27 13:15:3693
fhorschig07c5e0622015-12-03 16:48:5794 // 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 Dagob23ff362019-02-27 13:15:3697 void SendPolicies();
fhorschig07c5e0622015-12-03 16:48:5798
Yann Dago104339392020-02-25 17:40:0799#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 Dagoa26cfab72020-08-31 17:09:35103
104 void ReloadUpdaterPolicies();
Yann Dago104339392020-02-25 17:40:07105#endif // defined(OS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
106
fhorschig07c5e0622015-12-03 16:48:57107 // 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 Dagob23ff362019-02-27 13:15:36110 void SendStatus();
fhorschig07c5e0622015-12-03 16:48:57111
Jérôme Gingras4b90f6fc2020-05-28 18:48:57112 // Build a JSON string of all the policies.
113 std::string GetPoliciesAsJson() const;
114
Anton Urusovcd9784a2017-08-10 21:00:21115 void WritePoliciesToJSONFile(const base::FilePath& path) const;
fhorschig07c5e0622015-12-03 16:48:57116
Yann Dagob23ff362019-02-27 13:15:36117 void OnRefreshPoliciesDone();
fhorschig07c5e0622015-12-03 16:48:57118
119 policy::PolicyService* GetPolicyService() const;
120
121 std::string device_domain_;
122
Anton Urusovcd9784a2017-08-10 21:00:21123 scoped_refptr<ui::SelectFileDialog> export_policies_select_file_dialog_;
124
fhorschig07c5e0622015-12-03 16:48:57125 // 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.
tnagel8d2791ec2016-11-18 10:43:13128 std::unique_ptr<PolicyStatusProvider> user_status_provider_;
129 std::unique_ptr<PolicyStatusProvider> device_status_provider_;
Tien Mai0b22b082018-09-26 15:10:12130 std::unique_ptr<PolicyStatusProvider> machine_status_provider_;
Yann Dago7a25b6f2020-05-14 01:09:24131 std::unique_ptr<PolicyStatusProvider> updater_status_provider_;
fhorschig07c5e0622015-12-03 16:48:57132
Yann Dago104339392020-02-25 17:40:07133#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 Roman495db682019-07-12 16:03:24137 base::WeakPtrFactory<PolicyUIHandler> weak_factory_{this};
fhorschig07c5e0622015-12-03 16:48:57138
139 DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler);
140};
141
142#endif // CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_