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()) {
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);
392 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
393 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_ALL);
395 throw TypeMismatchException("Wrong DownloadNetworkType.");
397 if (ret != DOWNLOAD_ERROR_NONE) {
398 throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
402 std::map<std::string, std::string>::const_iterator iter;
403 for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
404 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
405 if (ret != DOWNLOAD_ERROR_NONE) {
406 throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
410 ret = download_start(downloadId);
411 if (ret != DOWNLOAD_ERROR_NONE) {
412 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
415 setCallbackToMap(downloadId, downloadCallback);
420 void DownloadManager::cancel(long downloadId)
424 ret = download_cancel(downloadId);
425 if (ret != DOWNLOAD_ERROR_NONE) {
426 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
427 throw NotFoundException("download id could not found.");
428 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
429 throw InvalidValuesException("download id is not valid.");
431 throw UnknownException(("Platform error while canceling download. " + _get_download_error(ret)).c_str());
435 void DownloadManager::pause(long downloadId)
439 ret = download_pause(downloadId);
440 if (ret != DOWNLOAD_ERROR_NONE) {
441 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
442 throw NotFoundException("download id could not found.");
443 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
444 throw InvalidValuesException("download id is not valid.");
446 throw UnknownException(("Platform error while pausing download. " + _get_download_error(ret)).c_str());
450 void DownloadManager::resume(long downloadId)
454 ret = download_start(downloadId);
455 if (ret != DOWNLOAD_ERROR_NONE) {
456 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
457 throw NotFoundException("download id could not found.");
458 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
459 throw InvalidValuesException("download id is not valid.");
461 throw UnknownException(("Platform error while resuming download. " + _get_download_error(ret)).c_str());
465 std::string DownloadManager::getState(long downloadId)
468 download_state_e state;
471 ret = download_get_state(downloadId, &state);
472 if (ret != DOWNLOAD_ERROR_NONE) {
473 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
474 throw NotFoundException("download id could not found.");
475 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
476 throw InvalidValuesException("download id is not valid.");
478 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
482 case DOWNLOAD_STATE_READY:
483 case DOWNLOAD_STATE_QUEUED:
484 result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
486 case DOWNLOAD_STATE_DOWNLOADING:
487 result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
489 case DOWNLOAD_STATE_PAUSED:
490 result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
492 case DOWNLOAD_STATE_COMPLETED:
493 result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
495 case DOWNLOAD_STATE_FAILED:
496 result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
498 case DOWNLOAD_STATE_CANCELED:
499 result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
502 result = "undefined";
503 LOGW("Unknown DownloadState was returned.");
510 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
516 char *destination = NULL;
517 char *fileName = NULL;
518 download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
519 char **fieldNames = NULL;
520 char *fieldValue = NULL;
523 ret = download_get_url(downloadId, &url);
524 if (ret != DOWNLOAD_ERROR_NONE) {
525 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
526 throw NotFoundException("download id could not found.");
527 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
528 throw InvalidValuesException("download id is not valid.");
530 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
533 ret = download_get_destination(downloadId, &destination);
534 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
535 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
536 throw NotFoundException("download id could not found.");
537 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
538 throw InvalidValuesException("download id is not valid.");
540 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
543 ret = download_get_file_name(downloadId, &fileName);
544 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
545 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
546 throw NotFoundException("download id could not found.");
547 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
548 throw InvalidValuesException("download id is not valid.");
550 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
553 ret = download_get_network_type(downloadId, &networkTypeValue);
554 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
555 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
556 throw NotFoundException("download id could not found.");
557 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
558 throw InvalidValuesException("download id is not valid.");
560 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
563 ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
564 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
565 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
566 throw NotFoundException("download id could not found.");
567 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
568 throw InvalidValuesException("download id is not valid.");
570 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
573 std::map<std::string, std::string> httpHeader;
574 for (i = 0; i < fieldLength; i++) {
575 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
576 if (ret != DOWNLOAD_ERROR_NONE) {
577 LOGW("Platform error while getting http header field. %s", _get_download_error(ret).c_str());
579 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
585 DownloadRequest *request = new DownloadRequest();
588 request->setUrl(url);
593 std::string virtualPath;
595 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
597 LOGW("Platform error while converting destination path.");
598 virtualPath = destination;
600 request->setDestination(virtualPath);
605 request->setFileName(fileName);
609 switch(networkTypeValue) {
610 case DOWNLOAD_NETWORK_DATA_NETWORK:
611 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
613 case DOWNLOAD_NETWORK_WIFI:
614 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
617 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
622 request->setHttpHeader(httpHeader);
629 std::string DownloadManager::getMIMEType(long downloadId)
632 char *mimeType = NULL;
633 std::string result("");
635 ret = download_get_mime_type(downloadId, &mimeType);
636 if (ret != DOWNLOAD_ERROR_NONE) {
637 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
638 throw NotFoundException("download id could not found.");
639 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
640 throw InvalidValuesException("download id is not valid.");
641 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
644 throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
657 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
661 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
662 if (ret != DOWNLOAD_ERROR_NONE) {
663 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
664 throw NotFoundException("download id could not found.");
665 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
666 throw InvalidValuesException("download id is not valid.");
668 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
671 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
672 if (ret != DOWNLOAD_ERROR_NONE) {
673 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
674 throw NotFoundException("download id could not found.");
675 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
676 throw InvalidValuesException("download id is not valid.");
678 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
681 setCallbackToMap(downloadId, downloadCallback);