[Download] Fix preventing crash. Prevent call callback twice 78/168378/1
authorLukasz Bardeli <l.bardeli@samsung.com>
Thu, 25 Jan 2018 10:53:43 +0000 (11:53 +0100)
committerLukasz Bardeli <l.bardeli@samsung.com>
Fri, 26 Jan 2018 06:01:41 +0000 (07:01 +0100)
Add condition to prevent call some callback twice and modify body of methods
OnFinished, OnCanceled and OnFailed to prevent crash.

[Verification] Code compiles without error. TCT passrate 100%

Change-Id: I7d194b4d762a52f0281e22555bfd54caa3cb4579
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
src/download/download_instance.cc

index 5a1dd70..73bd411 100644 (file)
@@ -187,6 +187,14 @@ void DownloadInstance::OnStateChanged(int download_id, download_state_e state, v
   ScopeLogger();
   CallbackPtr downCbPtr = static_cast<CallbackPtr>(user_data);
 
+  // Prevent to call finished, cancelled or failed function more than once
+  if (DOWNLOAD_STATE_COMPLETED == downCbPtr->state ||
+      DOWNLOAD_STATE_CANCELED == downCbPtr->state ||
+      DOWNLOAD_STATE_FAILED == downCbPtr->state) {
+    LoggerD("Already finished job, not calling callback for %d state", downCbPtr->state);
+    return;
+  }
+
   downCbPtr->state = state;
   downCbPtr->downloadId = download_id;
 
@@ -314,9 +322,8 @@ gboolean DownloadInstance::OnFinished(void* user_data) {
   out["fullPath"] = picojson::value(common::FilesystemProvider::Create().GetVirtualPath(fullPath));
 
   Instance::PostMessage(downCbPtr->instance, picojson::value(out).serialize().c_str());
-  downCbPtr->instance->download_callbacks.erase(callback_id);
-  delete (downCbPtr);
-
+  // downCbPtr is freed in destructor, it prevent from crash if OnFinished state
+  // was called after OnCanceled or OnFailed
   free(fullPath);
 
   return FALSE;
@@ -384,9 +391,8 @@ gboolean DownloadInstance::OnCanceled(void* user_data) {
   out["callbackId"] = picojson::value(static_cast<double>(callback_id));
 
   Instance::PostMessage(downCbPtr->instance, picojson::value(out).serialize().c_str());
-  downCbPtr->instance->download_callbacks.erase(callback_id);
-  delete (downCbPtr);
-
+  // downCbPtr is freed in destructor, it prevent from crash if OnFinished state
+  // was called after OnFinished or OnFailed
   return FALSE;
 }
 
@@ -443,9 +449,8 @@ gboolean DownloadInstance::OnFailed(void* user_data) {
   out["callbackId"] = picojson::value(static_cast<double>(downCbPtr->callbackId));
 
   Instance::PostMessage(downCbPtr->instance, picojson::value(out).serialize().c_str());
-  downCbPtr->instance->download_callbacks.erase(callback_id);
-  delete (downCbPtr);
-
+  // downCbPtr is freed in destructor, it prevent from crash if OnFinished state
+  // was called after OnFinished or OnCanceled
   return FALSE;
 }