[KeyManager] Implementation of Data.save().
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 1 Jul 2015 10:47:12 +0000 (12:47 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Wed, 1 Jul 2015 11:21:11 +0000 (13:21 +0200)
[Verification] Code compiles.

Change-Id: I93c9bf9f98ef288240f82139af32a67d6cc21e86
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/keymanager/keymanager_instance.cc

index c9741e5..9d9b26a 100755 (executable)
@@ -658,6 +658,65 @@ void KeyManagerInstance::LoadCertificateFromFile(const picojson::value& args,
 void KeyManagerInstance::SaveData(const picojson::value& args,
                                   picojson::object& out) {
   LoggerD("Enter");
+
+  RawBuffer* raw_buffer = new RawBuffer(std::move(Base64ToRawBuffer(args.get("rawData").get<std::string>())));
+  const auto& data = args.get("data");
+  const auto& alias = data.get("name").get<std::string>();
+  const auto& password_value = data.get("password");
+  const auto extractable = data.get("extractable").get<bool>();
+  double callback_id = args.get("callbackId").get<double>();
+
+  std::string password;
+
+  if (password_value.is<std::string>()) {
+    password = password_value.get<std::string>();
+  }
+
+  auto save_data = [raw_buffer, password, extractable, alias](const std::shared_ptr<picojson::value>& result) {
+    LoggerD("Enter save_data");
+
+    ckmc_raw_buffer_s raw_data { const_cast<unsigned char*>(&(*raw_buffer)[0]), raw_buffer->size() };
+    ckmc_policy_s policy { const_cast<char*>(password.c_str()), extractable };
+
+    int ret = ckmc_save_data(alias.c_str(), raw_data, policy);
+
+    PlatformResult success(ErrorCode::NO_ERROR);
+
+    switch (ret) {
+      case CKMC_ERROR_NONE:
+        break;
+
+      case CKMC_ERROR_INVALID_PARAMETER:
+        success = PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Failed to save data");
+        break;
+
+      default:
+        success = PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to save data");
+        break;
+    }
+
+    if (success) {
+      common::tools::ReportSuccess(result->get<picojson::object>());
+    } else {
+      LoggerE("Failed to save data: %d", ret);
+      common::tools::ReportError(success, &result->get<picojson::object>());
+    }
+
+    delete raw_buffer;
+  };
+
+  auto save_data_result = [this, callback_id](const std::shared_ptr<picojson::value>& result) {
+    LoggerD("Enter save_data_result");
+    result->get<picojson::object>()["callbackId"] = picojson::value{callback_id};
+    this->PostMessage(result->serialize().c_str());
+  };
+
+  TaskQueue::GetInstance().Queue<picojson::value>(
+      save_data,
+      save_data_result,
+      std::shared_ptr<picojson::value>{new picojson::value{picojson::object()}});
+
+  ReportSuccess(out);
 }
 
 void KeyManagerInstance::GetData(const picojson::value& args,