[WebSetting] Fixed error handling, code cleanup.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 27 Apr 2015 13:11:37 +0000 (15:11 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 28 Apr 2015 10:17:23 +0000 (19:17 +0900)
Change-Id: I6bf70e9c508d8ba643dd99dfd6d20898fa8cd737

src/websetting/websetting.cc
src/websetting/websetting.gyp
src/websetting/websetting.h
src/websetting/websetting_extension_utils.h [deleted file]
src/websetting/websetting_instance.cc

index 2b3ccdc63a4c5bfdfaeb7b893aaa41b8ce261c90..71cfd33470a0fdb8d1517e6465e8f7c7c97d1227 100644 (file)
@@ -8,10 +8,11 @@
 #include <unistd.h>
 #include <utility>
 
+#include "common/logger.h"
 #include "common/platform_result.h"
-#include "websetting/websetting_extension_utils.h"
 
 using common::ErrorCode;
+using common::PlatformResult;
 
 namespace {
 
@@ -22,14 +23,15 @@ const char kRuntimeRunningAppInterface[] =
 
 // The runtime process exports object for each running app on the session bus.
 GDBusProxy* CreateRunningAppProxy(const std::string& app_id) {
-  GError* error = NULL;
+  LoggerD("Entered");
+
+  GError* error = nullptr;
   GDBusConnection* connection =
-      g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
+      g_bus_get_sync(G_BUS_TYPE_SESSION, nullptr, &error);
   if (!connection) {
-    std::cerr << "Couldn't get the session bus connection: " << error->message
-              << std::endl;
+    LoggerE("Couldn't get the session bus connection: %s", error->message);
     g_error_free(error);
-    return NULL;
+    return nullptr;
   }
 
   std::string path = std::string(kRuntimeRunningManagerPath) + "/" + app_id;
@@ -39,13 +41,13 @@ GDBusProxy* CreateRunningAppProxy(const std::string& app_id) {
   // And that is why the substantiation is needed here.
   std::replace(path.begin(), path.end(), '.', '_');
   GDBusProxy* proxy = g_dbus_proxy_new_sync(
-      connection, G_DBUS_PROXY_FLAGS_NONE, NULL, kRuntimeServiceName,
-      path.c_str(), kRuntimeRunningAppInterface, NULL, &error);
+      connection, G_DBUS_PROXY_FLAGS_NONE, nullptr, kRuntimeServiceName,
+      path.c_str(), kRuntimeRunningAppInterface, nullptr, &error);
   if (!proxy) {
-    std::cerr << "Couldn't create proxy for " << kRuntimeRunningAppInterface
-              << ": " << error->message << std::endl;
+    LoggerE("Couldn't create proxy for %s: %s", kRuntimeRunningAppInterface,
+            error->message);
     g_error_free(error);
-    return NULL;
+    return nullptr;
   }
 
   return proxy;
@@ -54,46 +56,50 @@ GDBusProxy* CreateRunningAppProxy(const std::string& app_id) {
 }  // namespace
 
 WebSetting::WebSetting(const std::string& app_id)
-    : app_id_(app_id), running_app_proxy_(NULL) {}
+    : app_id_(app_id), running_app_proxy_(nullptr) {}
 
 WebSetting::~WebSetting() {
   if (running_app_proxy_) g_object_unref(running_app_proxy_);
 }
 
-std::unique_ptr<picojson::value> WebSetting::RemoveAllCookies() {
+common::PlatformResult WebSetting::RemoveAllCookies() {
+  LoggerD("Entered");
   if (!running_app_proxy_) {
-    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
-      return CreateResultMessage(ErrorCode::UNKNOWN_ERR);
+    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_))) {
+      LoggerE("Failed to create proxy");
+      return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unable to remove cookies.");
+    }
   }
-  GError* error = NULL;
+  GError* error = nullptr;
   GVariant* result =
-      g_dbus_proxy_call_sync(running_app_proxy_, "RemoveAllCookies", NULL,
-                             G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+      g_dbus_proxy_call_sync(running_app_proxy_, "RemoveAllCookies", nullptr,
+                             G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
   if (!result) {
-    std::cerr << "Fail to call 'RemoveuserAgentAllCookies':" << error->message
-              << std::endl;
+    LoggerE("Failed to call 'RemoveuserAgentAllCookies': %s", error->message);
     g_error_free(error);
-    return CreateResultMessage(ErrorCode::UNKNOWN_ERR);
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unable to remove cookies.");
   }
-  return CreateResultMessage();
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
 
-std::unique_ptr<picojson::value> WebSetting::SetUserAgentString(
+common::PlatformResult WebSetting::SetUserAgentString(
     const std::string& user_agent) {
+  LoggerD("Entered");
   if (!running_app_proxy_) {
-    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_)))
-      return CreateResultMessage(ErrorCode::UNKNOWN_ERR);
+    if (!(running_app_proxy_ = CreateRunningAppProxy(app_id_))) {
+      LoggerE("Failed to create proxy");
+      return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unable to set user agent.");
+    }
   }
-  GError* error = NULL;
+  GError* error = nullptr;
   GVariant* result =
       g_dbus_proxy_call_sync(running_app_proxy_, "SetUserAgentString",
                              g_variant_new("(s)", user_agent.c_str()),
-                             G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
+                             G_DBUS_CALL_FLAGS_NONE, -1, nullptr, &error);
   if (!result) {
-    std::cerr << "Fail to call 'SetUserAgentString':" << error->message
-              << std::endl;
+    LoggerE("Fail to call 'SetUserAgentString': %s", error->message);
     g_error_free(error);
-    return CreateResultMessage(ErrorCode::UNKNOWN_ERR);
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Unable to set user agent.");
   }
-  return CreateResultMessage();
+  return PlatformResult(ErrorCode::NO_ERROR);
 }
index b20a873663237ab6a702714ab5b87ccb79a4de56..5b35fcc509d627060fe607590a640538a9796294 100644 (file)
@@ -15,7 +15,6 @@
         'websetting_api.js',
         'websetting_extension.cc',
         'websetting_extension.h',
-        'websetting_extension_utils.h',
         'websetting_instance.cc',
         'websetting_instance.h',
       ],
index 80102155e3b64edf4fa719078e954ff234ea212c..73b0b1e5b1a92c7c78b6cc1e25f55219fc236b26 100644 (file)
@@ -9,7 +9,7 @@
 #include <memory>
 #include <string>
 
-#include "common/picojson.h"
+#include "common/platform_result.h"
 
 class WebSetting {
  public:
@@ -18,9 +18,8 @@ class WebSetting {
 
   std::string app_id() const { return app_id_; }
 
-  std::unique_ptr<picojson::value> RemoveAllCookies();
-  std::unique_ptr<picojson::value> SetUserAgentString(
-      const std::string& user_agent);
+  common::PlatformResult RemoveAllCookies();
+  common::PlatformResult SetUserAgentString(const std::string& user_agent);
 
  private:
   std::string app_id_;
diff --git a/src/websetting/websetting_extension_utils.h b/src/websetting/websetting_extension_utils.h
deleted file mode 100644 (file)
index 8a35965..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright (c) 2014 Samsung Electronics Co., Ltd. All Rights Reserved
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
-#define WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
-
-#include <memory>
-
-#include "common/picojson.h"
-#include "common/platform_result.h"
-#include "tizen/tizen.h"
-
-std::unique_ptr<picojson::value> CreateResultMessage() {
-  picojson::object obj;
-  obj["error"] = picojson::value();
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const common::ErrorCode& error) {
-  picojson::object obj;
-  obj["error"] = picojson::value(static_cast<double>(error));
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::object& data) {
-  picojson::object obj;
-  obj["data"] = picojson::value(data);
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::array& data) {
-  picojson::object obj;
-  obj["data"] = picojson::value(data);
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-std::unique_ptr<picojson::value> CreateResultMessage(
-    const picojson::value& data) {
-  picojson::object obj;
-  obj["data"] = data;
-  return std::unique_ptr<picojson::value>(new picojson::value(obj));
-}
-
-#endif  // WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_
index 063da26261a52830e938b9ab63fba3bff4622eb5..753d70810afcf87b53cd0bc9b3dbc8228134d460 100644 (file)
@@ -46,6 +46,8 @@ WebSettingInstance::~WebSettingInstance() {}
 
 void WebSettingInstance::WebSettingManagerSetUserAgentString(
     const picojson::value& args, picojson::object& out) {
+  LoggerD("Entered");
+
   const double callback_id = args.get("callbackId").get<double>();
   auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
     const char* runtime_name =
@@ -59,9 +61,13 @@ void WebSettingInstance::WebSettingManagerSetUserAgentString(
     }
 
     std::string userAgent = args.get("userAgentStr").to_str();
-    extension_->current_app()->SetUserAgentString(userAgent).release();
+    const auto& result = extension_->current_app()->SetUserAgentString(userAgent);
 
-    ReportSuccess(response->get<picojson::object>());
+    if (result) {
+      ReportSuccess(response->get<picojson::object>());
+    } else {
+      ReportError(result, &response->get<picojson::object>());
+    }
   };
 
   auto get_response =
@@ -79,6 +85,7 @@ void WebSettingInstance::WebSettingManagerSetUserAgentString(
 
 void WebSettingInstance::WebSettingManagerRemoveAllCookies(
     const picojson::value& args, picojson::object& out) {
+  LoggerD("Entered");
 
   const double callback_id = args.get("callbackId").get<double>();
   auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
@@ -92,8 +99,12 @@ void WebSettingInstance::WebSettingManagerRemoveAllCookies(
       return;
     }
 
-    extension_->current_app()->RemoveAllCookies().release();
-    ReportSuccess(response->get<picojson::object>());
+    const auto& result = extension_->current_app()->RemoveAllCookies();
+    if (result) {
+      ReportSuccess(response->get<picojson::object>());
+    } else {
+      ReportError(result, &response->get<picojson::object>());
+    }
   };
 
   auto get_response =