Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/ClipboardEvent.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 "mozilla/dom/ClipboardEvent.h"
8
#include "mozilla/ContentEvents.h"
9
#include "mozilla/dom/DataTransfer.h"
10
#include "nsIClipboard.h"
11
12
namespace mozilla {
13
namespace dom {
14
15
ClipboardEvent::ClipboardEvent(EventTarget* aOwner,
16
                               nsPresContext* aPresContext,
17
                               InternalClipboardEvent* aEvent)
18
  : Event(aOwner, aPresContext,
19
          aEvent ? aEvent : new InternalClipboardEvent(false, eVoidEvent))
20
0
{
21
0
  if (aEvent) {
22
0
    mEventIsInternal = false;
23
0
  } else {
24
0
    mEventIsInternal = true;
25
0
    mEvent->mTime = PR_Now();
26
0
  }
27
0
}
28
29
void
30
ClipboardEvent::InitClipboardEvent(const nsAString& aType, bool aCanBubble,
31
                                   bool aCancelable,
32
                                   DataTransfer* aClipboardData)
33
0
{
34
0
  NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
35
0
36
0
  Event::InitEvent(aType, aCanBubble, aCancelable);
37
0
  mEvent->AsClipboardEvent()->mClipboardData = aClipboardData;
38
0
}
39
40
already_AddRefed<ClipboardEvent>
41
ClipboardEvent::Constructor(const GlobalObject& aGlobal,
42
                            const nsAString& aType,
43
                            const ClipboardEventInit& aParam,
44
                            ErrorResult& aRv)
45
0
{
46
0
  nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
47
0
  RefPtr<ClipboardEvent> e = new ClipboardEvent(t, nullptr, nullptr);
48
0
  bool trusted = e->Init(t);
49
0
50
0
  RefPtr<DataTransfer> clipboardData;
51
0
  if (e->mEventIsInternal) {
52
0
    InternalClipboardEvent* event = e->mEvent->AsClipboardEvent();
53
0
    if (event) {
54
0
      // Always create a clipboardData for the copy event. If this is changed to
55
0
      // support other types of events, make sure that read/write privileges are
56
0
      // checked properly within DataTransfer.
57
0
      clipboardData = new DataTransfer(ToSupports(e), eCopy, false, -1);
58
0
      clipboardData->SetData(aParam.mDataType, aParam.mData,
59
0
                             *aGlobal.GetSubjectPrincipal(), aRv);
60
0
      NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
61
0
    }
62
0
  }
63
0
64
0
  e->InitClipboardEvent(aType, aParam.mBubbles, aParam.mCancelable,
65
0
                        clipboardData);
66
0
  e->SetTrusted(trusted);
67
0
  e->SetComposed(aParam.mComposed);
68
0
  return e.forget();
69
0
}
70
71
DataTransfer*
72
ClipboardEvent::GetClipboardData()
73
0
{
74
0
  InternalClipboardEvent* event = mEvent->AsClipboardEvent();
75
0
76
0
  if (!event->mClipboardData) {
77
0
    if (mEventIsInternal) {
78
0
      event->mClipboardData =
79
0
        new DataTransfer(ToSupports(this), eCopy, false, -1);
80
0
    } else {
81
0
      event->mClipboardData =
82
0
        new DataTransfer(ToSupports(this), event->mMessage,
83
0
                         event->mMessage == ePaste,
84
0
                         nsIClipboard::kGlobalClipboard);
85
0
    }
86
0
  }
87
0
88
0
  return event->mClipboardData;
89
0
}
90
91
} // namespace dom
92
} // namespace mozilla
93
94
using namespace mozilla;
95
using namespace mozilla::dom;
96
97
already_AddRefed<ClipboardEvent>
98
NS_NewDOMClipboardEvent(EventTarget* aOwner,
99
                        nsPresContext* aPresContext,
100
                        InternalClipboardEvent* aEvent)
101
0
{
102
0
  RefPtr<ClipboardEvent> it =
103
0
    new ClipboardEvent(aOwner, aPresContext, aEvent);
104
0
  return it.forget();
105
0
}