Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/base/nsCopySupport.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include "nsCopySupport.h"
8
#include "nsIDocumentEncoder.h"
9
#include "nsISupports.h"
10
#include "nsIContent.h"
11
#include "nsIComponentManager.h"
12
#include "nsIServiceManager.h"
13
#include "nsIClipboard.h"
14
#include "nsIFormControl.h"
15
#include "nsWidgetsCID.h"
16
#include "nsXPCOM.h"
17
#include "nsISupportsPrimitives.h"
18
#include "nsRange.h"
19
#include "imgIContainer.h"
20
#include "imgIRequest.h"
21
#include "nsIPresShell.h"
22
#include "nsFocusManager.h"
23
#include "mozilla/dom/DataTransfer.h"
24
25
#include "nsIDocShell.h"
26
#include "nsIContentViewerEdit.h"
27
#include "nsIClipboardHelper.h"
28
#include "nsISelectionController.h"
29
30
#include "nsPIDOMWindow.h"
31
#include "nsIDocument.h"
32
#include "nsIHTMLDocument.h"
33
#include "nsGkAtoms.h"
34
#include "nsIFrame.h"
35
#include "nsIURI.h"
36
#include "nsIURIMutator.h"
37
#include "nsISimpleEnumerator.h"
38
39
// image copy stuff
40
#include "nsIImageLoadingContent.h"
41
#include "nsIInterfaceRequestorUtils.h"
42
#include "nsContentUtils.h"
43
#include "nsContentCID.h"
44
45
#ifdef XP_WIN
46
#include "nsCExternalHandlerService.h"
47
#include "nsEscape.h"
48
#include "nsIMIMEInfo.h"
49
#include "nsIMIMEService.h"
50
#include "nsIURL.h"
51
#include "nsReadableUtils.h"
52
#include "nsXULAppAPI.h"
53
#endif
54
55
#include "mozilla/ContentEvents.h"
56
#include "mozilla/dom/Element.h"
57
#include "mozilla/EventDispatcher.h"
58
#include "mozilla/Preferences.h"
59
#include "mozilla/dom/Selection.h"
60
#include "mozilla/IntegerRange.h"
61
62
using namespace mozilla;
63
using namespace mozilla::dom;
64
65
static NS_DEFINE_CID(kCClipboardCID,           NS_CLIPBOARD_CID);
66
static NS_DEFINE_CID(kCTransferableCID,        NS_TRANSFERABLE_CID);
67
static NS_DEFINE_CID(kHTMLConverterCID,        NS_HTMLFORMATCONVERTER_CID);
68
69
// copy string data onto the transferable
70
static nsresult AppendString(nsITransferable *aTransferable,
71
                             const nsAString& aString,
72
                             const char* aFlavor);
73
74
// copy HTML node data
75
static nsresult AppendDOMNode(nsITransferable *aTransferable,
76
                              nsINode* aDOMNode);
77
78
#ifdef XP_WIN
79
// copy image as file promise onto the transferable
80
static nsresult AppendImagePromise(nsITransferable* aTransferable,
81
                                   imgIRequest* aImgRequest,
82
                                   nsIImageLoadingContent* aImageElement);
83
#endif
84
85
// Helper used for HTMLCopy and GetTransferableForSelection since both routines
86
// share common code.
87
static nsresult
88
SelectionCopyHelper(Selection *aSel, nsIDocument *aDoc,
89
                    bool doPutOnClipboard, int16_t aClipboardID,
90
                    uint32_t aFlags, nsITransferable ** aTransferable)
91
0
{
92
0
  // Clear the output parameter for the transferable, if provided.
93
0
  if (aTransferable) {
94
0
    *aTransferable = nullptr;
95
0
  }
96
0
97
0
  nsresult rv;
98
0
99
0
  nsCOMPtr<nsIDocumentEncoder> docEncoder;
100
0
  docEncoder = do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID);
101
0
  NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
102
0
103
0
  // note that we assign text/unicode as mime type, but in fact nsHTMLCopyEncoder
104
0
  // ignore it and use text/html or text/plain depending where the selection
105
0
  // is. if it is a selection into input/textarea element or in a html content
106
0
  // with pre-wrap style : text/plain. Otherwise text/html.
107
0
  // see nsHTMLCopyEncoder::SetSelection
108
0
  nsAutoString mimeType;
109
0
  mimeType.AssignLiteral(kUnicodeMime);
110
0
111
0
  // Do the first and potentially trial encoding as preformatted and raw.
112
0
  uint32_t flags = aFlags | nsIDocumentEncoder::OutputPreformatted
113
0
                          | nsIDocumentEncoder::OutputRaw
114
0
                          | nsIDocumentEncoder::OutputForPlainTextClipboardCopy;
115
0
116
0
  rv = docEncoder->Init(aDoc, mimeType, flags);
117
0
  NS_ENSURE_SUCCESS(rv, rv);
118
0
119
0
  rv = docEncoder->SetSelection(aSel);
120
0
  NS_ENSURE_SUCCESS(rv, rv);
