File: os_exchange_data_provider_mac.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (371 lines) | stat: -rw-r--r-- 12,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/dragdrop/os_exchange_data_provider_mac.h"

#import <Cocoa/Cocoa.h>

#include <algorithm>
#include <optional>
#include <string_view>

#include "base/apple/foundation_util.h"
#include "base/check_op.h"
#include "base/containers/span.h"
#include "base/memory/ptr_util.h"
#include "base/notreached.h"
#include "base/pickle.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "net/base/filename_util.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/clipboard/clipboard_format_type.h"
#import "ui/base/clipboard/clipboard_util_mac.h"
#include "ui/base/clipboard/custom_data_helper.h"
#include "ui/base/clipboard/file_info.h"
#include "ui/base/data_transfer_policy/data_transfer_policy_controller.h"
#include "url/gurl.h"

@interface CrPasteboardItemWrapper : NSObject <NSPasteboardWriting>
- (instancetype)initWithPasteboardItem:(NSPasteboardItem*)pasteboardItem;
@end

@implementation CrPasteboardItemWrapper {
  NSPasteboardItem* __strong _pasteboardItem;
}

- (instancetype)initWithPasteboardItem:(NSPasteboardItem*)pasteboardItem {
  if ((self = [super init])) {
    _pasteboardItem = pasteboardItem;
  }

  return self;
}

- (NSArray<NSString*>*)writableTypesForPasteboard:(NSPasteboard*)pasteboard {
  // If the NSPasteboardItem hasn't been added to an NSPasteboard, then the
  // -[NSPasteboardItem writableTypesForPasteboard:] will return -types. But if
  // it has been added to a pasteboard, it will return nil. This pasteboard item
  // was added implicitly by adding flavors to the owned pasteboard of
  // OwningProvider, so call -types to actually get data.
  //
  // Merge in the ui::kUTTypeChromiumInitiatedDrag type, so that all of Chromium
  // is marked to receive the drags. TODO(avi): Wire up MacViews so that
  // BridgedContentView properly registers the result of View::GetDropFormats()
  // rather than OSExchangeDataProviderMac::SupportedPasteboardTypes().
  return [_pasteboardItem.types
      arrayByAddingObject:ui::kUTTypeChromiumInitiatedDrag];
}

- (NSPasteboardWritingOptions)writingOptionsForType:(NSString*)type
                                         pasteboard:(NSPasteboard*)pasteboard {
  // It is critical to return 0 here. If any flavors are promised, then when the
  // app quits, AppKit will call in the promises, and the backing pasteboard
  // will likely be long-deallocated. Yes, AppKit will call in promises for
  // *all* promised flavors on *all* pasteboards, not just those pasteboards
  // used for copy/paste.
  return 0;
}

- (id)pasteboardPropertyListForType:(NSString*)type {
  if ([type isEqual:ui::kUTTypeChromiumInitiatedDrag])
    return [NSData data];

  // Like above, an NSPasteboardItem added to a pasteboard will return nil from
  // -pasteboardPropertyListForType:, so call -dataForType: instead.
  return [_pasteboardItem dataForType:type];
}

@end

namespace ui {

namespace {

class OwningProvider : public OSExchangeDataProviderMac {
 public:
  OwningProvider() : owned_pasteboard_(new UniquePasteboard) {}
  OwningProvider(const OwningProvider& provider) = default;

  std::unique_ptr<OSExchangeDataProvider> Clone() const override {
    return std::make_unique<OwningProvider>(*this);
  }

  NSPasteboard* GetPasteboard() const override {
    return owned_pasteboard_->get();
  }

 private:
  scoped_refptr<UniquePasteboard> owned_pasteboard_;
};

class WrappingProvider : public OSExchangeDataProviderMac {
 public:
  explicit WrappingProvider(NSPasteboard* pasteboard)
      : wrapped_pasteboard_(pasteboard) {}
  WrappingProvider(const WrappingProvider& provider) = default;

  std::unique_ptr<OSExchangeDataProvider> Clone() const override {
    return std::make_unique<WrappingProvider>(*this);
  }

  NSPasteboard* GetPasteboard() const override { return wrapped_pasteboard_; }

