From: Arkadiusz Pietraszek Date: Thu, 19 Apr 2018 11:19:32 +0000 (+0200) Subject: [Filesystem] Added fileHandle methods in new file file_handle.js X-Git-Tag: submit/tizen/20180518.121229~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d9a7314fecb714ca96af07a3c1c5ce449e6c00cd;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] Added fileHandle methods in new file file_handle.js ACR: http://suprem.sec.samsung.net/jira/browse/TWDAPI-121 Change-Id: I5beefd68490dd86d0cfe5e296201f3fa0ec7160b Signed-off-by: Szymon Jastrzebski Signed-off-by: Arkadiusz Pietraszek Signed-off-by: Jakub Skowron Signed-off-by: Pawel Wasowski --- diff --git a/src/filesystem/filesystem_api.js b/src/filesystem/filesystem_api.js index cd2442ce..38a9b269 100755 --- a/src/filesystem/filesystem_api.js +++ b/src/filesystem/filesystem_api.js @@ -18,4 +18,5 @@ //= require('common.js'); //= require('file_stream.js'); //= require('file.js'); +//= require('file_handle.js'); //= require('file_system_manager.js'); diff --git a/src/filesystem/js/file_handle.js b/src/filesystem/js/file_handle.js new file mode 100644 index 00000000..50e35b01 --- /dev/null +++ b/src/filesystem/js/file_handle.js @@ -0,0 +1,522 @@ +/* + * 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); + } +};