2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
23 #include <FilesystemUtils.h>
25 #include "DownloadState.h"
26 #include "DownloadNetworkType.h"
27 #include "DownloadManager.h"
32 static std::string _get_download_error(int err)
38 case DOWNLOAD_ERROR_INVALID_PARAMETER:
39 msg = "Invalid parameter";
41 case DOWNLOAD_ERROR_OUT_OF_MEMORY:
42 msg = "Out of memory";
44 case DOWNLOAD_ERROR_NETWORK_UNREACHABLE:
45 msg = "Network is unreachable";
47 case DOWNLOAD_ERROR_CONNECTION_TIMED_OUT:
48 msg = "Http session time-out";
50 case DOWNLOAD_ERROR_NO_SPACE:
51 msg = "No space left on device";
53 case DOWNLOAD_ERROR_FIELD_NOT_FOUND:
54 msg = "Specified field not found";
56 case DOWNLOAD_ERROR_INVALID_STATE:
57 msg = "Invalid state";
59 case DOWNLOAD_ERROR_CONNECTION_FAILED:
60 msg = "Connection failed";
62 case DOWNLOAD_ERROR_INVALID_URL:
65 case DOWNLOAD_ERROR_INVALID_DESTINATION:
66 msg = "Invalid destination";
68 case DOWNLOAD_ERROR_TOO_MANY_DOWNLOADS:
69 msg = "Full of available simultaneous downloads";
71 case DOWNLOAD_ERROR_QUEUE_FULL:
72 msg = "Full of available downloading items from server";
74 case DOWNLOAD_ERROR_ALREADY_COMPLETED:
75 msg = "The download is already completed";
77 case DOWNLOAD_ERROR_FILE_ALREADY_EXISTS:
78 msg = "It is failed to rename the downloaded file";
80 case DOWNLOAD_ERROR_CANNOT_RESUME:
81 msg = "It cannot resume";
83 case DOWNLOAD_ERROR_TOO_MANY_REDIRECTS:
84 msg = "In case of too may redirects from http response header";
86 case DOWNLOAD_ERROR_UNHANDLED_HTTP_CODE:
87 msg = "The download cannot handle the http status value";
89 case DOWNLOAD_ERROR_REQUEST_TIMEOUT:
90 msg = "There are no action after client create a download id";
92 case DOWNLOAD_ERROR_RESPONSE_TIMEOUT:
93 msg = "It does not call start API in some time although the download is created";
95 case DOWNLOAD_ERROR_SYSTEM_DOWN:
96 msg = "There are no response from client after rebooting download daemon";
98 case DOWNLOAD_ERROR_ID_NOT_FOUND:
99 msg = "The download id is not existed in download service module";
101 case DOWNLOAD_ERROR_NO_DATA:
102 msg = "No data because the set API is not called";
104 case DOWNLOAD_ERROR_IO_ERROR:
105 msg = "Internal I/O error";
107 case DOWNLOAD_ERROR_NONE:
111 msg = "Unknown error";
115 LOGE("Platform error %d <%s>", err, msg.c_str());
123 download_state_e state;
124 unsigned long long received;
126 } DOWNLOAD_EVENT_DATA_T;
128 static gboolean downloadEventCB(void *data) {
131 DOWNLOAD_EVENT_DATA_T *fnData = static_cast<DOWNLOAD_EVENT_DATA_T*>(data);
134 long downloadId = (long)fnData->downloadId;
136 DownloadManager *thisObj = (DownloadManager*)fnData->user_data;
138 throw UnknownException("UserData is NULL.");
141 DownloadCallback *callback = thisObj->getCallbackFromMap(downloadId);
143 throw UnknownException("Callback could not found.");
146 switch(fnData->state) {
147 case DOWNLOAD_STATE_QUEUED:
149 callback->onprogress(downloadId, 0, 0);
152 case DOWNLOAD_STATE_PAUSED:
154 callback->onpaused(downloadId);
157 case DOWNLOAD_STATE_DOWNLOADING:
159 unsigned long long totalSize = 0;
160 ret = download_get_content_size(downloadId, &totalSize);
161 if (ret != DOWNLOAD_ERROR_NONE) {
162 throw UnknownException(("Platform error while getting total file size. " + _get_download_error(ret)).c_str());
165 callback->onprogress(downloadId, fnData->received, totalSize);
168 case DOWNLOAD_STATE_COMPLETED:
170 ret = download_unset_state_changed_cb(downloadId);
171 if (ret != DOWNLOAD_ERROR_NONE) {
172 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
175 ret = download_unset_progress_cb(downloadId);
176 if (ret != DOWNLOAD_ERROR_NONE) {
177 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
180 char *fullPath = NULL;
181 ret = download_get_downloaded_file_path(downloadId, &fullPath);
182 if (ret != DOWNLOAD_ERROR_NONE || !fullPath) {
183 throw UnknownException(("Platform error while getting downloaded full path. " + _get_download_error(ret)).c_str());
186 ret = download_destroy(downloadId);
187 if (ret != DOWNLOAD_ERROR_NONE) {
188 LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
191 std::string virtualPath;
193 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, fullPath);
195 LOGW("Platform error while converting fullPath.");
196 virtualPath = fullPath;
199 callback->oncompleted(downloadId, virtualPath);
202 thisObj->removeCallbackFromMap(downloadId);
205 case DOWNLOAD_STATE_FAILED:
207 ret = download_unset_state_changed_cb(downloadId);
208 if (ret != DOWNLOAD_ERROR_NONE) {
209 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
212 ret = download_unset_progress_cb(downloadId);
213 if (ret != DOWNLOAD_ERROR_NONE) {
214 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
217 int err = DOWNLOAD_ERROR_NONE;
218 std::string errMessage;
219 ret = download_get_error(downloadId, (download_error_e*)&err);
220 if (ret != DOWNLOAD_ERROR_NONE) {
221 LOGW("Platform error while getting download error. ");
223 errMessage = _get_download_error(err);
226 ret = download_destroy(downloadId);
227 if (ret != DOWNLOAD_ERROR_NONE) {
228 LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
231 UnknownException error(errMessage.c_str());
232 callback->onfailed(downloadId, error);
233 thisObj->removeCallbackFromMap(downloadId);
236 case DOWNLOAD_STATE_CANCELED:
238 ret = download_unset_state_changed_cb(downloadId);
239 if (ret != DOWNLOAD_ERROR_NONE) {
240 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
243 ret = download_unset_progress_cb(downloadId);
244 if (ret != DOWNLOAD_ERROR_NONE) {
245 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
248 ret = download_destroy(downloadId);
249 if (ret != DOWNLOAD_ERROR_NONE) {
250 LOGW("Platform error while destroying download handle. downloadId=%d", downloadId);
253 callback->oncanceled(downloadId);
254 thisObj->removeCallbackFromMap(downloadId);
258 LOGW("State changed is ignored.");
261 } catch (const BasePlatformException &err) {
262 LOGE("download_state_changed_cb: %s", err.getMessage().c_str());
269 static void download_state_changed_cb(int downloadId, download_state_e state, void *user_data)
271 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
272 data->downloadId = downloadId;
275 data->user_data = user_data;
277 // download core f/w calls this callback function in another thread.
278 // so we should use g_idle_add() to switch context to main thread.
279 g_idle_add(downloadEventCB, static_cast<void*>(data));
282 static void download_progress_cb(int downloadId, unsigned long long received, void *user_data)
284 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
285 data->downloadId = downloadId;
286 data->state = DOWNLOAD_STATE_DOWNLOADING;
287 data->received = received;
288 data->user_data = user_data;
290 // download core f/w calls this callback function in another thread.
291 // so we should use g_idle_add() to switch context to main thread.
292 g_idle_add(downloadEventCB, static_cast<void*>(data));
295 DownloadManager::DownloadManager()
299 DownloadManager::~DownloadManager()
303 void DownloadManager::setCallbackToMap(long downloadId, DownloadCallback *callback)
305 DownloadCallback *value = mDownloadCallbacks[downloadId];
309 mDownloadCallbacks[downloadId] = callback;
312 DownloadCallback* DownloadManager::getCallbackFromMap(long downloadId)
314 return mDownloadCallbacks[downloadId];
317 void DownloadManager::removeCallbackFromMap(long downloadId) {
318 DownloadCallback *value = mDownloadCallbacks[downloadId];
319 mDownloadCallbacks.erase(downloadId);
325 long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback)
331 throw TypeMismatchException("request is NULL.");
334 std::string url = request->getUrl();
335 std::string destination = request->getDestination();
336 std::string fileName = request->getFileName();
337 std::string networkType = request->getNetworkType();
338 std::map<std::string, std::string> httpHeader = request->getHttpHeader();
341 throw InvalidValuesException("Invalid DownloadRequest.url.");
344 ret = download_create(&downloadId);
345 if (ret != DOWNLOAD_ERROR_NONE) {
346 throw UnknownException(("Platform error while creating download. " + _get_download_error(ret)).c_str());
349 ret = download_set_url(downloadId, url.c_str());
350 if (ret != DOWNLOAD_ERROR_NONE) {
351 throw UnknownException(("Platform error while setting url. " + _get_download_error(ret)).c_str());
354 if (!destination.empty()) {
355 std::string fullPath;
357 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(NULL, destination);
358 fullPath = path->getFullPath();
360 LOGW("Converting virtual path is failed. [%s]", destination.c_str());
361 fullPath = destination;
363 ret = download_set_destination(downloadId, fullPath.c_str());
364 if (ret != DOWNLOAD_ERROR_NONE) {
365 throw UnknownException(("Platform error while setting destination. " + _get_download_error(ret)).c_str());
369 if (!fileName.empty()) {
370 ret = download_set_file_name(downloadId, fileName.c_str());
371 if (ret != DOWNLOAD_ERROR_NONE) {
372 throw UnknownException(("Platform error while setting fileName. " + _get_download_error(ret)).c_str());
376 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
377 if (ret != DOWNLOAD_ERROR_NONE) {
378 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
381 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
382 if (ret != DOWNLOAD_ERROR_NONE) {
383 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
386 if (!networkType.empty() && networkType != TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
387 ret = DOWNLOAD_ERROR_NONE;
388 if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR) {
389 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_DATA_NETWORK);
390 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI) {
391 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_WIFI);
393 throw TypeMismatchException("Wrong DownloadNetworkType.");
395 if (ret != DOWNLOAD_ERROR_NONE) {
396 throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
400 std::map<std::string, std::string>::const_iterator iter;
401 for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
402 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
403 if (ret != DOWNLOAD_ERROR_NONE) {
404 throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
408 ret = download_start(downloadId);
409 if (ret != DOWNLOAD_ERROR_NONE) {
410 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
413 setCallbackToMap(downloadId, downloadCallback);
418 void DownloadManager::cancel(long downloadId)
422 ret = download_cancel(downloadId);
423 if (ret != DOWNLOAD_ERROR_NONE) {
424 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
425 throw NotFoundException("download id could not found.");
426 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
427 throw InvalidValuesException("download id is not valid.");
429 throw UnknownException(("Platform error while canceling download. " + _get_download_error(ret)).c_str());
433 void DownloadManager::pause(long downloadId)
437 ret = download_pause(downloadId);
438 if (ret != DOWNLOAD_ERROR_NONE) {
439 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
440 throw NotFoundException("download id could not found.");
441 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
442 throw InvalidValuesException("download id is not valid.");
444 throw UnknownException(("Platform error while pausing download. " + _get_download_error(ret)).c_str());
448 void DownloadManager::resume(long downloadId)
452 ret = download_start(downloadId);
453 if (ret != DOWNLOAD_ERROR_NONE) {
454 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
455 throw NotFoundException("download id could not found.");
456 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
457 throw InvalidValuesException("download id is not valid.");
459 throw UnknownException(("Platform error while resuming download. " + _get_download_error(ret)).c_str());
463 std::string DownloadManager::getState(long downloadId)
466 download_state_e state;
469 ret = download_get_state(downloadId, &state);
470 if (ret != DOWNLOAD_ERROR_NONE) {
471 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
472 throw NotFoundException("download id could not found.");
473 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
474 throw InvalidValuesException("download id is not valid.");
476 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
480 case DOWNLOAD_STATE_READY:
481 case DOWNLOAD_STATE_QUEUED:
482 result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
484 case DOWNLOAD_STATE_DOWNLOADING:
485 result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
487 case DOWNLOAD_STATE_PAUSED:
488 result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
490 case DOWNLOAD_STATE_COMPLETED:
491 result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
493 case DOWNLOAD_STATE_FAILED:
494 result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
496 case DOWNLOAD_STATE_CANCELED:
497 result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
500 result = "undefined";
501 LOGW("Unknown DownloadState was returned.");
508 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
514 char *destination = NULL;
515 char *fileName = NULL;
516 download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
517 char **fieldNames = NULL;
518 char *fieldValue = NULL;
521 ret = download_get_url(downloadId, &url);
522 if (ret != DOWNLOAD_ERROR_NONE) {
523 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
524 throw NotFoundException("download id could not found.");
525 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
526 throw InvalidValuesException("download id is not valid.");
528 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
531 ret = download_get_destination(downloadId, &destination);
532 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
533 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
534 throw NotFoundException("download id could not found.");
535 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
536 throw InvalidValuesException("download id is not valid.");
538 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
541 ret = download_get_file_name(downloadId, &fileName);
542 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
543 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
544 throw NotFoundException("download id could not found.");
545 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
546 throw InvalidValuesException("download id is not valid.");
548 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
551 ret = download_get_network_type(downloadId, &networkTypeValue);
552 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
553 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
554 throw NotFoundException("download id could not found.");
555 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
556 throw InvalidValuesException("download id is not valid.");
558 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
561 ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
562 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
563 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
564 throw NotFoundException("download id could not found.");
565 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
566 throw InvalidValuesException("download id is not valid.");
568 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
571 std::map<std::string, std::string> httpHeader;
572 for (i = 0; i < fieldLength; i++) {
573 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
574 if (ret != DOWNLOAD_ERROR_NONE) {
575 LOGW("Platform error while getting http header field. %s", _get_download_error(ret).c_str());
577 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
583 DownloadRequest *request = new DownloadRequest();
586 request->setUrl(url);
591 std::string virtualPath;
593 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
595 LOGW("Platform error while converting destination path.");
596 virtualPath = destination;
598 request->setDestination(virtualPath);
603 request->setFileName(fileName);
607 switch(networkTypeValue) {
608 case DOWNLOAD_NETWORK_DATA_NETWORK:
609 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
611 case DOWNLOAD_NETWORK_WIFI:
612 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
615 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
620 request->setHttpHeader(httpHeader);
627 std::string DownloadManager::getMIMEType(long downloadId)
630 char *mimeType = NULL;
631 std::string result("");
633 ret = download_get_mime_type(downloadId, &mimeType);
634 if (ret != DOWNLOAD_ERROR_NONE) {
635 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
636 throw NotFoundException("download id could not found.");
637 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
638 throw InvalidValuesException("download id is not valid.");
639 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
642 throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
655 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
659 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
660 if (ret != DOWNLOAD_ERROR_NONE) {
661 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
662 throw NotFoundException("download id could not found.");
663 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
664 throw InvalidValuesException("download id is not valid.");
666 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
669 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
670 if (ret != DOWNLOAD_ERROR_NONE) {
671 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
672 throw NotFoundException("download id could not found.");
673 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
674 throw InvalidValuesException("download id is not valid.");
676 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
679 setCallbackToMap(downloadId, downloadCallback);