summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/chromium/ClipboardChromium.cpp
blob: aff1466d282c7acc6d3917ec2a95477242508a64 (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
 * Copyright (C) 2006, 2007 Apple Inc.  All rights reserved.
 * Copyright (C) 2008, 2009 Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``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 APPLE COMPUTER, INC. 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 "config.h"
#include "ClipboardChromium.h"

#include "CachedImage.h"
#include "ChromiumBridge.h"
#include "ChromiumDataObject.h"
#include "ClipboardUtilitiesChromium.h"
#include "Document.h"
#include "DragData.h"
#include "Element.h"
#include "FileList.h"
#include "Frame.h"
#include "HTMLNames.h"
#include "Image.h"
#include "MIMETypeRegistry.h"
#include "NamedNodeMap.h"
#include "Pasteboard.h"
#include "PlatformString.h"
#include "Range.h"
#include "RenderImage.h"
#include "ScriptExecutionContext.h"
#include "StringBuilder.h"
#include "markup.h"

namespace WebCore {

using namespace HTMLNames;

// We provide the IE clipboard types (URL and Text), and the clipboard types specified in the WHATWG Web Applications 1.0 draft
// see http://www.whatwg.org/specs/web-apps/current-work/ Section 6.3.5.3

enum ClipboardDataType {
    ClipboardDataTypeNone,

    ClipboardDataTypeURL,
    ClipboardDataTypeURIList,
    ClipboardDataTypeDownloadURL,
    ClipboardDataTypePlainText,
    ClipboardDataTypeHTML,

    ClipboardDataTypeOther,
};

// Per RFC 2483, the line separator for "text/..." MIME types is CR-LF.
static char const* const textMIMETypeLineSeparator = "\r\n";

static ClipboardDataType clipboardTypeFromMIMEType(const String& type)
{
    String cleanType = type.stripWhiteSpace().lower();
    if (cleanType.isEmpty())
        return ClipboardDataTypeNone;

    // Includes two special cases for IE compatibility.
    if (cleanType == "text" || cleanType == "text/plain" || cleanType.startsWith("text/plain;"))
        return ClipboardDataTypePlainText;
    if (cleanType == "url")
        return ClipboardDataTypeURL;
    if (cleanType == "text/uri-list")
        return ClipboardDataTypeURIList;
    if (cleanType == "downloadurl")
        return ClipboardDataTypeDownloadURL;
    if (cleanType == "text/html")
        return ClipboardDataTypeHTML;

    return ClipboardDataTypeOther;
}

PassRefPtr<Clipboard> Clipboard::create(ClipboardAccessPolicy policy, DragData* dragData, Frame* frame)
{
    return ClipboardChromium::create(true, dragData->platformData(), policy, frame);
}

ClipboardChromium::ClipboardChromium(bool isForDragging,
                                     PassRefPtr<ChromiumDataObject> dataObject,
                                     ClipboardAccessPolicy policy,
                                     Frame* frame)
    : Clipboard(policy, isForDragging)
    , m_dataObject(dataObject)
    , m_frame(frame)
{
}

PassRefPtr<ClipboardChromium> ClipboardChromium::create(bool isForDragging,
    PassRefPtr<ChromiumDataObject> dataObject, ClipboardAccessPolicy policy, Frame* frame)
{
    return adoptRef(new ClipboardChromium(isForDragging, dataObject, policy, frame));
}

void ClipboardChromium::clearData(const String& type)
{
    if (policy() != ClipboardWritable || !m_dataObject)
        return;

    ClipboardDataType dataType = clipboardTypeFromMIMEType(type);
    switch (dataType) {
    case ClipboardDataTypeNone:
        // If called with no arguments, everything except the file list must be cleared.
        // (See HTML5 spec, "The DragEvent and DataTransfer interfaces")
        m_dataObject->clearAllExceptFiles();
        return;

    case ClipboardDataTypeURL:
    case ClipboardDataTypeURIList:
        m_dataObject->clearURL();
        return;

    case ClipboardDataTypeDownloadURL:
        m_dataObject->downloadMetadata = "";
        return;
        
    case ClipboardDataTypePlainText:
        m_dataObject->plainText = "";
        return;

    case ClipboardDataTypeHTML:
        m_dataObject->textHtml = "";
        m_dataObject->htmlBaseUrl = KURL();
        return;

    case ClipboardDataTypeOther:
        // Not yet implemented, see https://bugs.webkit.org/show_bug.cgi?id=34410
        return;
    }

    ASSERT_NOT_REACHED();
}

void ClipboardChromium::clearAllData()
{
    if (policy() != ClipboardWritable)
        return;

    m_dataObject->clear();
}

String ClipboardChromium::getData(const String& type, bool& success) const
{
    success = false;
    if (policy() != ClipboardReadable || !m_dataObject)
        return String();

    ClipboardDataType dataType = clipboardTypeFromMIMEType(type);
    switch (dataType) {
    case ClipboardDataTypeNone:
        return String();

    // Hack for URLs. file URLs are used internally for drop's default action, but we don't want
    // to expose them to the page, so we filter them out here.
    case ClipboardDataTypeURIList:
        {
            String text;
            for (size_t i = 0; i < m_dataObject->uriList.size(); ++i) {
                const String& uri = m_dataObject->uriList[i];
                if (protocolIs(uri, "file"))
                    continue;
                ASSERT(!uri.isEmpty());
                if (!text.isEmpty())
                    text.append(textMIMETypeLineSeparator);
                // URIs have already been canonicalized, so copy everything verbatim.
                text.append(uri);
            }
            success = !text.isEmpty();
            return text;
        }

    case ClipboardDataTypeURL:
        // In case of a previous setData('text/uri-list'), setData() has already
        // prepared the 'url' member, so we can just retrieve it here.
        if (!m_dataObject->url.isEmpty() && !m_dataObject->url.isLocalFile()) {
            success = true;
            return m_dataObject->url.string();
        }
        return String();

    case ClipboardDataTypeDownloadURL:
        success = !m_dataObject->downloadMetadata.isEmpty();
        return m_dataObject->downloadMetadata;
    
    case ClipboardDataTypePlainText:
        if (!isForDragging()) {
            // If this isn't for a drag, it's for a cut/paste event handler.
            // In this case, we need to check the clipboard.
            PasteboardPrivate::ClipboardBuffer buffer = 
                Pasteboard::generalPasteboard()->isSelectionMode() ?
                PasteboardPrivate::SelectionBuffer : 
                PasteboardPrivate::StandardBuffer;
            String text = ChromiumBridge::clipboardReadPlainText(buffer);
            success = !text.isEmpty();
            return text;
        }
        // Otherwise return whatever is stored in plainText.
        success = !m_dataObject->plainText.isEmpty();
        return m_dataObject->plainText;

    case ClipboardDataTypeHTML:
        if (!isForDragging()) {
            // If this isn't for a drag, it's for a cut/paste event handler.
            // In this case, we need to check the clipboard.
            PasteboardPrivate::ClipboardBuffer buffer = 
                Pasteboard::generalPasteboard()->isSelectionMode() ?
                PasteboardPrivate::SelectionBuffer : 
                PasteboardPrivate::StandardBuffer;
            String htmlText;
            KURL sourceURL;
            ChromiumBridge::clipboardReadHTML(buffer, &htmlText, &sourceURL);
            success = !htmlText.isEmpty();
            return htmlText;
        }
        // Otherwise return whatever is stored in textHtml.
        success = !m_dataObject->textHtml.isEmpty();
        return m_dataObject->textHtml;

    case ClipboardDataTypeOther:
        // not yet implemented, see https://bugs.webkit.org/show_bug.cgi?id=34410
        return String();
    }

    ASSERT_NOT_REACHED();
    return String();
}

bool ClipboardChromium::setData(const String& type, const String& data)
{
    if (policy() != ClipboardWritable)
        return false;

    ClipboardDataType dataType = clipboardTypeFromMIMEType(type);
    switch (dataType) {
    case ClipboardDataTypeNone:
        return false;

    case ClipboardDataTypeURL:
        // For setData(), "URL" must be treated as "text/uri-list".
        // (See HTML5 spec, "The DragEvent and DataTransfer interfaces")
    case ClipboardDataTypeURIList:
        m_dataObject->url = KURL();
        // Line separator is \r\n per RFC 2483 - however, for compatibility reasons
        // we also allow just \n here. 
        data.split('\n', m_dataObject->uriList);
        // Strip white space on all lines, including trailing \r from above split.
        // If this leaves a line empty, remove it completely.
        //
        // Also, copy the first valid URL into the 'url' member as well.
        // In case no entry is a valid URL (i.e., remarks only), then we leave 'url' empty.
        // I.e., in that case subsequent calls to getData("URL") will get an empty string.
        // This is in line with the HTML5 spec (see "The DragEvent and DataTransfer interfaces").
        for (size_t i = 0; i < m_dataObject->uriList.size(); /**/) {
            String& line = m_dataObject->uriList[i];
            line = line.stripWhiteSpace();
            if (line.isEmpty()) {
                m_dataObject->uriList.remove(i);
                continue;
            }
            ++i;
            // Only copy the first valid URL.
            if (m_dataObject->url.isValid())
                continue;
            // Skip remarks.
            if (line[0] == '#')
                continue;
            KURL url = KURL(ParsedURLString, line);
            if (url.isValid())
                m_dataObject->url = url;
        }
        if (m_dataObject->uriList.isEmpty()) {
            ASSERT(m_dataObject->url.isEmpty());
            return data.isEmpty();
        }
        return true;

    case ClipboardDataTypeDownloadURL:
        m_dataObject->downloadMetadata = data;
        return true;

    case ClipboardDataTypePlainText:
        m_dataObject->plainText = data;
        return true;

    case ClipboardDataTypeHTML:
        m_dataObject->textHtml = data;
        return true;

    case ClipboardDataTypeOther:
        // Not yet implemented, see https://bugs.webkit.org/show_bug.cgi?id=34410
        return false;
    }
    
    ASSERT_NOT_REACHED();
    return false;
}

// extensions beyond IE's API
HashSet<String> ClipboardChromium::types() const
{
    HashSet<String> results;
    if (policy() != ClipboardReadable && policy() != ClipboardTypesReadable)
        return results;

    if (!m_dataObject)
        return results;

    if (!m_dataObject->filenames.isEmpty())
        results.add("Files");

    // Hack for URLs. file URLs are used internally for drop's default action, but we don't want
    // to expose them to the page, so we filter them out here.
    if (m_dataObject->url.isValid() && !m_dataObject->url.isLocalFile()) {
        ASSERT(!m_dataObject->uriList.isEmpty());
        results.add("URL");
    }

    if (!m_dataObject->uriList.isEmpty()) {
        // Verify that the URI list contains at least one non-file URL.
        for (Vector<String>::const_iterator it = m_dataObject->uriList.begin();
             it != m_dataObject->uriList.end(); ++it) {
            if (!protocolIs(*it, "file")) {
                // Note that even if the URI list is not empty, it may not actually
                // contain a valid URL, so we can't return "URL" here.
                results.add("text/uri-list");
                break;
            }
        }
    }

    if (!m_dataObject->plainText.isEmpty()) {
        results.add("Text");
        results.add("text/plain");
    }

    return results;
}

PassRefPtr<FileList> ClipboardChromium::files() const
{
    if (policy() != ClipboardReadable)
        return FileList::create();

    if (!m_dataObject || m_dataObject->filenames.isEmpty())
        return FileList::create();

    ScriptExecutionContext* scriptExecutionContext = m_frame->document()->scriptExecutionContext();
    RefPtr<FileList> fileList = FileList::create();
    for (size_t i = 0; i < m_dataObject->filenames.size(); ++i)
        fileList->append(File::create(scriptExecutionContext, m_dataObject->filenames.at(i)));

    return fileList.release();
}

void ClipboardChromium::setDragImage(CachedImage* image, Node* node, const IntPoint& loc)
{
    if (policy() != ClipboardImageWritable && policy() != ClipboardWritable)
        return;

    if (m_dragImage)
        m_dragImage->removeClient(this);
    m_dragImage = image;
    if (m_dragImage)
        m_dragImage->addClient(this);

    m_dragLoc = loc;
    m_dragImageElement = node;
}

void ClipboardChromium::setDragImage(CachedImage* img, const IntPoint& loc)
{
    setDragImage(img, 0, loc);
}

void ClipboardChromium::setDragImageElement(Node* node, const IntPoint& loc)
{
    setDragImage(0, node, loc);
}

DragImageRef ClipboardChromium::createDragImage(IntPoint& loc) const
{
    DragImageRef result = 0;
    if (m_dragImageElement) {
        if (m_frame) {
            result = m_frame->nodeImage(m_dragImageElement.get());
            loc = m_dragLoc;
        }
    } else if (m_dragImage) {
        result = createDragImageFromImage(m_dragImage->image());
        loc = m_dragLoc;
    }
    return result;
}

static String imageToMarkup(const String& url, Element* element)
{
    StringBuilder markup;
    markup.append("<img src=\"");
    markup.append(url);
    markup.append("\"");
    // Copy over attributes.  If we are dragging an image, we expect things like
    // the id to be copied as well.
    NamedNodeMap* attrs = element->attributes();
    unsigned length = attrs->length();
    for (unsigned i = 0; i < length; ++i) {
        Attribute* attr = attrs->attributeItem(i);
        if (attr->localName() == "src")
            continue;
        markup.append(" ");
        markup.append(attr->localName());
        markup.append("=\"");
        String escapedAttr = attr->value();
        escapedAttr.replace("\"", "&quot;");
        markup.append(escapedAttr);
        markup.append("\"");
    }

    markup.append("/>");
    return markup.toString();
}

static CachedImage* getCachedImage(Element* element)
{
    // Attempt to pull CachedImage from element
    ASSERT(element);
    RenderObject* renderer = element->renderer();
    if (!renderer || !renderer->isImage())
        return 0;

    RenderImage* image = toRenderImage(renderer);
    if (image->cachedImage() && !image->cachedImage()->errorOccurred())
        return image->cachedImage();

    return 0;
}

static void writeImageToDataObject(ChromiumDataObject* dataObject, Element* element,
                                   const KURL& url)
{
    // Shove image data into a DataObject for use as a file
    CachedImage* cachedImage = getCachedImage(element);
    if (!cachedImage || !cachedImage->image() || !cachedImage->isLoaded())
        return;

    SharedBuffer* imageBuffer = cachedImage->image()->data();
    if (!imageBuffer || !imageBuffer->size())
        return;

    dataObject->fileContent = imageBuffer;

    // Determine the filename for the file contents of the image.  We try to
    // use the alt tag if one exists, otherwise we fall back on the suggested
    // filename in the http header, and finally we resort to using the filename
    // in the URL.
    String extension = MIMETypeRegistry::getPreferredExtensionForMIMEType(
        cachedImage->response().mimeType());
    dataObject->fileExtension = extension.isEmpty() ? "" : "." + extension;
    String title = element->getAttribute(altAttr);
    if (title.isEmpty())
        title = cachedImage->response().suggestedFilename();

    title = ClipboardChromium::validateFileName(title, dataObject);
    dataObject->fileContentFilename = title + dataObject->fileExtension;
}

void ClipboardChromium::declareAndWriteDragImage(Element* element, const KURL& url, const String& title, Frame* frame)
{
    if (!m_dataObject)
        return;

    m_dataObject->url = url;
    m_dataObject->urlTitle = title;

    // Write the bytes in the image to the file format.
    writeImageToDataObject(m_dataObject.get(), element, url);

    AtomicString imageURL = element->getAttribute(srcAttr);
    if (imageURL.isEmpty())
        return;

    String fullURL = frame->document()->completeURL(deprecatedParseURL(imageURL));
    if (fullURL.isEmpty())
        return;

    // Put img tag on the clipboard referencing the image
    m_dataObject->textHtml = imageToMarkup(fullURL, element);
}

void ClipboardChromium::writeURL(const KURL& url, const String& title, Frame*)
{
    if (!m_dataObject)
        return;
    ASSERT(!url.isEmpty());
    m_dataObject->url = url;
    m_dataObject->urlTitle = title;
    m_dataObject->uriList.clear();
    m_dataObject->uriList.append(url);

    // The URL can also be used as plain text.
    m_dataObject->plainText = url.string();

    // The URL can also be used as an HTML fragment.
    m_dataObject->textHtml = urlToMarkup(url, title);
    m_dataObject->htmlBaseUrl = url;
}

void ClipboardChromium::writeRange(Range* selectedRange, Frame* frame)
{
    ASSERT(selectedRange);
    if (!m_dataObject)
         return;

    m_dataObject->textHtml = createMarkup(selectedRange, 0, AnnotateForInterchange, false, AbsoluteURLs);
    m_dataObject->htmlBaseUrl = frame->document()->url();

    String str = frame->editor()->selectedText();
#if OS(WINDOWS)
    replaceNewlinesWithWindowsStyleNewlines(str);
#endif
    replaceNBSPWithSpace(str);
    m_dataObject->plainText = str;
}

void ClipboardChromium::writePlainText(const String& text)
{
    if (!m_dataObject)
        return;

    String str = text;
#if OS(WINDOWS)
    replaceNewlinesWithWindowsStyleNewlines(str);
#endif
    replaceNBSPWithSpace(str);
    m_dataObject->plainText = str;
}

bool ClipboardChromium::hasData()
{
    if (!m_dataObject)
        return false;

    return m_dataObject->hasData();
}

} // namespace WebCore