From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: deepak1556 <hop2deep@gmail.com>
Date: Thu, 30 Jan 2025 20:28:38 +0900
Subject: feat: separate content settings callback for sync and async clipboard

`AllowReadFromClipboard` is called from both the types without a way to differentiate.

[sync path] - third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
[async path] - third_party/blink/renderer/modules/clipboard/clipboard_promise.cc

This patch adds a new callback to separate these two paths so that we
can have sync permission checks for the sync path.

Additionally, `blink::PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ`
has been added to support type conversion in permission policy checks. We have extended
`blink::PermissionType` in `electron::WebContentsPermissionHelper::PermissionType`
but it is hard to import the latter into the content permission layer checks.

This patch will be removed when the deprecated sync api support is
removed.

diff --git a/components/permissions/permission_util.cc b/components/permissions/permission_util.cc
index 1e0b4bfd8b31f9ccbd663e8bb7c3990a9fa9c879..d9d7e902dfaaf360dc80bd292b56eeba9cfc5f12 100644
--- a/components/permissions/permission_util.cc
+++ b/components/permissions/permission_util.cc
@@ -552,7 +552,8 @@ ContentSettingsType PermissionUtil::PermissionTypeToContentSettingsTypeSafe(
       return ContentSettingsType::LOCAL_NETWORK;
     case PermissionType::LOOPBACK_NETWORK:
       return ContentSettingsType::LOOPBACK_NETWORK;
-    case PermissionType::NUM:
+    case PermissionType::GEOLOCATION_APPROXIMATE:
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:    case PermissionType::NUM:
       break;
   }
 
diff --git a/content/browser/permissions/permission_controller_impl.cc b/content/browser/permissions/permission_controller_impl.cc
index f263d96b11aee75bbd0e6b8f4ff5a520f7047e9e..d278eae0806c1d51e949c7026fa01f01494c8dd3 100644
--- a/content/browser/permissions/permission_controller_impl.cc
+++ b/content/browser/permissions/permission_controller_impl.cc
@@ -97,7 +97,8 @@ PermissionToSchedulingFeature(PermissionType permission_name) {
     case PermissionType::LOCAL_NETWORK_ACCESS:
     case PermissionType::LOCAL_NETWORK:
     case PermissionType::LOOPBACK_NETWORK:
-      return std::nullopt;
+    case PermissionType::GEOLOCATION_APPROXIMATE:
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:      return std::nullopt;
   }
 }
 
diff --git a/content/browser/permissions/permission_descriptor_util.cc b/content/browser/permissions/permission_descriptor_util.cc
index 8acf00d2618d9a0552750f0f8e1d734b52f790b1..d2ea06fa66c07baaf741382af40de154068a7d6b 100644
--- a/content/browser/permissions/permission_descriptor_util.cc
+++ b/content/browser/permissions/permission_descriptor_util.cc
@@ -180,7 +180,12 @@ content::PermissionDescriptorUtil::CreatePermissionDescriptorForPermissionType(
     case blink::PermissionType::LOOPBACK_NETWORK:
       return CreatePermissionDescriptor(
           blink::mojom::PermissionName::LOOPBACK_NETWORK);
-    case blink::PermissionType::NUM:
+    case blink::PermissionType::GEOLOCATION_APPROXIMATE:
+      return CreatePermissionDescriptor(
+          blink::mojom::PermissionName::GEOLOCATION_APPROXIMATE);
+    case blink::PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:
+      return CreatePermissionDescriptor(
+          blink::mojom::PermissionName::DEPRECATED_SYNC_CLIPBOARD_READ);    case blink::PermissionType::NUM:
       NOTREACHED();
   }
   NOTREACHED();
diff --git a/third_party/blink/common/permissions/permission_utils.cc b/third_party/blink/common/permissions/permission_utils.cc
index 0d873c13f1463d37e0fcdeacf68a84817b6524ca..19b8fa4b094b646ad6a5864c5e14a2f125fb7880 100644
--- a/third_party/blink/common/permissions/permission_utils.cc
+++ b/third_party/blink/common/permissions/permission_utils.cc
@@ -108,7 +108,10 @@ std::string GetPermissionString(PermissionType permission) {
       return "LocalNetwork";
     case PermissionType::LOOPBACK_NETWORK:
       return "LoopbackNetwork";
-    case PermissionType::NUM:
+    case PermissionType::GEOLOCATION_APPROXIMATE:
+      return "GeolocationApproximate";
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:
+      return "DeprecatedSyncClipboardRead";    case PermissionType::NUM:
       NOTREACHED();
   }
   NOTREACHED();
