blob: 3d0b89d3ac0a0c5821b5e95c7f4e5b3742875531 [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"
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 Violetc8240b02018-03-08 22:03:5924#include "extensions/buildflags/buildflags.h"
Anton Urusovcd9784a2017-08-10 21:00:2125#include "ui/shell_dialogs/select_file_dialog.h"
fhorschig07c5e0622015-12-03 16:48:5726
brettw00899e62016-11-12 02:10:1727#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5728#include "extensions/browser/extension_registry_observer.h"
29#endif
30
tnagel8d2791ec2016-11-18 10:43:1331class PolicyStatusProvider;
fhorschig07c5e0622015-12-03 16:48:5732
33// The JavaScript message handler for the chrome://policy page.
34class PolicyUIHandler : public content::WebUIMessageHandler,
brettw00899e62016-11-12 02:10:1735#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5736 public extensions::ExtensionRegistryObserver,
37#endif
38 public policy::PolicyService::Observer,
Anton Urusovcd9784a2017-08-10 21:00:2139 public policy::SchemaRegistry::Observer,
40 public ui::SelectFileDialog::Listener {
fhorschig07c5e0622015-12-03 16:48:5741 public:
42 PolicyUIHandler();
43 ~PolicyUIHandler() override;
44
fhorschig07c5e0622015-12-03 16:48:5745 static void AddCommonLocalizedStringsToSource(
46 content::WebUIDataSource* source);
47
48 // content::WebUIMessageHandler implementation.
49 void RegisterMessages() override;
50
brettw00899e62016-11-12 02:10:1751#if BUILDFLAG(ENABLE_EXTENSIONS)
fhorschig07c5e0622015-12-03 16:48:5752 // extensions::ExtensionRegistryObserver implementation.
53 void OnExtensionLoaded(content::BrowserContext* browser_context,
54 const extensions::Extension* extension) override;
limasdf0deef2042017-05-03 19:17:1755 void OnExtensionUnloaded(content::BrowserContext* browser_context,
56 const extensions::Extension* extension,
57 extensions::UnloadedExtensionReason reason) override;
fhorschig07c5e0622015-12-03 16:48:5758#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 Urusovcd9784a2017-08-10 21:00:2169 // 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
fhorschig07c5e0622015-12-03 16:48:5775 private:
Yann Dagob23ff362019-02-27 13:15:3676 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
fhorschig07c5e0622015-12-03 16:48:5783 // 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 Dagob23ff362019-02-27 13:15:3686 void SendPolicies();
fhorschig07c5e0622015-12-03 16:48:5787
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 Dagob23ff362019-02-27 13:15:3691 void SendStatus();
fhorschig07c5e0622015-12-03 16:48:5792
Anton Urusovcd9784a2017-08-10 21:00:2193 void WritePoliciesToJSONFile(const base::FilePath& path) const;
fhorschig07c5e0622015-12-03 16:48:5794
Yann Dagob23ff362019-02-27 13:15:3695 void OnRefreshPoliciesDone();
fhorschig07c5e0622015-12-03 16:48:5796
97 policy::PolicyService* GetPolicyService() const;
98
99 std::string device_domain_;
100
Anton Urusovcd9784a2017-08-10 21:00:21101 scoped_refptr<ui::SelectFileDialog> export_policies_select_file_dialog_;
102
fhorschig07c5e0622015-12-03 16:48:57103 // 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.
tnagel8d2791ec2016-11-18 10:43:13106 std::unique_ptr<PolicyStatusProvider> user_status_provider_;
107 std::unique_ptr<PolicyStatusProvider> device_status_provider_;
Tien Mai0b22b082018-09-26 15:10:12108 std::unique_ptr<PolicyStatusProvider> machine_status_provider_;
fhorschig07c5e0622015-12-03 16:48:57109
Jeremy Roman495db682019-07-12 16:03:24110 base::WeakPtrFactory<PolicyUIHandler> weak_factory_{this};
fhorschig07c5e0622015-12-03 16:48:57111
112 DISALLOW_COPY_AND_ASSIGN(PolicyUIHandler);
113};
114
115#endif // CHROME_BROWSER_UI_WEBUI_POLICY_UI_HANDLER_H_