Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/widget/headless/HeadlessClipboard.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "HeadlessClipboard.h"
6
7
#include "nsISupportsPrimitives.h"
8
#include "nsComponentManagerUtils.h"
9
#include "nsCOMPtr.h"
10
11
namespace mozilla {
12
namespace widget {
13
14
NS_IMPL_ISUPPORTS(HeadlessClipboard, nsIClipboard)
15
16
HeadlessClipboard::HeadlessClipboard()
17
  : mClipboard(MakeUnique<HeadlessClipboardData>())
18
0
{
19
0
}
20
21
NS_IMETHODIMP
22
HeadlessClipboard::SetData(nsITransferable *aTransferable,
23
                     nsIClipboardOwner *anOwner,
24
                     int32_t aWhichClipboard)
25
0
{
26
0
  if (aWhichClipboard != kGlobalClipboard) {
27
0
    return NS_ERROR_NOT_IMPLEMENTED;
28
0
  }
29
0
30
0
  // Clear out the clipboard in order to set the new data.
31
0
  EmptyClipboard(aWhichClipboard);
32
0
33
0
  // Only support plain text for now.
34
0
  nsCOMPtr<nsISupports> clip;
35
0
  uint32_t len;
36
0
  nsresult rv = aTransferable->GetTransferData(kUnicodeMime,
37
0
                                               getter_AddRefs(clip),
38
0
                                               &len);
39
0
  if (NS_FAILED(rv)) {
40
0
    return rv;
41
0
  }
42
0
  nsCOMPtr<nsISupportsString> wideString = do_QueryInterface(clip);
43
0
  if (!wideString) {
44
0
    return NS_ERROR_NOT_IMPLEMENTED;
45
0
  }
46
0
  nsAutoString utf16string;
47
0
  wideString->GetData(utf16string);
48
0
  mClipboard->SetText(utf16string);
49
0
50
0
  return NS_OK;
51
0
}
52
53
NS_IMETHODIMP
54
HeadlessClipboard::GetData(nsITransferable *aTransferable,
55
                     int32_t aWhichClipboard)
56
0
{
57
0
  if (aWhichClipboard != kGlobalClipboard) {
58
0
    return NS_ERROR_NOT_IMPLEMENTED;
59
0
  }
60
0
61
0
  nsresult rv;
62
0
  nsCOMPtr<nsISupportsString> dataWrapper =
63
0
    do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
64
0
  rv = dataWrapper->SetData(mClipboard->GetText());
65
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
66
0
    return rv;
67
0
  }
68
0
  nsCOMPtr<nsISupports> genericDataWrapper = do_QueryInterface(dataWrapper);
69
0
  uint32_t len = mClipboard->GetText().Length() * sizeof(char16_t);
70
0
  rv = aTransferable->SetTransferData(kUnicodeMime, genericDataWrapper, len);
71
0
  if (NS_WARN_IF(NS_FAILED(rv))) {
72
0
    return rv;
73
0
  }
74
0
  return NS_OK;
75
0
}
76
77
NS_IMETHODIMP
78
HeadlessClipboard::EmptyClipboard(int32_t aWhichClipboard)
79
0
{
80
0
  if (aWhichClipboard != kGlobalClipboard) {
81
0
    return NS_ERROR_NOT_IMPLEMENTED;
82
0
  }
83
0
  mClipboard->Clear();
84
0
  return NS_OK;
85
0
}
86
87
NS_IMETHODIMP
88
HeadlessClipboard::HasDataMatchingFlavors(const char **aFlavorList,
89
                                    uint32_t aLength, int32_t aWhichClipboard,
90
                                    bool *aHasType)
91
0
{
92
0
  *aHasType = false;
93
0
  if (aWhichClipboard != kGlobalClipboard) {
94
0
    return NS_ERROR_NOT_IMPLEMENTED;
95
0
  }
96
0
  // Retrieve the union of all aHasType in aFlavorList
97
0
  for (uint32_t i = 0; i < aLength; ++i) {
98
0
    const char *flavor = aFlavorList[i];
99
0
    if (!flavor) {
100
0
      continue;
101
0
    }
102
0
    if (!strcmp(flavor, kUnicodeMime) && mClipboard->HasText()) {
103
0
      *aHasType = true;
104
0
    }
105
0
  }
106
0
  return NS_OK;
107
0
}
108
109
NS_IMETHODIMP
110
HeadlessClipboard::SupportsSelectionClipboard(bool *aIsSupported)
111
0
{
112
0
  *aIsSupported = false;
113
0
  return NS_OK;
114
0
}
115
116
NS_IMETHODIMP
117
HeadlessClipboard::SupportsFindClipboard(bool* _retval)
118
0
{
119
0
  NS_ENSURE_ARG_POINTER(_retval);
120
0
121
0
  *_retval = false;
122
0
  return NS_OK;
123
0
}
124
125
} // namespace widget
126
} // namespace mozilla