summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/clipboard/data_object.cc
blob: 1a096dad2bbdf1ba2dfa1f35c5d1e6a634f9857a (plain)
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (c) 2008, 2009, 2012 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/clipboard/data_object.h"

#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/web_drag_data.h"
#include "third_party/blink/renderer/core/clipboard/clipboard_mime_types.h"
#include "third_party/blink/renderer/core/clipboard/clipboard_utilities.h"
#include "third_party/blink/renderer/core/clipboard/dragged_isolated_file_system.h"
#include "third_party/blink/renderer/core/clipboard/paste_mode.h"
#include "third_party/blink/renderer/core/clipboard/system_clipboard.h"
#include "third_party/blink/renderer/platform/file_metadata.h"
#include "third_party/blink/renderer/platform/wtf/hash_set.h"

namespace blink {

DataObject* DataObject::CreateFromClipboard(PasteMode paste_mode) {
  DataObject* data_object = Create();
#if DCHECK_IS_ON()
  HashSet<String> types_seen;
#endif
  uint64_t sequence_number = SystemClipboard::GetInstance().SequenceNumber();
  for (const String& type :
       SystemClipboard::GetInstance().ReadAvailableTypes()) {
    if (paste_mode == PasteMode::kPlainTextOnly && type != kMimeTypeTextPlain)
      continue;
    data_object->item_list_.push_back(
        DataObjectItem::CreateFromClipboard(type, sequence_number));
#if DCHECK_IS_ON()
    DCHECK(types_seen.insert(type).is_new_entry);
#endif
  }
  return data_object;
}

DataObject* DataObject::CreateFromString(const String& data) {
  DataObject* data_object = Create();
  data_object->Add(data, kMimeTypeTextPlain);
  return data_object;
}

DataObject* DataObject::Create() {
  return new DataObject;
}

DataObject::~DataObject() = default;

uint32_t DataObject::length() const {
  return item_list_.size();
}

DataObjectItem* DataObject::Item(uint32_t index) {
  if (index >= length())
    return nullptr;
  return item_list_[index];
}

void DataObject::DeleteItem(uint32_t index) {
  if (index >= length())
    return;
  item_list_.EraseAt(index);
  NotifyItemListChanged();
}

void DataObject::ClearAll() {
  if (item_list_.IsEmpty())
    return;
  item_list_.clear();
  NotifyItemListChanged();
}

DataObjectItem* DataObject::Add(const String& data, const String& type) {
  DataObjectItem* item = DataObjectItem::CreateFromString(type, data);
  if (!InternalAddStringItem(item))
    return nullptr;
  return item;
}

DataObjectItem* DataObject::Add(File* file) {
  if (!file)
    return nullptr;

  DataObjectItem* item = DataObjectItem::CreateFromFile(file);
  InternalAddFileItem(item);
  return item;
}

DataObjectItem* DataObject::Add(File* file, const String& file_system_id) {
  if (!file)
    return nullptr;

  DataObjectItem* item =
      DataObjectItem::CreateFromFileWithFileSystemId(file, file_system_id);
  InternalAddFileItem(item);
  return item;
}

void DataObject::ClearData(const String& type) {
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->Kind() == DataObjectItem::kStringKind &&
        item_list_[i]->GetType() == type) {
      // Per the spec, type must be unique among all items of kind 'string'.
      item_list_.EraseAt(i);
      NotifyItemListChanged();
      return;
    }
  }
}

Vector<String> DataObject::Types() const {
  Vector<String> results;
#if DCHECK_IS_ON()
  HashSet<String> types_seen;
#endif
  bool contains_files = false;
  for (const auto& item : item_list_) {
    switch (item->Kind()) {
      case DataObjectItem::kStringKind:
        // Per the spec, type must be unique among all items of kind 'string'.
        results.push_back(item->GetType());
#if DCHECK_IS_ON()
        DCHECK(types_seen.insert(item->GetType()).is_new_entry);
#endif
        break;
      case DataObjectItem::kFileKind:
        contains_files = true;
        break;
    }
  }
  if (contains_files) {
    results.push_back(kMimeTypeFiles);
#if DCHECK_IS_ON()
    DCHECK(types_seen.insert(kMimeTypeFiles).is_new_entry);
#endif
  }
  return results;
}

