static std::map<int, DatacontrolInformation*> IdMap;
+ReplyCallbackData::~ReplyCallbackData() {
+ ScopeLogger();
+ if (SQL == event_type) {
+ if (handle && DATA_CONTROL_ERROR_NONE != data_control_sql_destroy(handle)) {
+ LoggerE("Destroy SQL handle failed");
+ }
+ } else {
+ if (handle && DATA_CONTROL_ERROR_NONE != data_control_map_destroy(handle)) {
+ LoggerE("Destroy MAP handle failed");
+ }
+ }
+}
+
DatacontrolInstance::DatacontrolInstance() {
ScopeLogger();
using std::placeholders::_1;
DatacontrolInstance::~DatacontrolInstance() {
ScopeLogger();
+ for (auto& item : reply_map) {
+ int watch_id = item.first;
+ auto handle = item.second->handle;
+ LoggerD("Deleting callback number %d", item.first);
+ int result = ::data_control_remove_data_change_cb(handle, watch_id);
+ if (DATA_CONTROL_ERROR_NONE != result) {
+ LoggerE("RemoveChangeListener %d failed: %d (%s)", watch_id, result,
+ get_error_message(result));
+ }
+ }
+ reply_map.clear();
}
static void ReplyAsync(DatacontrolInstance* instance, int callbackId, bool isSuccess,
int callbackId = static_cast<int>(args.get("callbackId").get<double>());
int result = DATA_CONTROL_ERROR_NONE;
data_control_h handle = nullptr;
- std::function<int(data_control_h)> del;
if (SQL == type) {
result = CreateSQLHandle(providerId, dataId, &handle);
- del = data_control_sql_destroy;
} else {
result = CreateMAPHandle(providerId, dataId, &handle);
- del = data_control_map_destroy;
}
- auto deleter = [&del](data_control_h handle) {
- if (handle && DATA_CONTROL_ERROR_NONE != del(handle)) {
- LoggerE("Destroy handle failed");
- }
- };
-
- std::unique_ptr<std::remove_pointer<data_control_h>::type, decltype(deleter)> handle_ptr(handle,
- deleter);
-
- std::shared_ptr<ReplyCallbackData> user_data(new ReplyCallbackData());
- user_data->_instance = this;
- user_data->callbackId = callbackId;
- user_data->event_type = type;
-
if (DATA_CONTROL_ERROR_NONE != result) {
LogAndReportError(ServiceNotAvailableException("AddChangeListener failed"), out,
("AddChangeListener failed: %d (%s)", result, get_error_message(result)));
return;
}
+ std::shared_ptr<ReplyCallbackData> user_data(new ReplyCallbackData());
+ user_data->_instance = this;
+ user_data->callbackId = callbackId;
+ user_data->event_type = type;
+ user_data->handle = handle;
+
int watch_id = 0;
result = ::data_control_add_data_change_cb(handle, callback, user_data.get(), result_callback,
user_data.get(), &watch_id);
CHECK_PRIVILEGE_ACCESS(kPrivilegeDatasharing, &out);
CHECK_PRIVILEGE_ACCESS(kPrivilegeAppmanagerLaunch, &out);
- CHECK_EXIST(args, "providerId", out)
- CHECK_EXIST(args, "dataId", out)
CHECK_EXIST(args, "watchId", out)
- CHECK_EXIST(args, "type", out)
- const std::string& providerId = args.get("providerId").get<std::string>();
- const std::string& dataId = args.get("dataId").get<std::string>();
- const std::string& type = args.get("type").get<std::string>();
int watch_id = static_cast<int>(args.get("watchId").get<double>());
- data_control_h handle = nullptr;
- std::function<int(data_control_h)> del;
- int result = DATA_CONTROL_ERROR_NONE;
-
- if (SQL == type) {
- result = CreateSQLHandle(providerId, dataId, &handle);
- del = data_control_sql_destroy;
- } else {
- result = CreateMAPHandle(providerId, dataId, &handle);
- del = data_control_map_destroy;
- }
-
- if (DATA_CONTROL_ERROR_NONE != result) {
- // According to native documentation only IOError can be returned to webapi, other errors are
- // handled earlier
+ if (reply_map.end() == reply_map.find(watch_id)) {
LogAndReportError(IOException("RemoveChangeListener failed"), out,
- ("RemoveChangeListener failed: %d (%s)", result, get_error_message(result)));
+ ("RemoveChangeListener failed, watch_id: %d", watch_id));
return;
}
+ data_control_h handle = reply_map[watch_id]->handle;
- auto deleter = [&del](data_control_h handle) {
- if (handle && DATA_CONTROL_ERROR_NONE != del(handle)) {
- LoggerE("Destroy handle failed");
- }
- };
-
- std::unique_ptr<std::remove_pointer<data_control_h>::type, decltype(deleter)> handle_ptr(handle,
- deleter);
-
- result = ::data_control_remove_data_change_cb(handle, watch_id);
+ int result = ::data_control_remove_data_change_cb(handle, watch_id);
if (DATA_CONTROL_ERROR_NONE != result) {
// According to native documentation only IOError can be returned to webapi, other errors are