CHROMIUM: add mock admin policy interface

This make bluez provide the old interface org.bluze.AdminPolicy1 so that
Chrome can still set service allowlist. This should be reverted once
Chrome switches to org.bluez.AdminPolicySet1 and
org.bluez.AdminPolicyStatus1.

The revert task is tracking by b:195717647.

BUG=chromium:1237167
TEST=run bluetooth_AdapterEPHealth

Change-Id: Ic8962877ba677ea064e2089a58a53c716d1ce84f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/bluez/+/3077816
Reviewed-by: Miao-chen Chou <mcchou@chromium.org>
Tested-by: Yun-Hao Chung <howardchung@chromium.org>
Commit-Queue: Yun-Hao Chung <howardchung@chromium.org>
diff --git a/plugins/admin.c b/plugins/admin.c
index aea33cb..289f9df 100644
--- a/plugins/admin.c
+++ b/plugins/admin.c
@@ -35,6 +35,8 @@
 #define ADMIN_POLICY_STATUS_INTERFACE	"org.bluez.AdminPolicyStatus1"
 #define ADMIN_POLICY_STORAGE		STORAGEDIR "/admin_policy_settings"
 
+#define MOCK_ADMIN_POLICY_INTERFACE	"org.bluez.AdminPolicy1"
+
 #define DBUS_BLUEZ_SERVICE		"org.bluez"
 #define BTD_DEVICE_INTERFACE		"org.bluez.Device1"
 
@@ -178,6 +180,8 @@
 
 	g_dbus_emit_property_changed(dbus_conn, dev_data->path,
 			ADMIN_POLICY_STATUS_INTERFACE, "AffectedByPolicy");
+
+	btd_device_set_is_blocked_by_policy(dev_data->device, affected);
 }
 
 static void free_uuid_strings(char **uuid_strs, gsize num)
@@ -365,6 +369,11 @@
 					ADMIN_POLICY_STATUS_INTERFACE,
 					"ServiceAllowList");
 
+	g_dbus_emit_property_changed(dbus_conn,
+					adapter_get_path(policy_data->adapter),
+					MOCK_ADMIN_POLICY_INTERFACE,
+					"ServiceAllowList");
+
 	queue_foreach(devices, update_device_affected, NULL);
 
 	return dbus_message_new_method_return(msg);
@@ -511,6 +520,17 @@
 	btd_info(policy_data->adapter_id,
 				"Admin Policy Status interface registered");
 
+	if (!g_dbus_register_interface(dbus_conn, adapter_path,
+					MOCK_ADMIN_POLICY_INTERFACE,
+					admin_policy_adapter_methods, NULL,
+					admin_policy_adapter_properties,
+					policy_data, NULL)) {
+		btd_error(policy_data->adapter_id,
+			"Mock Admin Policy interface init failed on path %s",
+								adapter_path);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
@@ -532,6 +552,7 @@
 	data->device = device;
 	data->path = g_strdup(device_get_path(device));
 	data->affected = !btd_device_all_services_allowed(data->device);
+	btd_device_set_is_blocked_by_policy(data->device, data->affected);
 
 	if (!g_dbus_register_interface(dbus_conn, data->path,
 					ADMIN_POLICY_STATUS_INTERFACE,
diff --git a/src/device.c b/src/device.c
index 2395ead..0384ae5 100644
--- a/src/device.c
+++ b/src/device.c
@@ -295,6 +295,8 @@
 
 	GIOChannel	*att_io;
 	guint		store_id;
+
+	bool		is_blocked;
 };
 
 static const uint16_t uuid_list[] = {
@@ -3767,6 +3769,18 @@
 	return NULL;
 }
 
+static gboolean
+dev_property_get_is_blocked_by_policy(const GDBusPropertyTable *property,
+					     DBusMessageIter *iter, void *data)
+{
+	struct btd_device *device = data;
+	dbus_bool_t is_blocked = device->is_blocked;
+
+	dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &is_blocked);
+
+	return TRUE;
+}
+
 static const GDBusMethodTable device_methods[] = {
 	{ GDBUS_ASYNC_METHOD("Disconnect", NULL, NULL, dev_disconnect) },
 	{ GDBUS_ASYNC_METHOD("DisconnectLE", NULL, NULL, dev_disconnect_le) },
@@ -3827,6 +3841,8 @@
 	{ "WakeAllowed", "b", dev_property_get_wake_allowed,
 				dev_property_set_wake_allowed,
 				dev_property_wake_allowed_exist },
+	{ "IsBlockedByPolicy", "b", dev_property_get_is_blocked_by_policy,
+								NULL, NULL },
 	{ }
 };
 
@@ -7881,6 +7897,22 @@
 	return NULL;
 }
 
+void btd_device_set_is_blocked_by_policy(struct btd_device *dev, bool value)
+{
+	if (!dev) {
+		error("Unexpected error in setting IsBlockedByPolicy");
+		return;
+	}
+
+	if (dev->is_blocked == value)
+		return;
+
+	dev->is_blocked = value;
+
+	g_dbus_emit_property_changed(dbus_conn, dev->path,
+					DEVICE_INTERFACE, "IsBlockedByPolicy");
+}
+
 void btd_device_init(void)
 {
 	dbus_conn = btd_get_dbus_connection();
diff --git a/src/device.h b/src/device.h
index 6bdf511..3938ed9 100644
--- a/src/device.h
+++ b/src/device.h
@@ -193,6 +193,7 @@
 
 bool btd_device_all_services_allowed(struct btd_device *dev);
 void btd_device_update_allowed_services(struct btd_device *dev);
+void btd_device_set_is_blocked_by_policy(struct btd_device *dev, bool value);
 void btd_device_init(void);
 void btd_device_cleanup(void);
 bool device_log_devices_info(gpointer user_data);