dom/devicestorage/DeviceStorageRequestParent.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     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"
    18 namespace mozilla {
    19 namespace dom {
    20 namespace devicestorage {
    22 DeviceStorageRequestParent::DeviceStorageRequestParent(
    23   const DeviceStorageParams& aParams)
    24   : mParams(aParams)
    25   , mMutex("DeviceStorageRequestParent::mMutex")
    26   , mActorDestoryed(false)
    27 {
    28   MOZ_COUNT_CTOR(DeviceStorageRequestParent);
    30   DebugOnly<DeviceStorageUsedSpaceCache*> usedSpaceCache
    31     = DeviceStorageUsedSpaceCache::CreateOrGet();
    32   MOZ_ASSERT(usedSpaceCache);
    33 }
    35 void
    36 DeviceStorageRequestParent::Dispatch()
    37 {
    38   switch (mParams.type()) {
    39     case DeviceStorageParams::TDeviceStorageAddParams:
    40     {
    41       DeviceStorageAddParams p = mParams;
    43       nsRefPtr<DeviceStorageFile> dsf =
    44         new DeviceStorageFile(p.type(), p.storageName(), p.relpath());
    46       BlobParent* bp = static_cast<BlobParent*>(p.blobParent());
    47       nsCOMPtr<nsIDOMBlob> blob = bp->GetBlob();
    49       nsCOMPtr<nsIInputStream> stream;
    50       blob->GetInternalStream(getter_AddRefs(stream));
    52       nsRefPtr<CancelableRunnable> r = new WriteFileEvent(this, dsf, stream);
    54       nsCOMPtr<nsIEventTarget> target
    55         = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
    56       MOZ_ASSERT(target);
    57       target->Dispatch(r, NS_DISPATCH_NORMAL);
    58       break;
    59     }
    61     case DeviceStorageParams::TDeviceStorageCreateFdParams:
    62     {
    63       DeviceStorageCreateFdParams p = mParams;
    65       nsRefPtr<DeviceStorageFile> dsf =
    66         new DeviceStorageFile(p.type(), p.storageName(), p.relpath());
    68       nsRefPtr<CancelableRunnable> r = new CreateFdEvent(this, dsf);
    70       nsCOMPtr<nsIEventTarget> target
    71         = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
    72       MOZ_ASSERT(target);
    73       target->Dispatch(r, NS_DISPATCH_NORMAL);
    74       break;
    75     }
    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);
    85       nsCOMPtr<nsIEventTarget> target
    86         = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
    87       MOZ_ASSERT(target);
    88       target->Dispatch(r, NS_DISPATCH_NORMAL);
    89       break;
    90     }
    92     case DeviceStorageParams::TDeviceStorageDeleteParams:
    93     {
    94       DeviceStorageDeleteParams p = mParams;
    96       nsRefPtr<DeviceStorageFile> dsf =
    97         new DeviceStorageFile(p.type(), p.storageName(), p.relpath());
    98       nsRefPtr<CancelableRunnable> r = new DeleteFileEvent(this, dsf);
   100       nsCOMPtr<nsIEventTarget> target
   101         = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
   102       MOZ_ASSERT(target);
   103       target->Dispatch(r, NS_DISPATCH_NORMAL);
   104       break;
   105     }
   107     case DeviceStorageParams::TDeviceStorageFreeSpaceParams:
   108     {
   109       DeviceStorageFreeSpaceParams p = mParams;
   111       nsRefPtr<DeviceStorageFile> dsf =
   112         new DeviceStorageFile(p.type(), p.storageName());
   113       nsRefPtr<FreeSpaceFileEvent> r = new FreeSpaceFileEvent(this, dsf);
   115       nsCOMPtr<nsIEventTarget> target
   116         = do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID);
   117       MOZ_ASSERT(target);
   118       target->Dispatch(r, NS_DISPATCH_NORMAL);
   119       break;
   120     }
   122     case DeviceStorageParams::TDeviceStorageUsedSpaceParams:
   123     {
   124       DeviceStorageUsedSpaceCache* usedSpaceCache
   125         = DeviceStorageUsedSpaceCache::CreateOrGet();
   126       MOZ_ASSERT(usedSpaceCache);
   128       DeviceStorageUsedSpaceParams p = mParams;
   130       nsRefPtr<DeviceStorageFile> dsf =
   131         new DeviceStorageFile(p.type(), p.storageName());
   132       nsRefPtr<UsedSpaceFileEvent> r = new UsedSpaceFileEvent(this, dsf);
   134       usedSpaceCache->Dispatch(r);
   135       break;
   136     }
   138     case DeviceStorageParams::TDeviceStorageAvailableParams:
   139     {
   140       DeviceStorageAvailableParams p = mParams;
   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     }
   151     case DeviceStorageParams::TDeviceStorageStatusParams:
   152     {
   153       DeviceStorageStatusParams p = mParams;
   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     }
   164     case DeviceStorageParams::TDeviceStorageFormatParams:
   165     {
   166       DeviceStorageFormatParams p = mParams;
   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     }
   177     case DeviceStorageParams::TDeviceStorageMountParams:
   178     {
   179       DeviceStorageMountParams p = mParams;
   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     }
   190     case DeviceStorageParams::TDeviceStorageUnmountParams:
   191     {
   192       DeviceStorageUnmountParams p = mParams;
   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     }
   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());
   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 }
   226 bool
   227 DeviceStorageRequestParent::EnsureRequiredPermissions(
   228   mozilla::dom::ContentParent* aParent)
   229 {
   230   if (mozilla::Preferences::GetBool("device.storage.testing", false)) {
   231     return true;
   232   }
   234   nsString type;
   235   DeviceStorageRequestType requestType;
   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     }
   247     case DeviceStorageParams::TDeviceStorageCreateFdParams:
   248     {
   249       DeviceStorageCreateFdParams p = mParams;
   250       type = p.type();
   251       requestType = DEVICE_STORAGE_REQUEST_CREATEFD;
   252       break;
   253     }
   255     case DeviceStorageParams::TDeviceStorageGetParams:
   256     {
   257       DeviceStorageGetParams p = mParams;
   258       type = p.type();
   259       requestType = DEVICE_STORAGE_REQUEST_READ;
   260       break;
   261     }
   263     case DeviceStorageParams::TDeviceStorageDeleteParams:
   264     {
   265       DeviceStorageDeleteParams p = mParams;
   266       type = p.type();
   267       requestType = DEVICE_STORAGE_REQUEST_DELETE;
   268       break;
   269     }
   271     case DeviceStorageParams::TDeviceStorageFreeSpaceParams:
   272     {
   273       DeviceStorageFreeSpaceParams p = mParams;
   274       type = p.type();
   275       requestType = DEVICE_STORAGE_REQUEST_FREE_SPACE;
   276       break;
   277     }
   279     case DeviceStorageParams::TDeviceStorageUsedSpaceParams:
   280     {
   281       DeviceStorageUsedSpaceParams p = mParams;
   282       type = p.type();
   283       requestType = DEVICE_STORAGE_REQUEST_FREE_SPACE;
   284       break;
   285     }
   287     case DeviceStorageParams::TDeviceStorageAvailableParams:
   288     {
   289       DeviceStorageAvailableParams p = mParams;
   290       type = p.type();
   291       requestType = DEVICE_STORAGE_REQUEST_AVAILABLE;
   292       break;
   293     }
   295     case DeviceStorageParams::TDeviceStorageStatusParams:
   296     {
   297       DeviceStorageStatusParams p = mParams;
   298       type = p.type();
   299       requestType = DEVICE_STORAGE_REQUEST_STATUS;
   300       break;
   301     }
   303     case DeviceStorageParams::TDeviceStorageFormatParams:
   304     {
   305       DeviceStorageFormatParams p = mParams;
   306       type = p.type();
   307       requestType = DEVICE_STORAGE_REQUEST_FORMAT;
   308       break;
   309     }
   311     case DeviceStorageParams::TDeviceStorageMountParams:
   312     {
   313       DeviceStorageMountParams p = mParams;
   314       type = p.type();
   315       requestType = DEVICE_STORAGE_REQUEST_MOUNT;
   316       break;
   317     }
   319     case DeviceStorageParams::TDeviceStorageUnmountParams:
   320     {
   321       DeviceStorageUnmountParams p = mParams;
   322       type = p.type();
   323       requestType = DEVICE_STORAGE_REQUEST_UNMOUNT;
   324       break;
   325     }
   327     case DeviceStorageParams::TDeviceStorageEnumerationParams:
   328     {
   329       DeviceStorageEnumerationParams p = mParams;
   330       type = p.type();
   331       requestType = DEVICE_STORAGE_REQUEST_READ;
   332       break;
   333     }
   335     default:
   336     {
   337       return false;
   338     }
   339   }
   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   }
   349   nsAutoCString permissionName;
   350   nsresult rv = DeviceStorageTypeChecker::GetPermissionForType(type,
   351                                                                permissionName);
   352   if (NS_FAILED(rv)) {
   353     return false;
   354   }
   356   nsCString access;
   357   rv = DeviceStorageTypeChecker::GetAccessForRequest(requestType, access);
   358   if (NS_FAILED(rv)) {
   359     return false;
   360   }
   362   permissionName.AppendLiteral("-");
   363   permissionName.Append(access);
   365   if (!AssertAppProcessPermission(aParent, permissionName.get())) {
   366     return false;
   367   }
   369   return true;
   370 }
   372 DeviceStorageRequestParent::~DeviceStorageRequestParent()
   373 {
   374   MOZ_COUNT_DTOR(DeviceStorageRequestParent);
   375 }
   377 NS_IMPL_ADDREF(DeviceStorageRequestParent)
   378 NS_IMPL_RELEASE(DeviceStorageRequestParent)
   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 }
   391 DeviceStorageRequestParent::PostFreeSpaceResultEvent::PostFreeSpaceResultEvent(
   392   DeviceStorageRequestParent* aParent,
   393   uint64_t aFreeSpace)
   394   : CancelableRunnable(aParent)
   395   , mFreeSpace(aFreeSpace)
   396 {
   397 }
   399 DeviceStorageRequestParent::PostFreeSpaceResultEvent::
   400   ~PostFreeSpaceResultEvent() {}
   402 nsresult
   403 DeviceStorageRequestParent::PostFreeSpaceResultEvent::CancelableRun() {
   404   MOZ_ASSERT(NS_IsMainThread());
   406   FreeSpaceStorageResponse response(mFreeSpace);
   407   unused << mParent->Send__delete__(mParent, response);
   408   return NS_OK;
   409 }
   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 }
   421 DeviceStorageRequestParent::PostUsedSpaceResultEvent::
   422   ~PostUsedSpaceResultEvent() {}
   424 nsresult
   425 DeviceStorageRequestParent::PostUsedSpaceResultEvent::CancelableRun() {
   426   MOZ_ASSERT(NS_IsMainThread());
   428   UsedSpaceStorageResponse response(mUsedSpace);
   429   unused << mParent->Send__delete__(mParent, response);
   430   return NS_OK;
   431 }
   433 DeviceStorageRequestParent::PostErrorEvent::
   434   PostErrorEvent(DeviceStorageRequestParent* aParent, const char* aError)
   435   : CancelableRunnable(aParent)
   436 {
   437   CopyASCIItoUTF16(aError, mError);
   438 }
   440 DeviceStorageRequestParent::PostErrorEvent::~PostErrorEvent() {}
   442 nsresult
   443 DeviceStorageRequestParent::PostErrorEvent::CancelableRun() {
   444   MOZ_ASSERT(NS_IsMainThread());
   446   ErrorResponse response(mError);
   447   unused << mParent->Send__delete__(mParent, response);
   448   return NS_OK;
   449 }
   451 DeviceStorageRequestParent::PostSuccessEvent::
   452   PostSuccessEvent(DeviceStorageRequestParent* aParent)
   453   : CancelableRunnable(aParent)
   454 {
   455 }
   457 DeviceStorageRequestParent::PostSuccessEvent::~PostSuccessEvent() {}
   459 nsresult
   460 DeviceStorageRequestParent::PostSuccessEvent::CancelableRun() {
   461   MOZ_ASSERT(NS_IsMainThread());
   463   SuccessResponse response;
   464   unused << mParent->Send__delete__(mParent, response);
   465   return NS_OK;
   466 }
   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 }
   482 DeviceStorageRequestParent::PostBlobSuccessEvent::~PostBlobSuccessEvent() {}
   484 nsresult
   485 DeviceStorageRequestParent::PostBlobSuccessEvent::CancelableRun() {
   486   MOZ_ASSERT(NS_IsMainThread());
   488   nsString mime;
   489   CopyASCIItoUTF16(mMimeType, mime);
   491   nsString fullPath;
   492   mFile->GetFullPath(fullPath);
   493   nsCOMPtr<nsIDOMBlob> blob = new nsDOMFileFile(fullPath, mime, mLength, 
   494                                                 mFile->mFile,
   495                                                 mLastModificationDate);
   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   }
   505   BlobResponse response;
   506   response.blobParent() = actor;
   508   unused << mParent->Send__delete__(mParent, response);
   509   return NS_OK;
   510 }
   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 }
   524 DeviceStorageRequestParent::PostEnumerationSuccessEvent::
   525   ~PostEnumerationSuccessEvent() {}
   527 nsresult
   528 DeviceStorageRequestParent::PostEnumerationSuccessEvent::CancelableRun() {
   529   MOZ_ASSERT(NS_IsMainThread());
   531   EnumerationResponse response(mStorageType, mRelPath, mPaths);
   532   unused << mParent->Send__delete__(mParent, response);
   533   return NS_OK;
   534 }
   536 DeviceStorageRequestParent::CreateFdEvent::
   537   CreateFdEvent(DeviceStorageRequestParent* aParent,
   538                  DeviceStorageFile* aFile)
   539   : CancelableRunnable(aParent)
   540   , mFile(aFile)
   541 {
   542 }
   544 DeviceStorageRequestParent::CreateFdEvent::~CreateFdEvent()
   545 {
   546 }
   548 nsresult
   549 DeviceStorageRequestParent::CreateFdEvent::CancelableRun()
   550 {
   551   MOZ_ASSERT(!NS_IsMainThread());
   553   nsCOMPtr<nsIRunnable> r;
   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   }
   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   }
   573   return NS_DispatchToMainThread(r);
   574 }
   576 DeviceStorageRequestParent::WriteFileEvent::
   577   WriteFileEvent(DeviceStorageRequestParent* aParent,
   578                  DeviceStorageFile* aFile,
   579                  nsIInputStream* aInputStream)
   580   : CancelableRunnable(aParent)
   581   , mFile(aFile)
   582   , mInputStream(aInputStream)
   583 {
   584 }
   586 DeviceStorageRequestParent::WriteFileEvent::~WriteFileEvent()
   587 {
   588 }
   590 nsresult
   591 DeviceStorageRequestParent::WriteFileEvent::CancelableRun()
   592 {
   593   MOZ_ASSERT(!NS_IsMainThread());
   595   nsCOMPtr<nsIRunnable> r;
   597   if (!mInputStream) {
   598     r = new PostErrorEvent(mParent, POST_ERROR_EVENT_UNKNOWN);
   599     return NS_DispatchToMainThread(r);
   600   }
   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   }
   609   nsresult rv = mFile->Write(mInputStream);
   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   }
   618   return NS_DispatchToMainThread(r);
   619 }
   621 DeviceStorageRequestParent::DeleteFileEvent::
   622   DeleteFileEvent(DeviceStorageRequestParent* aParent, DeviceStorageFile* aFile)
   623   : CancelableRunnable(aParent)
   624   , mFile(aFile)
   625 {
   626 }
   628 DeviceStorageRequestParent::DeleteFileEvent::~DeleteFileEvent()
   629 {
   630 }
   632 nsresult
   633 DeviceStorageRequestParent::DeleteFileEvent::CancelableRun()
   634 {
   635   MOZ_ASSERT(!NS_IsMainThread());
   637   mFile->Remove();
   639   nsCOMPtr<nsIRunnable> r;
   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   }
   650   return NS_DispatchToMainThread(r);
   651 }
   653 DeviceStorageRequestParent::FreeSpaceFileEvent::
   654   FreeSpaceFileEvent(DeviceStorageRequestParent* aParent,
   655                      DeviceStorageFile* aFile)
   656   : CancelableRunnable(aParent)
   657   , mFile(aFile)
   658 {
   659 }
   661 DeviceStorageRequestParent::FreeSpaceFileEvent::~FreeSpaceFileEvent()
   662 {
   663 }
   665 nsresult
   666 DeviceStorageRequestParent::FreeSpaceFileEvent::CancelableRun()
   667 {
   668   MOZ_ASSERT(!NS_IsMainThread());
   670   int64_t freeSpace = 0;
   671   if (mFile) {
   672     mFile->GetDiskFreeSpace(&freeSpace);
   673   }
   675   nsCOMPtr<nsIRunnable> r;
   676   r = new PostFreeSpaceResultEvent(mParent, static_cast<uint64_t>(freeSpace));
   677   return NS_DispatchToMainThread(r);
   678 }
   680 DeviceStorageRequestParent::UsedSpaceFileEvent::
   681   UsedSpaceFileEvent(DeviceStorageRequestParent* aParent,
   682                      DeviceStorageFile* aFile)
   683   : CancelableRunnable(aParent)
   684   , mFile(aFile)
   685 {
   686 }
   688 DeviceStorageRequestParent::UsedSpaceFileEvent::~UsedSpaceFileEvent()
   689 {
   690 }
   692 nsresult
   693 DeviceStorageRequestParent::UsedSpaceFileEvent::CancelableRun()
   694 {
   695   MOZ_ASSERT(!NS_IsMainThread());
   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 }
   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 }
   731 DeviceStorageRequestParent::ReadFileEvent::~ReadFileEvent()
   732 {
   733 }
   735 nsresult
   736 DeviceStorageRequestParent::ReadFileEvent::CancelableRun()
   737 {
   738   MOZ_ASSERT(!NS_IsMainThread());
   740   nsCOMPtr<nsIRunnable> r;
   741   bool check = false;
   742   mFile->mFile->Exists(&check);
   744   if (!check) {
   745     r = new PostErrorEvent(mParent, POST_ERROR_EVENT_FILE_DOES_NOT_EXIST);
   746     return NS_DispatchToMainThread(r);
   747   }
   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   }
   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   }
   763   r = new PostBlobSuccessEvent(mParent, mFile, static_cast<uint64_t>(fileSize),
   764                                mMimeType, modDate);
   765   return NS_DispatchToMainThread(r);
   766 }
   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 }
   778 DeviceStorageRequestParent::EnumerateFileEvent::~EnumerateFileEvent()
   779 {
   780 }
   782 nsresult
   783 DeviceStorageRequestParent::EnumerateFileEvent::CancelableRun()
   784 {
   785   MOZ_ASSERT(!NS_IsMainThread());
   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   }
   797   nsTArray<nsRefPtr<DeviceStorageFile> > files;
   798   mFile->CollectFiles(files, mSince);
   800   InfallibleTArray<DeviceStorageFileValue> values;
   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   }
   808   r = new PostEnumerationSuccessEvent(mParent, mFile->mStorageType,
   809                                       mFile->mRootDir, values);
   810   return NS_DispatchToMainThread(r);
   811 }
   814 DeviceStorageRequestParent::PostPathResultEvent::
   815   PostPathResultEvent(DeviceStorageRequestParent* aParent,
   816                       const nsAString& aPath)
   817   : CancelableRunnable(aParent)
   818   , mPath(aPath)
   819 {
   820 }
   822 DeviceStorageRequestParent::PostPathResultEvent::~PostPathResultEvent()
   823 {
   824 }
   826 nsresult
   827 DeviceStorageRequestParent::PostPathResultEvent::CancelableRun()
   828 {
   829   MOZ_ASSERT(NS_IsMainThread());
   831   SuccessResponse response;
   832   unused << mParent->Send__delete__(mParent, response);
   833   return NS_OK;
   834 }
   836 DeviceStorageRequestParent::PostFileDescriptorResultEvent::
   837   PostFileDescriptorResultEvent(DeviceStorageRequestParent* aParent,
   838                                 const FileDescriptor& aFileDescriptor)
   839   : CancelableRunnable(aParent)
   840   , mFileDescriptor(aFileDescriptor)
   841 {
   842 }
   844 DeviceStorageRequestParent::PostFileDescriptorResultEvent::
   845   ~PostFileDescriptorResultEvent()
   846 {
   847 }
   849 nsresult
   850 DeviceStorageRequestParent::PostFileDescriptorResultEvent::CancelableRun()
   851 {
   852   MOZ_ASSERT(NS_IsMainThread());
   854   FileDescriptorResponse response(mFileDescriptor);
   855   unused << mParent->Send__delete__(mParent, response);
   856   return NS_OK;
   857 }
   859 DeviceStorageRequestParent::PostAvailableResultEvent::
   860   PostAvailableResultEvent(DeviceStorageRequestParent* aParent,
   861                            DeviceStorageFile* aFile)
   862   : CancelableRunnable(aParent)
   863   , mFile(aFile)
   864 {
   865 }
   867 DeviceStorageRequestParent::PostAvailableResultEvent::
   868   ~PostAvailableResultEvent()
   869 {
   870 }
   872 nsresult
   873 DeviceStorageRequestParent::PostAvailableResultEvent::CancelableRun()
   874 {
   875   MOZ_ASSERT(NS_IsMainThread());
   877   nsString state = NS_LITERAL_STRING("unavailable");
   878   if (mFile) {
   879     mFile->GetStatus(state);
   880   }
   882   AvailableStorageResponse response(state);
   883   unused << mParent->Send__delete__(mParent, response);
   884   return NS_OK;
   885 }
   887 DeviceStorageRequestParent::PostStatusResultEvent::
   888   PostStatusResultEvent(DeviceStorageRequestParent* aParent,
   889                            DeviceStorageFile* aFile)
   890   : CancelableRunnable(aParent)
   891   , mFile(aFile)
   892 {
   893 }
   895 DeviceStorageRequestParent::PostStatusResultEvent::
   896   ~PostStatusResultEvent()
   897 {
   898 }
   900 nsresult
   901 DeviceStorageRequestParent::PostStatusResultEvent::CancelableRun()
   902 {
   903   MOZ_ASSERT(NS_IsMainThread());
   905   nsString state = NS_LITERAL_STRING("undefined");
   906   if (mFile) {
   907     mFile->GetStorageStatus(state);
   908   }
   910   StorageStatusResponse response(state);
   911   unused << mParent->Send__delete__(mParent, response);
   912   return NS_OK;
   913 }
   915 DeviceStorageRequestParent::PostFormatResultEvent::
   916   PostFormatResultEvent(DeviceStorageRequestParent* aParent,
   917                            DeviceStorageFile* aFile)
   918   : CancelableRunnable(aParent)
   919   , mFile(aFile)
   920 {
   921 }
   923 DeviceStorageRequestParent::PostFormatResultEvent::
   924   ~PostFormatResultEvent()
   925 {
   926 }
   928 nsresult
   929 DeviceStorageRequestParent::PostFormatResultEvent::CancelableRun()
   930 {
   931   MOZ_ASSERT(NS_IsMainThread());
   933   nsString state = NS_LITERAL_STRING("unavailable");
   934   if (mFile) {
   935     mFile->DoFormat(state);
   936   }
   938   FormatStorageResponse response(state);
   939   unused << mParent->Send__delete__(mParent, response);
   940   return NS_OK;
   941 }
   943 DeviceStorageRequestParent::PostMountResultEvent::
   944   PostMountResultEvent(DeviceStorageRequestParent* aParent,
   945                            DeviceStorageFile* aFile)
   946   : CancelableRunnable(aParent)
   947   , mFile(aFile)
   948 {
   949 }
   951 DeviceStorageRequestParent::PostMountResultEvent::
   952   ~PostMountResultEvent()
   953 {
   954 }
   956 nsresult
   957 DeviceStorageRequestParent::PostMountResultEvent::CancelableRun()
   958 {
   959   MOZ_ASSERT(NS_IsMainThread());
   961   nsString state = NS_LITERAL_STRING("unavailable");
   962   if (mFile) {
   963     mFile->DoMount(state);
   964   }
   966   MountStorageResponse response(state);
   967   unused << mParent->Send__delete__(mParent, response);
   968   return NS_OK;
   969 }
   971 DeviceStorageRequestParent::PostUnmountResultEvent::
   972   PostUnmountResultEvent(DeviceStorageRequestParent* aParent,
   973                            DeviceStorageFile* aFile)
   974   : CancelableRunnable(aParent)
   975   , mFile(aFile)
   976 {
   977 }
   979 DeviceStorageRequestParent::PostUnmountResultEvent::
   980   ~PostUnmountResultEvent()
   981 {
   982 }
   984 nsresult
   985 DeviceStorageRequestParent::PostUnmountResultEvent::CancelableRun()
   986 {
   987   MOZ_ASSERT(NS_IsMainThread());
   989   nsString state = NS_LITERAL_STRING("unavailable");
   990   if (mFile) {
   991     mFile->DoUnmount(state);
   992   }
   994   UnmountStorageResponse response(state);
   995   unused << mParent->Send__delete__(mParent, response);
   996   return NS_OK;
   997 }
   999 } // namespace devicestorage
  1000 } // namespace dom
  1001 } // namespace mozilla

mercurial