Bug 1059702 - Use wifi.tethering.interface for wifi AP if it does exist. r=vchang
[gecko.git] / dom / filehandle / MetadataHelper.cpp
blob8d9c95a6dfefc12cda04ae3b659fd0e4b15662d1
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"
10 #include "js/Value.h"
11 #include "js/RootingAPI.h"
12 #include "jsapi.h"
13 #include "mozilla/dom/FileModeBinding.h"
14 #include "nsDebug.h"
15 #include "nsIFileStreams.h"
16 #include "nsIOutputStream.h"
18 namespace mozilla {
19 namespace dom {
21 nsresult
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);
32 return NS_OK;
35 nsresult
36 MetadataHelper::GetSuccessResult(JSContext* aCx,
37 JS::MutableHandle<JS::Value> aVal)
39 JS::Rooted<JSObject*> obj(aCx, JS_NewObject(aCx, nullptr, JS::NullPtr(),
40 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,
58 JSPROP_ENUMERATE)) {
59 return NS_ERROR_FAILURE;
63 aVal.setObject(*obj);
64 return NS_OK;
67 nsresult
68 MetadataHelper::AsyncMetadataGetter::DoStreamWork(nsISupports* aStream)
70 nsresult rv;
72 if (mReadWrite) {
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()) {
86 int64_t size;
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()) {
96 int64_t lastModified;
97 rv = metadata->GetLastModified(&lastModified);
98 NS_ENSURE_SUCCESS(rv, rv);
100 mParams->mLastModified = lastModified;
103 return NS_OK;
106 } // namespace dom
107 } // namespace mozilla