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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsDragService.h"
#include "AndroidGraphics.h"
#include "AndroidWidgetUtils.h"
#include "mozilla/dom/Document.h"
#include "mozilla/java/GeckoDragAndDropWrappers.h"
#include "mozilla/PresShell.h"
#include "mozilla/ScopeExit.h"
#include "nsArrayUtils.h"
#include "nsClipboard.h"
#include "nsComponentManagerUtils.h"
#include "nsIArray.h"
#include "nsITransferable.h"
#include "nsPrimitiveHelpers.h"
#include "nsViewManager.h"
#include "nsWindow.h"
NS_IMPL_ISUPPORTS_INHERITED0(nsDragService, nsBaseDragService)
using namespace mozilla;
using namespace mozilla::widget;
StaticRefPtr<nsDragService> sDragServiceInstance;
/* static */
already_AddRefed<nsDragService> nsDragService::GetInstance() {
if (!sDragServiceInstance) {
sDragServiceInstance = new nsDragService();
ClearOnShutdown(&sDragServiceInstance);
}
RefPtr<nsDragService> service = sDragServiceInstance.get();
return service.forget();
}
static nsWindow* GetWindow(dom::Document* aDocument) {
if (!aDocument) {
return nullptr;
}
PresShell* presShell = aDocument->GetPresShell();
if (!presShell) {
return nullptr;
}
RefPtr<nsViewManager> vm = presShell->GetViewManager();
if (!vm) {
return nullptr;
}
nsCOMPtr<nsIWidget> widget = vm->GetRootWidget();
if (!widget) {
return nullptr;
}
RefPtr<nsWindow> window = nsWindow::From(widget);
return window.get();
}
nsresult nsDragService::InvokeDragSessionImpl(
nsIArray* aTransferableArray, const Maybe<CSSIntRegion>& aRegion,
uint32_t aActionType) {
if (jni::GetAPIVersion() < 24) {
return NS_ERROR_NOT_AVAILABLE;
}
uint32_t count = 0;
aTransferableArray->GetLength(&count);
if (count != 1) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsITransferable> transferable =
do_QueryElementAt(aTransferableArray, 0);
nsAutoString html;
nsAutoString text;
nsresult rv = nsClipboard::GetTextFromTransferable(transferable, text, html);
if (NS_FAILED(rv)) {
return rv;
}
java::GeckoDragAndDrop::SetDragData(text, html);
if (nsWindow* window = GetWindow(mSourceDocument)) {
mTransferable = transferable;
nsBaseDragService::StartDragSession();
nsBaseDragService::OpenDragPopup();
auto bitmap = CreateDragImage(mSourceNode, aRegion);
window->StartDragAndDrop(bitmap);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsDragService::GetData(nsITransferable* aTransferable, uint32_t aItem) {
if (!aTransferable) {
return NS_ERROR_INVALID_ARG;
}
nsTArray<nsCString> flavors;
nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
for (const auto& flavor : flavors) {
nsCOMPtr<nsISupports> data;
rv = mTransferable->GetTransferData(flavor.get(), getter_AddRefs(data));
if (NS_FAILED(rv)) {
continue;
}
rv = aTransferable->SetTransferData(flavor.get(), data);
if (NS_SUCCEEDED(rv)) {
return rv;
}
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsDragService::GetNumDropItems(uint32_t* aNumItems) {
if (mTransferable) {
*aNumItems = 1;
return NS_OK;
}
*aNumItems = 0;
return NS_OK;
}
NS_IMETHODIMP
nsDragService::IsDataFlavorSupported(const char* aDataFlavor, bool* _retval) {
*_retval = false;
nsDependentCString dataFlavor(aDataFlavor);
auto logging = MakeScopeExit([&] {
MOZ_DRAGSERVICE_LOG("IsDataFlavorSupported: %s is%s found", aDataFlavor,
*_retval ? "" : " not");
});
nsTArray<nsCString> flavors;
nsresult rv = mTransferable->FlavorsTransferableCanImport(flavors);
if (NS_FAILED(rv)) {
return NS_OK;
}
for (const auto& flavor : flavors) {
if (dataFlavor.Equals(flavor)) {
*_retval = true;
return NS_OK;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsDragService::EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) {
java::GeckoDragAndDrop::EndDragSession();
nsresult rv = nsBaseDragService::EndDragSession(aDoneDrag, aKeyModifiers);
mTransferable = nullptr;
return rv;
}
NS_IMETHODIMP
nsDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
int32_t aImageY) {
nsBaseDragService::UpdateDragImage(aImage, aImageX, aImageY);
auto bitmap = CreateDragImage(mSourceNode, Nothing());
if (nsWindow* window = GetWindow(mSourceDocument)) {
window->UpdateDragImage(bitmap);
}
return NS_OK;
}
bool nsDragService::MustUpdateDataTransfer(EventMessage aMessage) {
// Android's drag and drop API sets drop item in drop event.
// So we have to invalidate data transfer cache on drop event.
return aMessage == eDrop;
}
java::sdk::Bitmap::LocalRef nsDragService::CreateDragImage(
nsINode* aNode, const Maybe<CSSIntRegion>& aRegion) {
LayoutDeviceIntRect dragRect;
RefPtr<SourceSurface> surface;
nsPresContext* pc;
DrawDrag(aNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
if (!surface) {
return nullptr;
}
RefPtr<DataSourceSurface> destDataSurface =
AndroidWidgetUtils::GetDataSourceSurfaceForAndroidBitmap(
surface, &dragRect, dragRect.width * 4);
if (!destDataSurface) {
return nullptr;
}
DataSourceSurface::ScopedMap destMap(destDataSurface,
DataSourceSurface::READ);
java::sdk::Bitmap::LocalRef bitmap;
auto pixels = mozilla::jni::ByteBuffer::New(
reinterpret_cast<int8_t*>(destMap.GetData()),
destMap.GetStride() * destDataSurface->GetSize().height);
bitmap = java::sdk::Bitmap::CreateBitmap(
dragRect.width, dragRect.height, java::sdk::Bitmap::Config::ARGB_8888());
bitmap->CopyPixelsFromBuffer(pixels);
return bitmap;
}
void nsDragService::SetData(nsITransferable* aTransferable) {
mTransferable = aTransferable;
// Reset DataTransfer
mDataTransfer = nullptr;
}
// static
void nsDragService::SetDropData(
mozilla::java::GeckoDragAndDrop::DropData::Param aDropData) {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<nsDragService> dragService = nsDragService::GetInstance();
if (!dragService) {
return;
}
if (!aDropData) {
dragService->SetData(nullptr);
return;
}
nsCString mime(aDropData->MimeType()->ToCString());
if (mime.EqualsLiteral("application/x-moz-draganddrop")) {
// The drop data isn't changed.
return;
}
if (!mime.EqualsLiteral("text/plain") && !mime.EqualsLiteral("text/html")) {
// Not supported data.
dragService->SetData(nullptr);
return;
}
nsString buffer(aDropData->Text()->ToString());
nsCOMPtr<nsISupports> wrapper;
nsPrimitiveHelpers::CreatePrimitiveForData(
mime, buffer.get(), buffer.Length() * 2, getter_AddRefs(wrapper));
if (!wrapper) {
dragService->SetData(nullptr);
return;
}
nsCOMPtr<nsITransferable> transferable =
do_CreateInstance("@mozilla.org/widget/transferable;1");
transferable->Init(nullptr);
transferable->SetTransferData(mime.get(), wrapper);
dragService->SetData(transferable);
}
|