--- /dev/null
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+function FileHandle(_id, _path, _mode) {
+ Object.defineProperties(this, {
+ id: {value: _id, writable: false, enumerable: false},
+ path: {value: _path, writable: false, enumerable: false},
+ mode: {value: _mode, writable: false, enumerable: false},
+ state: {value: 'opened', writable: true, enumerable: false}
+ });
+}
+
+FileHandle.prototype.seek = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'offset', type: types_.LONG}, {
+ name: 'whence',
+ type: types_.ENUM,
+ values: type_.getValues(BaseSeekPosition),
+ optional: true
+ }
+ ]);
+
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ var data = {id: this.id, offset: args.offset};
+ if (undefined === args.whence) {
+ data.whence = 'BEGIN';
+ } else {
+ data.whence = args.whence;
+ }
+ var result = native_.callSync('FileHandle_seek', data);
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+ return native_.getResultObject(result);
+};
+
+FileHandle.prototype.seekNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'offset', type: types_.LONG},
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}, {
+ name: 'whence',
+ type: types_.ENUM,
+ values: type_.getValues(BaseSeekPosition),
+ optional: true
+ }
+ ]);
+
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ var data = {id: this.id, offset: args.offset, blocking: false};
+ if (undefined === args.whence) {
+ data.whence = 'BEGIN';
+ } else {
+ data.whence = args.whence;
+ }
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_seek', data, callback);
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.readString = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'count', type: types_.LONG, optional: true, nullable: true},
+ {name: 'inputEncoding', type: types_.STRING, optional: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ((this.mode === 'w') || (this.mode === 'a')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
+ }
+ var data = {
+ id: this.id,
+ count: args.count,
+ inputEncoding: args.inputEncoding
+ };
+ var result = native_.callSync('FileHandle_readString', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+ return native_.getResultObject(result);
+};
+
+FileHandle.prototype.readStringNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'size', type: types_.LONG, optional: true, nullable: true},
+ {name: 'inputEncoding', type: types_.STRING, optional: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ((this.mode === 'w') || (this.mode === 'a')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
+ }
+ var data = {id: this.id, size: args.size, inputEncoding: args.inputEncoding, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_readString', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.writeString = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'string', type: types_.STRING},
+ {name: 'outputEncoding', type: types_.STRING, optional: true}
+ ]);
+ if (!('opened' === this.state)) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ('r' === this.mode) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var data = {id: this.id, string: args.string, outputEncoding: args.outputEncoding};
+ var result = native_.callSync('FileHandle_writeString', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+ return native_.getResultObject(result);
+};
+
+FileHandle.prototype.writeStringNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'string', type: types_.STRING},
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'outputEncoding', type: types_.STRING, optional: true}
+ ]);
+ if (!('opened' === this.state)) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ('r' === this.mode) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var data = {id: this.id, string: args.string, outputEncoding: args.outputEncoding, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_writeString', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.readBlob = function() {
+ var args = validator_.validateArgs(
+ arguments, [{name: 'size', type: types_.LONG, optional: true}]);
+
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ((this.mode === 'w') || (this.mode === 'a')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
+ }
+ var data = {id: this.id, size: args.size};
+
+ var result = native_.call('FileHandle_readData', data);
+ var encodedData = native_.getResultObject(result);
+ var data = StringToArray(encodedData, Uint8Array);
+ return new Blob([data]);
+};
+
+FileHandle.prototype.readBlobNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'size', type: types_.LONG, optional: true, nullable: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+
+ var data = {id: this.id, size: args.size, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ var encodedData = native_.getResultObject(result);
+ var data = StringToArray(encodedData, Uint8Array);
+ native_.callIfPossible(args.successCallback, new Blob([data]));
+ }
+ };
+
+ var result = native_.call('FileHandle_readData', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+function blobToUint8Array(b) {
+ var uri = URL.createObjectURL(b), xhr = new XMLHttpRequest(), i, ui8;
+ xhr.open('GET', uri, false);
+ xhr.send();
+ URL.revokeObjectURL(uri);
+ var stringUtf8 = unescape(encodeURIComponent(xhr.response));
+ ui8 = new Uint8Array(stringUtf8.length);
+ for (i = 0; i < stringUtf8.length; ++i) {
+ ui8[i] = stringUtf8.charCodeAt(i);
+ }
+ return ui8;
+}
+
+FileHandle.prototype.writeBlob = function() {
+ var args = validator_.validateArgs(
+ arguments, [{name: 'blob', type: types_.PLATFORM_OBJECT, values: Blob}]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+
+ var encodedData = ArrayToString(new Uint8Array(blobToUint8Array(args.blob)));
+ var data = {id: this.id, data: encodedData};
+ var result = native_.callSync('FileHandle_writeData', data);
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.writeBlobNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'blob', type: types_.PLATFORM_OBJECT, values: Blob},
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+ ]);
+ if (!('opened' === this.state)) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ } else if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+
+ var encodedData = ArrayToString(new Uint8Array(blobToUint8Array(args.blob)));
+ var data = {id: this.id, data: encodedData, blocking: false};
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_writeData', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.readData = function() {
+ var args = validator_.validateArgs(
+ arguments, [{name: 'size', type: types_.LONG, optional: true, nullable: true}]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ((this.mode === 'w') || (this.mode === 'a')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
+ }
+ var data = {id: this.id, size: args.size};
+ var result = native_.callSync('FileHandle_readData', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+ var encodedData = native_.getResultObject(result);
+ var data = StringToArray(encodedData, Uint8Array);
+ return new Uint8Array(data);
+};
+
+FileHandle.prototype.readDataNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'size', type: types_.LONG, optional: true, nullable: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if ((this.mode === 'w') || (this.mode === 'a')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is write-only');
+ }
+
+ var data = {id: this.id, size: args.size, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ var data_out =
+ new Uint8Array(StringToArray(native_.getResultObject(result), Uint8Array));
+ native_.callIfPossible(args.successCallback, data_out);
+ }
+ };
+
+ var result = native_.call('FileHandle_readData', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.writeData = function() {
+ var args = validator_.validateArgs(
+ arguments, [{name: 'data', type: types_.PLATFORM_OBJECT, values: Uint8Array}]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ } else if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var encodedData = ArrayToString(args.data);
+ var data = {id: this.id, data: encodedData};
+ var result = native_.callSync('FileHandle_writeData', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.writeDataNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'data', type: types_.PLATFORM_OBJECT, values: Uint8Array},
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+ ]);
+ if (!('opened' === this.state)) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ } else if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+
+ var encodedData = ArrayToString(args.data);
+
+ var data = {id: this.id, data: encodedData, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_writeData', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.flush = function() {
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var data = {id: this.id};
+ var result = native_.callSync('FileHandle_flush', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.flushNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var data = {id: this.id, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_flush', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.sync = function() {
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+
+ var data = {id: this.id};
+ var result = native_.callSync('FileHandle_sync', data);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.syncNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ if (this.mode === 'r') {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle state is read-only');
+ }
+ var data = {id: this.id, blocking: false};
+
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_sync', data, callback);
+
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.close = function() {
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+ var data = {id: this.id};
+ var result = native_.callSync('FileHandle_close', data);
+ this.state = 'closed';
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};
+
+FileHandle.prototype.closeNonBlocking = function() {
+ var args = validator_.validateArgs(arguments, [
+ {name: 'successCallback', type: types_.FUNCTION, optional: true, nullable: true},
+ {name: 'errorCallback', type: types_.FUNCTION, optional: true, nullable: true}
+ ]);
+ if (!(this.state === 'opened')) {
+ throw new WebAPIException(WebAPIException.IO_ERR, 'FileHandle is not opened');
+ }
+
+ var data = {id: this.id, blocking: false};
+ var callback = function(result) {
+ if (native_.isFailure(result)) {
+ native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
+ } else {
+ native_.callIfPossible(args.successCallback, native_.getResultObject(result));
+ }
+ };
+
+ var result = native_.call('FileHandle_close', data, callback);
+ this.state = 'closed';
+ if (native_.isFailure(result)) {
+ throw native_.getErrorObject(result);
+ }
+};