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 LogError("Platform error %d <%s>", err, msg.c_str());
123 download_state_e state;
124 unsigned long long received;
126 } DOWNLOAD_EVENT_DATA_T;
129 static gboolean downloadEventCB(void *data) {
132 DOWNLOAD_EVENT_DATA_T *fnData = static_cast<DOWNLOAD_EVENT_DATA_T*>(data);
135 long downloadId = (long)fnData->downloadId;
137 DownloadManager *thisObj = (DownloadManager*)fnData->user_data;
139 throw UnknownException("UserData is NULL.");
142 DownloadCallback *callback = thisObj->getCallbackFromMap(downloadId);
144 throw UnknownException("Callback could not found.");
147 switch(fnData->state) {
148 case DOWNLOAD_STATE_QUEUED:
150 callback->onprogress(downloadId, 0, 0);
153 case DOWNLOAD_STATE_PAUSED:
155 callback->onpaused(downloadId);
158 case DOWNLOAD_STATE_DOWNLOADING:
160 unsigned long long totalSize = 0;
161 ret = download_get_content_size(downloadId, &totalSize);
162 if (ret != DOWNLOAD_ERROR_NONE) {
163 throw UnknownException(("Platform error while getting total file size. " + _get_download_error(ret)).c_str());
166 callback->onprogress(downloadId, fnData->received, totalSize);
169 case DOWNLOAD_STATE_COMPLETED:
171 ret = download_unset_state_changed_cb(downloadId);
172 if (ret != DOWNLOAD_ERROR_NONE) {
173 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
176 ret = download_unset_progress_cb(downloadId);
177 if (ret != DOWNLOAD_ERROR_NONE) {
178 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
181 char *fullPath = NULL;
182 ret = download_get_downloaded_file_path(downloadId, &fullPath);
183 if (ret != DOWNLOAD_ERROR_NONE || !fullPath) {
184 throw UnknownException(("Platform error while getting downloaded full path. " + _get_download_error(ret)).c_str());
187 std::string virtualPath;
189 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, fullPath);
191 LogWarning("Platform error while converting fullPath.");
192 virtualPath = fullPath;
195 callback->oncompleted(downloadId, virtualPath);
198 thisObj->removeCallbackFromMap(downloadId);
201 case DOWNLOAD_STATE_FAILED:
203 ret = download_unset_state_changed_cb(downloadId);
204 if (ret != DOWNLOAD_ERROR_NONE) {
205 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
208 ret = download_unset_progress_cb(downloadId);
209 if (ret != DOWNLOAD_ERROR_NONE) {
210 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
213 int err = DOWNLOAD_ERROR_NONE;
214 std::string errMessage;
215 ret = download_get_error(downloadId, (download_error_e*)&err);
216 if (ret != DOWNLOAD_ERROR_NONE) {
217 LogWarning("Platform error while getting download error. ");
219 errMessage = _get_download_error(err);
222 UnknownException error(errMessage.c_str());
223 callback->onfailed(downloadId, error);
224 thisObj->removeCallbackFromMap(downloadId);
227 case DOWNLOAD_STATE_CANCELED:
229 ret = download_unset_state_changed_cb(downloadId);
230 if (ret != DOWNLOAD_ERROR_NONE) {
231 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
234 ret = download_unset_progress_cb(downloadId);
235 if (ret != DOWNLOAD_ERROR_NONE) {
236 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
239 callback->oncanceled(downloadId);
240 thisObj->removeCallbackFromMap(downloadId);
244 LogWarning("State changed is ignored.");
247 } catch (const BasePlatformException &err) {
248 LogError("download_state_changed_cb: %s", err.getMessage().c_str());
255 static void download_state_changed_cb(int downloadId, download_state_e state, void *user_data)
257 LogDebug("download_state_changed_cb, downloadId=%d, state=%d", downloadId, (int)state);
258 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
259 data->downloadId = downloadId;
262 data->user_data = user_data;
264 // download core f/w calls this callback function in another thread.
265 // so we should use g_idle_add() to switch context to main thread.
266 g_idle_add(downloadEventCB, static_cast<void*>(data));
269 static void download_progress_cb(int downloadId, unsigned long long received, void *user_data)
271 LogDebug("download_progress_cb, downloadId=%d, received=%ld", downloadId, received);
272 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
273 data->downloadId = downloadId;
274 data->state = DOWNLOAD_STATE_DOWNLOADING;
275 data->received = received;
276 data->user_data = user_data;
278 // download core f/w calls this callback function in another thread.
279 // so we should use g_idle_add() to switch context to main thread.
280 g_idle_add(downloadEventCB, static_cast<void*>(data));
283 DownloadManager::DownloadManager()
287 DownloadManager::~DownloadManager()
291 void DownloadManager::setCallbackToMap(long downloadId, DownloadCallback *callback)
293 DownloadCallback *value = mDownloadCallbacks[downloadId];
297 mDownloadCallbacks[downloadId] = callback;
300 DownloadCallback* DownloadManager::getCallbackFromMap(long downloadId)
302 return mDownloadCallbacks[downloadId];
305 void DownloadManager::removeCallbackFromMap(long downloadId) {
306 DownloadCallback *value = mDownloadCallbacks[downloadId];
307 mDownloadCallbacks.erase(downloadId);
313 long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback)
322 throw TypeMismatchException("request is NULL.");
325 std::string url = request->getUrl();
326 std::string destination = request->getDestination();
327 std::string fileName = request->getFileName();
328 std::string networkType = request->getNetworkType();
329 std::map<std::string, std::string> httpHeader = request->getHttpHeader();
331 LogDebug("url <%s>, destination <%s>, fileName <%s>", url.c_str(), destination.c_str(), fileName.c_str());
334 throw InvalidValuesException("Invalid DownloadRequest.url.");
337 ret = download_create(&downloadId);
338 if (ret != DOWNLOAD_ERROR_NONE) {
339 throw UnknownException(("Platform error while creating download. " + _get_download_error(ret)).c_str());
342 ret = download_set_url(downloadId, url.c_str());
343 if (ret != DOWNLOAD_ERROR_NONE) {
344 throw UnknownException(("Platform error while setting url. " + _get_download_error(ret)).c_str());
347 if (!destination.empty()) {
348 std::string fullPath;
350 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(NULL, destination);
351 fullPath = path->getFullPath();
353 LogWarning("Converting virtual path is failed. [%s]", destination.c_str());
354 fullPath = destination;
356 LogDebug("Converted FullPath = <%s>", fullPath.c_str());
357 ret = download_set_destination(downloadId, fullPath.c_str());
358 if (ret != DOWNLOAD_ERROR_NONE) {
359 throw UnknownException(("Platform error while setting destination. " + _get_download_error(ret)).c_str());
363 if (!fileName.empty()) {
364 ret = download_set_file_name(downloadId, fileName.c_str());
365 if (ret != DOWNLOAD_ERROR_NONE) {
366 throw UnknownException(("Platform error while setting fileName. " + _get_download_error(ret)).c_str());
370 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
371 if (ret != DOWNLOAD_ERROR_NONE) {
372 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
375 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
376 if (ret != DOWNLOAD_ERROR_NONE) {
377 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
380 if (!networkType.empty()) {
381 ret = DOWNLOAD_ERROR_NONE;
382 if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR) {
383 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_DATA_NETWORK);
384 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI) {
385 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_WIFI);
386 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
387 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_ALL);
389 throw TypeMismatchException("Wrong DownloadNetworkType.");
391 if (ret != DOWNLOAD_ERROR_NONE) {
392 throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
396 std::map<std::string, std::string>::const_iterator iter;
397 for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
398 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
399 if (ret != DOWNLOAD_ERROR_NONE) {
400 throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
404 ret = download_start(downloadId);
405 if (ret != DOWNLOAD_ERROR_NONE) {
406 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
409 LogDebug("downloadId: %d", downloadId);
411 setCallbackToMap(downloadId, downloadCallback);
416 void DownloadManager::cancel(long downloadId)
420 LogDebug("entered. downloadId = %d", 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 LogDebug("entered. downloadId = %d", 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 LogDebug("entered. downloadId = %d", downloadId);
456 ret = download_start(downloadId);
457 if (ret != DOWNLOAD_ERROR_NONE) {
458 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
459 throw NotFoundException("download id could not found.");
460 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
461 throw InvalidValuesException("download id is not valid.");
463 throw UnknownException(("Platform error while resuming download. " + _get_download_error(ret)).c_str());
467 std::string DownloadManager::getState(long downloadId)
470 download_state_e state;
473 LogDebug("entered. downloadId = %d", downloadId);
475 ret = download_get_state(downloadId, &state);
476 if (ret != DOWNLOAD_ERROR_NONE) {
477 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
478 throw NotFoundException("download id could not found.");
479 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
480 throw InvalidValuesException("download id is not valid.");
482 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
486 case DOWNLOAD_STATE_READY:
487 case DOWNLOAD_STATE_QUEUED:
488 result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
490 case DOWNLOAD_STATE_DOWNLOADING:
491 result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
493 case DOWNLOAD_STATE_PAUSED:
494 result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
496 case DOWNLOAD_STATE_COMPLETED:
497 result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
499 case DOWNLOAD_STATE_FAILED:
500 result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
502 case DOWNLOAD_STATE_CANCELED:
503 result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
506 result = "undefined";
507 LogWarning("Unknown DownloadState was returned.");
514 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
520 char *destination = NULL;
521 char *fileName = NULL;
522 download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
523 char **fieldNames = NULL;
524 char *fieldValue = NULL;
527 LogDebug("entered. downloadId = %d", downloadId);
529 ret = download_get_url(downloadId, &url);
530 if (ret != DOWNLOAD_ERROR_NONE) {
531 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
532 throw NotFoundException("download id could not found.");
533 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
534 throw InvalidValuesException("download id is not valid.");
536 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
539 ret = download_get_destination(downloadId, &destination);
540 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
541 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
542 throw NotFoundException("download id could not found.");
543 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
544 throw InvalidValuesException("download id is not valid.");
546 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
549 ret = download_get_file_name(downloadId, &fileName);
550 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
551 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
552 throw NotFoundException("download id could not found.");
553 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
554 throw InvalidValuesException("download id is not valid.");
556 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
559 ret = download_get_network_type(downloadId, &networkTypeValue);
560 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
561 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
562 throw NotFoundException("download id could not found.");
563 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
564 throw InvalidValuesException("download id is not valid.");
566 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
569 ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
570 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
571 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
572 throw NotFoundException("download id could not found.");
573 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
574 throw InvalidValuesException("download id is not valid.");
576 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
579 std::map<std::string, std::string> httpHeader;
580 for (i = 0; i < fieldLength; i++) {
581 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
582 if (ret != DOWNLOAD_ERROR_NONE) {
583 LogWarning("Platform error while getting http header field. " << _get_download_error(ret));
585 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
591 DownloadRequest *request = new DownloadRequest();
594 request->setUrl(url);
599 std::string virtualPath;
601 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
603 LogWarning("Platform error while converting destination path.");
604 virtualPath = destination;
606 request->setDestination(virtualPath);
611 request->setFileName(fileName);
615 switch(networkTypeValue) {
616 case DOWNLOAD_NETWORK_DATA_NETWORK:
617 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
619 case DOWNLOAD_NETWORK_WIFI:
620 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
623 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
628 request->setHttpHeader(httpHeader);
635 std::string DownloadManager::getMIMEType(long downloadId)
638 char *mimeType = NULL;
639 std::string result("");
641 LogDebug("entered. downloadId = %d", downloadId);
643 ret = download_get_mime_type(downloadId, &mimeType);
644 if (ret != DOWNLOAD_ERROR_NONE) {
645 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
646 throw NotFoundException("download id could not found.");
647 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
648 throw InvalidValuesException("download id is not valid.");
649 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
652 throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
665 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
669 LogDebug("entered. downloadId = %d", downloadId);
671 ret = download_set_state_changed_cb(downloadId, download_state_changed_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 state changed callback. " + _get_download_error(ret)).c_str());
681 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
682 if (ret != DOWNLOAD_ERROR_NONE) {
683 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
684 throw NotFoundException("download id could not found.");
685 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
686 throw InvalidValuesException("download id is not valid.");
688 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
691 setCallbackToMap(downloadId, downloadCallback);