121
0
122
0
  // SetSelection set the mime type to text/plain if the selection is inside a
123
0
  // text widget.
124
0
  rv = docEncoder->GetMimeType(mimeType);
125
0
  NS_ENSURE_SUCCESS(rv, rv);
126
0
  bool selForcedTextPlain = mimeType.EqualsLiteral(kTextMime);
127
0
128
0
  nsAutoString buf;
129
0
  rv = docEncoder->EncodeToString(buf);
130
0
  NS_ENSURE_SUCCESS(rv, rv);
131
0
132
0
  rv = docEncoder->GetMimeType(mimeType);
133
0
  NS_ENSURE_SUCCESS(rv, rv);
134
0
135
0
  if (!selForcedTextPlain && mimeType.EqualsLiteral(kTextMime)) {
136
0
    // SetSelection and EncodeToString use this case to signal that text/plain
137
0
    // was forced because the document is either not an nsIHTMLDocument or it's
138
0
    // XHTML.  We want to pretty print XHTML but not non-nsIHTMLDocuments.
139
0
    nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(aDoc);
140
0
    if (!htmlDoc) {
141
0
      selForcedTextPlain = true;
142
0
    }
143
0
  }
144
0
145
0
  // The mime type is ultimately text/html if the encoder successfully encoded
146
0
  // the selection as text/html.
147
0
  bool encodedTextHTML = mimeType.EqualsLiteral(kHTMLMime);
148
0
149
0
  // First, prepare the text/plain clipboard flavor.
150
0
  nsAutoString textPlainBuf;
151
0
  if (selForcedTextPlain) {
152
0
    // Nothing to do.  buf contains the final, preformatted, raw text/plain.
153
0
    textPlainBuf.Assign(buf);
154
0
  } else {
155
0
    // Redo the encoding, but this time use pretty printing.
156
0
    flags =
157
0
      nsIDocumentEncoder::OutputSelectionOnly |
158
0
      nsIDocumentEncoder::OutputAbsoluteLinks |
159
0
      nsIDocumentEncoder::SkipInvisibleContent |
160
0
      nsIDocumentEncoder::OutputDropInvisibleBreak |
161
0
      (aFlags & (nsIDocumentEncoder::OutputNoScriptContent |
162
0
                 nsIDocumentEncoder::OutputRubyAnnotation));
163
0
164
0
    mimeType.AssignLiteral(kTextMime);
165
0
    rv = docEncoder->Init(aDoc, mimeType, flags);
166
0
    NS_ENSURE_SUCCESS(rv, rv);
167
0
168
0
    rv = docEncoder->SetSelection(aSel);
169
0
    NS_ENSURE_SUCCESS(rv, rv);
170
0
171
0
    rv = docEncoder->EncodeToString(textPlainBuf);
172
0
    NS_ENSURE_SUCCESS(rv, rv);
173
0
  }
174
0
175
0
  // Second, prepare the text/html flavor.
176
0
  nsAutoString textHTMLBuf;
177
0
  nsAutoString htmlParentsBuf;
178
0
  nsAutoString htmlInfoBuf;
179
0
  if (encodedTextHTML) {
180
0
    // Redo the encoding, but this time use the passed-in flags.
181
0
    // Don't allow wrapping of CJK strings.
182
0
    mimeType.AssignLiteral(kHTMLMime);
183
0
    rv = docEncoder->Init(aDoc, mimeType,
184
0
                          aFlags |
185
0
                          nsIDocumentEncoder::OutputDisallowLineBreaking);
186
0
    NS_ENSURE_SUCCESS(rv, rv);
187
0
188
0
    rv = docEncoder->SetSelection(aSel);
189
0
    NS_ENSURE_SUCCESS(rv, rv);
190
0
191
0
    rv = docEncoder->EncodeToStringWithContext(htmlParentsBuf, htmlInfoBuf,
192
0
                                               textHTMLBuf);
193
0
    NS_ENSURE_SUCCESS(rv, rv);
194
0
  }
195
0
196
0
  // Get the Clipboard
197
0
  nsCOMPtr<nsIClipboard> clipboard;
198
0
  if (doPutOnClipboard) {
199
0
    clipboard = do_GetService(kCClipboardCID, &rv);
200
0
    if (NS_FAILED(rv))
201
0
      return rv;
202
0
  }
203
0
204
0
  if ((doPutOnClipboard && clipboard) || aTransferable != nullptr) {
205
0
    // Create a transferable for putting data on the Clipboard
206
0
    nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID);
