[Verification] Code compiles without errors.
Change-Id: I9e5f11b4e0a32ff045e2aa0e7384ae80a83d1e55
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
'cordova_file_api.js',
'cordova_file_extension.cc',
'cordova_file_extension.h',
+ 'cordova_file_instance.cc',
+ 'cordova_file_instance.h',
],
'include_dirs': [
'../',
*/
#include "file/cordova_file_extension.h"
+#include "file/cordova_file_instance.h"
// This will be generated from cordova_file_api.js
extern const char kSource_cordova_file_api[];
CordovaFileExtension::~CordovaFileExtension() {}
+common::Instance* CordovaFileExtension::CreateInstance() {
+ LoggerD("Entered");
+ return new CordovaFileInstance();
+}
+
} // file
} // cordova
} // extension
public:
CordovaFileExtension();
virtual ~CordovaFileExtension();
+
+ private:
+ // common::Extension implementation.
+ virtual common::Instance* CreateInstance();
};
} // file
--- /dev/null
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#include "file/cordova_file_instance.h"
+
+#include <unistd.h>
+
+#include <common/logger.h>
+#include <common/picojson.h>
+#include <common/platform_result.h>
+
+namespace extension {
+namespace cordova {
+namespace file {
+
+using common::ErrorCode;
+using common::PlatformResult;
+
+CordovaFileInstance::CordovaFileInstance() {
+ using std::placeholders::_1;
+ using std::placeholders::_2;
+
+ LoggerD("Entered");
+
+#define REGISTER_SYNC(c, x) \
+ RegisterSyncHandler(c, std::bind(&CordovaFileInstance::x, this, _1, _2));
+#define REGISTER_ASYNC(c, x) \
+ RegisterSyncHandler(c, std::bind(&CordovaFileInstance::x, this, _1, _2));
+
+ REGISTER_SYNC("File_truncate", Truncate);
+
+#undef REGISTER_SYNC
+#undef REGISTER_ASYNC
+}
+
+CordovaFileInstance::~CordovaFileInstance() {
+ LoggerD("Entered");
+}
+
+void CordovaFileInstance::Truncate(const picojson::value& args, picojson::object& out) {
+ LoggerD("Entered");
+
+ if (!args.contains("uri") || !args.contains("length")) {
+ LoggerE("Invalid parameter passed.");
+ ReportError(PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Invalid parameter passed."), &out);
+ return;
+ }
+
+ const double length = args.get("length").get<double>();
+ const std::string& path = args.get("uri").get<std::string>();
+
+ int ret = truncate(path.c_str(), length);
+ LoggerD("Truncate returned value [%d]", ret);
+
+ if (!ret) {
+ ReportSuccess(out);
+ } else {
+ ReportError(PlatformResult(ErrorCode::UNKNOWN_ERR, "File truncate failed."), &out);
+ }
+}
+
+} // file
+} // cordova
+} // extension
--- /dev/null
+/*
+ * Copyright (c) 2015 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.
+ */
+
+#ifndef FILE_CORDOVA_FILE_INSTANCE_H_
+#define FILE_CORDOVA_FILE_INSTANCE_H_
+
+#include <common/extension.h>
+#include <common/picojson.h>
+
+namespace extension {
+namespace cordova {
+namespace file {
+
+class CordovaFileInstance : public common::ParsedInstance {
+ public:
+ CordovaFileInstance();
+ virtual ~CordovaFileInstance();
+
+ private:
+ void Truncate(const picojson::value& args, picojson::object& out);
+};
+} // file
+} // cordova
+} // extension
+
+#endif // FILE_CORDOVA_FILE_INSTANCE_H_
cordova.define('cordova-plugin-file.tizen.FileWriter', function(require, exports, module) {
// TODO: remove -> end
+var utils_ = xwalk.utils;
+var native_ = new utils_.NativeManager(extension);
+
module.exports = {
- write: function(successCallback, errorCallback, args) {},
- truncate: function(successCallback, errorCallback, args) {}
+ write: function(successCallback, errorCallback, args) {
+ var uri = args[0];
+ var data = args[1];
+ var position = args[2];
+
+ var onSuccess = function (file) {
+ if (file.isDirectory) {
+ errorCallback && errorCallback(FileError.INVALID_MODIFICATION_ERR);
+ return;
+ }
+
+ var openStreamSuccess = function (stream) {
+ try {
+ stream.position = position;
+ stream.write(data);
+ var length = stream.position - position;
+ stream.close();
+ successCallback && successCallback(length);
+ } catch (error) {
+ errorCallback && errorCallback(ConvErrorCode(error.code));
+ }
+ }
+
+ var openStreamError = function (error) {
+ errorCallback && errorCallback(ConvErrorCode(error.code));
+ }
+
+ try {
+ file.openStream('rw', openStreamSuccess, openStreamError);
+ } catch (error) {
+ errorCallback && errorCallback(ConvErrorCode(error.code));
+ }
+ }
+
+ var onError = function (error) {
+ errorCallback && errorCallback(ConvErrorCode(error.code));
+ }
+
+ try {
+ tizen.filesystem.resolve(uri, onSuccess, onError, 'rw');
+ } catch (error) {
+ errorCallback && errorCallback(ConvErrorCode(error.code));
+ }
+ },
+
+ truncate: function(successCallback, errorCallback, args) {
+ var uriPrefix = 'file://';
+ var uri = args[0];
+ var length = args[1];
+
+ if (uri.indexOf(uriPrefix) === 0) {
+ uri = uri.substr(uriPrefix.length);
+ }
+
+ var callArgs = {
+ 'uri': uri,
+ 'length': length
+ };
+
+ var result = native_.callSync('File_truncate', callArgs);
+ if (native_.isFailure(result)) {
+ errorCallback && errorCallback(ConvErrorCode(native_.getErrorObject(result).code));
+ } else {
+ successCallback && successCallback(length);
+ }
+ }
};
//TODO: remove when added to public cordova repository -> begin