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 335d56aa7dc434cf0e3248e44f0d48eccbdc06c5..a7cfe652a7fb06fbc3f99378bbf30173862d2cef 100644
--- a/components/permissions/permission_util.cc
+++ b/components/permissions/permission_util.cc
@@ -369,6 +369,7 @@ ContentSettingsType PermissionUtil::PermissionTypeToContentSettingTypeSafe(
       return ContentSettingsType::AUTOMATIC_FULLSCREEN;
     case PermissionType::WEB_APP_INSTALLATION:
       return ContentSettingsType::WEB_APP_INSTALLATION;
+    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 6dd0324d7d899c7e207ab641d9615f47ff14d4ce..7303dc8fbfe22416b31e9955d143952ea1574c37 100644
--- a/content/browser/permissions/permission_controller_impl.cc
+++ b/content/browser/permissions/permission_controller_impl.cc
@@ -95,6 +95,7 @@ PermissionToSchedulingFeature(PermissionType permission_name) {
     case PermissionType::POINTER_LOCK:
     case PermissionType::AUTOMATIC_FULLSCREEN:
     case PermissionType::WEB_APP_INSTALLATION:
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:
       return std::nullopt;
   }
 }
diff --git a/third_party/blink/common/permissions/permission_utils.cc b/third_party/blink/common/permissions/permission_utils.cc
index 0aa40dfe497454424d7fd410e7e9ee8d6227ecd1..9ed047dad4ea6ed1f746bbe975a031f52f278c03 100644
--- a/third_party/blink/common/permissions/permission_utils.cc
+++ b/third_party/blink/common/permissions/permission_utils.cc
@@ -102,6 +102,8 @@ std::string GetPermissionString(PermissionType permission) {
       return "AutomaticFullscreen";
     case PermissionType::WEB_APP_INSTALLATION:
       return "WebAppInstallation";
+    case PermissionType::DEPRECATED_SYNC_CLIPBOARD_READ:
+      return "DeprecatedSyncClipboardRead";
     case PermissionType::NUM:
       NOTREACHED_IN_MIGRATION();
       return std::string();
@@ -177,6 +179,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 6c6f1d49d1ff702650d23c0fa3f2c40e7908c5a1..7dc5d2cb91923b1e1f23df74dc601bf90013f29b 100644
--- a/third_party/blink/public/common/permissions/permission_utils.h
+++ b/third_party/blink/public/common/permissions/permission_utils.h
@@ -64,6 +64,7 @@ enum class PermissionType {
   AUTOMATIC_FULLSCREEN = 40,
   HAND_TRACKING = 41,
   WEB_APP_INSTALLATION = 42,
+  DEPRECATED_SYNC_CLIPBOARD_READ = 43,
 
   // Always keep this at the end.
   NUM,
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 6119f7f3f73379b9bdd219342f23bdde4509ab8c..5e1edfe683e6df8db2a3c8060df523f7af47c230 100644
--- a/third_party/blink/public/platform/web_content_settings_client.h
+++ b/third_party/blink/public/platform/web_content_settings_client.h
@@ -58,6 +58,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; }
+
   // Controls whether enabling Web Components API for this frame.
   virtual bool AllowWebComponents(bool default_value) { return default_value; }
 
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 20ebd3f2f5fa7b16ad1b2081ca41b007bc78a354..b248e3135182d36a6524c2e626157a0e4c759d14 100644
--- a/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
+++ b/third_party/blink/renderer/core/editing/commands/clipboard_commands.cc
@@ -121,7 +121,7 @@ bool ClipboardCommands::CanReadClipboard(LocalFrame& frame,
     return true;
   }
   return frame.GetContentSettingsClient() &&
-         frame.GetContentSettingsClient()->AllowReadFromClipboard();
+         frame.GetContentSettingsClient()->AllowReadFromClipboardSync();
 }
 
 bool ClipboardCommands::CanWriteClipboard(LocalFrame& frame,
@@ -300,7 +300,7 @@ bool ClipboardCommands::PasteSupported(LocalFrame* frame) {
     return true;
   }
   return frame->GetContentSettingsClient() &&
-         frame->GetContentSettingsClient()->AllowReadFromClipboard();
+         frame->GetContentSettingsClient()->AllowReadFromClipboardSync();
 }
 
 bool ClipboardCommands::ExecuteCopy(LocalFrame& frame,