String DataObject::GetData(const String& type) const {
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->Kind() == DataObjectItem::kStringKind &&
        item_list_[i]->GetType() == type)
      return item_list_[i]->GetAsString();
  }
  return String();
}

void DataObject::SetData(const String& type, const String& data) {
  ClearData(type);
  if (!Add(data, type))
    NOTREACHED();
}

void DataObject::UrlAndTitle(String& url, String* title) const {
  DataObjectItem* item = FindStringItem(kMimeTypeTextURIList);
  if (!item)
    return;
  url = ConvertURIListToURL(item->GetAsString());
  if (title)
    *title = item->Title();
}

void DataObject::SetURLAndTitle(const String& url, const String& title) {
  ClearData(kMimeTypeTextURIList);
  InternalAddStringItem(DataObjectItem::CreateFromURL(url, title));
}

void DataObject::HtmlAndBaseURL(String& html, KURL& base_url) const {
  DataObjectItem* item = FindStringItem(kMimeTypeTextHTML);
  if (!item)
    return;
  html = item->GetAsString();
  base_url = item->BaseURL();
}

void DataObject::SetHTMLAndBaseURL(const String& html, const KURL& base_url) {
  ClearData(kMimeTypeTextHTML);
  InternalAddStringItem(DataObjectItem::CreateFromHTML(html, base_url));
}

bool DataObject::ContainsFilenames() const {
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->IsFilename())
      return true;
  }
  return false;
}

Vector<String> DataObject::Filenames() const {
  Vector<String> results;
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->IsFilename())
      results.push_back(item_list_[i]->GetAsFile()->GetPath());
  }
  return results;
}

void DataObject::AddFilename(const String& filename,
                             const String& display_name,
                             const String& file_system_id) {
  InternalAddFileItem(DataObjectItem::CreateFromFileWithFileSystemId(
      File::CreateForUserProvidedFile(filename, display_name), file_system_id));
}

void DataObject::AddSharedBuffer(scoped_refptr<SharedBuffer> buffer,
                                 const KURL& source_url,
                                 const String& filename_extension,
                                 const AtomicString& content_disposition) {
  InternalAddFileItem(DataObjectItem::CreateFromSharedBuffer(
      std::move(buffer), source_url, filename_extension, content_disposition));
}

DataObject::DataObject() : modifiers_(0) {}

DataObjectItem* DataObject::FindStringItem(const String& type) const {
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->Kind() == DataObjectItem::kStringKind &&
        item_list_[i]->GetType() == type)
      return item_list_[i];
  }
  return nullptr;
}

bool DataObject::InternalAddStringItem(DataObjectItem* item) {
  DCHECK_EQ(item->Kind(), DataObjectItem::kStringKind);
  for (wtf_size_t i = 0; i < item_list_.size(); ++i) {
    if (item_list_[i]->Kind() == DataObjectItem::kStringKind &&
        item_list_[i]->GetType() == item->GetType())
      return false;
  }

  item_list_.push_back(item);
  NotifyItemListChanged();
  return true;
}

void DataObject::InternalAddFileItem(DataObjectItem* item) {
  DCHECK_EQ(item->Kind(), DataObjectItem::kFileKind);
  item_list_.push_back(item);
  NotifyItemListChanged();
}

void DataObject::AddObserver(Observer* observer) {
  DCHECK(!observers_.Contains(observer));
  observers_.insert(observer);
}

void DataObject::NotifyItemListChanged() const {
  for (const auto& observer : observers_)
    observer->OnItemListChanged();
}

void DataObject::Trace(blink::Visitor* visitor) {
  visitor->Trace(item_list_);
  visitor->Trace(observers_);
  Supplementable<DataObject>::Trace(visitor);
}

