dom/devicestorage/DeviceStorageRequestChild.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/devicestorage/DeviceStorageRequestChild.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#include "DeviceStorageRequestChild.h"
    1.11 +#include "DeviceStorageFileDescriptor.h"
    1.12 +#include "nsDeviceStorage.h"
    1.13 +#include "nsDOMFile.h"
    1.14 +#include "mozilla/dom/ipc/Blob.h"
    1.15 +
    1.16 +namespace mozilla {
    1.17 +namespace dom {
    1.18 +namespace devicestorage {
    1.19 +
    1.20 +DeviceStorageRequestChild::DeviceStorageRequestChild()
    1.21 +  : mCallback(nullptr)
    1.22 +{
    1.23 +  MOZ_COUNT_CTOR(DeviceStorageRequestChild);
    1.24 +}
    1.25 +
    1.26 +DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
    1.27 +                                                     DeviceStorageFile* aDSFile)
    1.28 +  : mRequest(aRequest)
    1.29 +  , mDSFile(aDSFile)
    1.30 +  , mCallback(nullptr)
    1.31 +{
    1.32 +  MOZ_ASSERT(aRequest);
    1.33 +  MOZ_ASSERT(aDSFile);
    1.34 +  MOZ_COUNT_CTOR(DeviceStorageRequestChild);
    1.35 +}
    1.36 +
    1.37 +DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
    1.38 +                                                     DeviceStorageFile* aDSFile,
    1.39 +                                                     DeviceStorageFileDescriptor* aDSFileDescriptor)
    1.40 +  : mRequest(aRequest)
    1.41 +  , mDSFile(aDSFile)
    1.42 +  , mDSFileDescriptor(aDSFileDescriptor)
    1.43 +  , mCallback(nullptr)
    1.44 +{
    1.45 +  MOZ_ASSERT(aRequest);
    1.46 +  MOZ_ASSERT(aDSFile);
    1.47 +  MOZ_ASSERT(aDSFileDescriptor);
    1.48 +  MOZ_COUNT_CTOR(DeviceStorageRequestChild);
    1.49 +}
    1.50 +
    1.51 +DeviceStorageRequestChild::~DeviceStorageRequestChild() {
    1.52 +  MOZ_COUNT_DTOR(DeviceStorageRequestChild);
    1.53 +}
    1.54 +
    1.55 +bool
    1.56 +DeviceStorageRequestChild::
    1.57 +  Recv__delete__(const DeviceStorageResponseValue& aValue)
    1.58 +{
    1.59 +  if (mCallback) {
    1.60 +    mCallback->RequestComplete();
    1.61 +    mCallback = nullptr;
    1.62 +  }
    1.63 +
    1.64 +  nsCOMPtr<nsPIDOMWindow> window = mRequest->GetOwner();
    1.65 +  if (!window) {
    1.66 +    return true;
    1.67 +  }
    1.68 +
    1.69 +  switch (aValue.type()) {
    1.70 +
    1.71 +    case DeviceStorageResponseValue::TErrorResponse:
    1.72 +    {
    1.73 +      ErrorResponse r = aValue;
    1.74 +      mRequest->FireError(r.error());
    1.75 +      break;
    1.76 +    }
    1.77 +
    1.78 +    case DeviceStorageResponseValue::TSuccessResponse:
    1.79 +    {
    1.80 +      nsString fullPath;
    1.81 +      mDSFile->GetFullPath(fullPath);
    1.82 +      AutoJSContext cx;
    1.83 +      JS::Rooted<JS::Value> result(cx,
    1.84 +        StringToJsval(window, fullPath));
    1.85 +      mRequest->FireSuccess(result);
    1.86 +      break;
    1.87 +    }
    1.88 +
    1.89 +    case DeviceStorageResponseValue::TFileDescriptorResponse:
    1.90 +    {
    1.91 +      FileDescriptorResponse r = aValue;
    1.92 +
    1.93 +      nsString fullPath;
    1.94 +      mDSFile->GetFullPath(fullPath);
    1.95 +      AutoJSContext cx;
    1.96 +      JS::Rooted<JS::Value> result(cx,
    1.97 +        StringToJsval(window, fullPath));
    1.98 +
    1.99 +      mDSFileDescriptor->mDSFile = mDSFile;
   1.100 +      mDSFileDescriptor->mFileDescriptor = r.fileDescriptor();
   1.101 +      mRequest->FireSuccess(result);
   1.102 +      break;
   1.103 +    }
   1.104 +
   1.105 +    case DeviceStorageResponseValue::TBlobResponse:
   1.106 +    {
   1.107 +      BlobResponse r = aValue;
   1.108 +      BlobChild* actor = static_cast<BlobChild*>(r.blobChild());
   1.109 +      nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob();
   1.110 +
   1.111 +      nsCOMPtr<nsIDOMFile> file = do_QueryInterface(blob);
   1.112 +      AutoJSContext cx;
   1.113 +      JS::Rooted<JS::Value> result(cx,
   1.114 +        InterfaceToJsval(window, file, &NS_GET_IID(nsIDOMFile)));
   1.115 +      mRequest->FireSuccess(result);
   1.116 +      break;
   1.117 +    }
   1.118 +
   1.119 +    case DeviceStorageResponseValue::TFreeSpaceStorageResponse:
   1.120 +    {
   1.121 +      FreeSpaceStorageResponse r = aValue;
   1.122 +      AutoJSContext cx;
   1.123 +      JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.freeBytes())));
   1.124 +      mRequest->FireSuccess(result);
   1.125 +      break;
   1.126 +    }
   1.127 +
   1.128 +    case DeviceStorageResponseValue::TUsedSpaceStorageResponse:
   1.129 +    {
   1.130 +      UsedSpaceStorageResponse r = aValue;
   1.131 +      AutoJSContext cx;
   1.132 +      JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.usedBytes())));
   1.133 +      mRequest->FireSuccess(result);
   1.134 +      break;
   1.135 +    }
   1.136 +
   1.137 +    case DeviceStorageResponseValue::TAvailableStorageResponse:
   1.138 +    {
   1.139 +      AvailableStorageResponse r = aValue;
   1.140 +      AutoJSContext cx;
   1.141 +      JS::Rooted<JS::Value> result(
   1.142 +        cx, StringToJsval(window, r.mountState()));
   1.143 +      mRequest->FireSuccess(result);
   1.144 +      break;
   1.145 +    }
   1.146 +
   1.147 +    case DeviceStorageResponseValue::TStorageStatusResponse:
   1.148 +    {
   1.149 +      StorageStatusResponse r = aValue;
   1.150 +      AutoJSContext cx;
   1.151 +      JS::Rooted<JS::Value> result(
   1.152 +        cx, StringToJsval(window, r.storageStatus()));
   1.153 +      mRequest->FireSuccess(result);
   1.154 +      break;
   1.155 +    }
   1.156 +
   1.157 +    case DeviceStorageResponseValue::TFormatStorageResponse:
   1.158 +    {
   1.159 +      FormatStorageResponse r = aValue;
   1.160 +      AutoJSContext cx;
   1.161 +      JS::Rooted<JS::Value> result(
   1.162 +        cx, StringToJsval(window, r.mountState()));
   1.163 +      mRequest->FireSuccess(result);
   1.164 +      break;
   1.165 +    }
   1.166 +
   1.167 +    case DeviceStorageResponseValue::TMountStorageResponse:
   1.168 +    {
   1.169 +      MountStorageResponse r = aValue;
   1.170 +      AutoJSContext cx;
   1.171 +      JS::Rooted<JS::Value> result(
   1.172 +        cx, StringToJsval(window, r.storageStatus()));
   1.173 +      mRequest->FireSuccess(result);
   1.174 +      break;
   1.175 +    }
   1.176 +
   1.177 +    case DeviceStorageResponseValue::TUnmountStorageResponse:
   1.178 +    {
   1.179 +      UnmountStorageResponse r = aValue;
   1.180 +      AutoJSContext cx;
   1.181 +      JS::Rooted<JS::Value> result(
   1.182 +        cx, StringToJsval(window, r.storageStatus()));
   1.183 +      mRequest->FireSuccess(result);
   1.184 +      break;
   1.185 +    }
   1.186 +
   1.187 +    case DeviceStorageResponseValue::TEnumerationResponse:
   1.188 +    {
   1.189 +      EnumerationResponse r = aValue;
   1.190 +      nsDOMDeviceStorageCursor* cursor
   1.191 +        = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
   1.192 +
   1.193 +      uint32_t count = r.paths().Length();
   1.194 +      for (uint32_t i = 0; i < count; i++) {
   1.195 +        nsRefPtr<DeviceStorageFile> dsf
   1.196 +          = new DeviceStorageFile(r.type(), r.paths()[i].storageName(),
   1.197 +                                  r.rootdir(), r.paths()[i].name());
   1.198 +        cursor->mFiles.AppendElement(dsf);
   1.199 +      }
   1.200 +
   1.201 +      nsRefPtr<ContinueCursorEvent> event = new ContinueCursorEvent(cursor);
   1.202 +      event->Continue();
   1.203 +      break;
   1.204 +    }
   1.205 +
   1.206 +    default:
   1.207 +    {
   1.208 +      NS_RUNTIMEABORT("not reached");
   1.209 +      break;
   1.210 +    }
   1.211 +  }
   1.212 +  return true;
   1.213 +}
   1.214 +
   1.215 +void
   1.216 +DeviceStorageRequestChild::
   1.217 +  SetCallback(DeviceStorageRequestChildCallback *aCallback)
   1.218 +{
   1.219 +  mCallback = aCallback;
   1.220 +}
   1.221 +
   1.222 +} // namespace devicestorage
   1.223 +} // namespace dom
   1.224 +} // namespace mozilla

mercurial