Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / devicestorage / DeviceStorageRequestChild.cpp
blob5075f4ae8f7c40150e8ae1296cb1388b5a1343ae
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/. */
7 #include "DeviceStorageRequestChild.h"
8 #include "DeviceStorageFileDescriptor.h"
9 #include "nsDeviceStorage.h"
10 #include "nsDOMFile.h"
11 #include "mozilla/dom/ipc/Blob.h"
13 namespace mozilla {
14 namespace dom {
15 namespace devicestorage {
17 DeviceStorageRequestChild::DeviceStorageRequestChild()
18 : mCallback(nullptr)
20 MOZ_COUNT_CTOR(DeviceStorageRequestChild);
23 DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
24 DeviceStorageFile* aDSFile)
25 : mRequest(aRequest)
26 , mDSFile(aDSFile)
27 , mCallback(nullptr)
29 MOZ_ASSERT(aRequest);
30 MOZ_ASSERT(aDSFile);
31 MOZ_COUNT_CTOR(DeviceStorageRequestChild);
34 DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest* aRequest,
35 DeviceStorageFile* aDSFile,
36 DeviceStorageFileDescriptor* aDSFileDescriptor)
37 : mRequest(aRequest)
38 , mDSFile(aDSFile)
39 , mDSFileDescriptor(aDSFileDescriptor)
40 , mCallback(nullptr)
42 MOZ_ASSERT(aRequest);
43 MOZ_ASSERT(aDSFile);
44 MOZ_ASSERT(aDSFileDescriptor);
45 MOZ_COUNT_CTOR(DeviceStorageRequestChild);
48 DeviceStorageRequestChild::~DeviceStorageRequestChild() {
49 MOZ_COUNT_DTOR(DeviceStorageRequestChild);
52 bool
53 DeviceStorageRequestChild::
54 Recv__delete__(const DeviceStorageResponseValue& aValue)
56 if (mCallback) {
57 mCallback->RequestComplete();
58 mCallback = nullptr;
61 nsCOMPtr<nsPIDOMWindow> window = mRequest->GetOwner();
62 if (!window) {
63 return true;
66 switch (aValue.type()) {
68 case DeviceStorageResponseValue::TErrorResponse:
70 ErrorResponse r = aValue;
71 mRequest->FireError(r.error());
72 break;
75 case DeviceStorageResponseValue::TSuccessResponse:
77 nsString fullPath;
78 mDSFile->GetFullPath(fullPath);
79 AutoJSContext cx;
80 JS::Rooted<JS::Value> result(cx);
81 StringToJsval(window, fullPath, &result);
82 mRequest->FireSuccess(result);
83 break;
86 case DeviceStorageResponseValue::TFileDescriptorResponse:
88 FileDescriptorResponse r = aValue;
90 nsString fullPath;
91 mDSFile->GetFullPath(fullPath);
92 AutoJSContext cx;
93 JS::Rooted<JS::Value> result(cx);
94 StringToJsval(window, fullPath, &result);
96 mDSFileDescriptor->mDSFile = mDSFile;
97 mDSFileDescriptor->mFileDescriptor = r.fileDescriptor();
98 mRequest->FireSuccess(result);
99 break;
102 case DeviceStorageResponseValue::TBlobResponse:
104 BlobResponse r = aValue;
105 BlobChild* actor = static_cast<BlobChild*>(r.blobChild());
106 nsCOMPtr<nsIDOMBlob> blob = actor->GetBlob();
108 nsCOMPtr<nsIDOMFile> file = do_QueryInterface(blob);
109 AutoJSContext cx;
110 JS::Rooted<JS::Value> result(cx,
111 InterfaceToJsval(window, file, &NS_GET_IID(nsIDOMFile)));
112 mRequest->FireSuccess(result);
113 break;
116 case DeviceStorageResponseValue::TFreeSpaceStorageResponse:
118 FreeSpaceStorageResponse r = aValue;
119 AutoJSContext cx;
120 JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.freeBytes())));
121 mRequest->FireSuccess(result);
122 break;
125 case DeviceStorageResponseValue::TUsedSpaceStorageResponse:
127 UsedSpaceStorageResponse r = aValue;
128 AutoJSContext cx;
129 JS::Rooted<JS::Value> result(cx, JS_NumberValue(double(r.usedBytes())));
130 mRequest->FireSuccess(result);
131 break;
134 case DeviceStorageResponseValue::TAvailableStorageResponse:
136 AvailableStorageResponse r = aValue;
137 AutoJSContext cx;
138 JS::Rooted<JS::Value> result(cx);
139 StringToJsval(window, r.mountState(), &result);
140 mRequest->FireSuccess(result);
141 break;
144 case DeviceStorageResponseValue::TStorageStatusResponse:
146 StorageStatusResponse r = aValue;
147 AutoJSContext cx;
148 JS::Rooted<JS::Value> result(cx);
149 StringToJsval(window, r.storageStatus(), &result);
150 mRequest->FireSuccess(result);
151 break;
154 case DeviceStorageResponseValue::TFormatStorageResponse:
156 FormatStorageResponse r = aValue;
157 AutoJSContext cx;
158 JS::Rooted<JS::Value> result(cx);
159 StringToJsval(window, r.mountState(), &result);
160 mRequest->FireSuccess(result);
161 break;
164 case DeviceStorageResponseValue::TMountStorageResponse:
166 MountStorageResponse r = aValue;
167 AutoJSContext cx;
168 JS::Rooted<JS::Value> result(cx);
169 StringToJsval(window, r.storageStatus(), &result);
170 mRequest->FireSuccess(result);
171 break;
174 case DeviceStorageResponseValue::TUnmountStorageResponse:
176 UnmountStorageResponse r = aValue;
177 AutoJSContext cx;
178 JS::Rooted<JS::Value> result(cx);
179 StringToJsval(window, r.storageStatus(), &result);
180 mRequest->FireSuccess(result);
181 break;
184 case DeviceStorageResponseValue::TEnumerationResponse:
186 EnumerationResponse r = aValue;
187 nsDOMDeviceStorageCursor* cursor
188 = static_cast<nsDOMDeviceStorageCursor*>(mRequest.get());
190 uint32_t count = r.paths().Length();
191 for (uint32_t i = 0; i < count; i++) {
192 nsRefPtr<DeviceStorageFile> dsf
193 = new DeviceStorageFile(r.type(), r.paths()[i].storageName(),
194 r.rootdir(), r.paths()[i].name());
195 cursor->mFiles.AppendElement(dsf);
198 nsRefPtr<ContinueCursorEvent> event = new ContinueCursorEvent(cursor);
199 event->Continue();
200 break;
203 default:
205 NS_RUNTIMEABORT("not reached");
206 break;
209 return true;
212 void
213 DeviceStorageRequestChild::
214 SetCallback(DeviceStorageRequestChildCallback *aCallback)
216 mCallback = aCallback;
219 } // namespace devicestorage
220 } // namespace dom
221 } // namespace mozilla