namespace extension {
namespace download {
+std::vector<DownloadInstance*> DownloadInstance::instances_;
+std::mutex DownloadInstance::instances_mutex_;
+
namespace {
// The privileges that required in Download API
const std::string kPrivilegeDownload = "http://tizen.org/privilege/download";
REGISTER_SYNC("DownloadManager_resume", DownloadManagerResume);
REGISTER_SYNC("DownloadManager_getState", DownloadManagerGetstate);
#undef REGISTER_SYNC
+
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ instances_.push_back(this);
}
DownloadInstance::~DownloadInstance() {
it != downCbVector.end(); it++) {
delete (*it);
}
+
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ for (auto it = instances_.begin(); it != instances_.end(); it++) {
+ if (*it == this) {
+ instances_.erase(it);
+ break;
+ }
+ }
+}
+
+bool DownloadInstance::CheckInstance(DownloadInstance* instance) {
+ LoggerD("Entered");
+ for (auto vec_instance : instances_) {
+ if (vec_instance == instance) {
+ return true;
+ }
+ }
+
+ return false;
}
#define CHECK_EXIST(args, name, out) \
gboolean DownloadInstance::OnProgressChanged(void* user_data) {
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return FALSE;
+ }
+
DownloadInfoPtr diPtr = downCbPtr->instance->diMap[downCbPtr->callbackId];
picojson::value::object out;
int ret;
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return;
+ }
SLoggerD("OnStart for callbackId %d Called", downCbPtr->callbackId);
char* fullPath = NULL;
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return FALSE;
+ }
+
DownloadInfoPtr diPtr = downCbPtr->instance->diMap[downCbPtr->callbackId];
SLoggerD("OnFinished for callbackID %d Called", downCbPtr->callbackId);
gboolean DownloadInstance::OnPaused(void* user_data) {
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return FALSE;
+ }
+
DownloadInfoPtr diPtr = downCbPtr->instance->diMap[downCbPtr->callbackId];
SLoggerD("OnPaused for callbackID %d Called", downCbPtr->callbackId);
gboolean DownloadInstance::OnCanceled(void* user_data) {
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return FALSE;
+ }
+
DownloadInfoPtr diPtr = downCbPtr->instance->diMap[downCbPtr->callbackId];
SLoggerD("OnCanceled for callbackID %d Called", downCbPtr->callbackId);
picojson::object out;
DownloadCallback* downCbPtr = static_cast<DownloadCallback*>(user_data);
+ std::lock_guard<std::mutex> lock(instances_mutex_);
+ if (!CheckInstance(downCbPtr->instance)) {
+ return FALSE;
+ }
+
DownloadInstance* instance = downCbPtr->instance;
SLoggerD("OnFailed for callbackID %d Called", downCbPtr->callbackId);
#include <map>
#include <string>
#include <vector>
+#include <mutex>
#include "common/extension.h"
static gboolean OnPaused(void* user_data);
static gboolean OnCanceled(void* user_data);
static gboolean OnFailed(void* user_data);
+ static bool CheckInstance(DownloadInstance* instance);
struct DownloadInfo {
int callbackId;
typedef std::shared_ptr<DownloadInfo> DownloadInfoPtr;
typedef std::map<int, DownloadInfoPtr> DownloadInfoMap;
+ static std::mutex instances_mutex_;
+ static std::vector<DownloadInstance*> instances_;
+
DownloadCallbackVector downCbVector;
DownloadInfoMap diMap;
};