+++ /dev/null
-/*
- * Copyright (c) 2019 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.
- */
-
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <glib.h>
-
-#include "aul.h"
-#include "aul_api.h"
-#include "aul_util.h"
-#include "aul_watch_control.h"
-#include "aul_watch_control_internal.h"
-
-struct aul_watch_control_s {
- aul_watch_control_cb callback;
- void *user_data;
-};
-
-static GList *__controls;
-
-API int aul_watch_control_add_handler(aul_watch_control_cb callback,
- void *user_data, aul_watch_control_h *handle)
-{
- struct aul_watch_control_s *control;
-
- if (!callback || !handle) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- control = calloc(1, sizeof(struct aul_watch_control_s));
- if (!control) {
- _E("Out of memory");
- return AUL_R_ENOMEM;
- }
-
- control->callback = callback;
- control->user_data = user_data;
-
- __controls = g_list_append(__controls, control);
-
- *handle = control;
-
- return AUL_R_OK;
-}
-
-API int aul_watch_control_remove_handler(aul_watch_control_h handle)
-{
- if (!handle || !g_list_find(__controls, handle)) {
- _E("Invalid parameter");
- return AUL_R_EINVAL;
- }
-
- __controls = g_list_remove(__controls, handle);
- free(handle);
-
- return AUL_R_OK;
-}
-
-static void __foreach_control_cb(gpointer data, gpointer user_data)
-{
- struct aul_watch_control_s *control;
- bundle *b = (bundle *)user_data;
-
- control = (struct aul_watch_control_s *)data;
- control->callback(b, control->user_data);
-}
-
-void aul_watch_control_invoke(bundle *b)
-{
- if (!__controls)
- return;
-
- g_list_foreach(__controls, __foreach_control_cb, b);
-}
--- /dev/null
+/*
+ * Copyright (c) 2019 - 2022 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.
+ */
+
+#include "include/aul_watch_control.h"
+
+#include <algorithm>
+#include <list>
+#include <iterator>
+
+#include "aul_api.h"
+#include "aul_util.h"
+#include "aul_watch_control_internal.h"
+#include "include/aul.h"
+
+namespace {
+
+class WatchControl {
+ public:
+ WatchControl(aul_watch_control_cb cb, void* user_data)
+ : cb_(cb), user_data_(user_data) {
+ }
+
+ void Invoke(bundle* b) {
+ if (cb_)
+ cb_(b, user_data_);
+ }
+
+ private:
+ aul_watch_control_cb cb_;
+ void* user_data_;
+};
+
+std::list<WatchControl*> controls;
+
+} // namespace
+
+extern "C" API int aul_watch_control_add_handler(aul_watch_control_cb callback,
+ void* user_data, aul_watch_control_h* handle) {
+ if (callback == nullptr || handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* control = new (std::nothrow) WatchControl(callback, user_data);
+ if (control == nullptr) {
+ _E("Out of memory");
+ return AUL_R_ENOMEM;
+ }
+
+ controls.push_back(control);
+ *handle = static_cast<aul_watch_control_h>(control);
+ return AUL_R_OK;
+}
+
+extern "C" API int aul_watch_control_remove_handler(
+ aul_watch_control_h handle) {
+ if (handle == nullptr) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ auto* control = static_cast<WatchControl*>(handle);
+ auto found = std::find(controls.begin(), controls.end(), control);
+ if (found == controls.end()) {
+ _E("Invalid parameter");
+ return AUL_R_EINVAL;
+ }
+
+ controls.erase(found);
+ delete control;
+ return AUL_R_OK;
+}
+
+void aul_watch_control_invoke(bundle* b) {
+ if (controls.empty())
+ return;
+
+ for (auto control : controls)
+ control->Invoke(b);
+}