207
0
    if (trans) {
208
0
      trans->Init(aDoc->GetLoadContext());
209
0
      if (encodedTextHTML) {
210
0
        // Set up a format converter so that clipboard flavor queries work.
211
0
        // This converter isn't really used for conversions.
212
0
        nsCOMPtr<nsIFormatConverter> htmlConverter =
213
0
          do_CreateInstance(kHTMLConverterCID);
214
0
        trans->SetConverter(htmlConverter);
215
0
216
0
        if (!textHTMLBuf.IsEmpty()) {
217
0
          // Add the html DataFlavor to the transferable
218
0
          rv = AppendString(trans, textHTMLBuf, kHTMLMime);
219
0
          NS_ENSURE_SUCCESS(rv, rv);
220
0
        }
221
0
222
0
        // Add the htmlcontext DataFlavor to the transferable
223
0
        // Even if parents is empty string, this flavor should
224
0
        // be attached to the transferable
225
0
        rv = AppendString(trans, htmlParentsBuf, kHTMLContext);
226
0
        NS_ENSURE_SUCCESS(rv, rv);
227
0
228
0
        if (!htmlInfoBuf.IsEmpty()) {
229
0
          // Add the htmlinfo DataFlavor to the transferable
230
0
          rv = AppendString(trans, htmlInfoBuf, kHTMLInfo);
231
0
          NS_ENSURE_SUCCESS(rv, rv);
232
0
        }
233
0
234
0
        if (!textPlainBuf.IsEmpty()) {
235
0
          // unicode text
236
0
          // Add the unicode DataFlavor to the transferable
237
0
          // If we didn't have this, then nsDataObj::GetData matches text/unicode against
238
0
          // the kURLMime flavour which is not desirable (eg. when pasting into Notepad)
239
0
          rv = AppendString(trans, textPlainBuf, kUnicodeMime);
240
0
          NS_ENSURE_SUCCESS(rv, rv);
241
0
        }
242
0
243
0
        // Try and get source URI of the items that are being dragged
244
0
        nsIURI *uri = aDoc->GetDocumentURI();
245
0
        if (uri) {
246
0
          nsAutoCString spec;
247
0
          nsresult rv = uri->GetSpec(spec);
248
0
          NS_ENSURE_SUCCESS(rv, rv);
249
0
          if (!spec.IsEmpty()) {
250
0
            nsAutoString shortcut;
251
0
            AppendUTF8toUTF16(spec, shortcut);
252
0
253
0
            // Add the URL DataFlavor to the transferable. Don't use kURLMime, as it will
254
0
            // cause an unnecessary UniformResourceLocator to be added which confuses
255
0
            // some apps eg. Outlook 2000 - (See Bug 315370). Don't use
256
0
            // kURLDataMime, as it will cause a bogus 'url ' flavor to
257
0
            // show up on the Mac clipboard, confusing other apps, like
258
0
            // Terminal (see bug 336012).
259
0
            rv = AppendString(trans, shortcut, kURLPrivateMime);
260
0
            NS_ENSURE_SUCCESS(rv, rv);
261
0
          }
262
0
        }
263
0
      } else {
264
0
        if (!textPlainBuf.IsEmpty()) {
265
0
          // Add the unicode DataFlavor to the transferable
266
0
          rv = AppendString(trans, textPlainBuf, kUnicodeMime);
267
0
          NS_ENSURE_SUCCESS(rv, rv);
268
0
        }
269
0
      }
270
0
271
0
      if (doPutOnClipboard && clipboard) {
272
0
        // put the transferable on the clipboard
273
0
        clipboard->SetData(trans, nullptr, aClipboardID);
274
0
      }
275
0
276
0
      // Return the transferable to the caller if requested.
277
0
      if (aTransferable != nullptr) {
278
0
        trans.swap(*aTransferable);
279
0
      }
280
0
    }
281
0
  }
282
0
  return rv;
283
0
}
284
285
nsresult
286
nsCopySupport::HTMLCopy(Selection* aSel, nsIDocument* aDoc,
287
                        int16_t aClipboardID, bool aWithRubyAnnotation)
288
0
{
289
0
  uint32_t flags = nsIDocumentEncoder::SkipInvisibleContent;
290
0
  if (aWithRubyAnnotation) {
291
0
    flags |= nsIDocumentEncoder::OutputRubyAnnotation;
292
0
  }
293
0
  return SelectionCopyHelper(aSel, aDoc, true, aClipboardID, flags, nullptr);
294
0
}
295
296
nsresult
297
nsCopySupport::ClearSelectionCache()
298
0
{
299
0
  nsresult rv;
300
0
  nsCOMPtr<nsIClipboard> clipboard = do_GetService(kCClipboardCID, &rv);
301
0
  clipboard->EmptyClipboard(nsIClipboard::kSelectionCache);
302
0
  return rv;
303
0
}
304
305
nsresult
306
nsCopySupport::GetTransferableForSelection(Selection* aSel,
307
                                           nsIDocument* aDoc,
308
                                           nsITransferable** aTransferable)
309
0
{
310
0
  return SelectionCopyHelper(aSel, aDoc, false, 0,
311
0
                             nsIDocumentEncoder::SkipInvisibleContent,
312
0
                             aTransferable);
313
0
}
314
315
nsresult
316
nsCopySupport::GetTransferableForNode(nsINode* aNode,
317
                                      nsIDocument* aDoc,
318
                                      nsITransferable** aTransferable)