DataObject* DataObject::Create(WebDragData data) {
  DataObject* data_object = Create();
  bool has_file_system = false;

  WebVector<WebDragData::Item> items = data.Items();
  for (unsigned i = 0; i < items.size(); ++i) {
    WebDragData::Item item = items[i];

    switch (item.storage_type) {
      case WebDragData::Item::kStorageTypeString:
        if (String(item.string_type) == kMimeTypeTextURIList)
          data_object->SetURLAndTitle(item.string_data, item.title);
        else if (String(item.string_type) == kMimeTypeTextHTML)
          data_object->SetHTMLAndBaseURL(item.string_data, item.base_url);
        else
          data_object->SetData(item.string_type, item.string_data);
        break;
      case WebDragData::Item::kStorageTypeFilename:
        has_file_system = true;
        data_object->AddFilename(item.filename_data, item.display_name_data,
                                 data.FilesystemId());
        break;
      case WebDragData::Item::kStorageTypeBinaryData:
        // This should never happen when dragging in.
        break;
      case WebDragData::Item::kStorageTypeFileSystemFile: {
        // FIXME: The file system URL may refer a user visible file, see
        // http://crbug.com/429077
        has_file_system = true;
        FileMetadata file_metadata;
        file_metadata.length = item.file_system_file_size;

        data_object->Add(
            File::CreateForFileSystemFile(item.file_system_url, file_metadata,
                                          File::kIsNotUserVisible),
            item.file_system_id);
      } break;
    }
  }

  data_object->SetFilesystemId(data.FilesystemId());

  if (has_file_system)
    DraggedIsolatedFileSystem::PrepareForDataObject(data_object);

  return data_object;
}

WebDragData DataObject::ToWebDragData() {
  WebDragData data;
  data.Initialize();
  data.SetModifierKeyState(modifiers_);
  WebVector<WebDragData::Item> item_list(length());

  for (wtf_size_t i = 0; i < length(); ++i) {
    DataObjectItem* original_item = Item(i);
    WebDragData::Item item;
    if (original_item->Kind() == DataObjectItem::kStringKind) {
      item.storage_type = WebDragData::Item::kStorageTypeString;
      item.string_type = original_item->GetType();
      item.string_data = original_item->GetAsString();
      item.title = original_item->Title();
      item.base_url = original_item->BaseURL();
    } else if (original_item->Kind() == DataObjectItem::kFileKind) {
      if (original_item->GetSharedBuffer()) {
        item.storage_type = WebDragData::Item::kStorageTypeBinaryData;
        item.binary_data = original_item->GetSharedBuffer();
        item.binary_data_source_url = original_item->BaseURL();
        item.binary_data_filename_extension =
            original_item->FilenameExtension();
        item.binary_data_content_disposition = original_item->Title();
      } else if (original_item->IsFilename()) {
        Blob* blob = original_item->GetAsFile();
        if (blob->IsFile()) {
          File* file = ToFile(blob);
          if (file->HasBackingFile()) {
            item.storage_type = WebDragData::Item::kStorageTypeFilename;
            item.filename_data = file->GetPath();
            item.display_name_data = file->name();
          } else if (!file->FileSystemURL().IsEmpty()) {
            item.storage_type = WebDragData::Item::kStorageTypeFileSystemFile;
            item.file_system_url = file->FileSystemURL();
            item.file_system_file_size = file->size();
            item.file_system_id = original_item->FileSystemId();
          } else {
            // FIXME: support dragging constructed Files across renderers, see
            // http://crbug.com/394955
            item.storage_type = WebDragData::Item::kStorageTypeString;
            item.string_type = "text/plain";
            item.string_data = file->name();
          }
        } else {
          NOTREACHED();
        }
      } else {
        NOTREACHED();
      }
    } else {
      NOTREACHED();
    }
    item_list[i] = item;
  }
  data.SwapItems(item_list);
  return data;
}

}  // namespace blink