Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/events/DataTransferItem.h
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
#ifndef mozilla_dom_DataTransferItem_h
8
#define mozilla_dom_DataTransferItem_h
9
10
#include "mozilla/ErrorResult.h"
11
#include "mozilla/dom/DataTransfer.h"
12
#include "mozilla/dom/DOMString.h"
13
#include "mozilla/dom/File.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
class FileSystemEntry;
19
class FunctionStringCallback;
20
21
class DataTransferItem final : public nsISupports
22
                             , public nsWrapperCache
23
{
24
public:
25
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
26
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DataTransferItem);
27
28
public:
29
  // The spec only talks about the "file" and "string" kinds. Due to the Moz*
30
  // APIs, it is possible to attach any type to a DataTransferItem, meaning that
31
  // we can have other kinds then just FILE and STRING. These others are simply
32
  // marked as "other" and can only be produced throug the Moz* APIs.
33
  enum eKind {
34
    KIND_FILE,
35
    KIND_STRING,
36
    KIND_OTHER,
37
  };
38
39
  DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType,
40
                   eKind aKind = KIND_OTHER)
41
    : mIndex(0)
42
    , mChromeOnly(false)
43
    , mKind(aKind)
44
    , mType(aType)
45
    , mDataTransfer(aDataTransfer)
46
0
  {
47
0
    MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer");
48
0
  }
49
50
  already_AddRefed<DataTransferItem> Clone(DataTransfer* aDataTransfer) const;
51
52
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
53
54
  void GetAsString(FunctionStringCallback* aCallback,
55
                   nsIPrincipal& aSubjectPrincipal,
56
                   ErrorResult& aRv);
57
58
  void GetKind(nsAString& aKind) const
59
  {
60
    switch (mKind) {
61
    case KIND_FILE:
62
      aKind = NS_LITERAL_STRING("file");
63
      return;
64
    case KIND_STRING:
65
      aKind = NS_LITERAL_STRING("string");
66
      return;
67
    default:
68
      aKind = NS_LITERAL_STRING("other");
69
      return;
70
    }
71
  }
72
73
  void GetInternalType(nsAString& aType) const
74
0
  {
75
0
    aType = mType;
76
0
  }
77
78
  void GetType(nsAString& aType);
79
80
  eKind Kind() const
81
0
  {
82
0
    return mKind;
83
0
  }
84
85
  already_AddRefed<File>
86
  GetAsFile(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
87
88
  already_AddRefed<FileSystemEntry>
89
  GetAsEntry(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
90
91
  DataTransfer* GetParentObject() const
92
  {
93
    return mDataTransfer;
94
  }
95
96
  nsIPrincipal* Principal() const
97
0
  {
98
0
    return mPrincipal;
99
0
  }
100
  void SetPrincipal(nsIPrincipal* aPrincipal)
101
0
  {
102
0
    mPrincipal = aPrincipal;
103
0
  }
104
105
  already_AddRefed<nsIVariant> DataNoSecurityCheck();
106
  // Data may return null if the clipboard state has changed since the type was detected.
107
  already_AddRefed<nsIVariant> Data(nsIPrincipal* aPrincipal, ErrorResult& aRv);
108
109
  // Note: This can modify the mKind.  Callers of this method must let the
110
  // relevant DataTransfer know, because its types list can change as a result.
111
  void SetData(nsIVariant* aData);
112
113
  uint32_t Index() const
114
0
  {
115
0
    return mIndex;
116
0
  }
117
  void SetIndex(uint32_t aIndex)
118
0
  {
119
0
    mIndex = aIndex;
120
0
  }
121
  void FillInExternalData();
122
123
  bool ChromeOnly() const
124
0
  {
125
0
    return mChromeOnly;
126
0
  }
127
  void SetChromeOnly(bool aChromeOnly)
128
0
  {
129
0
    mChromeOnly = aChromeOnly;
130
0
  }
131
132
  static eKind KindFromData(nsIVariant* aData);
133
134
private:
135
0
  ~DataTransferItem() {}
136
  already_AddRefed<File> CreateFileFromInputStream(nsIInputStream* aStream);
137
138
  // The index in the 2d mIndexedItems array
139
  uint32_t mIndex;
140
141
  bool mChromeOnly;
142
  eKind mKind;
143
  const nsString mType;
144
  nsCOMPtr<nsIVariant> mData;
145
  nsCOMPtr<nsIPrincipal> mPrincipal;
146
  RefPtr<DataTransfer> mDataTransfer;
147
148
  // File cache for nsIFile application/x-moz-file entries.
149
  RefPtr<File> mCachedFile;
150
};
151
152
} // namespace dom
153
} // namespace mozilla
154
155
#endif /* mozilla_dom_DataTransferItem_h */