From ad9b13a54da33ce7579c49f76b1994e59fb5311a Mon Sep 17 00:00:00 2001 From: jusung son Date: Wed, 25 Jul 2018 17:11:17 +0900 Subject: [PATCH] Guarantee the integrity of singletons Change-Id: Ia913d29ae9b4b34625135c7a20086a266363459a Signed-off-by: jusung son --- watchface-complication/complication-connector.cc | 42 ++++++++++++------- watchface-complication/complication-connector.h | 2 +- watchface-complication/complication.cc | 14 +++++-- watchface-complication/editables-manager.cc | 53 ++++++++++++++++-------- watchface-complication/editables-manager.h | 2 +- 5 files changed, 75 insertions(+), 38 deletions(-) diff --git a/watchface-complication/complication-connector.cc b/watchface-complication/complication-connector.cc index 0d0ebbf..24e75a0 100644 --- a/watchface-complication/complication-connector.cc +++ b/watchface-complication/complication-connector.cc @@ -42,9 +42,12 @@ namespace watchface_complication { ComplicationConnector& ComplicationConnector::GetInst() { static ComplicationConnector w_inst; - - if (w_inst.impl_ == nullptr) - w_inst.Init(); + int ret; + if (w_inst.impl_ == nullptr) { + ret = w_inst.Init(); + if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) + THROW(ret); + } return w_inst; } @@ -139,7 +142,7 @@ int ComplicationConnector::SubscribeSignal(SigType type, std::string id, path = ComplicationConnector::GetInst().EncodeStr( type == Complication ? CompPath : EditablePath, id, sub_id); - } catch (int e) { + } catch (...) { throw; } @@ -206,39 +209,50 @@ std::string ComplicationConnector::EncodeStr(EncodeType type, std::string appid, return EncodeStr(type, str); } -void ComplicationConnector::Init() { +int ComplicationConnector::Init() { GError *error = NULL; int pid = getpid(); int ret; char appid[MAX_PACKAGE_STR_SIZE] = {0, }; std::string encoded_name; int owner_id; + std::unique_ptr tmp_impl; - impl_ = std::unique_ptr(new Impl()); - impl_->conn_ = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error); - if (impl_->conn_ == NULL) { + try { + tmp_impl = std::unique_ptr(new Impl()); + } catch (const std::bad_alloc &ba) { + LOGE("Out of memory"); + return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; + } + tmp_impl->conn_ = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error); + if (tmp_impl->conn_ == NULL) { if (error != NULL) { LOGE("Failed to get dbus [%s]", error->message); g_error_free(error); } - return; + return WATCHFACE_COMPLICATION_ERROR_IO_ERROR; } ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid)); if (ret != AUL_R_OK) { + g_object_unref(tmp_impl->conn_); LOGE("Fail to get appid"); - return; + return WATCHFACE_COMPLICATION_ERROR_IO_ERROR;; } - impl_->appid_ = std::string(appid); - encoded_name = EncodeStr(ComplicationConnector::Name, impl_->appid_); + tmp_impl->appid_ = std::string(appid); + encoded_name = EncodeStr(ComplicationConnector::Name, tmp_impl->appid_); LOGI("own name %s", encoded_name.c_str()); - owner_id = g_bus_own_name_on_connection(impl_->conn_, encoded_name.c_str(), + owner_id = g_bus_own_name_on_connection(tmp_impl->conn_, encoded_name.c_str(), G_BUS_NAME_OWNER_FLAGS_NONE, NULL, NULL, NULL, NULL); if (!owner_id) { + g_object_unref(tmp_impl->conn_); LOGE("g_bus_own_name_on_connection, error"); - return; + return WATCHFACE_COMPLICATION_ERROR_IO_ERROR; } + + impl_ = std::move(tmp_impl); + return WATCHFACE_COMPLICATION_ERROR_NONE; } std::string ComplicationConnector::GetCmdStr(CmdType type) { diff --git a/watchface-complication/complication-connector.h b/watchface-complication/complication-connector.h index 9af27b9..7f35437 100644 --- a/watchface-complication/complication-connector.h +++ b/watchface-complication/complication-connector.h @@ -71,7 +71,7 @@ class EXPORT_API ComplicationConnector { private: ComplicationConnector(); virtual ~ComplicationConnector(); - void Init(); + int Init(); private: enum EncodeType { diff --git a/watchface-complication/complication.cc b/watchface-complication/complication.cc index dfd2e21..93b91f7 100644 --- a/watchface-complication/complication.cc +++ b/watchface-complication/complication.cc @@ -646,6 +646,7 @@ std::unique_ptr& Complication::GetLastContext() const { int Complication::TouchLaunch() { const char* context_data_raw = NULL; + int ret; std::string provider_appid = DBManager::GetProviderAppId( impl_->cur_provider_id_.c_str()); if (provider_appid.empty()) { @@ -679,10 +680,15 @@ int Complication::TouchLaunch() { LOGE("Fail to encode bundle"); return WATCHFACE_COMPLICATION_ERROR_IO_ERROR; } - int ret = aul_complication_launch_with_extra_data( - ComplicationConnector::GetInst().GetAppId().c_str(), - provider_appid.c_str(), getuid(), TOUCH_LAUNCH_DATA_KEY, - reinterpret_cast(raw_data)); + try { + ret = aul_complication_launch_with_extra_data( + ComplicationConnector::GetInst().GetAppId().c_str(), + provider_appid.c_str(), getuid(), TOUCH_LAUNCH_DATA_KEY, + reinterpret_cast(raw_data)); + } catch (Exception &ex) { + LOGE("%s %d", ex.what(), ex.GetErrorCode()); + ret = ex.GetErrorCode(); + } free(raw_data); LOGI("Touch launch the provider app: %d, %s", ret, provider_appid.c_str()); diff --git a/watchface-complication/editables-manager.cc b/watchface-complication/editables-manager.cc index 17ccf3a..299d374 100644 --- a/watchface-complication/editables-manager.cc +++ b/watchface-complication/editables-manager.cc @@ -36,10 +36,13 @@ namespace watchface_complication { EditablesManager& EditablesManager::GetInst() { static EditablesManager w_inst; + int ret; - if (w_inst.impl_ == nullptr) - w_inst.Init(); - + if (w_inst.impl_ == nullptr) { + ret = w_inst.Init(); + if (ret != WATCHFACE_COMPLICATION_ERROR_NONE) + THROW(ret); + } return w_inst; } @@ -47,15 +50,18 @@ EditablesManager::EditablesManager() = default; EditablesManager::~EditablesManager() = default; EditablesManager::Impl::Impl() = default; -void EditablesManager::Init() { +int EditablesManager::Init() { int ret = SQLITE_OK; int open_flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE); + std::unique_ptr tmp_impl; - impl_ = std::unique_ptr(new Impl()); - if (impl_->setting_db_ != NULL) { - LOGI("setting db already exist"); - return; + try { + tmp_impl = std::unique_ptr(new Impl()); + } catch (const std::bad_alloc &ba) { + LOGE("Out of memory"); + return WATCHFACE_COMPLICATION_ERROR_OUT_OF_MEMORY; } + std::string context_table_command = "CREATE TABLE IF NOT EXISTS editable_context" \ "(editable_id INTEGER NOT NULL, provider_id TEXT NOT NULL, "\ @@ -66,32 +72,43 @@ void EditablesManager::Init() { "CREATE TABLE IF NOT EXISTS editable_setting" \ "(editable_id INTEGER NOT NULL, setting_data TEXT NOT NULL, "\ "PRIMARY KEY(editable_id))"; - std::string db_path = impl_->GetEditablesDataPath(); + std::string db_path = tmp_impl->GetEditablesDataPath(); if (db_path.empty()) { LOGE("GetSettingDataPath failed"); - return; + return WATCHFACE_COMPLICATION_ERROR_DB; } LOGI("db path : %s", db_path.c_str()); - ret = sqlite3_open_v2(db_path.c_str(), &impl_->setting_db_, open_flags, NULL); + ret = sqlite3_open_v2(db_path.c_str(), &tmp_impl->setting_db_, open_flags, NULL); if (ret != SQLITE_OK) { LOGE("database creation failed with error: %d", ret); - return; + return WATCHFACE_COMPLICATION_ERROR_DB; } - ret = sqlite3_exec(impl_->setting_db_, setting_table_command.c_str(), + ret = sqlite3_exec(tmp_impl->setting_db_, setting_table_command.c_str(), NULL, NULL, NULL); - if (ret != SQLITE_OK) + if (ret != SQLITE_OK) { + sqlite3_close_v2(tmp_impl->setting_db_); LOGE("database setting table creation failed: %d", ret); + return WATCHFACE_COMPLICATION_ERROR_DB; + } - ret = sqlite3_open_v2(db_path.c_str(), &impl_->context_db_, open_flags, NULL); + ret = sqlite3_open_v2(db_path.c_str(), &tmp_impl->context_db_, open_flags, NULL); if (ret != SQLITE_OK) { + sqlite3_close_v2(tmp_impl->setting_db_); LOGE("database creation failed with error: %d", ret); - return; + return WATCHFACE_COMPLICATION_ERROR_DB; } - ret = sqlite3_exec(impl_->context_db_, context_table_command.c_str(), + ret = sqlite3_exec(tmp_impl->context_db_, context_table_command.c_str(), NULL, NULL, NULL); - if (ret != SQLITE_OK) + if (ret != SQLITE_OK) { + sqlite3_close_v2(tmp_impl->setting_db_); + sqlite3_close_v2(tmp_impl->context_db_); LOGE("database context table creation failed: %d", ret); + return WATCHFACE_COMPLICATION_ERROR_DB; + } + + impl_ = std::move(tmp_impl); + return WATCHFACE_COMPLICATION_ERROR_NONE; } int EditablesManager::StoreSetting(int editable_id, bundle_raw* raw_data) { diff --git a/watchface-complication/editables-manager.h b/watchface-complication/editables-manager.h index 9135405..da630d3 100644 --- a/watchface-complication/editables-manager.h +++ b/watchface-complication/editables-manager.h @@ -37,7 +37,7 @@ class EXPORT_API EditablesManager { private: EditablesManager(); virtual ~EditablesManager(); - void Init(); + int Init(); private: class Impl; -- 2.7.4