@@ -189,6 +192,7 @@ PermissionTypeToPermissionsPolicyFeature(PermissionType permission) {
     case PermissionType::NOTIFICATIONS:
     case PermissionType::KEYBOARD_LOCK:
     case PermissionType::POINTER_LOCK:
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:
       return std::nullopt;
 
     case PermissionType::NUM:
diff --git a/third_party/blink/public/common/permissions/permission_utils.h b/third_party/blink/public/common/permissions/permission_utils.h
index b124d53fdd245f055f57ad00250112e2637e1cd1..31158388db2df745af999adc9d07fc9272a2d6f8 100644
--- a/third_party/blink/public/common/permissions/permission_utils.h
+++ b/third_party/blink/public/common/permissions/permission_utils.h
@@ -69,7 +69,7 @@ enum class PermissionType {
   LOCAL_NETWORK = 44,
   LOOPBACK_NETWORK = 45,
   GEOLOCATION_APPROXIMATE = 46,
-
+  DEPRECATED_SYNC_CLIPBOARD_READ = 47,
   // Always keep this at the end.
   NUM,
   MIN_VALUE = MIDI_SYSEX,
diff --git a/third_party/blink/public/mojom/permissions/permission.mojom b/third_party/blink/public/mojom/permissions/permission.mojom
index 699f6dfd43f0f6f8ab2b717e75ad74fa9dca0789..8a2e1ec60c337d4fb40292b53ee8949e0ce2b8a4 100644
--- a/third_party/blink/public/mojom/permissions/permission.mojom
+++ b/third_party/blink/public/mojom/permissions/permission.mojom
@@ -47,7 +47,7 @@ enum PermissionName {
   WEB_PRINTING,
   SMART_CARD,
   GEOLOCATION_APPROXIMATE,
-};
+  DEPRECATED_SYNC_CLIPBOARD_READ};
 
 struct MidiPermissionDescriptor {
   bool sysex;
diff --git a/third_party/blink/public/platform/web_content_settings_client.h b/third_party/blink/public/platform/web_content_settings_client.h
index 36410ff29d9c82e59f93fbb82968064bd330dfde..6c3f994e0b184f78bd9442002bb4dfae66e50518 100644
--- a/third_party/blink/public/platform/web_content_settings_client.h
+++ b/third_party/blink/public/platform/web_content_settings_client.h
@@ -54,6 +54,9 @@ class WebContentSettingsClient {
   // Controls whether access to write the clipboard is allowed for this frame.
   virtual bool AllowWriteToClipboard() { return false; }
 
+  // Controls whether synchronous access to read the clipboard is allowed for this frame.
+  virtual bool AllowReadFromClipboardSync() { return false; }
+
   // Reports that passive mixed content was found at the provided URL.
   virtual void PassiveInsecureContentFound(const WebURL&) {}
 
diff --git a/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc b/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
index 8fded9303e74737d82ca6d54e00807ebabf6c1ac..c0b66eb9a62f8f75e3c4de43f467ddd09d8dc2d6 100644
--- a/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
+++ b/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
@@ -123,7 +123,7 @@ bool ClipboardCommands::CanReadClipboard(LocalFrame& frame,
     return true;
   }
   return frame.GetContentSettingsClient() &&
-         frame.GetContentSettingsClient()->AllowReadFromClipboard();
+         frame.GetContentSettingsClient()->AllowReadFromClipboardSync();
 }
 
 bool ClipboardCommands::CanWriteClipboard(LocalFrame& frame,
@@ -312,7 +312,7 @@ bool ClipboardCommands::PasteSupported(LocalFrame* frame) {
     return true;
   }
   return frame->GetContentSettingsClient() &&
-         frame->GetContentSettingsClient()->AllowReadFromClipboard();
+         frame->GetContentSettingsClient()->AllowReadFromClipboardSync();
 }
 
 bool ClipboardCommands::ExecuteCopy(LocalFrame& frame,
diff --git a/third_party/blink/renderer/modules/permissions/permission_utils.cc b/third_party/blink/renderer/modules/permissions/permission_utils.cc
index 27cdc852f5974951f0a10157a0473e67791f6688..e9b7dcb070567580e8c6ad11b2bb943083e804f6 100644
--- a/third_party/blink/renderer/modules/permissions/permission_utils.cc
+++ b/third_party/blink/renderer/modules/permissions/permission_utils.cc
@@ -151,6 +151,8 @@ String PermissionNameToString(PermissionName name) {
       return "web-printing";
     case PermissionName::SMART_CARD:
       return "smart-card";
+    case PermissionName::DEPRECATED_SYNC_CLIPBOARD_READ:
+      return "deprecated-sync-clipboard-read";
   }
 }
 
