| // Copyright 2011 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "content/browser/renderer_host/clipboard_host_impl.h" |
| |
| #import <Cocoa/Cocoa.h> |
| #include <stddef.h> |
| |
| #include "base/functional/bind.h" |
| #include "base/functional/callback_helpers.h" |
| #include "base/strings/sys_string_conversions.h" |
| #include "content/public/browser/browser_thread.h" |
| #import "ui/base/cocoa/find_pasteboard.h" |
| |
| namespace content { |
| namespace { |
| |
| // The number of utf16 code units that will be written to the find pasteboard, |
| // longer texts are silently ignored. This is to prevent that a compromised |
| // renderer can write unlimited amounts of data into the find pasteboard. |
| static constexpr size_t kMaxFindPboardStringLength = 4096; |
| |
| } // namespace |
| |
| void ClipboardHostImpl::WriteStringToFindPboard(const std::u16string& text) { |
| if (text.length() <= kMaxFindPboardStringLength) { |
| NSString* nsText = base::SysUTF16ToNSString(text); |
| if (nsText) { |
| [[FindPasteboard sharedInstance] setFindText:nsText]; |
| } |
| } |
| } |
| |
| void ClipboardHostImpl::GetPlatformPermissionState( |
| GetPlatformPermissionStateCallback callback) { |
| // Note: This method is only called when the MacSystemClipboardPermissionCheck |
| // runtime flag is enabled in the renderer process. |
| |
| // Check macOS system privacy settings for programmatic clipboard access using |
| // the accessBehavior property available in macOS 15.4+. These settings only |
| // affect programmatic access - direct user actions like ⌘V always work. |
| blink::mojom::PlatformClipboardPermissionState state = |
| blink::mojom::PlatformClipboardPermissionState::kAsk; |
| |
| if (@available(macOS 15.4, *)) { |
| NSPasteboardAccessBehavior access_behavior = |
| [NSPasteboard generalPasteboard].accessBehavior; |
| |
| switch (access_behavior) { |
| case NSPasteboardAccessBehaviorAlwaysAllow: |
| state = blink::mojom::PlatformClipboardPermissionState::kAllow; |
| break; |
| case NSPasteboardAccessBehaviorAlwaysDeny: |
| state = blink::mojom::PlatformClipboardPermissionState::kDeny; |
| break; |
| case NSPasteboardAccessBehaviorAsk: |
| state = blink::mojom::PlatformClipboardPermissionState::kAsk; |
| break; |
| case NSPasteboardAccessBehaviorDefault: |
| // Default behavior for the General pasteboard is to ask upon |
| // programmatic access |
| state = blink::mojom::PlatformClipboardPermissionState::kAsk; |
| break; |
| } |
| } else { |
| // The behavior of older macOS versions is effectively kAllow. |
| state = blink::mojom::PlatformClipboardPermissionState::kAllow; |
| } |
| |
| std::move(callback).Run(state); |
| } |
| |
| } // namespace content |