319
0
{
320
0
  // Make a temporary selection with aNode in a single range.
321
0
  // XXX We should try to get rid of the Selection object here.
322
0
  // XXX bug 1245883
323
0
  RefPtr<Selection> selection = new Selection();
324
0
  RefPtr<nsRange> range = new nsRange(aNode);
325
0
  ErrorResult result;
326
0
  range->SelectNode(*aNode, result);
327
0
  if (NS_WARN_IF(result.Failed())) {
328
0
    return result.StealNSResult();
329
0
  }
330
0
  selection->AddRangeInternal(*range, aDoc, result);
331
0
  if (NS_WARN_IF(result.Failed())) {
332
0
    return result.StealNSResult();
333
0
  }
334
0
  // It's not the primary selection - so don't skip invisible content.
335
0
  uint32_t flags = 0;
336
0
  return SelectionCopyHelper(selection, aDoc, false, 0, flags,
337
0
                             aTransferable);
338
0
}
339
340
nsresult
341
nsCopySupport::GetContents(const nsACString& aMimeType, uint32_t aFlags, Selection *aSel, nsIDocument *aDoc, nsAString& outdata)
342
0
{
343
0
  nsresult rv = NS_OK;
344
0
345
0
  nsCOMPtr<nsIDocumentEncoder> docEncoder;
346
0
347
0
  nsAutoCString encoderContractID(NS_DOC_ENCODER_CONTRACTID_BASE);
348
0
  encoderContractID.Append(aMimeType);
349
0
350
0
  docEncoder = do_CreateInstance(encoderContractID.get());
351
0
  NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
352
0
353
0
  uint32_t flags = aFlags | nsIDocumentEncoder::SkipInvisibleContent;
354
0
355
0
  if (aMimeType.EqualsLiteral("text/plain"))
356
0
    flags |= nsIDocumentEncoder::OutputPreformatted;
357
0
358
0
  NS_ConvertASCIItoUTF16 unicodeMimeType(aMimeType);
359
0
360
0
  rv = docEncoder->Init(aDoc, unicodeMimeType, flags);
361
0
  if (NS_FAILED(rv)) return rv;
362
0
363
0
  if (aSel)
364
0
  {
365
0
    rv = docEncoder->SetSelection(aSel);
366
0
    if (NS_FAILED(rv)) return rv;
367
0
  }
368
0
369
0
  // encode the selection
370
0
  return docEncoder->EncodeToString(outdata);
371
0
}
372
373
374
nsresult
375
nsCopySupport::ImageCopy(nsIImageLoadingContent* aImageElement,
376
                         nsILoadContext* aLoadContext,
377
                         int32_t aCopyFlags)
378
0
{
379
0
  nsresult rv;
380
0
381
0
  // create a transferable for putting data on the Clipboard
382
0
  nsCOMPtr<nsITransferable> trans(do_CreateInstance(kCTransferableCID, &rv));
383
0
  NS_ENSURE_SUCCESS(rv, rv);
384
0
  trans->Init(aLoadContext);
385
0
386
0
  if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_TEXT) {
387
0
    // get the location from the element
388
0
    nsCOMPtr<nsIURI> uri;
389
0
    rv = aImageElement->GetCurrentURI(getter_AddRefs(uri));
390
0
    NS_ENSURE_SUCCESS(rv, rv);
391
0
    NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
392
0
393
0
    nsAutoCString location;
394
0
    rv = uri->GetSpec(location);
395
0
    NS_ENSURE_SUCCESS(rv, rv);
396
0
397
0
    // append the string to the transferable
398
0
    rv = AppendString(trans, NS_ConvertUTF8toUTF16(location), kUnicodeMime);
399
0
    NS_ENSURE_SUCCESS(rv, rv);
400
0
  }
401
0
402
0
  if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_HTML) {
403
0
    // append HTML data to the transferable
404
0
    nsCOMPtr<nsINode> node(do_QueryInterface(aImageElement, &rv));
405
0
    NS_ENSURE_SUCCESS(rv, rv);
406
0
407
0
    rv = AppendDOMNode(trans, node);
408
0
    NS_ENSURE_SUCCESS(rv, rv);
409
0
  }
410
0
411
0
  if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_DATA) {
412
0
    // get the image data and its request from the element
413
0
    nsCOMPtr<imgIRequest> imgRequest;
414
0
    nsCOMPtr<imgIContainer> image =
415
0
      nsContentUtils::GetImageFromContent(aImageElement,
416
0
                                          getter_AddRefs(imgRequest));
417
0
    NS_ENSURE_TRUE(image, NS_ERROR_FAILURE);
418
0
419
#ifdef XP_WIN
420
    rv = AppendImagePromise(trans, imgRequest, aImageElement);
421
    NS_ENSURE_SUCCESS(rv, rv);
422
#endif
423
424
0
    nsCOMPtr<nsISupportsInterfacePointer>
425
0
      imgPtr(do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv));
426
0
    NS_ENSURE_SUCCESS(rv, rv);
427
0
428
0
    rv = imgPtr->SetData(image);
429
0
    NS_ENSURE_SUCCESS(rv, rv);
