1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "MetadataHelper.h"
9 #include "FileHandle.h"
11 #include "js/RootingAPI.h"
13 #include "mozilla/dom/FileModeBinding.h"
15 #include "nsIFileStreams.h"
16 #include "nsIOutputStream.h"
22 MetadataHelper::DoAsyncRun(nsISupports
* aStream
)
24 bool readWrite
= mFileHandle
->mMode
== FileMode::Readwrite
;
26 nsRefPtr
<AsyncMetadataGetter
> getter
=
27 new AsyncMetadataGetter(aStream
, mParams
, readWrite
);
29 nsresult rv
= getter
->AsyncWork(this, nullptr);
30 NS_ENSURE_SUCCESS(rv
, rv
);
36 MetadataHelper::GetSuccessResult(JSContext
* aCx
,
37 JS::MutableHandle
<JS::Value
> aVal
)
39 JS::Rooted
<JSObject
*> obj(aCx
, JS_NewObject(aCx
, nullptr, JS::NullPtr(),
41 NS_ENSURE_TRUE(obj
, NS_ERROR_OUT_OF_MEMORY
);
43 if (mParams
->SizeRequested()) {
44 JS::Rooted
<JS::Value
> val(aCx
, JS_NumberValue(mParams
->Size()));
46 if (!JS_DefineProperty(aCx
, obj
, "size", val
, JSPROP_ENUMERATE
)) {
47 return NS_ERROR_FAILURE
;
51 if (mParams
->LastModifiedRequested()) {
52 double msec
= mParams
->LastModified();
53 JSObject
*date
= JS_NewDateObjectMsec(aCx
, msec
);
54 NS_ENSURE_TRUE(date
, NS_ERROR_OUT_OF_MEMORY
);
56 JS::Rooted
<JS::Value
> dateRoot(aCx
, JS::ObjectValue(*date
));
57 if (!JS_DefineProperty(aCx
, obj
, "lastModified", dateRoot
,
59 return NS_ERROR_FAILURE
;
68 MetadataHelper::AsyncMetadataGetter::DoStreamWork(nsISupports
* aStream
)
73 // Force a flush (so all pending writes are flushed to the disk and file
74 // metadata is updated too).
76 nsCOMPtr
<nsIOutputStream
> ostream
= do_QueryInterface(aStream
);
77 NS_ASSERTION(ostream
, "This should always succeed!");
79 rv
= ostream
->Flush();
80 NS_ENSURE_SUCCESS(rv
, rv
);
83 nsCOMPtr
<nsIFileMetadata
> metadata
= do_QueryInterface(aStream
);
85 if (mParams
->SizeRequested()) {
87 rv
= metadata
->GetSize(&size
);
88 NS_ENSURE_SUCCESS(rv
, rv
);
90 NS_ENSURE_TRUE(size
>= 0, NS_ERROR_FAILURE
);
92 mParams
->mSize
= uint64_t(size
);
95 if (mParams
->LastModifiedRequested()) {
97 rv
= metadata
->GetLastModified(&lastModified
);
98 NS_ENSURE_SUCCESS(rv
, rv
);
100 mParams
->mLastModified
= lastModified
;
107 } // namespace mozilla