From 23f4f77ecfd5d9d749ca5795a1e52464a3967ecf Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Mon, 27 Apr 2015 15:11:37 +0200 Subject: [PATCH] [WebSetting] Fixed error handling, code cleanup. Change-Id: I6bf70e9c508d8ba643dd99dfd6d20898fa8cd737 --- src/websetting/websetting.cc | 68 +++++++++++---------- src/websetting/websetting.gyp | 1 - src/websetting/websetting.h | 7 +-- src/websetting/websetting_extension_utils.h | 48 --------------- src/websetting/websetting_instance.cc | 19 ++++-- 5 files changed, 55 insertions(+), 88 deletions(-) delete mode 100644 src/websetting/websetting_extension_utils.h diff --git a/src/websetting/websetting.cc b/src/websetting/websetting.cc index 2b3ccdc6..71cfd334 100644 --- a/src/websetting/websetting.cc +++ b/src/websetting/websetting.cc @@ -8,10 +8,11 @@ #include #include +#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 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 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); } diff --git a/src/websetting/websetting.gyp b/src/websetting/websetting.gyp index b20a8736..5b35fcc5 100644 --- a/src/websetting/websetting.gyp +++ b/src/websetting/websetting.gyp @@ -15,7 +15,6 @@ 'websetting_api.js', 'websetting_extension.cc', 'websetting_extension.h', - 'websetting_extension_utils.h', 'websetting_instance.cc', 'websetting_instance.h', ], diff --git a/src/websetting/websetting.h b/src/websetting/websetting.h index 80102155..73b0b1e5 100644 --- a/src/websetting/websetting.h +++ b/src/websetting/websetting.h @@ -9,7 +9,7 @@ #include #include -#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 RemoveAllCookies(); - std::unique_ptr 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 index 8a359658..00000000 --- a/src/websetting/websetting_extension_utils.h +++ /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 - -#include "common/picojson.h" -#include "common/platform_result.h" -#include "tizen/tizen.h" - -std::unique_ptr CreateResultMessage() { - picojson::object obj; - obj["error"] = picojson::value(); - return std::unique_ptr(new picojson::value(obj)); -} - -std::unique_ptr CreateResultMessage( - const common::ErrorCode& error) { - picojson::object obj; - obj["error"] = picojson::value(static_cast(error)); - return std::unique_ptr(new picojson::value(obj)); -} - -std::unique_ptr CreateResultMessage( - const picojson::object& data) { - picojson::object obj; - obj["data"] = picojson::value(data); - return std::unique_ptr(new picojson::value(obj)); -} - -std::unique_ptr CreateResultMessage( - const picojson::array& data) { - picojson::object obj; - obj["data"] = picojson::value(data); - return std::unique_ptr(new picojson::value(obj)); -} - -std::unique_ptr CreateResultMessage( - const picojson::value& data) { - picojson::object obj; - obj["data"] = data; - return std::unique_ptr(new picojson::value(obj)); -} - -#endif // WEBSETTING_WEBSETTING_EXTENSION_UTILS_H_ diff --git a/src/websetting/websetting_instance.cc b/src/websetting/websetting_instance.cc index 063da262..753d7081 100644 --- a/src/websetting/websetting_instance.cc +++ b/src/websetting/websetting_instance.cc @@ -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(); auto get = [=](const std::shared_ptr& 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()); + if (result) { + ReportSuccess(response->get()); + } else { + ReportError(result, &response->get()); + } }; 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(); auto get = [=](const std::shared_ptr& response) -> void { @@ -92,8 +99,12 @@ void WebSettingInstance::WebSettingManagerRemoveAllCookies( return; } - extension_->current_app()->RemoveAllCookies().release(); - ReportSuccess(response->get()); + const auto& result = extension_->current_app()->RemoveAllCookies(); + if (result) { + ReportSuccess(response->get()); + } else { + ReportError(result, &response->get()); + } }; auto get_response = -- 2.34.1