|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "DeviceStorageRequestParent.h" |
|
7 #include "nsDOMFile.h" |
|
8 #include "nsIMIMEService.h" |
|
9 #include "nsCExternalHandlerService.h" |
|
10 #include "mozilla/unused.h" |
|
11 #include "mozilla/dom/ipc/Blob.h" |
|
12 #include "ContentParent.h" |
|
13 #include "nsProxyRelease.h" |
|
14 #include "AppProcessChecker.h" |
|
15 #include "mozilla/Preferences.h" |
|
16 #include "nsNetCID.h" |
|
17 |
|
18 namespace mozilla { |
|
19 namespace dom { |
|
20 namespace devicestorage { |
|
21 |
|
22 DeviceStorageRequestParent::DeviceStorageRequestParent( |
|
23 const DeviceStorageParams& aParams) |
|
24 : mParams(aParams) |
|
25 , mMutex("DeviceStorageRequestParent::mMutex") |
|
26 , mActorDestoryed(false) |
|
27 { |
|
28 MOZ_COUNT_CTOR(DeviceStorageRequestParent); |
|
29 |
|
30 DebugOnly<DeviceStorageUsedSpaceCache*> usedSpaceCache |
|
31 = DeviceStorageUsedSpaceCache::CreateOrGet(); |
|
32 MOZ_ASSERT(usedSpaceCache); |
|
33 } |
|
34 |
|
35 void |
|
36 DeviceStorageRequestParent::Dispatch() |
|
37 { |
|
38 switch (mParams.type()) { |
|
39 case DeviceStorageParams::TDeviceStorageAddParams: |
|
40 { |
|
41 DeviceStorageAddParams p = mParams; |
|
42 |
|
43 nsRefPtr<DeviceStorageFile> dsf = |
|
44 new DeviceStorageFile(p.type(), p.storageName(), p.relpath()); |
|
45 |
|
46 BlobParent* bp = static_cast<BlobParent*>(p.blobParent()); |
|
47 nsCOMPtr<nsIDOMBlob> blob = bp->GetBlob(); |
|
48 |
|
49 nsCOMPtr<nsIInputStream> stream; |
|
50 blob->GetInternalStream(getter_AddRefs(stream)); |
|
51 |
|
52 nsRefPtr<CancelableRunnable> r = new WriteFileEvent(this, dsf, stream); |
|
53 |
|
54 nsCOMPtr<nsIEventTarget> target |
|
55 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
56 MOZ_ASSERT(target); |
|
57 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
58 break; |
|
59 } |
|
60 |
|
61 case DeviceStorageParams::TDeviceStorageCreateFdParams: |
|
62 { |
|
63 DeviceStorageCreateFdParams p = mParams; |
|
64 |
|
65 nsRefPtr<DeviceStorageFile> dsf = |
|
66 new DeviceStorageFile(p.type(), p.storageName(), p.relpath()); |
|
67 |
|
68 nsRefPtr<CancelableRunnable> r = new CreateFdEvent(this, dsf); |
|
69 |
|
70 nsCOMPtr<nsIEventTarget> target |
|
71 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
72 MOZ_ASSERT(target); |
|
73 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
74 break; |
|
75 } |
|
76 |
|
77 case DeviceStorageParams::TDeviceStorageGetParams: |
|
78 { |
|
79 DeviceStorageGetParams p = mParams; |
|
80 nsRefPtr<DeviceStorageFile> dsf = |
|
81 new DeviceStorageFile(p.type(), p.storageName(), |
|
82 p.rootDir(), p.relpath()); |
|
83 nsRefPtr<CancelableRunnable> r = new ReadFileEvent(this, dsf); |
|
84 |
|
85 nsCOMPtr<nsIEventTarget> target |
|
86 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
87 MOZ_ASSERT(target); |
|
88 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
89 break; |
|
90 } |
|
91 |
|
92 case DeviceStorageParams::TDeviceStorageDeleteParams: |
|
93 { |
|
94 DeviceStorageDeleteParams p = mParams; |
|
95 |
|
96 nsRefPtr<DeviceStorageFile> dsf = |
|
97 new DeviceStorageFile(p.type(), p.storageName(), p.relpath()); |
|
98 nsRefPtr<CancelableRunnable> r = new DeleteFileEvent(this, dsf); |
|
99 |
|
100 nsCOMPtr<nsIEventTarget> target |
|
101 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
102 MOZ_ASSERT(target); |
|
103 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
104 break; |
|
105 } |
|
106 |
|
107 case DeviceStorageParams::TDeviceStorageFreeSpaceParams: |
|
108 { |
|
109 DeviceStorageFreeSpaceParams p = mParams; |
|
110 |
|
111 nsRefPtr<DeviceStorageFile> dsf = |
|
112 new DeviceStorageFile(p.type(), p.storageName()); |
|
113 nsRefPtr<FreeSpaceFileEvent> r = new FreeSpaceFileEvent(this, dsf); |
|
114 |
|
115 nsCOMPtr<nsIEventTarget> target |
|
116 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
117 MOZ_ASSERT(target); |
|
118 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
119 break; |
|
120 } |
|
121 |
|
122 case DeviceStorageParams::TDeviceStorageUsedSpaceParams: |
|
123 { |
|
124 DeviceStorageUsedSpaceCache* usedSpaceCache |
|
125 = DeviceStorageUsedSpaceCache::CreateOrGet(); |
|
126 MOZ_ASSERT(usedSpaceCache); |
|
127 |
|
128 DeviceStorageUsedSpaceParams p = mParams; |
|
129 |
|
130 nsRefPtr<DeviceStorageFile> dsf = |
|
131 new DeviceStorageFile(p.type(), p.storageName()); |
|
132 nsRefPtr<UsedSpaceFileEvent> r = new UsedSpaceFileEvent(this, dsf); |
|
133 |
|
134 usedSpaceCache->Dispatch(r); |
|
135 break; |
|
136 } |
|
137 |
|
138 case DeviceStorageParams::TDeviceStorageAvailableParams: |
|
139 { |
|
140 DeviceStorageAvailableParams p = mParams; |
|
141 |
|
142 nsRefPtr<DeviceStorageFile> dsf = |
|
143 new DeviceStorageFile(p.type(), p.storageName()); |
|
144 nsRefPtr<PostAvailableResultEvent> r |
|
145 = new PostAvailableResultEvent(this, dsf); |
|
146 DebugOnly<nsresult> rv = NS_DispatchToMainThread(r); |
|
147 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
148 break; |
|
149 } |
|
150 |
|
151 case DeviceStorageParams::TDeviceStorageStatusParams: |
|
152 { |
|
153 DeviceStorageStatusParams p = mParams; |
|
154 |
|
155 nsRefPtr<DeviceStorageFile> dsf = |
|
156 new DeviceStorageFile(p.type(), p.storageName()); |
|
157 nsRefPtr<PostStatusResultEvent> r |
|
158 = new PostStatusResultEvent(this, dsf); |
|
159 DebugOnly<nsresult> rv = NS_DispatchToMainThread(r); |
|
160 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
161 break; |
|
162 } |
|
163 |
|
164 case DeviceStorageParams::TDeviceStorageFormatParams: |
|
165 { |
|
166 DeviceStorageFormatParams p = mParams; |
|
167 |
|
168 nsRefPtr<DeviceStorageFile> dsf = |
|
169 new DeviceStorageFile(p.type(), p.storageName()); |
|
170 nsRefPtr<PostFormatResultEvent> r |
|
171 = new PostFormatResultEvent(this, dsf); |
|
172 DebugOnly<nsresult> rv = NS_DispatchToMainThread(r); |
|
173 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
174 break; |
|
175 } |
|
176 |
|
177 case DeviceStorageParams::TDeviceStorageMountParams: |
|
178 { |
|
179 DeviceStorageMountParams p = mParams; |
|
180 |
|
181 nsRefPtr<DeviceStorageFile> dsf = |
|
182 new DeviceStorageFile(p.type(), p.storageName()); |
|
183 nsRefPtr<PostMountResultEvent> r |
|
184 = new PostMountResultEvent(this, dsf); |
|
185 DebugOnly<nsresult> rv = NS_DispatchToMainThread(r); |
|
186 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
187 break; |
|
188 } |
|
189 |
|
190 case DeviceStorageParams::TDeviceStorageUnmountParams: |
|
191 { |
|
192 DeviceStorageUnmountParams p = mParams; |
|
193 |
|
194 nsRefPtr<DeviceStorageFile> dsf = |
|
195 new DeviceStorageFile(p.type(), p.storageName()); |
|
196 nsRefPtr<PostUnmountResultEvent> r |
|
197 = new PostUnmountResultEvent(this, dsf); |
|
198 DebugOnly<nsresult> rv = NS_DispatchToMainThread(r); |
|
199 MOZ_ASSERT(NS_SUCCEEDED(rv)); |
|
200 break; |
|
201 } |
|
202 |
|
203 case DeviceStorageParams::TDeviceStorageEnumerationParams: |
|
204 { |
|
205 DeviceStorageEnumerationParams p = mParams; |
|
206 nsRefPtr<DeviceStorageFile> dsf |
|
207 = new DeviceStorageFile(p.type(), p.storageName(), |
|
208 p.rootdir(), NS_LITERAL_STRING("")); |
|
209 nsRefPtr<CancelableRunnable> r |
|
210 = new EnumerateFileEvent(this, dsf, p.since()); |
|
211 |
|
212 nsCOMPtr<nsIEventTarget> target |
|
213 = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID); |
|
214 MOZ_ASSERT(target); |
|
215 target->Dispatch(r, NS_DISPATCH_NORMAL); |
|
216 break; |
|
217 } |
|
218 default: |
|
219 { |
|
220 NS_RUNTIMEABORT("not reached"); |
|
221 break; |
|
222 } |
|
223 } |
|
224 } |
|
225 |
|
226 bool |
|
227 DeviceStorageRequestParent::EnsureRequiredPermissions( |
|
228 mozilla::dom::ContentParent* aParent) |
|
229 { |
|
230 if (mozilla::Preferences::GetBool("device.storage.testing", false)) { |
|
231 return true; |
|
232 } |
|
233 |
|
234 nsString type; |
|
235 DeviceStorageRequestType requestType; |
|
236 |
|
237 switch (mParams.type()) |
|
238 { |
|
239 case DeviceStorageParams::TDeviceStorageAddParams: |
|
240 { |
|
241 DeviceStorageAddParams p = mParams; |
|
242 type = p.type(); |
|
243 requestType = DEVICE_STORAGE_REQUEST_CREATE; |
|
244 break; |
|
245 } |
|
246 |
|
247 case DeviceStorageParams::TDeviceStorageCreateFdParams: |
|
248 { |
|
249 DeviceStorageCreateFdParams p = mParams; |
|
250 type = p.type(); |
|
251 requestType = DEVICE_STORAGE_REQUEST_CREATEFD; |
|
252 break; |
|
253 } |
|
254 |
|
255 case DeviceStorageParams::TDeviceStorageGetParams: |
|
256 { |
|
257 DeviceStorageGetParams p = mParams; |
|
258 type = p.type(); |
|
259 requestType = DEVICE_STORAGE_REQUEST_READ; |
|
260 break; |
|
261 } |
|
262 |
|
263 case DeviceStorageParams::TDeviceStorageDeleteParams: |
|
264 { |
|
265 DeviceStorageDeleteParams p = mParams; |
|
266 type = p.type(); |
|
267 requestType = DEVICE_STORAGE_REQUEST_DELETE; |
|
268 break; |
|
269 } |
|
270 |
|
271 case DeviceStorageParams::TDeviceStorageFreeSpaceParams: |
|
272 { |
|
273 DeviceStorageFreeSpaceParams p = mParams; |
|
274 type = p.type(); |
|
275 requestType = DEVICE_STORAGE_REQUEST_FREE_SPACE; |
|
276 break; |
|
277 } |
|
278 |
|
279 case DeviceStorageParams::TDeviceStorageUsedSpaceParams: |
|
280 { |
|
281 DeviceStorageUsedSpaceParams p = mParams; |
|
282 type = p.type(); |
|
283 requestType = DEVICE_STORAGE_REQUEST_FREE_SPACE; |
|
284 break; |
|
285 } |
|
286 |
|
287 case DeviceStorageParams::TDeviceStorageAvailableParams: |
|
288 { |
|
289 DeviceStorageAvailableParams p = mParams; |
|
290 type = p.type(); |
|
291 requestType = DEVICE_STORAGE_REQUEST_AVAILABLE; |
|
292 break; |
|
293 } |
|
294 |
|
295 case DeviceStorageParams::TDeviceStorageStatusParams: |
|
296 { |
|
297 DeviceStorageStatusParams p = mParams; |
|
298 type = p.type(); |
|
299 requestType = DEVICE_STORAGE_REQUEST_STATUS; |
|
300 break; |
|
301 } |
|
302 |
|
303 case DeviceStorageParams::TDeviceStorageFormatParams: |
|
304 { |
|
305 DeviceStorageFormatParams p = mParams; |
|
306 type = p.type(); |
|
307 requestType = DEVICE_STORAGE_REQUEST_FORMAT; |
|
308 break; |
|
309 } |
|
310 |
|
311 case DeviceStorageParams::TDeviceStorageMountParams: |
|
312 { |
|
313 DeviceStorageMountParams p = mParams; |
|
314 type = p.type(); |
|
315 requestType = DEVICE_STORAGE_REQUEST_MOUNT; |
|
316 break; |
|
317 } |
|
318 |
|
319 case DeviceStorageParams::TDeviceStorageUnmountParams: |
|
320 { |
|
321 DeviceStorageUnmountParams p = mParams; |
|
322 type = p.type(); |
|
323 requestType = DEVICE_STORAGE_REQUEST_UNMOUNT; |
|
324 break; |
|
325 } |
|
326 |
|
327 case DeviceStorageParams::TDeviceStorageEnumerationParams: |
|
328 { |
|
329 DeviceStorageEnumerationParams p = mParams; |
|
330 type = p.type(); |
|
331 requestType = DEVICE_STORAGE_REQUEST_READ; |
|
332 break; |
|
333 } |
|
334 |
|
335 default: |
|
336 { |
|
337 return false; |
|
338 } |
|
339 } |
|
340 |
|
341 // The 'apps' type is special. We only want this exposed |
|
342 // if the caller has the "webapps-manage" permission. |
|
343 if (type.EqualsLiteral("apps")) { |
|
344 if (!AssertAppProcessPermission(aParent, "webapps-manage")) { |
|
345 return false; |
|
346 } |
|
347 } |
|
348 |
|
349 nsAutoCString permissionName; |
|
350 nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(type, |
|
351 permissionName); |
|
352 if (NS_FAILED(rv)) { |
|
353 return false; |
|
354 } |
|
355 |
|
356 nsCString access; |
|
357 rv = DeviceStorageTypeChecker::GetAccessForRequest(requestType, access); |
|
358 if (NS_FAILED(rv)) { |
|
359 return false; |
|
360 } |
|
361 |
|
362 permissionName.AppendLiteral("-"); |
|
363 permissionName.Append(access); |
|
364 |
|
365 if (!AssertAppProcessPermission(aParent, permissionName.get())) { |
|
366 return false; |
|
367 } |
|
368 |
|
369 return true; |
|
370 } |
|
371 |
|
372 DeviceStorageRequestParent::~DeviceStorageRequestParent() |
|
373 { |
|
374 MOZ_COUNT_DTOR(DeviceStorageRequestParent); |
|
375 } |
|
376 |
|
377 NS_IMPL_ADDREF(DeviceStorageRequestParent) |
|
378 NS_IMPL_RELEASE(DeviceStorageRequestParent) |
|
379 |
|
380 void |
|
381 DeviceStorageRequestParent::ActorDestroy(ActorDestroyReason) |
|
382 { |
|
383 MutexAutoLock lock(mMutex); |
|
384 mActorDestoryed = true; |
|
385 int32_t count = mRunnables.Length(); |
|
386 for (int32_t index = 0; index < count; index++) { |
|
387 mRunnables[index]->Cancel(); |
|
388 } |
|
389 } |
|
390 |
|
391 DeviceStorageRequestParent::PostFreeSpaceResultEvent::PostFreeSpaceResultEvent( |
|
392 DeviceStorageRequestParent* aParent, |
|
393 uint64_t aFreeSpace) |
|
394 : CancelableRunnable(aParent) |
|
395 , mFreeSpace(aFreeSpace) |
|
396 { |
|
397 } |
|
398 |
|
399 DeviceStorageRequestParent::PostFreeSpaceResultEvent:: |
|
400 ~PostFreeSpaceResultEvent() {} |
|
401 |
|
402 nsresult |
|
403 DeviceStorageRequestParent::PostFreeSpaceResultEvent::CancelableRun() { |
|
404 MOZ_ASSERT(NS_IsMainThread()); |
|
405 |
|
406 FreeSpaceStorageResponse response(mFreeSpace); |
|
407 unused << mParent->Send__delete__(mParent, response); |
|
408 return NS_OK; |
|
409 } |
|
410 |
|
411 DeviceStorageRequestParent::PostUsedSpaceResultEvent:: |
|
412 PostUsedSpaceResultEvent(DeviceStorageRequestParent* aParent, |
|
413 const nsAString& aType, |
|
414 uint64_t aUsedSpace) |
|
415 : CancelableRunnable(aParent) |
|
416 , mType(aType) |
|
417 , mUsedSpace(aUsedSpace) |
|
418 { |
|
419 } |
|
420 |
|
421 DeviceStorageRequestParent::PostUsedSpaceResultEvent:: |
|
422 ~PostUsedSpaceResultEvent() {} |
|
423 |
|
424 nsresult |
|
425 DeviceStorageRequestParent::PostUsedSpaceResultEvent::CancelableRun() { |
|
426 MOZ_ASSERT(NS_IsMainThread()); |
|
427 |
|
428 UsedSpaceStorageResponse response(mUsedSpace); |
|
429 unused << mParent->Send__delete__(mParent, response); |
|
430 return NS_OK; |
|
431 } |
|
432 |
|
433 DeviceStorageRequestParent::PostErrorEvent:: |
|
434 PostErrorEvent(DeviceStorageRequestParent* aParent, const char* aError) |
|
435 : CancelableRunnable(aParent) |
|
436 { |
|
437 CopyASCIItoUTF16(aError, mError); |
|
438 } |
|
439 |
|
440 DeviceStorageRequestParent::PostErrorEvent::~PostErrorEvent() {} |
|
441 |
|
442 nsresult |
|
443 DeviceStorageRequestParent::PostErrorEvent::CancelableRun() { |
|
444 MOZ_ASSERT(NS_IsMainThread()); |
|
445 |
|
446 ErrorResponse response(mError); |
|
447 unused << mParent->Send__delete__(mParent, response); |
|
448 return NS_OK; |
|
449 } |
|
450 |
|
451 DeviceStorageRequestParent::PostSuccessEvent:: |
|
452 PostSuccessEvent(DeviceStorageRequestParent* aParent) |
|
453 : CancelableRunnable(aParent) |
|
454 { |
|
455 } |
|
456 |
|
457 DeviceStorageRequestParent::PostSuccessEvent::~PostSuccessEvent() {} |
|
458 |
|
459 nsresult |
|
460 DeviceStorageRequestParent::PostSuccessEvent::CancelableRun() { |
|
461 MOZ_ASSERT(NS_IsMainThread()); |
|
462 |
|
463 SuccessResponse response; |
|
464 unused << mParent->Send__delete__(mParent, response); |
|
465 return NS_OK; |
|
466 } |
|
467 |
|
468 DeviceStorageRequestParent::PostBlobSuccessEvent:: |
|
469 PostBlobSuccessEvent(DeviceStorageRequestParent* aParent, |
|
470 DeviceStorageFile* aFile, |
|
471 uint32_t aLength, |
|
472 nsACString& aMimeType, |
|
473 uint64_t aLastModifiedDate) |
|
474 : CancelableRunnable(aParent) |
|
475 , mLength(aLength) |
|
476 , mLastModificationDate(aLastModifiedDate) |
|
477 , mFile(aFile) |
|
478 , mMimeType(aMimeType) |
|
479 { |
|
480 } |
|
481 |
|
482 DeviceStorageRequestParent::PostBlobSuccessEvent::~PostBlobSuccessEvent() {} |
|
483 |
|
484 nsresult |
|
485 DeviceStorageRequestParent::PostBlobSuccessEvent::CancelableRun() { |
|
486 MOZ_ASSERT(NS_IsMainThread()); |
|
487 |
|
488 nsString mime; |
|
489 CopyASCIItoUTF16(mMimeType, mime); |
|
490 |
|
491 nsString fullPath; |
|
492 mFile->GetFullPath(fullPath); |
|
493 nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(fullPath, mime, mLength, |
|
494 mFile->mFile, |
|
495 mLastModificationDate); |
|
496 |
|
497 ContentParent* cp = static_cast<ContentParent*>(mParent->Manager()); |
|
498 BlobParent* actor = cp->GetOrCreateActorForBlob(blob); |
|
499 if (!actor) { |
|
500 ErrorResponse response(NS_LITERAL_STRING(POST_ERROR_EVENT_UNKNOWN)); |
|
501 unused << mParent->Send__delete__(mParent, response); |
|
502 return NS_OK; |
|
503 } |
|
504 |
|
505 BlobResponse response; |
|
506 response.blobParent() = actor; |
|
507 |
|
508 unused << mParent->Send__delete__(mParent, response); |
|
509 return NS_OK; |
|
510 } |
|
511 |
|
512 DeviceStorageRequestParent::PostEnumerationSuccessEvent:: |
|
513 PostEnumerationSuccessEvent(DeviceStorageRequestParent* aParent, |
|
514 const nsAString& aStorageType, |
|
515 const nsAString& aRelPath, |
|
516 InfallibleTArray<DeviceStorageFileValue>& aPaths) |
|
517 : CancelableRunnable(aParent) |
|
518 , mStorageType(aStorageType) |
|
519 , mRelPath(aRelPath) |
|
520 , mPaths(aPaths) |
|
521 { |
|
522 } |
|
523 |
|
524 DeviceStorageRequestParent::PostEnumerationSuccessEvent:: |
|
525 ~PostEnumerationSuccessEvent() {} |
|
526 |
|
527 nsresult |
|
528 DeviceStorageRequestParent::PostEnumerationSuccessEvent::CancelableRun() { |
|
529 MOZ_ASSERT(NS_IsMainThread()); |
|
530 |
|
531 EnumerationResponse response(mStorageType, mRelPath, mPaths); |
|
532 unused << mParent->Send__delete__(mParent, response); |
|
533 return NS_OK; |
|
534 } |
|
535 |
|
536 DeviceStorageRequestParent::CreateFdEvent:: |
|
537 CreateFdEvent(DeviceStorageRequestParent* aParent, |
|
538 DeviceStorageFile* aFile) |
|
539 : CancelableRunnable(aParent) |
|
540 , mFile(aFile) |
|
541 { |
|
542 } |
|
543 |
|
544 DeviceStorageRequestParent::CreateFdEvent::~CreateFdEvent() |
|
545 { |
|
546 } |
|
547 |
|
548 nsresult |
|
549 DeviceStorageRequestParent::CreateFdEvent::CancelableRun() |
|
550 { |
|
551 MOZ_ASSERT(!NS_IsMainThread()); |
|
552 |
|
553 nsCOMPtr<nsIRunnable> r; |
|
554 |
|
555 bool check = false; |
|
556 mFile->mFile->Exists(&check); |
|
557 if (check) { |
|
558 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_EXISTS); |
|
559 return NS_DispatchToMainThread(r); |
|
560 } |
|
561 |
|
562 FileDescriptor fileDescriptor; |
|
563 nsresult rv = mFile->CreateFileDescriptor(fileDescriptor); |
|
564 if (NS_FAILED(rv)) { |
|
565 NS_WARNING("CreateFileDescriptor failed"); |
|
566 mFile->Dump("CreateFileDescriptor failed"); |
|
567 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
568 } |
|
569 else { |
|
570 r = new PostFileDescriptorResultEvent(mParent, fileDescriptor); |
|
571 } |
|
572 |
|
573 return NS_DispatchToMainThread(r); |
|
574 } |
|
575 |
|
576 DeviceStorageRequestParent::WriteFileEvent:: |
|
577 WriteFileEvent(DeviceStorageRequestParent* aParent, |
|
578 DeviceStorageFile* aFile, |
|
579 nsIInputStream* aInputStream) |
|
580 : CancelableRunnable(aParent) |
|
581 , mFile(aFile) |
|
582 , mInputStream(aInputStream) |
|
583 { |
|
584 } |
|
585 |
|
586 DeviceStorageRequestParent::WriteFileEvent::~WriteFileEvent() |
|
587 { |
|
588 } |
|
589 |
|
590 nsresult |
|
591 DeviceStorageRequestParent::WriteFileEvent::CancelableRun() |
|
592 { |
|
593 MOZ_ASSERT(!NS_IsMainThread()); |
|
594 |
|
595 nsCOMPtr<nsIRunnable> r; |
|
596 |
|
597 if (!mInputStream) { |
|
598 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
599 return NS_DispatchToMainThread(r); |
|
600 } |
|
601 |
|
602 bool check = false; |
|
603 mFile->mFile->Exists(&check); |
|
604 if (check) { |
|
605 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_EXISTS); |
|
606 return NS_DispatchToMainThread(r); |
|
607 } |
|
608 |
|
609 nsresult rv = mFile->Write(mInputStream); |
|
610 |
|
611 if (NS_FAILED(rv)) { |
|
612 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
613 } |
|
614 else { |
|
615 r = new PostPathResultEvent(mParent, mFile->mPath); |
|
616 } |
|
617 |
|
618 return NS_DispatchToMainThread(r); |
|
619 } |
|
620 |
|
621 DeviceStorageRequestParent::DeleteFileEvent:: |
|
622 DeleteFileEvent(DeviceStorageRequestParent* aParent, DeviceStorageFile* aFile) |
|
623 : CancelableRunnable(aParent) |
|
624 , mFile(aFile) |
|
625 { |
|
626 } |
|
627 |
|
628 DeviceStorageRequestParent::DeleteFileEvent::~DeleteFileEvent() |
|
629 { |
|
630 } |
|
631 |
|
632 nsresult |
|
633 DeviceStorageRequestParent::DeleteFileEvent::CancelableRun() |
|
634 { |
|
635 MOZ_ASSERT(!NS_IsMainThread()); |
|
636 |
|
637 mFile->Remove(); |
|
638 |
|
639 nsCOMPtr<nsIRunnable> r; |
|
640 |
|
641 bool check = false; |
|
642 mFile->mFile->Exists(&check); |
|
643 if (check) { |
|
644 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
645 } |
|
646 else { |
|
647 r = new PostPathResultEvent(mParent, mFile->mPath); |
|
648 } |
|
649 |
|
650 return NS_DispatchToMainThread(r); |
|
651 } |
|
652 |
|
653 DeviceStorageRequestParent::FreeSpaceFileEvent:: |
|
654 FreeSpaceFileEvent(DeviceStorageRequestParent* aParent, |
|
655 DeviceStorageFile* aFile) |
|
656 : CancelableRunnable(aParent) |
|
657 , mFile(aFile) |
|
658 { |
|
659 } |
|
660 |
|
661 DeviceStorageRequestParent::FreeSpaceFileEvent::~FreeSpaceFileEvent() |
|
662 { |
|
663 } |
|
664 |
|
665 nsresult |
|
666 DeviceStorageRequestParent::FreeSpaceFileEvent::CancelableRun() |
|
667 { |
|
668 MOZ_ASSERT(!NS_IsMainThread()); |
|
669 |
|
670 int64_t freeSpace = 0; |
|
671 if (mFile) { |
|
672 mFile->GetDiskFreeSpace(&freeSpace); |
|
673 } |
|
674 |
|
675 nsCOMPtr<nsIRunnable> r; |
|
676 r = new PostFreeSpaceResultEvent(mParent, static_cast<uint64_t>(freeSpace)); |
|
677 return NS_DispatchToMainThread(r); |
|
678 } |
|
679 |
|
680 DeviceStorageRequestParent::UsedSpaceFileEvent:: |
|
681 UsedSpaceFileEvent(DeviceStorageRequestParent* aParent, |
|
682 DeviceStorageFile* aFile) |
|
683 : CancelableRunnable(aParent) |
|
684 , mFile(aFile) |
|
685 { |
|
686 } |
|
687 |
|
688 DeviceStorageRequestParent::UsedSpaceFileEvent::~UsedSpaceFileEvent() |
|
689 { |
|
690 } |
|
691 |
|
692 nsresult |
|
693 DeviceStorageRequestParent::UsedSpaceFileEvent::CancelableRun() |
|
694 { |
|
695 MOZ_ASSERT(!NS_IsMainThread()); |
|
696 |
|
697 uint64_t picturesUsage = 0, videosUsage = 0, musicUsage = 0, totalUsage = 0; |
|
698 mFile->AccumDiskUsage(&picturesUsage, &videosUsage, |
|
699 &musicUsage, &totalUsage); |
|
700 nsCOMPtr<nsIRunnable> r; |
|
701 if (mFile->mStorageType.EqualsLiteral(DEVICESTORAGE_PICTURES)) { |
|
702 r = new PostUsedSpaceResultEvent(mParent, mFile->mStorageType, |
|
703 picturesUsage); |
|
704 } |
|
705 else if (mFile->mStorageType.EqualsLiteral(DEVICESTORAGE_VIDEOS)) { |
|
706 r = new PostUsedSpaceResultEvent(mParent, mFile->mStorageType, videosUsage); |
|
707 } |
|
708 else if (mFile->mStorageType.EqualsLiteral(DEVICESTORAGE_MUSIC)) { |
|
709 r = new PostUsedSpaceResultEvent(mParent, mFile->mStorageType, musicUsage); |
|
710 } else { |
|
711 r = new PostUsedSpaceResultEvent(mParent, mFile->mStorageType, totalUsage); |
|
712 } |
|
713 return NS_DispatchToMainThread(r); |
|
714 } |
|
715 |
|
716 DeviceStorageRequestParent::ReadFileEvent:: |
|
717 ReadFileEvent(DeviceStorageRequestParent* aParent, DeviceStorageFile* aFile) |
|
718 : CancelableRunnable(aParent) |
|
719 , mFile(aFile) |
|
720 { |
|
721 nsCOMPtr<nsIMIMEService> mimeService |
|
722 = do_GetService(NS_MIMESERVICE_CONTRACTID); |
|
723 if (mimeService) { |
|
724 nsresult rv = mimeService->GetTypeFromFile(mFile->mFile, mMimeType); |
|
725 if (NS_FAILED(rv)) { |
|
726 mMimeType.Truncate(); |
|
727 } |
|
728 } |
|
729 } |
|
730 |
|
731 DeviceStorageRequestParent::ReadFileEvent::~ReadFileEvent() |
|
732 { |
|
733 } |
|
734 |
|
735 nsresult |
|
736 DeviceStorageRequestParent::ReadFileEvent::CancelableRun() |
|
737 { |
|
738 MOZ_ASSERT(!NS_IsMainThread()); |
|
739 |
|
740 nsCOMPtr<nsIRunnable> r; |
|
741 bool check = false; |
|
742 mFile->mFile->Exists(&check); |
|
743 |
|
744 if (!check) { |
|
745 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST); |
|
746 return NS_DispatchToMainThread(r); |
|
747 } |
|
748 |
|
749 int64_t fileSize; |
|
750 nsresult rv = mFile->mFile->GetFileSize(&fileSize); |
|
751 if (NS_FAILED(rv)) { |
|
752 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
753 return NS_DispatchToMainThread(r); |
|
754 } |
|
755 |
|
756 PRTime modDate; |
|
757 rv = mFile->mFile->GetLastModifiedTime(&modDate); |
|
758 if (NS_FAILED(rv)) { |
|
759 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN); |
|
760 return NS_DispatchToMainThread(r); |
|
761 } |
|
762 |
|
763 r = new PostBlobSuccessEvent(mParent, mFile, static_cast<uint64_t>(fileSize), |
|
764 mMimeType, modDate); |
|
765 return NS_DispatchToMainThread(r); |
|
766 } |
|
767 |
|
768 DeviceStorageRequestParent::EnumerateFileEvent:: |
|
769 EnumerateFileEvent(DeviceStorageRequestParent* aParent, |
|
770 DeviceStorageFile* aFile, |
|
771 uint64_t aSince) |
|
772 : CancelableRunnable(aParent) |
|
773 , mFile(aFile) |
|
774 , mSince(aSince) |
|
775 { |
|
776 } |
|
777 |
|
778 DeviceStorageRequestParent::EnumerateFileEvent::~EnumerateFileEvent() |
|
779 { |
|
780 } |
|
781 |
|
782 nsresult |
|
783 DeviceStorageRequestParent::EnumerateFileEvent::CancelableRun() |
|
784 { |
|
785 MOZ_ASSERT(!NS_IsMainThread()); |
|
786 |
|
787 nsCOMPtr<nsIRunnable> r; |
|
788 if (mFile->mFile) { |
|
789 bool check = false; |
|
790 mFile->mFile->Exists(&check); |
|
791 if (!check) { |
|
792 r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST); |
|
793 return NS_DispatchToMainThread(r); |
|
794 } |
|
795 } |
|
796 |
|
797 nsTArray<nsRefPtr<DeviceStorageFile> > files; |
|
798 mFile->CollectFiles(files, mSince); |
|
799 |
|
800 InfallibleTArray<DeviceStorageFileValue> values; |
|
801 |
|
802 uint32_t count = files.Length(); |
|
803 for (uint32_t i = 0; i < count; i++) { |
|
804 DeviceStorageFileValue dsvf(files[i]->mStorageName, files[i]->mPath); |
|
805 values.AppendElement(dsvf); |
|
806 } |
|
807 |
|
808 r = new PostEnumerationSuccessEvent(mParent, mFile->mStorageType, |
|
809 mFile->mRootDir, values); |
|
810 return NS_DispatchToMainThread(r); |
|
811 } |
|
812 |
|
813 |
|
814 DeviceStorageRequestParent::PostPathResultEvent:: |
|
815 PostPathResultEvent(DeviceStorageRequestParent* aParent, |
|
816 const nsAString& aPath) |
|
817 : CancelableRunnable(aParent) |
|
818 , mPath(aPath) |
|
819 { |
|
820 } |
|
821 |
|
822 DeviceStorageRequestParent::PostPathResultEvent::~PostPathResultEvent() |
|
823 { |
|
824 } |
|
825 |
|
826 nsresult |
|
827 DeviceStorageRequestParent::PostPathResultEvent::CancelableRun() |
|
828 { |
|
829 MOZ_ASSERT(NS_IsMainThread()); |
|
830 |
|
831 SuccessResponse response; |
|
832 unused << mParent->Send__delete__(mParent, response); |
|
833 return NS_OK; |
|
834 } |
|
835 |
|
836 DeviceStorageRequestParent::PostFileDescriptorResultEvent:: |
|
837 PostFileDescriptorResultEvent(DeviceStorageRequestParent* aParent, |
|
838 const FileDescriptor& aFileDescriptor) |
|
839 : CancelableRunnable(aParent) |
|
840 , mFileDescriptor(aFileDescriptor) |
|
841 { |
|
842 } |
|
843 |
|
844 DeviceStorageRequestParent::PostFileDescriptorResultEvent:: |
|
845 ~PostFileDescriptorResultEvent() |
|
846 { |
|
847 } |
|
848 |
|
849 nsresult |
|
850 DeviceStorageRequestParent::PostFileDescriptorResultEvent::CancelableRun() |
|
851 { |
|
852 MOZ_ASSERT(NS_IsMainThread()); |
|
853 |
|
854 FileDescriptorResponse response(mFileDescriptor); |
|
855 unused << mParent->Send__delete__(mParent, response); |
|
856 return NS_OK; |
|
857 } |
|
858 |
|
859 DeviceStorageRequestParent::PostAvailableResultEvent:: |
|
860 PostAvailableResultEvent(DeviceStorageRequestParent* aParent, |
|
861 DeviceStorageFile* aFile) |
|
862 : CancelableRunnable(aParent) |
|
863 , mFile(aFile) |
|
864 { |
|
865 } |
|
866 |
|
867 DeviceStorageRequestParent::PostAvailableResultEvent:: |
|
868 ~PostAvailableResultEvent() |
|
869 { |
|
870 } |
|
871 |
|
872 nsresult |
|
873 DeviceStorageRequestParent::PostAvailableResultEvent::CancelableRun() |
|
874 { |
|
875 MOZ_ASSERT(NS_IsMainThread()); |
|
876 |
|
877 nsString state = NS_LITERAL_STRING("unavailable"); |
|
878 if (mFile) { |
|
879 mFile->GetStatus(state); |
|
880 } |
|
881 |
|
882 AvailableStorageResponse response(state); |
|
883 unused << mParent->Send__delete__(mParent, response); |
|
884 return NS_OK; |
|
885 } |
|
886 |
|
887 DeviceStorageRequestParent::PostStatusResultEvent:: |
|
888 PostStatusResultEvent(DeviceStorageRequestParent* aParent, |
|
889 DeviceStorageFile* aFile) |
|
890 : CancelableRunnable(aParent) |
|
891 , mFile(aFile) |
|
892 { |
|
893 } |
|
894 |
|
895 DeviceStorageRequestParent::PostStatusResultEvent:: |
|
896 ~PostStatusResultEvent() |
|
897 { |
|
898 } |
|
899 |
|
900 nsresult |
|
901 DeviceStorageRequestParent::PostStatusResultEvent::CancelableRun() |
|
902 { |
|
903 MOZ_ASSERT(NS_IsMainThread()); |
|
904 |
|
905 nsString state = NS_LITERAL_STRING("undefined"); |
|
906 if (mFile) { |
|
907 mFile->GetStorageStatus(state); |
|
908 } |
|
909 |
|
910 StorageStatusResponse response(state); |
|
911 unused << mParent->Send__delete__(mParent, response); |
|
912 return NS_OK; |
|
913 } |
|
914 |
|
915 DeviceStorageRequestParent::PostFormatResultEvent:: |
|
916 PostFormatResultEvent(DeviceStorageRequestParent* aParent, |
|
917 DeviceStorageFile* aFile) |
|
918 : CancelableRunnable(aParent) |
|
919 , mFile(aFile) |
|
920 { |
|
921 } |
|
922 |
|
923 DeviceStorageRequestParent::PostFormatResultEvent:: |
|
924 ~PostFormatResultEvent() |
|
925 { |
|
926 } |
|
927 |
|
928 nsresult |
|
929 DeviceStorageRequestParent::PostFormatResultEvent::CancelableRun() |
|
930 { |
|
931 MOZ_ASSERT(NS_IsMainThread()); |
|
932 |
|
933 nsString state = NS_LITERAL_STRING("unavailable"); |
|
934 if (mFile) { |
|
935 mFile->DoFormat(state); |
|
936 } |
|
937 |
|
938 FormatStorageResponse response(state); |
|
939 unused << mParent->Send__delete__(mParent, response); |
|
940 return NS_OK; |
|
941 } |
|
942 |
|
943 DeviceStorageRequestParent::PostMountResultEvent:: |
|
944 PostMountResultEvent(DeviceStorageRequestParent* aParent, |
|
945 DeviceStorageFile* aFile) |
|
946 : CancelableRunnable(aParent) |
|
947 , mFile(aFile) |
|
948 { |
|
949 } |
|
950 |
|
951 DeviceStorageRequestParent::PostMountResultEvent:: |
|
952 ~PostMountResultEvent() |
|
953 { |
|
954 } |
|
955 |
|
956 nsresult |
|
957 DeviceStorageRequestParent::PostMountResultEvent::CancelableRun() |
|
958 { |
|
959 MOZ_ASSERT(NS_IsMainThread()); |
|
960 |
|
961 nsString state = NS_LITERAL_STRING("unavailable"); |
|
962 if (mFile) { |
|
963 mFile->DoMount(state); |
|
964 } |
|
965 |
|
966 MountStorageResponse response(state); |
|
967 unused << mParent->Send__delete__(mParent, response); |
|
968 return NS_OK; |
|
969 } |
|
970 |
|
971 DeviceStorageRequestParent::PostUnmountResultEvent:: |
|
972 PostUnmountResultEvent(DeviceStorageRequestParent* aParent, |
|
973 DeviceStorageFile* aFile) |
|
974 : CancelableRunnable(aParent) |
|
975 , mFile(aFile) |
|
976 { |
|
977 } |
|
978 |
|
979 DeviceStorageRequestParent::PostUnmountResultEvent:: |
|
980 ~PostUnmountResultEvent() |
|
981 { |
|
982 } |
|
983 |
|
984 nsresult |
|
985 DeviceStorageRequestParent::PostUnmountResultEvent::CancelableRun() |
|
986 { |
|
987 MOZ_ASSERT(NS_IsMainThread()); |
|
988 |
|
989 nsString state = NS_LITERAL_STRING("unavailable"); |
|
990 if (mFile) { |
|
991 mFile->DoUnmount(state); |
|
992 } |
|
993 |
|
994 UnmountStorageResponse response(state); |
|
995 unused << mParent->Send__delete__(mParent, response); |
|
996 return NS_OK; |
|
997 } |
|
998 |
|
999 } // namespace devicestorage |
|
1000 } // namespace dom |
|
1001 } // namespace mozilla |