[Metadata] Implementation of handles management 74/237174/5
authorPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Thu, 25 Jun 2020 12:36:21 +0000 (14:36 +0200)
committerPiotr Kosko/Native/Web API (PLT) /SRPOL/Engineer/Samsung Electronics <p.kosko@samsung.com>
Mon, 6 Jul 2020 05:48:03 +0000 (07:48 +0200)
Implementation supports creation a handle for a file, with returned
basic structure of MetadataFileHandle.

[ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-265

[Verifcation] Code compiles without errors.
Checked with Chrome console.

Change-Id: I421afbed3b618e7d376b9a93a8cf484d334a72b2

src/metadata/metadata.gyp
src/metadata/metadata_api.js
src/metadata/metadata_file_handle.cc [new file with mode: 0644]
src/metadata/metadata_file_handle.h [new file with mode: 0644]
src/metadata/metadata_instance.cc
src/metadata/metadata_instance.h

index e46da54..0f43bf8 100644 (file)
@@ -15,6 +15,8 @@
         'metadata_extension.h',
         'metadata_instance.cc',
         'metadata_instance.h',
+        'metadata_file_handle.cc',
+        'metadata_file_handle.h',
       ],
       'includes': [
         '../common/pkg-config.gypi',
index 9d08ad8..5b877c9 100755 (executable)
@@ -24,12 +24,34 @@ var native_ = new xwalk.utils.NativeManager(extension);
 function MetadataManager() {}
 
 MetadataManager.prototype.createFileHandle = function() {
-    // TODO
-    throw new WebAPIException(WebAPIException.NOT_SUPPORTED_ERR, 'Not implemented yet');
+    var args = validator_.validateArgs(arguments, [
+        {
+            name: 'path',
+            type: validator_.Types.STRING,
+            optional: false,
+            nullable: false
+        }
+    ]);
+    try {
+        // get normalized URI of a file
+        args.path = tizen.filesystem.toURI(args.path);
+    } catch (e) {
+        throw new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Path is invalid');
+    }
+
+    var result = native_.callSync('MetadataManagerCreateFileHandle', { uri: args.path });
+    if (native_.isFailure(result)) {
+        throw native_.getErrorObject(result);
+    }
+
+    return new MetadataFileHandle(native_.getResultObject(result));
 };
 
-function MetadataFileHandle() {
-    // TODO
+function MetadataFileHandle(data) {
+    Object.defineProperties(this, {
+        _id: { value: data.id, writable: false, enumerable: false },
+        path: { value: data.path, writable: false, enumerable: true }
+    });
 }
 
 // Exports
diff --git a/src/metadata/metadata_file_handle.cc b/src/metadata/metadata_file_handle.cc
new file mode 100644 (file)
index 0000000..ed7dc30
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright (c) 2020 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 "metadata/metadata_file_handle.h"
+
+#include "common/logger.h"
+#include "common/platform_exception.h"
+
+namespace extension {
+namespace metadata {
+
+using namespace common;
+
+PlatformResult convertErrorCode(int err) {
+  switch (err) {
+    case METADATA_EXTRACTOR_ERROR_FILE_EXISTS:
+      return LogAndCreateResult(ErrorCode::NOT_FOUND_ERR, "File does not exist.");
+    case METADATA_EXTRACTOR_ERROR_PERMISSION_DENIED:
+      return LogAndCreateResult(ErrorCode::SECURITY_ERR, "Permission denied.");
+    case METADATA_EXTRACTOR_ERROR_INVALID_PARAMETER:
+    case METADATA_EXTRACTOR_ERROR_OUT_OF_MEMORY:
+    case METADATA_EXTRACTOR_ERROR_OPERATION_FAILED:
+    default:
+      return LogAndCreateResult(ErrorCode::ABORT_ERR, "Unknown error occurred.");
+  }
+}
+
+MetadataFileHandle::MetadataFileHandle(int id) : handle_(nullptr), id_(id) {
+  ScopeLogger();
+}
+
+// 'this' owns a handle_, and path_ and invalidates 'o'
+MetadataFileHandle::MetadataFileHandle(MetadataFileHandle&& o)
+    : handle_(nullptr), id_(o.id_), path_(std::move(o.path_)) {
+  ScopeLogger();
+  handle_ = o.handle_;
+  o.handle_ = nullptr;
+}
+
+MetadataFileHandle& MetadataFileHandle::operator=(MetadataFileHandle&& o) {
+  ScopeLogger();
+  // 'this' owns a handle_, and path_ and invalidates 'o'
+  handle_ = o.handle_;
+  o.handle_ = nullptr;
+  id_ = o.id_;
+  path_ = std::move(o.path_);
+  return *this;
+}
+
+MetadataFileHandle::~MetadataFileHandle() {
+  ScopeLogger();
+  if (handle_) {
+    int result = metadata_extractor_destroy(handle_);
+    if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+      LoggerW("Error during destruction of handle: %d", result);
+      return;
+    }
+  }
+}
+
+PlatformResult MetadataFileHandle::Initialize(const std::string& path) {
+  ScopeLogger();
+  int result = metadata_extractor_create(&handle_);
+  if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+    LoggerE("Error during creation of metadata extractor handle, object is not valid: %d", result);
+    return convertErrorCode(result);
+  }
+
+  result = metadata_extractor_set_path(handle_, path.c_str());
+  if (METADATA_EXTRACTOR_ERROR_NONE != result) {
+    LoggerE("Error during path set, object is not valid: %d", result);
+    return convertErrorCode(result);
+  }
+  this->path_ = path;
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+picojson::value MetadataFileHandle::ToJSON() {
+  ScopeLogger();
+  picojson::value result = picojson::value(picojson::object());
+  picojson::object& result_obj = result.get<picojson::object>();
+  result_obj.insert(std::make_pair("id", picojson::value((double)id_)));
+  result_obj.insert(std::make_pair("path", picojson::value(path_)));
+
+  return result;
+}
+
+}  // namespace metadata
+}  // namespace extension
diff --git a/src/metadata/metadata_file_handle.h b/src/metadata/metadata_file_handle.h
new file mode 100644 (file)
index 0000000..e6800a9
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2020 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 METADATA_METADATA_FILE_HANDLE_H_
+#define METADATA_METADATA_FILE_HANDLE_H_
+
+#include <metadata_extractor.h>
+#include <string>
+
+#include "common/picojson.h"
+#include "common/platform_result.h"
+
+namespace extension {
+namespace metadata {
+
+class MetadataFileHandle {
+ public:
+  MetadataFileHandle(int id);
+  MetadataFileHandle(MetadataFileHandle&& o);
+  MetadataFileHandle& operator=(MetadataFileHandle&& o);
+  virtual ~MetadataFileHandle();
+
+  picojson::value ToJSON();
+
+  common::PlatformResult Initialize(const std::string& path);
+
+ private:
+  metadata_extractor_h handle_;
+  int id_;
+  std::string path_;
+};
+
+}  // namespace metadata
+}  // namespace extension
+
+#endif  // METADATA_METADATA_FILE_HANDLE_H_
index efae2d1..8734db0 100644 (file)
 #include "common/logger.h"
 #include "common/picojson.h"
 #include "common/platform_exception.h"
-#include "common/tools.h"
 
 namespace extension {
 namespace metadata {
 
 using namespace common;
 
-MetadataInstance::MetadataInstance() {
+const std::string URI_PREFIX = "file://";
+
+std::string ltrim(const std::string& s) {
+  ScopeLogger();
+  std::string str = s;
+  std::string::iterator i;
+  for (i = str.begin(); i != str.end(); ++i) {
+    if (!isspace(*i)) {
+      break;
+    }
+  }
+  str.erase(str.begin(), i);
+  return str;
+}
+
+std::string convertUriToPath(const std::string& str) {
+  ScopeLogger();
+  std::string path = ltrim(str);
+  std::string prefix = path.substr(0, URI_PREFIX.size());
+
+  if (prefix == URI_PREFIX) {
+    return path.substr(URI_PREFIX.size());
+  } else {
+    return path;
+  }
+}
+
+MetadataInstance::MetadataInstance() : next_handle_id_(1) {
   ScopeLogger();
   using std::placeholders::_1;
   using std::placeholders::_2;
@@ -43,10 +69,21 @@ MetadataInstance::~MetadataInstance() {
   ScopeLogger();
 }
 
-void MetadataInstance::MetadataManagerCreateFileHandle(const picojson::value& args, picojson::object& out) {
+void MetadataInstance::MetadataManagerCreateFileHandle(const picojson::value& args,
+                                                       picojson::object& out) {
   ScopeLogger();
+  const std::string& uri = args.get("uri").get<std::string>();
+  auto path = convertUriToPath(uri);
 
-  LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
+  auto m = MetadataFileHandle{next_handle_id_};
+  auto res = m.Initialize(path);
+  if (!res) {
+    LogAndReportError(res, &out);
+    return;
+  }
+
+  ReportSuccess(m.ToJSON(), out);
+  handles_map_.insert(std::pair<int, MetadataFileHandle>(next_handle_id_++, std::move(m)));
 }
 
 void MetadataInstance::MetadataFileHandleGet(const picojson::value& args, picojson::object& out) {
@@ -55,7 +92,8 @@ void MetadataInstance::MetadataFileHandleGet(const picojson::value& args, picojs
   LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
 }
 
-void MetadataInstance::MetadataFileHandleRelease(const picojson::value& args, picojson::object& out) {
+void MetadataInstance::MetadataFileHandleRelease(const picojson::value& args,
+                                                 picojson::object& out) {
   ScopeLogger();
 
   LogAndReportError(PlatformResult(ErrorCode::NOT_SUPPORTED_ERR, "Not implemented yet"), &out);
index 26d0613..694dbc1 100644 (file)
@@ -18,6 +18,7 @@
 #define METADATA_METADATA_INSTANCE_H_
 
 #include "common/extension.h"
+#include "metadata/metadata_file_handle.h"
 
 namespace extension {
 namespace metadata {
@@ -32,6 +33,8 @@ class MetadataInstance : public common::ParsedInstance {
   void MetadataFileHandleGet(const picojson::value& args, picojson::object& out);
   void MetadataFileHandleRelease(const picojson::value& args, picojson::object& out);
 
+  std::map<int, MetadataFileHandle> handles_map_;
+  int next_handle_id_;
 };
 
 }  // namespace metadata