430
0
431
0
    // copy the image data onto the transferable
432
0
    rv = trans->SetTransferData(kNativeImageMime, imgPtr,
433
0
                                sizeof(nsISupports*));
434
0
    NS_ENSURE_SUCCESS(rv, rv);
435
0
  }
436
0
437
0
  // get clipboard
438
0
  nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &rv));
439
0
  NS_ENSURE_SUCCESS(rv, rv);
440
0
441
0
  // check whether the system supports the selection clipboard or not.
442
0
  bool selectionSupported;
443
0
  rv = clipboard->SupportsSelectionClipboard(&selectionSupported);
444
0
  NS_ENSURE_SUCCESS(rv, rv);
445
0
446
0
  // put the transferable on the clipboard
447
0
  if (selectionSupported) {
448
0
    rv = clipboard->SetData(trans, nullptr, nsIClipboard::kSelectionClipboard);
449
0
    NS_ENSURE_SUCCESS(rv, rv);
450
0
  }
451
0
452
0
  return clipboard->SetData(trans, nullptr, nsIClipboard::kGlobalClipboard);
453
0
}
454
455
static nsresult AppendString(nsITransferable *aTransferable,
456
                             const nsAString& aString,
457
                             const char* aFlavor)
458
0
{
459
0
  nsresult rv;
460
0
461
0
  nsCOMPtr<nsISupportsString>
462
0
    data(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
463
0
  NS_ENSURE_SUCCESS(rv, rv);
464
0
465
0
  rv = data->SetData(aString);
466
0
  NS_ENSURE_SUCCESS(rv, rv);
467
0
468
0
  rv = aTransferable->AddDataFlavor(aFlavor);
469
0
  NS_ENSURE_SUCCESS(rv, rv);
470
0
471
0
  return aTransferable->SetTransferData(aFlavor, data,
472
0
                                        aString.Length() * sizeof(char16_t));
473
0
}
474
475
static nsresult AppendDOMNode(nsITransferable *aTransferable,
476
                              nsINode *aDOMNode)
477
0
{
478
0
  nsresult rv;
479
0
480
0
  // selializer
481
0
  nsCOMPtr<nsIDocumentEncoder>
482
0
    docEncoder(do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID, &rv));
483
0
  NS_ENSURE_SUCCESS(rv, rv);
484
0
485
0
  // get document for the encoder
486
0
  nsCOMPtr<nsIDocument> document = aDOMNode->OwnerDoc();
487
0
488
0
  // Note that XHTML is not counted as HTML here, because we can't copy it
489
0
  // properly (all the copy code for non-plaintext assumes using HTML
490
0
  // serializers and parsers is OK, and those mess up XHTML).
491
0
  DebugOnly<nsCOMPtr<nsIHTMLDocument>> htmlDoc =
492
0
    nsCOMPtr<nsIHTMLDocument>(do_QueryInterface(document, &rv));
493
0
  NS_ENSURE_SUCCESS(rv, NS_OK);
494
0
495
0
  NS_ENSURE_TRUE(document->IsHTMLDocument(), NS_OK);
496
0
497
0
  // init encoder with document and node
498
0
  rv = docEncoder->NativeInit(document, NS_LITERAL_STRING(kHTMLMime),
499
0
                              nsIDocumentEncoder::OutputAbsoluteLinks |
500
0
                              nsIDocumentEncoder::OutputEncodeBasicEntities);
501
0
  NS_ENSURE_SUCCESS(rv, rv);
502
0
503
0
  rv = docEncoder->SetNode(aDOMNode);
504
0
  NS_ENSURE_SUCCESS(rv, rv);
505
0
506
0
  // serialize to string
507
0
  nsAutoString html, context, info;
508
0
  rv = docEncoder->EncodeToStringWithContext(context, info, html);
509
0
  NS_ENSURE_SUCCESS(rv, rv);
510
0
511
0
  // copy them to the transferable
512
0
  if (!html.IsEmpty()) {
513
0
    rv = AppendString(aTransferable, html, kHTMLMime);
514
0
    NS_ENSURE_SUCCESS(rv, rv);
515
0
  }
516
0
517
0
  if (!info.IsEmpty()) {
518
0
    rv = AppendString(aTransferable, info, kHTMLInfo);
519
0
    NS_ENSURE_SUCCESS(rv, rv);
520
0
  }
521
0
522
0
  // add a special flavor, even if we don't have html context data
523
0
  return AppendString(aTransferable, context, kHTMLContext);
524
0
}
525
526
#ifdef XP_WIN
527
static nsresult AppendImagePromise(nsITransferable* aTransferable,
528
                                   imgIRequest* aImgRequest,
529
                                   nsIImageLoadingContent* aImageElement)
