[File] Added FileWriter implementation.
authorTomasz Marciniak <t.marciniak@samsung.com>
Tue, 3 Nov 2015 13:26:29 +0000 (14:26 +0100)
committerTomasz Marciniak <t.marciniak@samsung.com>
Thu, 5 Nov 2015 07:58:19 +0000 (08:58 +0100)
[Verification] Code compiles without errors.

Change-Id: I9e5f11b4e0a32ff045e2aa0e7384ae80a83d1e55
Signed-off-by: Tomasz Marciniak <t.marciniak@samsung.com>
src/file/cordova_file.gyp
src/file/cordova_file_extension.cc
src/file/cordova_file_extension.h
src/file/cordova_file_instance.cc [new file with mode: 0644]
src/file/cordova_file_instance.h [new file with mode: 0644]
src/file/js/FileWriter.js

index fd9b7d77fa41c8a65b034b5f0419fdef11bc7186..709906b2261d7e8d5bc10a79c66f007e15c0df9b 100644 (file)
@@ -10,6 +10,8 @@
         'cordova_file_api.js',
         'cordova_file_extension.cc',
         'cordova_file_extension.h',
+        'cordova_file_instance.cc',
+        'cordova_file_instance.h',
       ],
       'include_dirs': [
         '../',
index 69eb837d35a2523e133ca4f66b41690ca2139ed5..02236b879a14dda4ede5edd3c94d4ef285976f05 100755 (executable)
@@ -15,6 +15,7 @@
  */
 
 #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[];
@@ -34,6 +35,11 @@ CordovaFileExtension::CordovaFileExtension() {
 
 CordovaFileExtension::~CordovaFileExtension() {}
 
+common::Instance* CordovaFileExtension::CreateInstance() {
+  LoggerD("Entered");
+  return new CordovaFileInstance();
+}
+
 }  // file
 }  // cordova
 }  // extension
index 0c3bc12949061da7f2a32cfd33adaef8ef1ae1f3..bbf4707b5930e621cf36f6351632b78fbb1e18dd 100755 (executable)
@@ -27,6 +27,10 @@ class CordovaFileExtension : public common::Extension {
  public:
   CordovaFileExtension();
   virtual ~CordovaFileExtension();
+
+ private:
+  // common::Extension implementation.
+  virtual common::Instance* CreateInstance();
 };
 
 }  // file
diff --git a/src/file/cordova_file_instance.cc b/src/file/cordova_file_instance.cc
new file mode 100644 (file)
index 0000000..53ec8a5
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * 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
diff --git a/src/file/cordova_file_instance.h b/src/file/cordova_file_instance.h
new file mode 100644 (file)
index 0000000..e85b6a1
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * 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_
index 7782fef3b9bcd494ccdd131356b44dfb119bea27..91b42223c4c1dd56e9512435aa1ef6ac0090cd8a 100644 (file)
 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