 private:
  __strong NSPasteboard* wrapped_pasteboard_;
};

}  // namespace

OSExchangeDataProviderMac::OSExchangeDataProviderMac() = default;
OSExchangeDataProviderMac::OSExchangeDataProviderMac(
    const OSExchangeDataProviderMac&) = default;
OSExchangeDataProviderMac& OSExchangeDataProviderMac::operator=(
    const OSExchangeDataProviderMac&) = default;

OSExchangeDataProviderMac::~OSExchangeDataProviderMac() = default;

// static
std::unique_ptr<OSExchangeDataProviderMac>
OSExchangeDataProviderMac::CreateProvider() {
  return std::make_unique<OwningProvider>();
}

// static
std::unique_ptr<OSExchangeDataProviderMac>
OSExchangeDataProviderMac::CreateProviderWrappingPasteboard(
    NSPasteboard* pasteboard) {
  return std::make_unique<WrappingProvider>(pasteboard);
}

void OSExchangeDataProviderMac::MarkRendererTaintedFromOrigin(
    const url::Origin& origin) {
  NSString* string = origin.opaque()
                         ? [NSString string]
                         : base::SysUTF8ToNSString(origin.Serialize());
  [GetPasteboard() setString:string
                     forType:kUTTypeChromiumRendererInitiatedDrag];
}

bool OSExchangeDataProviderMac::IsRendererTainted() const {
  return [GetPasteboard().types
      containsObject:kUTTypeChromiumRendererInitiatedDrag];
}

std::optional<url::Origin> OSExchangeDataProviderMac::GetRendererTaintedOrigin()
    const {
  NSString* item =
      [GetPasteboard() stringForType:kUTTypeChromiumRendererInitiatedDrag];
  if (!item) {
    return std::nullopt;
  }

  if (0 == [item length]) {
    return url::Origin();
  }

  return url::Origin::Create(GURL(base::SysNSStringToUTF8(item)));
}

void OSExchangeDataProviderMac::MarkAsFromPrivileged() {
  [GetPasteboard() setData:[NSData data]
                   forType:kUTTypeChromiumPrivilegedInitiatedDrag];
}

bool OSExchangeDataProviderMac::IsFromPrivileged() const {
  return [GetPasteboard().types
      containsObject:kUTTypeChromiumPrivilegedInitiatedDrag];
}

void OSExchangeDataProviderMac::SetString(std::u16string_view string) {
  [GetPasteboard() setString:base::SysUTF16ToNSString(string)
                     forType:NSPasteboardTypeString];
}

void OSExchangeDataProviderMac::SetURL(const GURL& url,
                                       std::u16string_view title) {
  NSArray<NSPasteboardItem*>* items = clipboard_util::PasteboardItemsFromUrls(
      @[ base::SysUTF8ToNSString(url.spec()) ],
      @[ base::SysUTF16ToNSString(title) ]);
  clipboard_util::AddDataToPasteboard(GetPasteboard(), items.firstObject);
}

void OSExchangeDataProviderMac::SetFilename(const base::FilePath& path) {
  std::vector<FileInfo> filenames(1, FileInfo(path, base::FilePath()));
  clipboard_util::WriteFilesToPasteboard(GetPasteboard(), filenames);
}

void OSExchangeDataProviderMac::SetFilenames(
    const std::vector<FileInfo>& filenames) {
  clipboard_util::WriteFilesToPasteboard(GetPasteboard(), filenames);
}

void OSExchangeDataProviderMac::SetPickledData(
    const ClipboardFormatType& format,
    const base::Pickle& data) {
  NSData* ns_data = [NSData dataWithBytes:data.data() length:data.size()];
  [GetPasteboard() setData:ns_data forType:format.ToNSString()];
}

std::optional<std::u16string> OSExchangeDataProviderMac::GetString() const {
  NSString* item = [GetPasteboard() stringForType:NSPasteboardTypeString];
  if (item) {
    return base::SysNSStringToUTF16(item);
  }

  // There was no NSString, check for an NSURL.
  if (std::optional<UrlInfo> url_info =
          GetURLAndTitle(FilenameToURLPolicy::DO_NOT_CONVERT_FILENAMES);
      url_info.has_value()) {
    return base::UTF8ToUTF16(url_info->url.spec());
  }

  return std::nullopt;
}

std::optional<OSExchangeDataProvider::UrlInfo>
OSExchangeDataProviderMac::GetURLAndTitle(FilenameToURLPolicy policy) const {
  NSArray<URLAndTitle*>* urls_and_titles =
      clipboard_util::URLsAndTitlesFromPasteboard(
          GetPasteboard(), policy == FilenameToURLPolicy::CONVERT_FILENAMES);
  if (!urls_and_titles.count) {
    return std::nullopt;
  }

  GURL url(base::SysNSStringToUTF8(urls_and_titles.firstObject.URL));
  return UrlInfo{std::move(url),
                 base::SysNSStringToUTF16(urls_and_titles.firstObject.title)};
}

std::optional<std::vector<GURL>> OSExchangeDataProviderMac::GetURLs(
    FilenameToURLPolicy policy) const {
  NSArray<URLAndTitle*>* urls_and_titles =
      clipboard_util::URLsAndTitlesFromPasteboard(
          GetPasteboard(), policy == FilenameToURLPolicy::CONVERT_FILENAMES);
  if (!urls_and_titles.count) {
    return std::nullopt;
  }

  std::vector<GURL> local_urls;
  for (URLAndTitle* url_and_title in urls_and_titles) {
    local_urls.emplace_back(base::SysNSStringToUTF8(url_and_title.URL));
  }
  return local_urls;
}

std::optional<std::vector<FileInfo>> OSExchangeDataProviderMac::GetFilenames()
    const {
  std::vector<FileInfo> files =
      clipboard_util::FilesFromPasteboard(GetPasteboard());
  if (files.empty()) {
    return std::nullopt;
  }

  return files;
}

std::optional<base::Pickle> OSExchangeDataProviderMac::GetPickledData(
    const ClipboardFormatType& format) const {
  NSData* ns_data = [GetPasteboard() dataForType:format.ToNSString()];
  if (!ns_data) {
    return std::nullopt;
  }

  return base::Pickle::WithData(base::apple::NSDataToSpan(ns_data));
}

bool OSExchangeDataProviderMac::HasString() const {
  return GetString().has_value();
}

bool OSExchangeDataProviderMac::HasURL(FilenameToURLPolicy policy) const {
  return GetURLAndTitle(policy).has_value();
}

bool OSExchangeDataProviderMac::HasFile() const {
  return [GetPasteboard().types containsObject:NSPasteboardTypeFileURL];
}

bool OSExchangeDataProviderMac::HasCustomFormat(
    const ClipboardFormatType& format) const {
  return [GetPasteboard().types containsObject:format.ToNSString()];
}

void OSExchangeDataProviderMac::SetFileContents(
    const base::FilePath& filename,
    const std::string& file_contents) {
  NOTIMPLEMENTED();
}

std::optional<OSExchangeDataProvider::FileContentsInfo>
OSExchangeDataProviderMac::GetFileContents() const {
  NOTIMPLEMENTED();
  return std::nullopt;
}

bool OSExchangeDataProviderMac::HasFileContents() const {
  NOTIMPLEMENTED();
  return false;
}

void OSExchangeDataProviderMac::SetDragImage(
    const gfx::ImageSkia& image,
    const gfx::Vector2d& cursor_offset) {
  drag_image_ = image;
  cursor_offset_ = cursor_offset;
}

gfx::ImageSkia OSExchangeDataProviderMac::GetDragImage() const {
  return drag_image_;
}

gfx::Vector2d OSExchangeDataProviderMac::GetDragImageOffset() const {
  return cursor_offset_;
}

NSArray<NSDraggingItem*>* OSExchangeDataProviderMac::GetDraggingItems() const {
  // What's going on here is that initiating a drag (-[NSView
  // beginDraggingSessionWithItems...]) requires a dragging item. Even though
  // pasteboard items are NSPasteboardWriters, they are locked to their
  // pasteboard and cannot be used to initiate a drag with another pasteboard
  // (hello https://crbug.com/928684). Therefore, wrap them.

  NSArray<NSPasteboardItem*>* pasteboard_items =
      GetPasteboard().pasteboardItems;
  if (!pasteboard_items) {
    return nil;
  }

  NSMutableArray<NSDraggingItem*>* drag_items = [NSMutableArray array];
  for (NSPasteboardItem* item in pasteboard_items) {
    CrPasteboardItemWrapper* wrapper =
        [[CrPasteboardItemWrapper alloc] initWithPasteboardItem:item];
    NSDraggingItem* drag_item =
        [[NSDraggingItem alloc] initWithPasteboardWriter:wrapper];

    [drag_items addObject:drag_item];
  }

  return drag_items;
}

// static
NSArray* OSExchangeDataProviderMac::SupportedPasteboardTypes() {
  return @[
    kUTTypeChromiumInitiatedDrag, kUTTypeChromiumPrivilegedInitiatedDrag,
    kUTTypeChromiumRendererInitiatedDrag, kUTTypeChromiumDataTransferCustomData,
    kUTTypeWebKitWebUrlsWithTitles, kUTTypeChromiumSourceUrl,
    NSPasteboardTypeFileURL, NSPasteboardTypeHTML, NSPasteboardTypeRTF,
    NSPasteboardTypeString, NSPasteboardTypeURL
  ];
}

void OSExchangeDataProviderMac::SetSource(
    std::unique_ptr<DataTransferEndpoint> data_source) {}

DataTransferEndpoint* OSExchangeDataProviderMac::GetSource() const {
  return nullptr;
}

}  // namespace ui