530
{
531
  nsresult rv;
532
533
  NS_ENSURE_TRUE(aImgRequest, NS_OK);
534
535
  uint32_t imageStatus;
536
  rv = aImgRequest->GetImageStatus(&imageStatus);
537
  NS_ENSURE_SUCCESS(rv, rv);
538
  if (!(imageStatus & imgIRequest::STATUS_FRAME_COMPLETE) ||
539
      (imageStatus & imgIRequest::STATUS_ERROR)) {
540
    return NS_OK;
541
  }
542
543
  bool isMultipart;
544
  rv = aImgRequest->GetMultipart(&isMultipart);
545
  NS_ENSURE_SUCCESS(rv, rv);
546
  if (isMultipart) {
547
    return NS_OK;
548
  }
549
550
  nsCOMPtr<nsINode> node = do_QueryInterface(aImageElement, &rv);
551
  NS_ENSURE_SUCCESS(rv, rv);
552
553
  // Fix the file extension in the URL if necessary
554
  nsCOMPtr<nsIMIMEService> mimeService =
555
    do_GetService(NS_MIMESERVICE_CONTRACTID);
556
  NS_ENSURE_TRUE(mimeService, NS_OK);
557
558
  nsCOMPtr<nsIURI> imgUri;
559
  rv = aImgRequest->GetFinalURI(getter_AddRefs(imgUri));
560
  NS_ENSURE_SUCCESS(rv, rv);
561
562
  nsCOMPtr<nsIURL> imgUrl = do_QueryInterface(imgUri);
563
  NS_ENSURE_TRUE(imgUrl, NS_OK);
564
565
  nsAutoCString extension;
566
  rv = imgUrl->GetFileExtension(extension);
567
  NS_ENSURE_SUCCESS(rv, rv);
568
569
  nsCString mimeType;
570
  rv = aImgRequest->GetMimeType(getter_Copies(mimeType));
571
  NS_ENSURE_SUCCESS(rv, rv);
572
573
  nsCOMPtr<nsIMIMEInfo> mimeInfo;
574
  mimeService->GetFromTypeAndExtension(mimeType, EmptyCString(),
575
                                       getter_AddRefs(mimeInfo));
576
  NS_ENSURE_TRUE(mimeInfo, NS_OK);
577
578
  nsAutoCString spec;
579
  rv = imgUrl->GetSpec(spec);
580
  NS_ENSURE_SUCCESS(rv, rv);
581
582
  // pass out the image source string
583
  nsString imageSourceString;
584
  CopyUTF8toUTF16(spec, imageSourceString);
585
586
  bool validExtension;
587
  if (extension.IsEmpty() ||
588
      NS_FAILED(mimeInfo->ExtensionExists(extension,
589
                                          &validExtension)) ||
590
      !validExtension) {
591
    // Fix the file extension in the URL
592
    nsAutoCString primaryExtension;
593
    mimeInfo->GetPrimaryExtension(primaryExtension);
594
595
    rv = NS_MutateURI(imgUri)
596
           .Apply(NS_MutatorMethod(&nsIURLMutator::SetFileExtension,
597
                                   primaryExtension, nullptr))
598
           .Finalize(imgUrl);
599
    NS_ENSURE_SUCCESS(rv, rv);
600
  }
601
602
  nsAutoCString fileName;
603
  imgUrl->GetFileName(fileName);
604
605
  NS_UnescapeURL(fileName);
606
607
  // make the filename safe for the filesystem
608
  fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
609
610
  nsString imageDestFileName;
611
  CopyUTF8toUTF16(fileName, imageDestFileName);
612
613
  rv = AppendString(aTransferable, imageSourceString, kFilePromiseURLMime);
614
  NS_ENSURE_SUCCESS(rv, rv);
615
616
  rv = AppendString(aTransferable, imageDestFileName, kFilePromiseDestFilename);
617
  NS_ENSURE_SUCCESS(rv, rv);
618
619
  aTransferable->SetRequestingPrincipal(node->NodePrincipal());
620
  aTransferable->SetContentPolicyType(nsIContentPolicy::TYPE_INTERNAL_IMAGE);
621
622
  // add the dataless file promise flavor
623
  return aTransferable->AddDataFlavor(kFilePromiseMime);
624
}
625
#endif // XP_WIN
626
627
nsIContent*
628
nsCopySupport::GetSelectionForCopy(nsIDocument* aDocument, Selection** aSelection)
629
0
{
630
0
  *aSelection = nullptr;
631
0
632
0
  nsIPresShell* presShell = aDocument->GetShell();
633
0
  if (!presShell)
634
0
    return nullptr;
635
0
636
0
  nsCOMPtr<nsIContent> focusedContent;
637
0
  nsCOMPtr<nsISelectionController> selectionController =
638
0
    presShell->GetSelectionControllerForFocusedContent(
639
0
      getter_AddRefs(focusedContent));
640
0
  if (!selectionController) {
641
0
    return nullptr;
642
0
  }
643
0
644
0
  RefPtr<Selection> sel =
645
0
    selectionController->GetSelection(nsISelectionController::SELECTION_NORMAL);
646
0
  sel.forget(aSelection);
647
0
  return focusedContent;
648
0
}
649
650
bool
651
nsCopySupport::CanCopy(nsIDocument* aDocument)
652
0
{
653
0
  if (!aDocument)
654
0
    return false;
655
0
656
0
  RefPtr<Selection> sel;
657
0
  GetSelectionForCopy(aDocument, getter_AddRefs(sel));
658
0
  NS_ENSURE_TRUE(sel, false);
659
0
660
0
  return !sel->IsCollapsed();
661
0
}
662
663
static bool
664
IsInsideRuby(nsINode* aNode)
665
0
{
666
0
  for (; aNode; aNode = aNode->GetParent()) {
667
0
    if (aNode->IsHTMLElement(nsGkAtoms::ruby)) {
668
0
      return true;
669
0
    }
670
0
  }
671
0
  return false;
672
0
}
673
674
static bool
675
IsSelectionInsideRuby(Selection* aSelection)
676
0
{
677
0
  uint32_t rangeCount = aSelection->RangeCount();;
678
0
  for (auto i : IntegerRange(rangeCount)) {
679
0
    nsRange* range = aSelection->GetRangeAt(i);
680
0
    if (!IsInsideRuby(range->GetCommonAncestor())) {
681
0
      return false;
682
0
    }
683
0
  }
684
0
  return true;
685
0
}
686
687
static Element*
688
GetElementOrNearestFlattenedTreeParentElement(nsINode* aNode)
689
0
{
690
0
  if (!aNode->IsContent()) {
691
0
    return nullptr;
692
0
  }
693
0
  for (nsIContent* content = aNode->AsContent();
694
0
      content;
695
0
      content = content->GetFlattenedTreeParent()) {
696
0
    if (content->IsElement()) {
697
0
      return content->AsElement();
698
0
    }
699
0
  }
700
0
  return nullptr;
701
0
}
702
703
bool
704
nsCopySupport::FireClipboardEvent(EventMessage aEventMessage,
705
                                  int32_t aClipboardType,
706
                                  nsIPresShell* aPresShell,
707
                                  Selection* aSelection,
708
                                  bool* aActionTaken)
