#include <gio/gio.h>
#include <string>
+#include <utility>
#include "theme/dbus/request_broker.h"
#include "theme/utils/logging.h"
namespace ttm {
namespace dbus {
+static tizen_base::Bundle ErrorResultBundle() {
+ tizen_base::Bundle b;
+ b.Add(CMD_RESULT_KEY, "error");
+ return b;
+}
+
void RequestBroker::OnReceiveDbusMethod(GDBusConnection* connection,
const gchar* sender, const gchar* object_path, const gchar* interface_name,
const gchar* method_name, GVariant* parameters,
}
tizen_base::Bundle result = it->second.GetHandler()->OnRequest(cmd, b);
- reply_body =
- g_variant_new("(is)", Command::RESULT, result.ToRaw().first.get());
+ reply_body = g_variant_new("(s)", result.ToRaw().first.get());
g_dbus_method_invocation_return_value(invocation, reply_body);
}
" <method name='SendData'>"
" <arg type='i' name='command' direction='in'/>"
" <arg type='s' name='serialized' direction='in'/>"
- " <arg type='i' name='command' direction='out'/>"
" <arg type='s' name='serialized' direction='out'/>"
" </method>"
" </interface>"
GError* error = nullptr;
GDBusNodeInfo* introspection_data =
g_dbus_node_info_new_for_xml(introspection_xml, &error);
- if (!introspection_data) {
+ if (introspection_data == nullptr) {
LOG(ERROR) << "g_dbus_node_info_new_for_xml is failed";
if (error != nullptr) {
LOG(ERROR) << "g_dbus_node_info_new_for_xml err ["
int owner_id = g_bus_own_name_on_connection(connection_,
kDbusBusName, G_BUS_NAME_OWNER_FLAGS_NONE,
nullptr, nullptr, nullptr, nullptr);
- if (!owner_id) {
+ if (owner_id == 0) {
g_object_unref(connection_);
LOG(ERROR) << "g_bus_own_name_on_connection error";
return false;
GError* err = nullptr;
GDBusMessage* msg = g_dbus_message_new_method_call(kDbusInterfaceName,
kDbusObjectPath, kDbusInterfaceName, kDBusMethodSendData);
- if (!msg) {
+ if (msg == nullptr) {
LOG(ERROR) << "Can't allocate new method call";
- return {};
+ return ErrorResultBundle();
}
-
g_dbus_message_set_body(msg,
g_variant_new("(is)",
static_cast<int>(cmd),
GDBusMessage* reply = g_dbus_connection_send_message_with_reply_sync(
connection_, msg, G_DBUS_SEND_MESSAGE_FLAGS_NONE,
-1, nullptr, nullptr, &err);
+ if (reply == nullptr) {
+ LOG(ERROR) << "g_dbus_connection_send_message_with_reply_sync() is failed";
+ if (err) {
+ LOG(ERROR) << "error[" << err->message << "]";
+ g_error_free(err);
+ }
+ return ErrorResultBundle();
+ }
GVariant* reply_body = g_dbus_message_get_body(reply);
- int command;
+ if (reply_body == nullptr) {
+ LOG(ERROR) << "g_dbus_message_get_body() is failed";
+ return ErrorResultBundle();
+ }
+
char* serialized = nullptr;
- g_variant_get(reply_body, "(i&s)", &command, &serialized);
+ g_variant_get(reply_body, "(&s)", &serialized);
tizen_base::Bundle result(serialized);
-
return result;
}
void RequestBroker::OnResultReceivedCb(GObject* source_object,
GAsyncResult* res, gpointer user_data) {
+ auto param = static_cast<std::pair<Command, RequestBroker*>*>(user_data);
GDBusConnection* conn = reinterpret_cast<GDBusConnection*>(source_object);
GError* err = nullptr;
GDBusMessage* reply = g_dbus_connection_send_message_with_reply_finish(conn,
res, &err);
- if (!reply) {
+ if (reply == nullptr) {
LOG(ERROR) << "No reply . err[" << (err ? err->message : "Unknown") << "]";
return;
}
- int command;
char* serialized = nullptr;
GVariant* reply_body = g_dbus_message_get_body(reply);
- g_variant_get(reply_body, "(i&s)", &command, &serialized);
- RequestBroker* broker = static_cast<RequestBroker*>(user_data);
- Command cmd = static_cast<Command>(command);
+ g_variant_get(reply_body, "(&s)", &serialized);
+ RequestBroker* broker = param->second;
tizen_base::Bundle b(serialized);
g_object_unref(reply);
- auto it = broker->filters_.find(cmd);
+ auto it = broker->filters_.find(param->first);
if (it == broker->filters_.end())
return;
- it->second.GetHandler()->OnRequest(cmd, b);
+ it->second.GetHandler()->OnRequest(Command::RESULT, b);
}
void RequestBroker::SendDataAsync(Command cmd, tizen_base::Bundle& data) {
GDBusMessage* msg = g_dbus_message_new_method_call(
kDbusBusName, kDbusObjectPath, kDbusInterfaceName, kDBusMethodSendData);
- if (!msg) {
+ if (msg == nullptr) {
LOG(ERROR) << "Can't allocate new method call";
return;
}
g_dbus_message_set_body(msg, g_variant_new("(is)", static_cast<int>(cmd),
reinterpret_cast<char*>(data.ToRaw().first.get())));
+ std::pair<Command, RequestBroker*> param = std::make_pair(cmd, this);
+
g_dbus_connection_send_message_with_reply(connection_, msg,
G_DBUS_SEND_MESSAGE_FLAGS_NONE, -1, nullptr,
- nullptr, OnResultReceivedCb, this);
+ nullptr, OnResultReceivedCb, ¶m);
}
} // namespace dbus
namespace ttm {
namespace dbus {
+#define CMD_GET_IDS_KEY "__GET_IDS__"
+#define CMD_SET_ID_KEY "__SET_ID__"
+#define CMD_GET_KEY "__GET__"
+#define CMD_RESULT_KEY "__RESULT__"
+#define CMD_ADD_KEY "__ADD__"
+#define CMD_UPDATE_KEY "__UPDATE__"
+#define CMD_REMOVE_KEY "__REMOVE__"
+#define CMD_CHANGED_KEY "__CHANGED__"
+#define CMD_REMOVED_KEY "__REMOVED__"
+
enum class Command : int {
GET_IDS,
SET_ID,
const tizen_base::Bundle& args) {
switch (cmd) {
case dbus::Command::CHANGED: {
- const ThemeInfo* info = new ThemeInfo(args);
+ tizen_base::Bundle b(args.GetString(CMD_CHANGED_KEY));
+ const ThemeInfo* info = new ThemeInfo(b);
for (auto& listener : listeners_) {
std::shared_ptr<IThemeEvent> ev = listener.second;
ev->OnThemeLoaded(info, args);
break;
}
case dbus::Command::REMOVED: {
- std::string id = args.GetString("__ID__");
+ std::string id = args.GetString(CMD_REMOVE_KEY);
for (auto& listener : listeners_) {
std::shared_ptr<IThemeEvent> ev = listener.second;
ev->OnThemeUnloaded(id);
std::vector<std::string> ThemeInfoLoader::QueryThemeId() {
tizen_base::Bundle b;
- std::string val("get_ids");
- b.Add("__QUERY__", val.c_str());
-
tizen_base::Bundle reply =
dbus::RequestBroker::GetInst().SendData(dbus::Command::GET_IDS, b);
+ if (reply.GetString(CMD_RESULT_KEY) == "error")
+ return {};
- return reply.GetStringArray(val);
+ return reply.GetStringArray(CMD_GET_IDS_KEY);
}
void ThemeInfoLoader::SetCurrent(const std::string& id) {
tizen_base::Bundle b;
- b.Add("__SET_CURRENT__", id);
+ b.Add(CMD_SET_ID_KEY, id);
tizen_base::Bundle reply =
dbus::RequestBroker::GetInst().SendData(dbus::Command::SET_ID, b);
return cache_;
tizen_base::Bundle b;
- b.Add("__LOAD__", "__CURRENT__");
+ b.Add(CMD_GET_KEY, "");
tizen_base::Bundle reply =
dbus::RequestBroker::GetInst().SendData(dbus::Command::GET, b);
+ std::string str = reply.GetString(CMD_GET_KEY);
+ tizen_base::Bundle serialized(str);
- cache_ = std::make_shared<ThemeInfo>(reply);
+ cache_ = std::make_shared<ThemeInfo>(serialized);
return cache_;
}
return cache_;
tizen_base::Bundle b;
- b.Add("__LOAD__", id);
+ b.Add(CMD_GET_KEY, id);
tizen_base::Bundle reply =
dbus::RequestBroker::GetInst().SendData(dbus::Command::GET, b);
+ tizen_base::Bundle serialized(
+ reply.GetString(CMD_GET_KEY));
- std::shared_ptr<ThemeInfo> info = std::make_shared<ThemeInfo>(reply);
+ std::shared_ptr<ThemeInfo> info = std::make_shared<ThemeInfo>(serialized);
return info;
}
tizen_base::Bundle b;
if (cmd != dbus::Command::SET_ID) {
// this temporary return value should be fixed.
- b.Add("result", "error");
+ b.Add(CMD_RESULT_KEY, "error");
return b;
}
- bool r = proxy_->SetCurrentTheme(args.GetString("id"));
- b.Add("result", r ? "ok" : "error");
+ bool r = proxy_->SetCurrentTheme(args.GetString(CMD_SET_ID_KEY));
+ b.Add(CMD_RESULT_KEY, r ? "ok" : "error");
return b;
}
const tizen_base::Bundle& args) {
bool result = false;
switch (cmd) {
- case dbus::Command::ADD:
- result = InstallTheme(args);
+ case dbus::Command::ADD: {
+ tizen_base::Bundle b(args.GetString(CMD_ADD_KEY));
+ result = InstallTheme(b);
break;
- case dbus::Command::REMOVE:
- result = UninstallTheme(args);
+ }
+ case dbus::Command::REMOVE: {
+ result = UninstallTheme(args.GetString(CMD_REMOVE_KEY));
break;
- case dbus::Command::UPDATE:
- result = UpdateTheme(args);
+ }
+ case dbus::Command::UPDATE: {
+ tizen_base::Bundle b(args.GetString(CMD_UPDATE_KEY));
+ result = UpdateTheme(b);
break;
- default:
+ }
+ default: {
LOG(ERROR) << "Invalid command type";
break;
+ }
}
tizen_base::Bundle b;
// this temporary return value should be fixed.
- if (result)
- b.Add("result", "ok");
- else
- b.Add("result", "error");
+ b.Add(CMD_RESULT_KEY, result ? "ok" : "error");
return b;
}
}
bool PackageEventRequestHandler::UninstallTheme(
- const tizen_base::Bundle& args) {
- return proxy_->RemoveTheme(args.GetString("id"));
+ const std::string& id) {
+ return proxy_->RemoveTheme(id);
}
} // namespace provider
private:
bool InstallTheme(const tizen_base::Bundle& args);
bool UpdateTheme(const tizen_base::Bundle& args);
- bool UninstallTheme(const tizen_base::Bundle& args);
+ bool UninstallTheme(const std::string& id);
std::shared_ptr<ThemeInfoProxy> proxy_;
};
tizen_base::Bundle b;
switch (cmd) {
case dbus::Command::GET: {
- std::string id = args.GetString("id");
+ std::string id = args.GetString(CMD_GET_KEY);
std::shared_ptr<loader::ThemeInfo> themeinfo = proxy_->LoadTheme(id);
- return themeinfo->Serialize();
+ if (themeinfo == nullptr) {
+ b.Add(CMD_RESULT_KEY, "error");
+ return b;
+ }
+ b.Add(CMD_RESULT_KEY, "ok");
+ b.Add(CMD_GET_KEY,
+ reinterpret_cast<char*>(themeinfo->Serialize().ToRaw().first.get()));
+ return b;
}
case dbus::Command::GET_IDS: {
std::vector<std::string> ids = proxy_->GetThemeIds();
- b.Add("ids", ids);
+ b.Add(CMD_RESULT_KEY, "ok");
+ b.Add(CMD_GET_IDS_KEY, ids);
return b;
}
default: {
// this temporary return value should be fixed.
- b.Add("result", "error");
+ b.Add(CMD_RESULT_KEY, "error");
return b;
}
}
+++ /dev/null
-/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
- *
- * 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 "unit_tests/mock/glib_mock.h"
-
-#include <cstdarg>
-#include <glib.h>
-
-#include "unit_tests/mock/mock_hook.h"
-#include "unit_tests/mock/test_fixture.h"
-
-extern "C" void g_variant_get(GVariant* value, const gchar* format_string,
- ...) {
- va_list ap;
- va_start(ap, format_string);
- int* ptr0 = va_arg(ap, int*);
- if (!ptr0) {
- va_end(ap);
- return;
- }
- char** ptr1 = va_arg(ap, char**);
- if (!ptr1) {
- va_end(ap);
- return;
- }
- MOCK_HOOK_P4(GlibMock, g_variant_get, value, format_string, ptr0, ptr1);
- va_end(ap);
-}
+++ /dev/null
-/*
- * 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 UNIT_TESTS_MOCK_GLIB_MOCK_H_
-#define UNIT_TESTS_MOCK_GLIB_MOCK_H_
-
-#include <glib.h>
-#include <gmock/gmock.h>
-
-#include "mock/module_mock.h"
-
-class GlibMock : public virtual ModuleMock {
- public:
- virtual ~GlibMock() {}
- MOCK_METHOD4(g_variant_get, void(GVariant*, const gchar*, int*, char**));
-};
-
-#endif // UNIT_TESTS_MOCK_GLIB_MOCK_H_
proxy_->SaveTheme(info);
tizen_base::Bundle b;
- b.Add("id", "testid");
+ b.Add(CMD_SET_ID_KEY, "testid");
handler.OnRequest(ttm::dbus::Command::SET_ID, b);
auto cur = proxy_->GetLoadedTheme();
PackageEventRequestHandler handler(proxy_);
ThemeInfo info(b_);
- tizen_base::Bundle b = info.Serialize();
+ tizen_base::Bundle b;
+ b.Add(CMD_ADD_KEY,
+ reinterpret_cast<char*>(info.Serialize().ToRaw().first.get()));
handler.OnRequest(ttm::dbus::Command::ADD, b);
auto theme = proxy_->LoadTheme("testid");
ThemeInfo info2(v2);
b = info2.Serialize();
+ b.Add(CMD_UPDATE_KEY,
+ reinterpret_cast<char*>(info2.Serialize().ToRaw().first.get()));
handler.OnRequest(ttm::dbus::Command::UPDATE, b);
theme = proxy_->LoadTheme("testid");
EXPECT_EQ(theme->GetId(), "testid");
EXPECT_EQ(theme->GetVersion(), "2.0");
+ b.Add(CMD_REMOVE_KEY, "testid");
handler.OnRequest(ttm::dbus::Command::REMOVE, b);
theme = proxy_->LoadTheme("testid");
EXPECT_EQ(theme, nullptr);
proxy_->SaveTheme(info2);
tizen_base::Bundle b;
- b.Add("id", "testid");
+ b.Add(CMD_GET_KEY, "testid");
auto rb = handler.OnRequest(ttm::dbus::Command::GET, b);
- ThemeInfo theme(rb);
+ tizen_base::Bundle result(rb.GetString(CMD_GET_KEY));
+ ThemeInfo theme(result);
EXPECT_EQ(theme.GetId(), "testid");
rb = handler.OnRequest(ttm::dbus::Command::GET_IDS, {});
- std::vector<std::string> ids = rb.GetStringArray("ids");
+ std::vector<std::string> ids = rb.GetStringArray(CMD_GET_IDS_KEY);
EXPECT_EQ(ids.size(), 2);
}
#include "theme/loader/theme_info.h"
#include "theme/loader/theme_info_loader.h"
#include "unit_tests/mock/gio_mock.h"
-#include "unit_tests/mock/glib_mock.h"
#include "unit_tests/mock/test_fixture.h"
using namespace ttm::loader;
}
};
-class Mocks : public ::testing::NiceMock<GioMock>,
- public ::testing::NiceMock<GlibMock> {};
+class Mocks : public ::testing::NiceMock<GioMock> {};
class ThemeInfoLoaderTest : public TestFixture {
public:
TEST_F(ThemeInfoLoaderTest, QueryThemeId_N) {
EXPECT_CALL(GetMock<GioMock>(), g_dbus_message_new_method_call(_, _, _, _)).
WillOnce(Return(reinterpret_cast<GDBusMessage*>(1)));
+ GDBusMessage* msg =
+ reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
+ WillOnce(Return(msg));
tizen_base::Bundle b;
std::vector<std::string> ids_vec;
- b.Add("ids", ids_vec);
+ b.Add(CMD_RESULT_KEY, "ok");
+ b.Add(CMD_GET_IDS_KEY, ids_vec);
auto br = b.ToRaw();
char* b_raw = reinterpret_cast<char*>(br.first.get());
- EXPECT_CALL(GetMock<GlibMock>(), g_variant_get(_, _, _, _)).
- WillOnce(SetArgPointee<3>(b_raw));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_get_body(_)).WillOnce(
+ Return(reinterpret_cast<GVariant*>(g_variant_new("(s)", b_raw))));
std::vector<std::string> ids = loader_->QueryThemeId();
EXPECT_EQ(0, ids.size());
}
TEST_F(ThemeInfoLoaderTest, LoadCurrent) {
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_new_method_call(_, _, _, _)).
+ WillOnce(Return(reinterpret_cast<GDBusMessage*>(1)));
+ GDBusMessage* msg =
+ reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
+ WillOnce(Return(msg));
+ tizen_base::Bundle b;
+ ThemeInfo ti;
+ b.Add(CMD_RESULT_KEY, "ok");
+ b.Add(CMD_GET_KEY,
+ reinterpret_cast<char*>(ti.Serialize().ToRaw().first.get()));
+ auto br = b.ToRaw();
+ char* b_raw = reinterpret_cast<char*>(br.first.get());
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_get_body(_)).WillOnce(
+ Return(reinterpret_cast<GVariant*>(g_variant_new("(s)", b_raw))));
+
std::shared_ptr<ThemeInfo> info = loader_->LoadCurrent();
EXPECT_NE(nullptr, info);
}
EXPECT_CALL(GetMock<GioMock>(),
g_dbus_message_new_method_call(StrEq("org.tizen.ThemeManager"),
StrEq("/org/tizen/ThemeManager"), StrEq("org.tizen.ThemeManager"),
- StrEq("SendData"))).Times(1);
+ StrEq("SendData"))).
+ WillOnce(Return(reinterpret_cast<GDBusMessage*>(1)));
+ GDBusMessage* msg =
+ reinterpret_cast<GDBusMessage*>(g_object_new(G_TYPE_OBJECT, NULL));
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_connection_send_message_with_reply_sync(_, _, _, _, _, _, _)).
+ WillOnce(Return(msg));
+ tizen_base::Bundle b;
+ ThemeInfo ti;
+ b.Add(CMD_RESULT_KEY, "ok");
+ b.Add(CMD_GET_KEY,
+ reinterpret_cast<char*>(ti.Serialize().ToRaw().first.get()));
+ auto br = b.ToRaw();
+ char* b_raw = reinterpret_cast<char*>(br.first.get());
+ EXPECT_CALL(GetMock<GioMock>(),
+ g_dbus_message_get_body(_)).WillOnce(
+ Return(reinterpret_cast<GVariant*>(g_variant_new("(s)", b_raw))));
std::shared_ptr<ThemeInfo> info = loader_->Load(std::string("id"));
EXPECT_NE(nullptr, info);