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"
15 namespace devicestorage
{
17 DeviceStorageRequestChild::DeviceStorageRequestChild()
20 MOZ_COUNT_CTOR(DeviceStorageRequestChild
);
23 DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest
* aRequest
,
24 DeviceStorageFile
* aDSFile
)
31 MOZ_COUNT_CTOR(DeviceStorageRequestChild
);
34 DeviceStorageRequestChild::DeviceStorageRequestChild(DOMRequest
* aRequest
,
35 DeviceStorageFile
* aDSFile
,
36 DeviceStorageFileDescriptor
* aDSFileDescriptor
)
39 , mDSFileDescriptor(aDSFileDescriptor
)
44 MOZ_ASSERT(aDSFileDescriptor
);
45 MOZ_COUNT_CTOR(DeviceStorageRequestChild
);
48 DeviceStorageRequestChild::~DeviceStorageRequestChild() {
49 MOZ_COUNT_DTOR(DeviceStorageRequestChild
);
53 DeviceStorageRequestChild::
54 Recv__delete__(const DeviceStorageResponseValue
& aValue
)
57 mCallback
->RequestComplete();
61 nsCOMPtr
<nsPIDOMWindow
> window
= mRequest
->GetOwner();
66 switch (aValue
.type()) {
68 case DeviceStorageResponseValue::TErrorResponse
:
70 ErrorResponse r
= aValue
;
71 mRequest
->FireError(r
.error());
75 case DeviceStorageResponseValue::TSuccessResponse
:
78 mDSFile
->GetFullPath(fullPath
);
80 JS::Rooted
<JS::Value
> result(cx
);
81 StringToJsval(window
, fullPath
, &result
);
82 mRequest
->FireSuccess(result
);
86 case DeviceStorageResponseValue::TFileDescriptorResponse
:
88 FileDescriptorResponse r
= aValue
;
91 mDSFile
->GetFullPath(fullPath
);
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
);
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
);
110 JS::Rooted
<JS::Value
> result(cx
,
111 InterfaceToJsval(window
, file
, &NS_GET_IID(nsIDOMFile
)));
112 mRequest
->FireSuccess(result
);
116 case DeviceStorageResponseValue::TFreeSpaceStorageResponse
:
118 FreeSpaceStorageResponse r
= aValue
;
120 JS::Rooted
<JS::Value
> result(cx
, JS_NumberValue(double(r
.freeBytes())));
121 mRequest
->FireSuccess(result
);
125 case DeviceStorageResponseValue::TUsedSpaceStorageResponse
:
127 UsedSpaceStorageResponse r
= aValue
;
129 JS::Rooted
<JS::Value
> result(cx
, JS_NumberValue(double(r
.usedBytes())));
130 mRequest
->FireSuccess(result
);
134 case DeviceStorageResponseValue::TAvailableStorageResponse
:
136 AvailableStorageResponse r
= aValue
;
138 JS::Rooted
<JS::Value
> result(cx
);
139 StringToJsval(window
, r
.mountState(), &result
);
140 mRequest
->FireSuccess(result
);
144 case DeviceStorageResponseValue::TStorageStatusResponse
:
146 StorageStatusResponse r
= aValue
;
148 JS::Rooted
<JS::Value
> result(cx
);
149 StringToJsval(window
, r
.storageStatus(), &result
);
150 mRequest
->FireSuccess(result
);
154 case DeviceStorageResponseValue::TFormatStorageResponse
:
156 FormatStorageResponse r
= aValue
;
158 JS::Rooted
<JS::Value
> result(cx
);
159 StringToJsval(window
, r
.mountState(), &result
);
160 mRequest
->FireSuccess(result
);
164 case DeviceStorageResponseValue::TMountStorageResponse
:
166 MountStorageResponse r
= aValue
;
168 JS::Rooted
<JS::Value
> result(cx
);
169 StringToJsval(window
, r
.storageStatus(), &result
);
170 mRequest
->FireSuccess(result
);
174 case DeviceStorageResponseValue::TUnmountStorageResponse
:
176 UnmountStorageResponse r
= aValue
;
178 JS::Rooted
<JS::Value
> result(cx
);
179 StringToJsval(window
, r
.storageStatus(), &result
);
180 mRequest
->FireSuccess(result
);
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
);
205 NS_RUNTIMEABORT("not reached");
213 DeviceStorageRequestChild::
214 SetCallback(DeviceStorageRequestChildCallback
*aCallback
)
216 mCallback
= aCallback
;
219 } // namespace devicestorage
221 } // namespace mozilla