709
0
{
710
0
  if (aActionTaken) {
711
0
    *aActionTaken = false;
712
0
  }
713
0
714
0
  EventMessage originalEventMessage = aEventMessage;
715
0
  if (originalEventMessage == ePasteNoFormatting) {
716
0
    originalEventMessage = ePaste;
717
0
  }
718
0
719
0
  NS_ASSERTION(originalEventMessage == eCut || originalEventMessage == eCopy ||
720
0
               originalEventMessage == ePaste,
721
0
               "Invalid clipboard event type");
722
0
723
0
  nsCOMPtr<nsIPresShell> presShell = aPresShell;
724
0
  if (!presShell)
725
0
    return false;
726
0
727
0
  nsCOMPtr<nsIDocument> doc = presShell->GetDocument();
728
0
  if (!doc)
729
0
    return false;
730
0
731
0
  nsCOMPtr<nsPIDOMWindowOuter> piWindow = doc->GetWindow();
732
0
  if (!piWindow)
733
0
    return false;
734
0
735
0
  // Event target of clipboard events should be an element node which
736
0
  // contains selection start container.
737
0
  RefPtr<Element> targetElement;
738
0
739
0
  // If a selection was not supplied, try to find it.
740
0
  RefPtr<Selection> sel = aSelection;
741
0
  if (!sel) {
742
0
    GetSelectionForCopy(doc, getter_AddRefs(sel));
743
0
  }
744
0
745
0
  // Retrieve the event target node from the start of the selection.
746
0
  if (sel) {
747
0
    nsRange* range = sel->GetRangeAt(0);
748
0
    if (range) {
749
0
      targetElement =
750
0
        GetElementOrNearestFlattenedTreeParentElement(
751
0
          range->GetStartContainer());
752
0
    }
753
0
  }
754
0
755
0
  // If there is no selection ranges, use the <body> or <frameset> element.
756
0
  if (!targetElement) {
757
0
    targetElement = doc->GetBody();
758
0
    if (!targetElement) {
759
0
      return false;
760
0
    }
761
0
  }
762
0
763
0
  // It seems to be unsafe to fire an event handler during reflow (bug 393696)
764
0
  if (!nsContentUtils::IsSafeToRunScript()) {
765
0
    nsContentUtils::WarnScriptWasIgnored(doc);
766
0
    return false;
767
0
  }
768
0
769
0
  nsCOMPtr<nsIDocShell> docShell = piWindow->GetDocShell();
770
0
  const bool chromeShell =
771
0
    docShell && docShell->ItemType() == nsIDocShellTreeItem::typeChrome;
772
0
773
0
  // next, fire the cut, copy or paste event
774
0
  bool doDefault = true;
775
0
  RefPtr<DataTransfer> clipboardData;
776
0
  if (chromeShell || Preferences::GetBool("dom.event.clipboardevents.enabled", true)) {
777
0
    clipboardData =
778
0
      new DataTransfer(doc->GetScopeObject(), aEventMessage,
779
0
                       originalEventMessage == ePaste, aClipboardType);
780
0
781
0
    nsEventStatus status = nsEventStatus_eIgnore;
782
0
    InternalClipboardEvent evt(true, originalEventMessage);
783
0
    evt.mClipboardData = clipboardData;
784
0
    EventDispatcher::Dispatch(targetElement, presShell->GetPresContext(), &evt,
785
0
                              nullptr, &status);
786
0
    // If the event was cancelled, don't do the clipboard operation
787
0
    doDefault = (status != nsEventStatus_eConsumeNoDefault);
788
0
  }
789
0
790
0
  // When this function exits, the event dispatch is over. We want to disconnect
791
0
  // our DataTransfer, which means setting its mode to `Protected` and clearing
792
0
  // all stored data, before we return.
793
0
  auto clearAfter = MakeScopeExit([&] {
794
0
    if (clipboardData) {
795
0
      clipboardData->Disconnect();
796
0
797
0
      // NOTE: Disconnect may not actually clear the DataTransfer if the
798
0
      // dom.events.dataTransfer.protected.enabled pref is not on, so we make
799
0
      // sure we clear here, as not clearing could provide the DataTransfer
800
0
      // access to information from the system clipboard at an arbitrary point
801
0
      // in the future.
802
0
      if (originalEventMessage == ePaste) {
803
0
        clipboardData->ClearAll();
804
0
      }
805
0
    }
806
0
  });
807
0
808
0
  // No need to do anything special during a paste. Either an event listener
809
0
  // took care of it and cancelled the event, or the caller will handle it.
810
0
  // Return true to indicate that the event wasn't cancelled.
811
0
  if (originalEventMessage == ePaste) {
812
0
    if (aActionTaken) {
813
0
      *aActionTaken = true;
814
0
    }
815
0
    return doDefault;
816
0
  }
817
0
818
0
  // Update the presentation in case the event handler modified the selection,
819
0
  // see bug 602231.
820
0
  presShell->FlushPendingNotifications(FlushType::Frames);
821
0
  if (presShell->IsDestroying())
822
0
    return false;
823
0
824
0
  // if the event was not cancelled, do the default copy. If the event was cancelled,
825
0
  // use the data added to the data transfer and copy that instead.
826
0
  uint32_t count = 0;
827
0
  if (doDefault) {
828
0
    // find the focused node
829
0
    nsIContent* sourceContent = targetElement.get();
830
0
    if (targetElement->IsInNativeAnonymousSubtree()) {
831
0
      sourceContent = targetElement->FindFirstNonChromeOnlyAccessContent();
832
0
    }
833
0
834
0
    // check if we are looking at a password input
835
0
    nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(sourceContent);
836
0
    if (formControl) {
837
0
      if (formControl->ControlType() == NS_FORM_INPUT_PASSWORD) {
838
0
        return false;
839
0
      }
840
0
    }
841
0
842
0
    // when cutting non-editable content, do nothing
843
0
    // XXX this is probably the wrong editable flag to check
844
0
    if (originalEventMessage != eCut || targetElement->IsEditable()) {
845
0
      // get the data from the selection if any
846
0
      if (sel->IsCollapsed()) {
847
0
        if (aActionTaken) {
848
0
          *aActionTaken = true;
849
0
        }
850
0
        return false;
851
0
      }
852
0
      // XXX Code which decides whether we should copy text with ruby
853
0
      // annotation is currenct depending on whether each range of the
854
0
      // selection is inside a same ruby container. But we really should
855
0
      // expose the full functionality in browser. See bug 1130891.
856
0
      bool withRubyAnnotation = IsSelectionInsideRuby(sel);
857
0
      // call the copy code
858
0
      nsresult rv = HTMLCopy(sel, doc, aClipboardType, withRubyAnnotation);
859
0
      if (NS_FAILED(rv)) {
860
0
        return false;
861
0
      }
862
0
    } else {
863
0
      return false;
864
0
    }
865
0
  } else if (clipboardData) {
866
0
    // check to see if any data was put on the data transfer.
867
0
    count = clipboardData->MozItemCount();
868
0
    if (count) {
869
0
      nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1"));
870
0
      NS_ENSURE_TRUE(clipboard, false);
871
0
872
0
      nsCOMPtr<nsITransferable> transferable =
873
0
        clipboardData->GetTransferable(0, doc->GetLoadContext());
874
0
875
0
      NS_ENSURE_TRUE(transferable, false);
876
0
877
0
      // put the transferable on the clipboard
878
0
      nsresult rv = clipboard->SetData(transferable, nullptr, aClipboardType);
879
0
      if (NS_FAILED(rv)) {
880
0
        return false;
881
0
      }
882
0
    }
883
0
  }
884
0
885
0
  // Now that we have copied, update the clipboard commands. This should have
886
0
  // the effect of updating the enabled state of the paste menu item.
887
0
  if (doDefault || count) {
888
0
    piWindow->UpdateCommands(NS_LITERAL_STRING("clipboard"), nullptr, 0);
889
0
  }
890
0
891
0
  if (aActionTaken) {
892
0
    *aActionTaken = true;
893
0
  }
894
0
  return doDefault;
895
0
}