g_idle_add(OnProgressChanged, down_cb_ptr);
}
+#define CHECK_CONNECTION_ERROR(ret) \
+ switch (ret) { \
+ case CONNECTION_ERROR_NONE: \
+ break; \
+ case CONNECTION_ERROR_NOT_SUPPORTED: \
+ return LogAndCreateResult( \
+ common::ErrorCode::NOT_SUPPORTED_ERR, \
+ "The networkType of the given DownloadRequest is not supported", \
+ ("The networkType of the given DownloadRequest is not supported")); \
+ default: \
+ return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occurred", \
+ ("Connection problem occurred")); \
+ }
+
+common::PlatformResult DownloadInstance::CheckNetworkConnection(const std::string& network_type,
+ bool& network_support,
+ bool& network_available,
+ DownloadInfoPtr di_ptr) {
+ connection_h connection = nullptr;
+ int ret = connection_create(&connection);
+ CHECK_CONNECTION_ERROR(ret)
+ SCOPE_EXIT {
+ connection_destroy(connection);
+ };
+
+ connection_type_e connection_type = CONNECTION_TYPE_DISCONNECTED;
+ ret = connection_get_type(connection, &connection_type);
+ CHECK_CONNECTION_ERROR(ret)
+
+ if (CONNECTION_TYPE_DISCONNECTED == connection_type) {
+ return LogAndCreateResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occurred",
+ ("Connection type is disconnected"));
+ }
+
+ if ("CELLULAR" == network_type) {
+ // check if connection type is supported
+ bool cell_support = false;
+ system_info_get_platform_bool("http://tizen.org/feature/network.telephony", &cell_support);
+
+ // check if connection is available
+ connection_cellular_state_e cell_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
+ ret = connection_get_cellular_state(connection, &cell_state);
+ CHECK_CONNECTION_ERROR(ret)
+ bool cell_available = (CONNECTION_CELLULAR_STATE_CONNECTED == cell_state);
+
+ // setup download parameters using cellular network
+ network_support = cell_support;
+ network_available = cell_available;
+ di_ptr->network_type = DOWNLOAD_NETWORK_DATA_NETWORK;
+ } else if ("WIFI" == network_type) {
+ // check if connection type is supported
+ bool wifi_support = false;
+ system_info_get_platform_bool("http://tizen.org/feature/network.wifi", &wifi_support);
+
+ // check if connection is available
+ connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
+ ret = connection_get_wifi_state(connection, &wifi_state);
+ CHECK_CONNECTION_ERROR(ret)
+ bool wifi_available = (CONNECTION_WIFI_STATE_CONNECTED == wifi_state);
+
+ // setup download parameters using wifi network
+ network_support = wifi_support;
+ network_available = wifi_available;
+ di_ptr->network_type = DOWNLOAD_NETWORK_WIFI;
+ } else if (("ALL" == network_type) && (CONNECTION_TYPE_DISCONNECTED != connection_type)) {
+ // setup download parameters using 'all' network
+ network_support = true;
+ network_available = true;
+ di_ptr->network_type = DOWNLOAD_NETWORK_ALL;
+ } else {
+ return LogAndCreateResult(
+ common::ErrorCode::INVALID_VALUES_ERR,
+ "The input parameter contains an invalid network type.",
+ ("The input parameter contains an invalid network type: %s", network_type.c_str()));
+ }
+ return common::PlatformResult(common::ErrorCode::NO_ERROR);
+}
+#undef CHECK_CONNECTION_ERROR
+
void DownloadInstance::DownloadManagerStart(const picojson::value& args, picojson::object& out) {
ScopeLogger();
CHECK_PRIVILEGE_ACCESS(kPrivilegeDownload, &out);
CHECK_EXIST(args, "url", out)
int ret;
- std::string networkType;
+ std::string network_type;
DownloadInfoPtr di_ptr(new DownloadInfo);
}
if (!args.get("networkType").is<picojson::null>()) {
- networkType = args.get("networkType").is<std::string>()
- ? args.get("networkType").get<std::string>()
- : "ALL";
+ network_type = args.get("networkType").is<std::string>()
+ ? args.get("networkType").get<std::string>()
+ : "ALL";
}
bool network_support = false;
- bool cell_support = false;
- bool wifi_support = false;
-
- system_info_get_platform_bool("http://tizen.org/feature/network.telephony", &cell_support);
- system_info_get_platform_bool("http://tizen.org/feature/network.wifi", &wifi_support);
-
-#define CHECK_CONNECTION_ERROR(ret) \
- if (CONNECTION_ERROR_NONE != ret) { \
- LogAndReportError( \
- common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occured"), \
- &out, ("Connection type is disconnected")); \
- return; \
- }
-
- connection_h connection = nullptr;
- ret = connection_create(&connection);
- CHECK_CONNECTION_ERROR(ret)
- SCOPE_EXIT {
- connection_destroy(connection);
- };
-
- connection_cellular_state_e cell_state = CONNECTION_CELLULAR_STATE_OUT_OF_SERVICE;
- connection_wifi_state_e wifi_state = CONNECTION_WIFI_STATE_DEACTIVATED;
-
- ret = connection_get_cellular_state(connection, &cell_state);
- CHECK_CONNECTION_ERROR(ret)
- ret = connection_get_wifi_state(connection, &wifi_state);
- CHECK_CONNECTION_ERROR(ret)
-
- connection_type_e connection_type = CONNECTION_TYPE_DISCONNECTED;
- ret = connection_get_type(connection, &connection_type);
- CHECK_CONNECTION_ERROR(ret)
-
-#undef CHECK_CONNECTION_ERROR
-
- if (CONNECTION_TYPE_DISCONNECTED == connection_type) {
- LogAndReportError(
- common::PlatformResult(common::ErrorCode::UNKNOWN_ERR, "Connection problem occured"), &out,
- ("Connection type is disconnected"));
- return;
- }
-
bool network_available = false;
- bool cell_available = (CONNECTION_CELLULAR_STATE_CONNECTED == cell_state);
- bool wifi_available = (CONNECTION_WIFI_STATE_CONNECTED == wifi_state);
- if ("CELLULAR" == networkType) {
- network_support = cell_support;
- network_available = cell_available;
- di_ptr->network_type = DOWNLOAD_NETWORK_DATA_NETWORK;
- } else if ("WIFI" == networkType) {
- network_support = wifi_support;
- network_available = wifi_available;
- di_ptr->network_type = DOWNLOAD_NETWORK_WIFI;
- } else if (("ALL" == networkType) && (CONNECTION_TYPE_DISCONNECTED != connection_type)) {
- network_support = true;
- network_available = true;
- di_ptr->network_type = DOWNLOAD_NETWORK_ALL;
- } else {
- LogAndReportError(
- common::PlatformResult(common::ErrorCode::INVALID_VALUES_ERR,
- "The input parameter contains an invalid network type."),
- &out, ("The input parameter contains an invalid network type: %s", networkType.c_str()));
+ common::PlatformResult result =
+ CheckNetworkConnection(network_type, network_support, network_available, di_ptr);
+ if (!result) {
+ LogAndReportError(result, &out);
return;
}
/*
- * There is no relevant feature for networkType == "ALL".
+ * There is no relevant feature for network_type == "ALL".
*/
- if (!network_support && ("ALL" != networkType)) {
+ if (!network_support && ("ALL" != network_type)) {
LogAndReportError(common::PlatformResult(common::ErrorCode::NOT_SUPPORTED_ERR,
"The networkType of the given DownloadRequest "
"is not supported on this device."),
- &out, ("Requested network type (%s) is not supported.", networkType.c_str()));
+ &out,
+ ("Requested network type (%s) is not supported.", network_type.c_str()));
return;
}
LogAndReportError(common::PlatformResult(common::ErrorCode::NETWORK_ERR,
"The networkType of the given DownloadRequest "
"is currently not available on this device."),
- &out, ("Requested network type (%s) is not available.", networkType.c_str()));
+ &out,
+ ("Requested network type (%s) is not available.", network_type.c_str()));
return;
}
virtual ~DownloadInstance();
private:
- void DownloadManagerStart(const picojson::value& args, picojson::object& out);
- void DownloadManagerCancel(const picojson::value& args, picojson::object& out);
- void DownloadManagerPause(const picojson::value& args, picojson::object& out);
- void DownloadManagerResume(const picojson::value& args, picojson::object& out);
- void DownloadManagerGetstate(const picojson::value& args, picojson::object& out);
- void DownloadManagerGetmimetype(const picojson::value& args, picojson::object& out);
-
- bool GetDownloadID(const int download_id, int& native_download_id);
-
- static common::PlatformResult convertError(int err, const std::string& message = "");
- static void OnStateChanged(int download_id, download_state_e state, void* user_data);
- static void progress_changed_cb(int download_id, long long unsigned received, void* user_data);
- static void OnStart(int download_id, void* user_data);
-
- static gboolean OnProgressChanged(void* user_data);
- static gboolean OnFinished(void* user_data);
- static gboolean OnPaused(void* user_data);
- static gboolean OnCanceled(void* user_data);
- static gboolean OnFailed(void* user_data);
- static bool CheckInstance(DownloadInstance* instance);
-
struct DownloadInfo {
int download_id;
std::string url;
typedef std::shared_ptr<DownloadInfo> DownloadInfoPtr;
typedef std::map<int, DownloadInfoPtr> DownloadInfoMap;
+ common::PlatformResult CheckNetworkConnection(const std::string& network_type,
+ bool& network_support, bool& network_available,
+ DownloadInfoPtr di_ptr);
+ void DownloadManagerStart(const picojson::value& args, picojson::object& out);
+ void DownloadManagerCancel(const picojson::value& args, picojson::object& out);
+ void DownloadManagerPause(const picojson::value& args, picojson::object& out);
+ void DownloadManagerResume(const picojson::value& args, picojson::object& out);
+ void DownloadManagerGetstate(const picojson::value& args, picojson::object& out);
+ void DownloadManagerGetmimetype(const picojson::value& args, picojson::object& out);
+
+ bool GetDownloadID(const int download_id, int& native_download_id);
+
+ static common::PlatformResult convertError(int err, const std::string& message = "");
+ static void OnStateChanged(int download_id, download_state_e state, void* user_data);
+ static void progress_changed_cb(int download_id, long long unsigned received, void* user_data);
+ static void OnStart(int download_id, void* user_data);
+
+ static gboolean OnProgressChanged(void* user_data);
+ static gboolean OnFinished(void* user_data);
+ static gboolean OnPaused(void* user_data);
+ static gboolean OnCanceled(void* user_data);
+ static gboolean OnFailed(void* user_data);
+ static bool CheckInstance(DownloadInstance* instance);
+
static std::mutex instances_mutex_;
static std::vector<DownloadInstance*> instances_;