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 ret = download_destroy(downloadId);
188 if (ret != DOWNLOAD_ERROR_NONE) {
189 LogWarning("Platform error while destroying download handle. downloadId=" << downloadId);
192 std::string virtualPath;
194 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, fullPath);
196 LogWarning("Platform error while converting fullPath.");
197 virtualPath = fullPath;
200 callback->oncompleted(downloadId, virtualPath);
203 thisObj->removeCallbackFromMap(downloadId);
206 case DOWNLOAD_STATE_FAILED:
208 ret = download_unset_state_changed_cb(downloadId);
209 if (ret != DOWNLOAD_ERROR_NONE) {
210 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
213 ret = download_unset_progress_cb(downloadId);
214 if (ret != DOWNLOAD_ERROR_NONE) {
215 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
218 int err = DOWNLOAD_ERROR_NONE;
219 std::string errMessage;
220 ret = download_get_error(downloadId, (download_error_e*)&err);
221 if (ret != DOWNLOAD_ERROR_NONE) {
222 LogWarning("Platform error while getting download error. ");
224 errMessage = _get_download_error(err);
227 ret = download_destroy(downloadId);
228 if (ret != DOWNLOAD_ERROR_NONE) {
229 LogWarning("Platform error while destroying download handle. downloadId=" << downloadId);
232 UnknownException error(errMessage.c_str());
233 callback->onfailed(downloadId, error);
234 thisObj->removeCallbackFromMap(downloadId);
237 case DOWNLOAD_STATE_CANCELED:
239 ret = download_unset_state_changed_cb(downloadId);
240 if (ret != DOWNLOAD_ERROR_NONE) {
241 throw UnknownException(("Platform error while unsetting state changed callback. " + _get_download_error(ret)).c_str());
244 ret = download_unset_progress_cb(downloadId);
245 if (ret != DOWNLOAD_ERROR_NONE) {
246 throw UnknownException(("Platform error while unsetting progress callback. " + _get_download_error(ret)).c_str());
249 ret = download_destroy(downloadId);
250 if (ret != DOWNLOAD_ERROR_NONE) {
251 LogWarning("Platform error while destroying download handle. downloadId=" << downloadId);
254 callback->oncanceled(downloadId);
255 thisObj->removeCallbackFromMap(downloadId);
259 LogWarning("State changed is ignored.");
262 } catch (const BasePlatformException &err) {
263 LogError("download_state_changed_cb: %s", err.getMessage().c_str());
270 static void download_state_changed_cb(int downloadId, download_state_e state, void *user_data)
272 LogDebug("download_state_changed_cb, downloadId=%d, state=%d", downloadId, (int)state);
273 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
274 data->downloadId = downloadId;
277 data->user_data = user_data;
279 // download core f/w calls this callback function in another thread.
280 // so we should use g_idle_add() to switch context to main thread.
281 g_idle_add(downloadEventCB, static_cast<void*>(data));
284 static void download_progress_cb(int downloadId, unsigned long long received, void *user_data)
286 LogDebug("download_progress_cb, downloadId=%d, received=%ld", downloadId, received);
287 DOWNLOAD_EVENT_DATA_T *data = new DOWNLOAD_EVENT_DATA_T;
288 data->downloadId = downloadId;
289 data->state = DOWNLOAD_STATE_DOWNLOADING;
290 data->received = received;
291 data->user_data = user_data;
293 // download core f/w calls this callback function in another thread.
294 // so we should use g_idle_add() to switch context to main thread.
295 g_idle_add(downloadEventCB, static_cast<void*>(data));
298 DownloadManager::DownloadManager()
302 DownloadManager::~DownloadManager()
306 void DownloadManager::setCallbackToMap(long downloadId, DownloadCallback *callback)
308 DownloadCallback *value = mDownloadCallbacks[downloadId];
312 mDownloadCallbacks[downloadId] = callback;
315 DownloadCallback* DownloadManager::getCallbackFromMap(long downloadId)
317 return mDownloadCallbacks[downloadId];
320 void DownloadManager::removeCallbackFromMap(long downloadId) {
321 DownloadCallback *value = mDownloadCallbacks[downloadId];
322 mDownloadCallbacks.erase(downloadId);
328 long DownloadManager::start(DownloadRequest *request, DownloadCallback *downloadCallback)
337 throw TypeMismatchException("request is NULL.");
340 std::string url = request->getUrl();
341 std::string destination = request->getDestination();
342 std::string fileName = request->getFileName();
343 std::string networkType = request->getNetworkType();
344 std::map<std::string, std::string> httpHeader = request->getHttpHeader();
346 LogDebug("url <%s>, destination <%s>, fileName <%s>", url.c_str(), destination.c_str(), fileName.c_str());
349 throw InvalidValuesException("Invalid DownloadRequest.url.");
352 ret = download_create(&downloadId);
353 if (ret != DOWNLOAD_ERROR_NONE) {
354 throw UnknownException(("Platform error while creating download. " + _get_download_error(ret)).c_str());
357 ret = download_set_url(downloadId, url.c_str());
358 if (ret != DOWNLOAD_ERROR_NONE) {
359 throw UnknownException(("Platform error while setting url. " + _get_download_error(ret)).c_str());
362 if (!destination.empty()) {
363 std::string fullPath;
365 DeviceAPI::Filesystem::IPathPtr path = DeviceAPI::Filesystem::Utils::fromVirtualPath(NULL, destination);
366 fullPath = path->getFullPath();
368 LogWarning("Converting virtual path is failed. [%s]", destination.c_str());
369 fullPath = destination;
371 LogDebug("Converted FullPath = <%s>", fullPath.c_str());
372 ret = download_set_destination(downloadId, fullPath.c_str());
373 if (ret != DOWNLOAD_ERROR_NONE) {
374 throw UnknownException(("Platform error while setting destination. " + _get_download_error(ret)).c_str());
378 if (!fileName.empty()) {
379 ret = download_set_file_name(downloadId, fileName.c_str());
380 if (ret != DOWNLOAD_ERROR_NONE) {
381 throw UnknownException(("Platform error while setting fileName. " + _get_download_error(ret)).c_str());
385 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
386 if (ret != DOWNLOAD_ERROR_NONE) {
387 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
390 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
391 if (ret != DOWNLOAD_ERROR_NONE) {
392 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
395 if (!networkType.empty()) {
396 ret = DOWNLOAD_ERROR_NONE;
397 if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR) {
398 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_DATA_NETWORK);
399 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI) {
400 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_WIFI);
401 } else if (networkType == TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL) {
402 ret = download_set_network_type(downloadId, DOWNLOAD_NETWORK_ALL);
404 throw TypeMismatchException("Wrong DownloadNetworkType.");
406 if (ret != DOWNLOAD_ERROR_NONE) {
407 throw UnknownException(("Platform error while setting network type. " + _get_download_error(ret)).c_str());
411 std::map<std::string, std::string>::const_iterator iter;
412 for (iter = httpHeader.begin(); iter != httpHeader.end(); ++iter) {
413 ret = download_add_http_header_field(downloadId, iter->first.c_str(), iter->second.c_str());
414 if (ret != DOWNLOAD_ERROR_NONE) {
415 throw UnknownException(("Platform error while setting http header fields. " + _get_download_error(ret)).c_str());
419 ret = download_start(downloadId);
420 if (ret != DOWNLOAD_ERROR_NONE) {
421 throw UnknownException(("Platform error while starting download. " + _get_download_error(ret)).c_str());
424 LogDebug("downloadId: %d", downloadId);
426 setCallbackToMap(downloadId, downloadCallback);
431 void DownloadManager::cancel(long downloadId)
435 LogDebug("entered. downloadId = %d", downloadId);
437 ret = download_cancel(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 canceling download. " + _get_download_error(ret)).c_str());
448 void DownloadManager::pause(long downloadId)
452 LogDebug("entered. downloadId = %d", downloadId);
454 ret = download_pause(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 pausing download. " + _get_download_error(ret)).c_str());
465 void DownloadManager::resume(long downloadId)
469 LogDebug("entered. downloadId = %d", downloadId);
471 ret = download_start(downloadId);
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 resuming download. " + _get_download_error(ret)).c_str());
482 std::string DownloadManager::getState(long downloadId)
485 download_state_e state;
488 LogDebug("entered. downloadId = %d", downloadId);
490 ret = download_get_state(downloadId, &state);
491 if (ret != DOWNLOAD_ERROR_NONE) {
492 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
493 throw NotFoundException("download id could not found.");
494 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
495 throw InvalidValuesException("download id is not valid.");
497 throw UnknownException(("Platform error while getting state. " + _get_download_error(ret)).c_str());
501 case DOWNLOAD_STATE_READY:
502 case DOWNLOAD_STATE_QUEUED:
503 result = TIZEN_ENUM_DOWNLOAD_STATE_QUEUED;
505 case DOWNLOAD_STATE_DOWNLOADING:
506 result = TIZEN_ENUM_DOWNLOAD_STATE_DOWNLOADING;
508 case DOWNLOAD_STATE_PAUSED:
509 result = TIZEN_ENUM_DOWNLOAD_STATE_PAUSED;
511 case DOWNLOAD_STATE_COMPLETED:
512 result = TIZEN_ENUM_DOWNLOAD_STATE_COMPLETED;
514 case DOWNLOAD_STATE_FAILED:
515 result = TIZEN_ENUM_DOWNLOAD_STATE_FAILED;
517 case DOWNLOAD_STATE_CANCELED:
518 result = TIZEN_ENUM_DOWNLOAD_STATE_CANCELED;
521 result = "undefined";
522 LogWarning("Unknown DownloadState was returned.");
529 DownloadRequest* DownloadManager::getDownloadRequest(long downloadId)
535 char *destination = NULL;
536 char *fileName = NULL;
537 download_network_type_e networkTypeValue = DOWNLOAD_NETWORK_ALL;
538 char **fieldNames = NULL;
539 char *fieldValue = NULL;
542 LogDebug("entered. downloadId = %d", downloadId);
544 ret = download_get_url(downloadId, &url);
545 if (ret != DOWNLOAD_ERROR_NONE) {
546 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
547 throw NotFoundException("download id could not found.");
548 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
549 throw InvalidValuesException("download id is not valid.");
551 throw UnknownException(("Platform error while getting url. " + _get_download_error(ret)).c_str());
554 ret = download_get_destination(downloadId, &destination);
555 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
556 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
557 throw NotFoundException("download id could not found.");
558 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
559 throw InvalidValuesException("download id is not valid.");
561 throw UnknownException(("Platform error while getting destination. " + _get_download_error(ret)).c_str());
564 ret = download_get_file_name(downloadId, &fileName);
565 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
566 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
567 throw NotFoundException("download id could not found.");
568 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
569 throw InvalidValuesException("download id is not valid.");
571 throw UnknownException(("Platform error while getting fileName. " + _get_download_error(ret)).c_str());
574 ret = download_get_network_type(downloadId, &networkTypeValue);
575 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
576 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
577 throw NotFoundException("download id could not found.");
578 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
579 throw InvalidValuesException("download id is not valid.");
581 throw UnknownException(("Platform error while getting network type. " + _get_download_error(ret)).c_str());
584 ret = download_get_http_header_field_list(downloadId, &fieldNames, &fieldLength);
585 if (ret != DOWNLOAD_ERROR_NONE && ret != DOWNLOAD_ERROR_NO_DATA) {
586 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
587 throw NotFoundException("download id could not found.");
588 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
589 throw InvalidValuesException("download id is not valid.");
591 throw UnknownException(("Platform error while getting http header fields. " + _get_download_error(ret)).c_str());
594 std::map<std::string, std::string> httpHeader;
595 for (i = 0; i < fieldLength; i++) {
596 ret = download_get_http_header_field(downloadId, fieldNames[i], &fieldValue);
597 if (ret != DOWNLOAD_ERROR_NONE) {
598 LogWarning("Platform error while getting http header field. " << _get_download_error(ret));
600 httpHeader.insert(make_pair(std::string(fieldNames[i]), std::string(fieldValue)));
606 DownloadRequest *request = new DownloadRequest();
609 request->setUrl(url);
614 std::string virtualPath;
616 virtualPath = DeviceAPI::Filesystem::Utils::toVirtualPath(NULL, destination);
618 LogWarning("Platform error while converting destination path.");
619 virtualPath = destination;
621 request->setDestination(virtualPath);
626 request->setFileName(fileName);
630 switch(networkTypeValue) {
631 case DOWNLOAD_NETWORK_DATA_NETWORK:
632 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_CELLULAR);
634 case DOWNLOAD_NETWORK_WIFI:
635 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_WIFI);
638 request->setNetworkType(TIZEN_ENUM_DOWNLOAD_NETWORK_TYPE_ALL);
643 request->setHttpHeader(httpHeader);
650 std::string DownloadManager::getMIMEType(long downloadId)
653 char *mimeType = NULL;
654 std::string result("");
656 LogDebug("entered. downloadId = %d", downloadId);
658 ret = download_get_mime_type(downloadId, &mimeType);
659 if (ret != DOWNLOAD_ERROR_NONE) {
660 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
661 throw NotFoundException("download id could not found.");
662 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
663 throw InvalidValuesException("download id is not valid.");
664 } else if (ret == DOWNLOAD_ERROR_NO_DATA) {
667 throw UnknownException(("Platform error while getting MIME type. " + _get_download_error(ret)).c_str());
680 void DownloadManager::setListener(long downloadId, DownloadCallback *downloadCallback)
684 LogDebug("entered. downloadId = %d", downloadId);
686 ret = download_set_state_changed_cb(downloadId, download_state_changed_cb, this);
687 if (ret != DOWNLOAD_ERROR_NONE) {
688 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
689 throw NotFoundException("download id could not found.");
690 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
691 throw InvalidValuesException("download id is not valid.");
693 throw UnknownException(("Platform error while setting state changed callback. " + _get_download_error(ret)).c_str());
696 ret = download_set_progress_cb(downloadId, download_progress_cb, this);
697 if (ret != DOWNLOAD_ERROR_NONE) {
698 if (ret == DOWNLOAD_ERROR_ID_NOT_FOUND) {
699 throw NotFoundException("download id could not found.");
700 } else if (ret == DOWNLOAD_ERROR_INVALID_PARAMETER) {
701 throw InvalidValuesException("download id is not valid.");
703 throw UnknownException(("Platform error while setting progress callback. " + _get_download_error(ret)).c_str());
706 setCallbackToMap(downloadId, downloadCallback);