!178 修改格式

Merge pull request !178 from ann_lesley/master
This commit is contained in:
openharmony_ci 2022-07-29 03:39:21 +00:00 committed by Gitee
commit b5073cee00
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
28 changed files with 1846 additions and 1846 deletions

View File

@ -1,96 +1,96 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dump_service_impl.h"
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <utility>
#include <map>
#include "dumper_factory.h"
#include "i_dumper.h"
#include "task_info_dumper_factory.h"
namespace OHOS::Request::Download {
DumpServiceImpl::DumpServiceImpl()
{
InitDumperFactoryMap();
}
DumpServiceImpl::~DumpServiceImpl()
{
dumperFactoryMap_.clear();
}
void DumpServiceImpl::InitDumperFactoryMap()
{
dumperFactoryMap_.insert(std::make_pair(DumperType::TASK_INFO_DUMPER, std::make_shared<TaskInfoDumperFactory>()));
}
DumpServiceImpl &DumpServiceImpl::GetInstance()
{
static DumpServiceImpl instance;
return instance;
}
DumperType DumpServiceImpl::GetDumperType(const std::string &cmd) const
{
if (cmd == "-h") {
return HELP_DUMPER;
} else if (cmd == "-t") {
return TASK_INFO_DUMPER;
} else {
return DUMPER_NUM;
}
}
int DumpServiceImpl::Dump(int fd, const std::vector<std::string> &args)
{
if (args.empty()) {
DumpHelp(fd);
return 0;
}
DumperType dumperType = GetDumperType(args[0]);
if (dumperType == HELP_DUMPER) {
DumpHelp(fd);
return 0;
}
DumperFactoryMap::const_iterator it = dumperFactoryMap_.find(dumperType);
if (it == dumperFactoryMap_.end()) {
dprintf(fd, "invalid arg\n");
return 0;
}
auto dumper = it->second->CreateDumper();
if (dumper != nullptr) {
dumper->Dump(fd, {args.begin() + 1, args.end()});
}
return 0;
}
void DumpServiceImpl::DumpHelp(int fd) const
{
constexpr const char *DEFAULT_HELPER =
"usage:\n"
" -h help text for the tool\n"
" -t [taskid] with no taskid: display all task summary info; taskid: display one task detail info\n";
dprintf(fd, "%s\n", DEFAULT_HELPER);
}
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "dump_service_impl.h"
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <utility>
#include <map>
#include "dumper_factory.h"
#include "i_dumper.h"
#include "task_info_dumper_factory.h"
namespace OHOS::Request::Download {
DumpServiceImpl::DumpServiceImpl()
{
InitDumperFactoryMap();
}
DumpServiceImpl::~DumpServiceImpl()
{
dumperFactoryMap_.clear();
}
void DumpServiceImpl::InitDumperFactoryMap()
{
dumperFactoryMap_.insert(std::make_pair(DumperType::TASK_INFO_DUMPER, std::make_shared<TaskInfoDumperFactory>()));
}
DumpServiceImpl &DumpServiceImpl::GetInstance()
{
static DumpServiceImpl instance;
return instance;
}
DumperType DumpServiceImpl::GetDumperType(const std::string &cmd) const
{
if (cmd == "-h") {
return HELP_DUMPER;
} else if (cmd == "-t") {
return TASK_INFO_DUMPER;
} else {
return DUMPER_NUM;
}
}
int DumpServiceImpl::Dump(int fd, const std::vector<std::string> &args)
{
if (args.empty()) {
DumpHelp(fd);
return 0;
}
DumperType dumperType = GetDumperType(args[0]);
if (dumperType == HELP_DUMPER) {
DumpHelp(fd);
return 0;
}
DumperFactoryMap::const_iterator it = dumperFactoryMap_.find(dumperType);
if (it == dumperFactoryMap_.end()) {
dprintf(fd, "invalid arg\n");
return 0;
}
auto dumper = it->second->CreateDumper();
if (dumper != nullptr) {
dumper->Dump(fd, {args.begin() + 1, args.end()});
}
return 0;
}
void DumpServiceImpl::DumpHelp(int fd) const
{
constexpr const char *DEFAULT_HELPER =
"usage:\n"
" -h help text for the tool\n"
" -t [taskid] with no taskid: display all task summary info; taskid: display one task detail info\n";
dprintf(fd, "%s\n", DEFAULT_HELPER);
}
}

View File

@ -1,50 +1,50 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DUMP_SERVICE_IMPL_H
#define DUMP_SERVICE_IMPL_H
#include <map>
#include "dumper_factory.h"
namespace OHOS::Request::Download {
enum DumperType : uint32_t {
HELP_DUMPER = 0,
TASK_INFO_DUMPER,
DUMPER_NUM,
};
class DumpServiceImpl {
public:
static DumpServiceImpl &GetInstance();
int Dump(int fd, const std::vector<std::string> &args);
private:
DumpServiceImpl();
virtual ~DumpServiceImpl();
DumpServiceImpl(DumpServiceImpl const &) = delete;
void operator=(DumpServiceImpl const &) = delete;
DumpServiceImpl(DumpServiceImpl &&) = delete;
DumpServiceImpl &operator=(DumpServiceImpl &&) = delete;
void InitDumperFactoryMap();
void DumpHelp(int fd) const;
DumperType GetDumperType(const std::string &cmd) const;
private:
using DumperFactoryMap = std::map<DumperType, std::shared_ptr<DumperFactory>>;
DumperFactoryMap dumperFactoryMap_;
};
}
#endif // DUMP_SERVICE_IMPL_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DUMP_SERVICE_IMPL_H
#define DUMP_SERVICE_IMPL_H
#include <map>
#include "dumper_factory.h"
namespace OHOS::Request::Download {
enum DumperType : uint32_t {
HELP_DUMPER = 0,
TASK_INFO_DUMPER,
DUMPER_NUM,
};
class DumpServiceImpl {
public:
static DumpServiceImpl &GetInstance();
int Dump(int fd, const std::vector<std::string> &args);
private:
DumpServiceImpl();
virtual ~DumpServiceImpl();
DumpServiceImpl(DumpServiceImpl const &) = delete;
void operator=(DumpServiceImpl const &) = delete;
DumpServiceImpl(DumpServiceImpl &&) = delete;
DumpServiceImpl &operator=(DumpServiceImpl &&) = delete;
void InitDumperFactoryMap();
void DumpHelp(int fd) const;
DumperType GetDumperType(const std::string &cmd) const;
private:
using DumperFactoryMap = std::map<DumperType, std::shared_ptr<DumperFactory>>;
DumperFactoryMap dumperFactoryMap_;
};
}
#endif // DUMP_SERVICE_IMPL_H

View File

@ -1,192 +1,192 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iomanip>
#include "download_service_manager.h"
#include "dump_task_info.h"
namespace OHOS::Request::Download {
bool DumpTaskInfo::Dump(int fd, const std::vector<std::string> &args)
{
uint32_t argsNum = args.size();
if (argsNum > 1) {
dprintf(fd, "too many args, -t accept no arg or one arg \n");
return false;
}
if (argsNum == 0) {
DumpAllTask(fd);
} else {
DumpTaskDetailInfo(fd, std::stoul(args[0]));
}
return true;
}
void DumpTaskInfo::DumpAllTaskTile(int fd) const
{
std::ostringstream buffer;
buffer << std::left;
FormatSummaryTitle(buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
void DumpTaskInfo::FormatSummaryTitle(std::ostringstream &buffer) const
{
for (const auto &it: summaryColumnTitle_) {
buffer << std::setw(it.first) << it.second;
}
}
void DumpTaskInfo::FormatDetailTitle(std::ostringstream &buffer) const
{
for (const auto &it: detailColumnTitle_) {
buffer << std::setw(it.first) << it.second;
}
}
void DumpTaskInfo::DumpTaskDetailInfoTile(int fd) const
{
std::ostringstream buffer;
buffer << std::left;
FormatSummaryTitle(buffer);
FormatDetailTitle(buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
void DumpTaskInfo::FormatSummaryContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
{
for (const auto &it: dumpSummaryCfg_) {
auto columnFormatFun = it.second;
buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
}
}
void DumpTaskInfo::FormatDetailContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
{
for (const auto &it: dumpDetailCfg_) {
auto columnFormatFun = it.second;
buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
}
}
bool DumpTaskInfo::DumpAllTask(int fd) const
{
std::vector<DownloadInfo> taskVector;
auto instance = DownloadServiceManager::GetInstance();
if (instance == nullptr) {
dprintf(fd, "not enough memory\n");
return false;
}
instance->QueryAllTask(taskVector);
dprintf(fd, "task num: %lu\n", taskVector.size());
if (taskVector.empty()) {
return true;
}
DumpAllTaskTile(fd);
for (const auto &iter: taskVector) {
std::ostringstream buffer;
buffer << std::left;
FormatSummaryContent(iter, buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
taskVector.clear();
return true;
}
bool DumpTaskInfo::DumpTaskDetailInfo(int fd, uint32_t taskId) const
{
DownloadInfo downloadInfo;
auto instance = DownloadServiceManager::GetInstance();
if (instance == nullptr) {
dprintf(fd, "not enough memory\n");
return false;
}
bool ret = instance->Query(taskId, downloadInfo);
if (!ret) {
dprintf(fd, "invalid task id %u\n", taskId);
return false;
}
DumpTaskDetailInfoTile(fd);
std::ostringstream buffer;
buffer << std::left;
FormatSummaryContent(downloadInfo, buffer);
FormatDetailContent(downloadInfo, buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
return true;
}
std::string DumpTaskInfo::DumpTaskID(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadId());
}
std::string DumpTaskInfo::DumpTaskType(const DownloadInfo &taskInfo) const
{
return taskInfo.GetTaskType();
}
std::string DumpTaskInfo::DumpTaskStatus(const DownloadInfo &taskInfo) const
{
DownloadStatus status = taskInfo.GetStatus();
std::vector<std::pair<DownloadStatus, std::string>> mapping = {
{SESSION_SUCCESS, "complete"},
{SESSION_RUNNING, "running"},
{SESSION_PENDING, "pending"},
{SESSION_PAUSED, "pause"},
{SESSION_FAILED, "failed"},
{SESSION_UNKNOWN, "unknown"},
};
for (const auto &it: mapping) {
if (it.first == status) {
return it.second;
}
}
return "unknown";
}
std::string DumpTaskInfo::DumpFileName(const DownloadInfo &taskInfo) const
{
return taskInfo.GetFileName();
}
std::string DumpTaskInfo::DumpRoaming(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetRoaming());
}
std::string DumpTaskInfo::DumpNetworkType(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetNetworkType());
}
std::string DumpTaskInfo::DumpMetered(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetMetered());
}
std::string DumpTaskInfo::DumpFileSize(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadTotalBytes());
}
std::string DumpTaskInfo::DumpTransferredSize(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadedBytes());
}
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <iomanip>
#include "download_service_manager.h"
#include "dump_task_info.h"
namespace OHOS::Request::Download {
bool DumpTaskInfo::Dump(int fd, const std::vector<std::string> &args)
{
uint32_t argsNum = args.size();
if (argsNum > 1) {
dprintf(fd, "too many args, -t accept no arg or one arg \n");
return false;
}
if (argsNum == 0) {
DumpAllTask(fd);
} else {
DumpTaskDetailInfo(fd, std::stoul(args[0]));
}
return true;
}
void DumpTaskInfo::DumpAllTaskTile(int fd) const
{
std::ostringstream buffer;
buffer << std::left;
FormatSummaryTitle(buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
void DumpTaskInfo::FormatSummaryTitle(std::ostringstream &buffer) const
{
for (const auto &it: summaryColumnTitle_) {
buffer << std::setw(it.first) << it.second;
}
}
void DumpTaskInfo::FormatDetailTitle(std::ostringstream &buffer) const
{
for (const auto &it: detailColumnTitle_) {
buffer << std::setw(it.first) << it.second;
}
}
void DumpTaskInfo::DumpTaskDetailInfoTile(int fd) const
{
std::ostringstream buffer;
buffer << std::left;
FormatSummaryTitle(buffer);
FormatDetailTitle(buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
void DumpTaskInfo::FormatSummaryContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
{
for (const auto &it: dumpSummaryCfg_) {
auto columnFormatFun = it.second;
buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
}
}
void DumpTaskInfo::FormatDetailContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const
{
for (const auto &it: dumpDetailCfg_) {
auto columnFormatFun = it.second;
buffer << std::setw(it.first) << (this->*columnFormatFun)(taskInfo);
}
}
bool DumpTaskInfo::DumpAllTask(int fd) const
{
std::vector<DownloadInfo> taskVector;
auto instance = DownloadServiceManager::GetInstance();
if (instance == nullptr) {
dprintf(fd, "not enough memory\n");
return false;
}
instance->QueryAllTask(taskVector);
dprintf(fd, "task num: %lu\n", taskVector.size());
if (taskVector.empty()) {
return true;
}
DumpAllTaskTile(fd);
for (const auto &iter: taskVector) {
std::ostringstream buffer;
buffer << std::left;
FormatSummaryContent(iter, buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
}
taskVector.clear();
return true;
}
bool DumpTaskInfo::DumpTaskDetailInfo(int fd, uint32_t taskId) const
{
DownloadInfo downloadInfo;
auto instance = DownloadServiceManager::GetInstance();
if (instance == nullptr) {
dprintf(fd, "not enough memory\n");
return false;
}
bool ret = instance->Query(taskId, downloadInfo);
if (!ret) {
dprintf(fd, "invalid task id %u\n", taskId);
return false;
}
DumpTaskDetailInfoTile(fd);
std::ostringstream buffer;
buffer << std::left;
FormatSummaryContent(downloadInfo, buffer);
FormatDetailContent(downloadInfo, buffer);
dprintf(fd, "%s\n", buffer.str().c_str());
return true;
}
std::string DumpTaskInfo::DumpTaskID(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadId());
}
std::string DumpTaskInfo::DumpTaskType(const DownloadInfo &taskInfo) const
{
return taskInfo.GetTaskType();
}
std::string DumpTaskInfo::DumpTaskStatus(const DownloadInfo &taskInfo) const
{
DownloadStatus status = taskInfo.GetStatus();
std::vector<std::pair<DownloadStatus, std::string>> mapping = {
{SESSION_SUCCESS, "complete"},
{SESSION_RUNNING, "running"},
{SESSION_PENDING, "pending"},
{SESSION_PAUSED, "pause"},
{SESSION_FAILED, "failed"},
{SESSION_UNKNOWN, "unknown"},
};
for (const auto &it: mapping) {
if (it.first == status) {
return it.second;
}
}
return "unknown";
}
std::string DumpTaskInfo::DumpFileName(const DownloadInfo &taskInfo) const
{
return taskInfo.GetFileName();
}
std::string DumpTaskInfo::DumpRoaming(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetRoaming());
}
std::string DumpTaskInfo::DumpNetworkType(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetNetworkType());
}
std::string DumpTaskInfo::DumpMetered(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetMetered());
}
std::string DumpTaskInfo::DumpFileSize(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadTotalBytes());
}
std::string DumpTaskInfo::DumpTransferredSize(const DownloadInfo &taskInfo) const
{
return std::to_string(taskInfo.GetDownloadedBytes());
}
}

View File

@ -1,89 +1,89 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DUMP_TASK_INFO_H
#define DUMP_TASK_INFO_H
#include <vector>
#include <string>
#include <memory>
#include <sstream>
#include "i_dumper.h"
namespace OHOS::Request::Download {
class DownloadInfo;
class DumpTaskInfo : public IDumper {
public:
DumpTaskInfo() = default;
~DumpTaskInfo() override {};
bool Dump(int fd, const std::vector<std::string> &args) override;
private:
void DumpAllTaskTile(int fd) const;
bool DumpAllTask(int fd) const;
void DumpTaskDetailInfoTile(int fd) const;
bool DumpTaskDetailInfo(int fd, uint32_t taskId) const;
void FormatSummaryTitle(std::ostringstream &buffer) const;
void FormatDetailTitle(std::ostringstream &buffer) const;
void FormatSummaryContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const;
void FormatDetailContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const;
private:
std::string DumpTaskID(const DownloadInfo &taskInfo) const;
std::string DumpTaskType(const DownloadInfo &taskInfo) const;
std::string DumpTaskStatus(const DownloadInfo &taskInfo) const;
std::string DumpFileName(const DownloadInfo &taskInfo) const;
std::string DumpRoaming(const DownloadInfo &taskInfo) const;
std::string DumpNetworkType(const DownloadInfo &taskInfo) const;
std::string DumpMetered(const DownloadInfo &taskInfo) const;
std::string DumpFileSize(const DownloadInfo &taskInfo) const;
std::string DumpTransferredSize(const DownloadInfo &taskInfo) const;
private:
static const int32_t columnWidthInt = 12;
static const int32_t columnWidthShort = 8;
static const int32_t columnWidthFileName = 256;
using ColumnDumpFunc = std::string (DumpTaskInfo::*)(const DownloadInfo &taskInfo) const;
std::vector<std::pair<int32_t, std::string>> summaryColumnTitle_ = {
{columnWidthInt, "id"},
{columnWidthInt, "type"},
{columnWidthInt, "status"},
};
std::vector<std::pair<int32_t, ColumnDumpFunc>> dumpSummaryCfg_ = {
{columnWidthInt, &DumpTaskInfo::DumpTaskID},
{columnWidthInt, &DumpTaskInfo::DumpTaskType},
{columnWidthInt, &DumpTaskInfo::DumpTaskStatus},
};
std::vector<std::pair<int32_t, std::string>> detailColumnTitle_ = {
{columnWidthShort, "roaming"},
{columnWidthShort, "network"},
{columnWidthShort, "meter"},
{columnWidthInt, "file_size"},
{columnWidthInt, "tran_size "},
{columnWidthFileName, "file_name"},
};
std::vector<std::pair<int32_t, ColumnDumpFunc>> dumpDetailCfg_ = {
{columnWidthShort, &DumpTaskInfo::DumpRoaming},
{columnWidthShort, &DumpTaskInfo::DumpNetworkType},
{columnWidthShort, &DumpTaskInfo::DumpMetered},
{columnWidthInt, &DumpTaskInfo::DumpFileSize},
{columnWidthInt, &DumpTaskInfo::DumpTransferredSize},
{columnWidthFileName, &DumpTaskInfo::DumpFileName},
};
};
}
#endif // DUMP_TASK_INFO_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DUMP_TASK_INFO_H
#define DUMP_TASK_INFO_H
#include <vector>
#include <string>
#include <memory>
#include <sstream>
#include "i_dumper.h"
namespace OHOS::Request::Download {
class DownloadInfo;
class DumpTaskInfo : public IDumper {
public:
DumpTaskInfo() = default;
~DumpTaskInfo() override {};
bool Dump(int fd, const std::vector<std::string> &args) override;
private:
void DumpAllTaskTile(int fd) const;
bool DumpAllTask(int fd) const;
void DumpTaskDetailInfoTile(int fd) const;
bool DumpTaskDetailInfo(int fd, uint32_t taskId) const;
void FormatSummaryTitle(std::ostringstream &buffer) const;
void FormatDetailTitle(std::ostringstream &buffer) const;
void FormatSummaryContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const;
void FormatDetailContent(const DownloadInfo &taskInfo, std::ostringstream &buffer) const;
private:
std::string DumpTaskID(const DownloadInfo &taskInfo) const;
std::string DumpTaskType(const DownloadInfo &taskInfo) const;
std::string DumpTaskStatus(const DownloadInfo &taskInfo) const;
std::string DumpFileName(const DownloadInfo &taskInfo) const;
std::string DumpRoaming(const DownloadInfo &taskInfo) const;
std::string DumpNetworkType(const DownloadInfo &taskInfo) const;
std::string DumpMetered(const DownloadInfo &taskInfo) const;
std::string DumpFileSize(const DownloadInfo &taskInfo) const;
std::string DumpTransferredSize(const DownloadInfo &taskInfo) const;
private:
static const int32_t columnWidthInt = 12;
static const int32_t columnWidthShort = 8;
static const int32_t columnWidthFileName = 256;
using ColumnDumpFunc = std::string (DumpTaskInfo::*)(const DownloadInfo &taskInfo) const;
std::vector<std::pair<int32_t, std::string>> summaryColumnTitle_ = {
{columnWidthInt, "id"},
{columnWidthInt, "type"},
{columnWidthInt, "status"},
};
std::vector<std::pair<int32_t, ColumnDumpFunc>> dumpSummaryCfg_ = {
{columnWidthInt, &DumpTaskInfo::DumpTaskID},
{columnWidthInt, &DumpTaskInfo::DumpTaskType},
{columnWidthInt, &DumpTaskInfo::DumpTaskStatus},
};
std::vector<std::pair<int32_t, std::string>> detailColumnTitle_ = {
{columnWidthShort, "roaming"},
{columnWidthShort, "network"},
{columnWidthShort, "meter"},
{columnWidthInt, "file_size"},
{columnWidthInt, "tran_size "},
{columnWidthFileName, "file_name"},
};
std::vector<std::pair<int32_t, ColumnDumpFunc>> dumpDetailCfg_ = {
{columnWidthShort, &DumpTaskInfo::DumpRoaming},
{columnWidthShort, &DumpTaskInfo::DumpNetworkType},
{columnWidthShort, &DumpTaskInfo::DumpMetered},
{columnWidthInt, &DumpTaskInfo::DumpFileSize},
{columnWidthInt, &DumpTaskInfo::DumpTransferredSize},
{columnWidthFileName, &DumpTaskInfo::DumpFileName},
};
};
}
#endif // DUMP_TASK_INFO_H

View File

@ -1,29 +1,29 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_DUMPER_FACTORY_H
#define REQUEST_DUMPER_FACTORY_H
#include <memory>
namespace OHOS::Request::Download {
class IDumper;
class DumperFactory {
public:
virtual ~DumperFactory() {};
virtual std::shared_ptr<IDumper> CreateDumper() = 0;
};
}
#endif // REQUEST_DUMPER_FACTORY_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_DUMPER_FACTORY_H
#define REQUEST_DUMPER_FACTORY_H
#include <memory>
namespace OHOS::Request::Download {
class IDumper;
class DumperFactory {
public:
virtual ~DumperFactory() {};
virtual std::shared_ptr<IDumper> CreateDumper() = 0;
};
}
#endif // REQUEST_DUMPER_FACTORY_H

View File

@ -1,30 +1,30 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_IDUMPER_H
#define REQUEST_IDUMPER_H
#include <vector>
namespace OHOS::Request::Download {
class IDumper {
public:
IDumper() = default;
virtual ~IDumper() {};
virtual bool Dump(int fd, const std::vector<std::string> &args) = 0;
};
}
#endif // REQUEST_IDUMPER_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_IDUMPER_H
#define REQUEST_IDUMPER_H
#include <vector>
namespace OHOS::Request::Download {
class IDumper {
public:
IDumper() = default;
virtual ~IDumper() {};
virtual bool Dump(int fd, const std::vector<std::string> &args) = 0;
};
}
#endif // REQUEST_IDUMPER_H

View File

@ -1,24 +1,24 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "task_info_dumper_factory.h"
#include "dump_task_info.h"
namespace OHOS::Request::Download {
std::shared_ptr<IDumper> TaskInfoDumperFactory::CreateDumper()
{
return std::make_shared<DumpTaskInfo>();
}
}
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "task_info_dumper_factory.h"
#include "dump_task_info.h"
namespace OHOS::Request::Download {
std::shared_ptr<IDumper> TaskInfoDumperFactory::CreateDumper()
{
return std::make_shared<DumpTaskInfo>();
}
}

View File

@ -1,27 +1,27 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_DUMP_TASK_INFO_FACTORY_H
#define REQUEST_DUMP_TASK_INFO_FACTORY_H
#include "dumper_factory.h"
namespace OHOS::Request::Download {
class TaskInfoDumperFactory : public DumperFactory {
public:
std::shared_ptr<IDumper> CreateDumper() override;
};
}
#endif // REQUEST_DUMP_TASK_INFO_FACTORY_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef REQUEST_DUMP_TASK_INFO_FACTORY_H
#define REQUEST_DUMP_TASK_INFO_FACTORY_H
#include "dumper_factory.h"
namespace OHOS::Request::Download {
class TaskInfoDumperFactory : public DumperFactory {
public:
std::shared_ptr<IDumper> CreateDumper() override;
};
}
#endif // REQUEST_DUMP_TASK_INFO_FACTORY_H

View File

@ -1,35 +1,35 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_COMMON_H
#define DOWNLOAD_COMMON_H
namespace OHOS::Request::Download {
enum DownloadError {
E_DOWNLOAD_OK,
E_DOWNLOAD_SA_DIED,
E_DOWNLOAD_READ_PARCEL_ERROR,
E_DOWNLOAD_WRITE_PARCEL_ERROR,
E_DOWNLOAD_PUBLISH_FAIL,
E_DOWNLOAD_TRANSACT_ERROR,
E_DOWNLOAD_DEAL_FAILED,
E_DOWNLOAD_PARAMETERS_INVALID,
E_DOWNLOAD_SET_RTC_FAILED,
E_DOWNLOAD_NOT_FOUND,
E_DOWNLOAD_NO_PERMISSION,
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_COMMON_H
#define DOWNLOAD_COMMON_H
namespace OHOS::Request::Download {
enum DownloadError {
E_DOWNLOAD_OK,
E_DOWNLOAD_SA_DIED,
E_DOWNLOAD_READ_PARCEL_ERROR,
E_DOWNLOAD_WRITE_PARCEL_ERROR,
E_DOWNLOAD_PUBLISH_FAIL,
E_DOWNLOAD_TRANSACT_ERROR,
E_DOWNLOAD_DEAL_FAILED,
E_DOWNLOAD_PARAMETERS_INVALID,
E_DOWNLOAD_SET_RTC_FAILED,
E_DOWNLOAD_NOT_FOUND,
E_DOWNLOAD_NO_PERMISSION,
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_COMMON_H

View File

@ -1,33 +1,33 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_NOTIFY_INTERFACE_H
#define DOWNLOAD_NOTIFY_INTERFACE_H
#include "iremote_broker.h"
#include "iremote_object.h"
namespace OHOS::Request::Download {
class DownloadNotifyInterface : public IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Download.DownloadNotifyInterface");
virtual void OnCallBack(MessageParcel &data) = 0;
};
enum {
DOWNLOAD_NOTIFY,
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_NOTIFY_INTERFACE_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_NOTIFY_INTERFACE_H
#define DOWNLOAD_NOTIFY_INTERFACE_H
#include "iremote_broker.h"
#include "iremote_object.h"
namespace OHOS::Request::Download {
class DownloadNotifyInterface : public IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Download.DownloadNotifyInterface");
virtual void OnCallBack(MessageParcel &data) = 0;
};
enum {
DOWNLOAD_NOTIFY,
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_NOTIFY_INTERFACE_H

View File

@ -1,55 +1,55 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_SERVICE_INTERFACE_H
#define DOWNLOAD_SERVICE_INTERFACE_H
#include <string>
#include "download_config.h"
#include "download_info.h"
#include "download_notify_interface.h"
#include "iremote_broker.h"
namespace OHOS::Request::Download {
class DownloadServiceInterface : public IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Download.DownloadServiceInterface");
virtual int32_t Request(const DownloadConfig &config) = 0;
virtual bool Pause(uint32_t taskId) = 0;
virtual bool Query(uint32_t taskId, DownloadInfo &info) = 0;
virtual bool QueryMimeType(uint32_t taskId, std::string &mimeType) = 0;
virtual bool Remove(uint32_t taskId) = 0;
virtual bool Resume(uint32_t taskId) = 0;
virtual bool On(uint32_t taskId, const std::string &type, const sptr<DownloadNotifyInterface> &listener) = 0;
virtual bool Off(uint32_t taskId, const std::string &type) = 0;
virtual bool CheckPermission() = 0;
virtual bool SetStartId(uint32_t startId) = 0;
};
enum {
CMD_REQUEST,
CMD_PAUSE,
CMD_QUERY,
CMD_QUERYMIMETYPE,
CMD_REMOVE,
CMD_RESUME,
CMD_ON,
CMD_OFF,
CMD_CHECKPERMISSION,
CMD_SETSTARTID,
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_SERVICE_INTERFACE_H
#define DOWNLOAD_SERVICE_INTERFACE_H
#include <string>
#include "download_config.h"
#include "download_info.h"
#include "download_notify_interface.h"
#include "iremote_broker.h"
namespace OHOS::Request::Download {
class DownloadServiceInterface : public IRemoteBroker {
public:
DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Download.DownloadServiceInterface");
virtual int32_t Request(const DownloadConfig &config) = 0;
virtual bool Pause(uint32_t taskId) = 0;
virtual bool Query(uint32_t taskId, DownloadInfo &info) = 0;
virtual bool QueryMimeType(uint32_t taskId, std::string &mimeType) = 0;
virtual bool Remove(uint32_t taskId) = 0;
virtual bool Resume(uint32_t taskId) = 0;
virtual bool On(uint32_t taskId, const std::string &type, const sptr<DownloadNotifyInterface> &listener) = 0;
virtual bool Off(uint32_t taskId, const std::string &type) = 0;
virtual bool CheckPermission() = 0;
virtual bool SetStartId(uint32_t startId) = 0;
};
enum {
CMD_REQUEST,
CMD_PAUSE,
CMD_QUERY,
CMD_QUERYMIMETYPE,
CMD_REMOVE,
CMD_RESUME,
CMD_ON,
CMD_OFF,
CMD_CHECKPERMISSION,
CMD_SETSTARTID,
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_SERVICE_INTERFACE_H

View File

@ -1,101 +1,101 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_ASYNC_CALL_H
#define DOWNLOAD_ASYNC_CALL_H
#include <functional>
#include <memory>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
namespace OHOS::Request::Download {
class AsyncCall final {
public:
class Context {
public:
using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>;
using OutputAction = std::function<napi_status(napi_env, napi_value *)>;
using ExecAction = std::function<void(Context *)>;
Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {};
virtual ~Context() {};
void SetAction(InputAction input, OutputAction output = nullptr)
{
input_ = input;
output_ = output;
}
void SetAction(OutputAction output)
{
SetAction(nullptr, std::move(output));
}
virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self)
{
if (input_ == nullptr) {
return napi_ok;
}
return input_(env, argc, argv, self);
}
virtual napi_status operator()(napi_env env, napi_value *result)
{
if (output_ == nullptr) {
*result = nullptr;
return napi_ok;
}
return output_(env, result);
}
virtual void Exec()
{
if (exec_ == nullptr) {
return;
}
exec_(this);
};
protected:
friend class AsyncCall;
InputAction input_ = nullptr;
OutputAction output_ = nullptr;
ExecAction exec_ = nullptr;
};
// The default AsyncCallback in the parameters is at the end position.
static constexpr size_t ASYNC_DEFAULT_POS = -1;
AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS);
~AsyncCall();
napi_value Call(napi_env env, Context::ExecAction exec = nullptr);
napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr);
private:
enum { ARG_ERROR, ARG_DATA, ARG_BUTT };
static void OnExecute(napi_env env, void *data);
static void OnComplete(napi_env env, napi_status status, void *data);
struct AsyncContext {
std::shared_ptr<Context> ctx = nullptr;
napi_ref callback = nullptr;
napi_ref self = nullptr;
napi_deferred defer = nullptr;
napi_async_work work = nullptr;
};
static void DeleteContext(napi_env env, AsyncContext *context);
AsyncContext *context_ = nullptr;
napi_env env_ = nullptr;
};
} // namespace OHOS::Request::Download
#endif // REQUEST_ASYNC_CALL_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_ASYNC_CALL_H
#define DOWNLOAD_ASYNC_CALL_H
#include <functional>
#include <memory>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "napi/native_node_api.h"
namespace OHOS::Request::Download {
class AsyncCall final {
public:
class Context {
public:
using InputAction = std::function<napi_status(napi_env, size_t, napi_value *, napi_value)>;
using OutputAction = std::function<napi_status(napi_env, napi_value *)>;
using ExecAction = std::function<void(Context *)>;
Context(InputAction input, OutputAction output) : input_(std::move(input)), output_(std::move(output)) {};
virtual ~Context() {};
void SetAction(InputAction input, OutputAction output = nullptr)
{
input_ = input;
output_ = output;
}
void SetAction(OutputAction output)
{
SetAction(nullptr, std::move(output));
}
virtual napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self)
{
if (input_ == nullptr) {
return napi_ok;
}
return input_(env, argc, argv, self);
}
virtual napi_status operator()(napi_env env, napi_value *result)
{
if (output_ == nullptr) {
*result = nullptr;
return napi_ok;
}
return output_(env, result);
}
virtual void Exec()
{
if (exec_ == nullptr) {
return;
}
exec_(this);
};
protected:
friend class AsyncCall;
InputAction input_ = nullptr;
OutputAction output_ = nullptr;
ExecAction exec_ = nullptr;
};
// The default AsyncCallback in the parameters is at the end position.
static constexpr size_t ASYNC_DEFAULT_POS = -1;
AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr<Context> context, size_t pos = ASYNC_DEFAULT_POS);
~AsyncCall();
napi_value Call(napi_env env, Context::ExecAction exec = nullptr);
napi_value SyncCall(napi_env env, Context::ExecAction exec = nullptr);
private:
enum { ARG_ERROR, ARG_DATA, ARG_BUTT };
static void OnExecute(napi_env env, void *data);
static void OnComplete(napi_env env, napi_status status, void *data);
struct AsyncContext {
std::shared_ptr<Context> ctx = nullptr;
napi_ref callback = nullptr;
napi_ref self = nullptr;
napi_deferred defer = nullptr;
napi_async_work work = nullptr;
};
static void DeleteContext(napi_env env, AsyncContext *context);
AsyncContext *context_ = nullptr;
napi_env env_ = nullptr;
};
} // namespace OHOS::Request::Download
#endif // REQUEST_ASYNC_CALL_H

View File

@ -1,102 +1,102 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_BASE_CONTEXT_H
#define DOWNLOAD_BASE_CONTEXT_H
#include <list>
#include "event_manager.h"
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
typedef void (*AsyncWorkExecutor)(napi_env env, void *data);
typedef void (*AsyncWorkCallback)(napi_env env, napi_status status, void *data);
// support no argument
class BaseContext {
public:
ACE_DISALLOW_COPY_AND_MOVE(BaseContext);
BaseContext() = delete;
explicit BaseContext(napi_env env, napi_callback_info info, EventManager *manager);
virtual ~BaseContext();
virtual void ParseParams(napi_value *params, size_t paramsCount);
void SetParseOK(bool parseOK);
void SetExecOK(bool execOK);
void SetErrorCode(int32_t errorCode);
napi_status SetCallback(napi_value callback);
void DeleteCallback();
void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback);
void DeleteAsyncWork();
napi_value CreatePromise();
[[nodiscard]] bool IsParseOK() const;
[[nodiscard]] bool IsExecOK() const;
[[nodiscard]] napi_env GetEnv() const;
[[nodiscard]] napi_callback_info GetInfo() const;
[[nodiscard]] int32_t GetErrorCode() const;
[[nodiscard]] napi_value GetCallback() const;
[[nodiscard]] napi_deferred GetDeferred() const;
[[nodiscard]] const std::string &GetAsyncWorkName() const;
void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
protected:
EventManager *manager_;
private:
napi_env env_;
napi_callback_info info_;
bool parseOK_;
bool execOK_;
int32_t errorCode_;
napi_ref callback_;
napi_async_work asyncWork_;
napi_deferred deferred_;
std::string asyncWorkName_;
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_BASE_CONTEXT_H */
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_BASE_CONTEXT_H
#define DOWNLOAD_BASE_CONTEXT_H
#include <list>
#include "event_manager.h"
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
typedef void (*AsyncWorkExecutor)(napi_env env, void *data);
typedef void (*AsyncWorkCallback)(napi_env env, napi_status status, void *data);
// support no argument
class BaseContext {
public:
ACE_DISALLOW_COPY_AND_MOVE(BaseContext);
BaseContext() = delete;
explicit BaseContext(napi_env env, napi_callback_info info, EventManager *manager);
virtual ~BaseContext();
virtual void ParseParams(napi_value *params, size_t paramsCount);
void SetParseOK(bool parseOK);
void SetExecOK(bool execOK);
void SetErrorCode(int32_t errorCode);
napi_status SetCallback(napi_value callback);
void DeleteCallback();
void CreateAsyncWork(const std::string &name, AsyncWorkExecutor executor, AsyncWorkCallback callback);
void DeleteAsyncWork();
napi_value CreatePromise();
[[nodiscard]] bool IsParseOK() const;
[[nodiscard]] bool IsExecOK() const;
[[nodiscard]] napi_env GetEnv() const;
[[nodiscard]] napi_callback_info GetInfo() const;
[[nodiscard]] int32_t GetErrorCode() const;
[[nodiscard]] napi_value GetCallback() const;
[[nodiscard]] napi_deferred GetDeferred() const;
[[nodiscard]] const std::string &GetAsyncWorkName() const;
void Emit(const std::string &type, const std::pair<napi_value, napi_value> &argv);
protected:
EventManager *manager_;
private:
napi_env env_;
napi_callback_info info_;
bool parseOK_;
bool execOK_;
int32_t errorCode_;
napi_ref callback_;
napi_async_work asyncWork_;
napi_deferred deferred_;
std::string asyncWorkName_;
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_BASE_CONTEXT_H */

View File

@ -1,55 +1,55 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_BASE_NOTIFY_H
#define DOWNLOAD_BASE_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_notify_stub.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
struct NotifyData {
napi_env env;
napi_ref ref;
std::string type;
DownloadTask *task;
uint32_t firstArgv;
uint32_t secondArgv;
};
class DownloadBaseNotify : public DownloadNotifyStub {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadBaseNotify);
explicit DownloadBaseNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadBaseNotify();
void OnCallBack(MessageParcel &data) override;
protected:
NotifyData *GetNotifyData();
protected:
napi_env env_;
std::string type_;
DownloadTask *task_;
napi_ref ref_;
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_BASE_NOTIFY_H
#define DOWNLOAD_BASE_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_notify_stub.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
struct NotifyData {
napi_env env;
napi_ref ref;
std::string type;
DownloadTask *task;
uint32_t firstArgv;
uint32_t secondArgv;
};
class DownloadBaseNotify : public DownloadNotifyStub {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadBaseNotify);
explicit DownloadBaseNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadBaseNotify();
void OnCallBack(MessageParcel &data) override;
protected:
NotifyData *GetNotifyData();
protected:
napi_env env_;
std::string type_;
DownloadTask *task_;
napi_ref ref_;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_BASE_NOTIFY_H

View File

@ -1,92 +1,92 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_CONFIG_H
#define DOWNLOAD_CONFIG_H
#include <map>
#include <string>
namespace OHOS::Request::Download {
class DownloadConfig final {
public:
DownloadConfig();
void SetUrl(const std::string &url);
void SetHeader(const std::string &key, const std::string &val);
void SetMetered(bool enableMetered);
void SetRoaming(bool enableRoaming);
void SetDescription(const std::string &description);
void SetNetworkType(uint32_t type);
void SetFilePath(const std::string &filePath);
void SetTitle(const std::string &title);
void SetFD(int32_t fd);
void SetFDError(int32_t fdError);
[[nodiscard]] const std::string &GetUrl() const;
[[nodiscard]] const std::map<std::string, std::string> &GetHeader() const;
[[nodiscard]] bool GetMetered() const;
[[nodiscard]] bool GetRoaming() const;
[[nodiscard]] const std::string &GetDescription() const;
[[nodiscard]] uint32_t GetNetworkType() const;
[[nodiscard]] const std::string &GetFilePath() const;
[[nodiscard]] const std::string &GetTitle() const;
int32_t GetFD() const;
int32_t GetFDError() const;
void Dump(bool isFull = true) const;
private:
std::string url_;
std::map<std::string, std::string> header_;
bool enableMetered_;
bool enableRoaming_;
std::string description_;
uint32_t networkType_;
std::string filePath_;
std::string title_;
int32_t fd_;
int32_t fdError_;
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_CONFIG_H */
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_CONFIG_H
#define DOWNLOAD_CONFIG_H
#include <map>
#include <string>
namespace OHOS::Request::Download {
class DownloadConfig final {
public:
DownloadConfig();
void SetUrl(const std::string &url);
void SetHeader(const std::string &key, const std::string &val);
void SetMetered(bool enableMetered);
void SetRoaming(bool enableRoaming);
void SetDescription(const std::string &description);
void SetNetworkType(uint32_t type);
void SetFilePath(const std::string &filePath);
void SetTitle(const std::string &title);
void SetFD(int32_t fd);
void SetFDError(int32_t fdError);
[[nodiscard]] const std::string &GetUrl() const;
[[nodiscard]] const std::map<std::string, std::string> &GetHeader() const;
[[nodiscard]] bool GetMetered() const;
[[nodiscard]] bool GetRoaming() const;
[[nodiscard]] const std::string &GetDescription() const;
[[nodiscard]] uint32_t GetNetworkType() const;
[[nodiscard]] const std::string &GetFilePath() const;
[[nodiscard]] const std::string &GetTitle() const;
int32_t GetFD() const;
int32_t GetFDError() const;
void Dump(bool isFull = true) const;
private:
std::string url_;
std::map<std::string, std::string> header_;
bool enableMetered_;
bool enableRoaming_;
std::string description_;
uint32_t networkType_;
std::string filePath_;
std::string title_;
int32_t fd_;
int32_t fdError_;
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_CONFIG_H */

View File

@ -1,74 +1,74 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_EVENT_H
#define DOWNLOAD_EVENT_H
#include <string>
#include "napi/native_api.h"
#include "noncopyable.h"
#include "download_task.h"
#include "async_call.h"
namespace OHOS::Request::Download {
enum EventType {
NO_ARG_EVENT,
ONE_ARG_EVENT,
TWO_ARG_EVENT,
};
class DownloadEvent final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadEvent);
DownloadEvent() = default;
~DownloadEvent() = default;
static napi_value On(napi_env env, napi_callback_info info);
static napi_value Off(napi_env env, napi_callback_info info);
static int32_t GetEventType(const std::string &type);
private:
static sptr<DownloadNotifyInterface> CreateNotify(napi_env env,
const DownloadTask *task, const std::string &type, napi_ref callbackRef);
private:
struct EventOffContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
std::string type_ = "";
bool result = false;
napi_status status = napi_generic_failure;
EventOffContext() : Context(nullptr, nullptr) {};
EventOffContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~EventOffContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_EVENT_H
#define DOWNLOAD_EVENT_H
#include <string>
#include "napi/native_api.h"
#include "noncopyable.h"
#include "download_task.h"
#include "async_call.h"
namespace OHOS::Request::Download {
enum EventType {
NO_ARG_EVENT,
ONE_ARG_EVENT,
TWO_ARG_EVENT,
};
class DownloadEvent final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadEvent);
DownloadEvent() = default;
~DownloadEvent() = default;
static napi_value On(napi_env env, napi_callback_info info);
static napi_value Off(napi_env env, napi_callback_info info);
static int32_t GetEventType(const std::string &type);
private:
static sptr<DownloadNotifyInterface> CreateNotify(napi_env env,
const DownloadTask *task, const std::string &type, napi_ref callbackRef);
private:
struct EventOffContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
std::string type_ = "";
bool result = false;
napi_status status = napi_generic_failure;
EventOffContext() : Context(nullptr, nullptr) {};
EventOffContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~EventOffContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_EVENT_H

View File

@ -1,36 +1,36 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_FAIL_NOTIFY_H
#define DOWNLOAD_FAIL_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_base_notify.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadFailNotify final : public DownloadBaseNotify {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadFailNotify);
explicit DownloadFailNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadFailNotify();
void OnCallBack(MessageParcel &data) override;
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_FAIL_NOTIFY_H
#define DOWNLOAD_FAIL_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_base_notify.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadFailNotify final : public DownloadBaseNotify {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadFailNotify);
explicit DownloadFailNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadFailNotify();
void OnCallBack(MessageParcel &data) override;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_FAIL_NOTIFY_H

View File

@ -1,112 +1,112 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_INFO_H
#define DOWNLOAD_INFO_H
#include <map>
#include <string>
#include "constant.h"
namespace OHOS::Request::Download {
class DownloadInfo final {
public:
DownloadInfo();
void SetDescription(const std::string &description);
void SetDownloadedBytes(uint32_t downloadedBytes);
void SetDownloadId(uint32_t downloadId);
void SetFailedReason(ErrorCode failedReason);
void SetFileName(const std::string &fileName);
void SetFilePath(const std::string &filePath);
void SetPausedReason(PausedReason pausedReason);
void SetStatus(DownloadStatus status);
void SetTargetURI(const std::string &targetURI);
void SetDownloadTitle(const std::string & downloadTitle);
void SetDownloadTotalBytes(uint32_t downloadTotalBytes);
void SetNetworkType(uint32_t networkType);
void SetRoaming(bool enableRoaming);
void SetMetered(bool enableMetered);
[[nodiscard]] const std::string &GetDescription() const;
[[nodiscard]] uint32_t GetDownloadedBytes() const;
[[nodiscard]] uint32_t GetDownloadId() const;
[[nodiscard]] ErrorCode GetFailedReason() const;
[[nodiscard]] const std::string &GetFileName() const;
[[nodiscard]] const std::string &GetFilePath() const;
[[nodiscard]] PausedReason GetPausedReason() const;
[[nodiscard]] DownloadStatus GetStatus() const;
[[nodiscard]] const std::string &GetTargetURI() const;
[[nodiscard]] const std::string &GetDownloadTitle() const;
[[nodiscard]] uint32_t GetDownloadTotalBytes() const;
[[nodiscard]] uint32_t GetNetworkType() const;
[[nodiscard]] bool GetMetered() const;
[[nodiscard]] bool GetRoaming() const;
[[nodiscard]] std::string GetTaskType() const;
void Dump();
private:
std::string description_;
uint32_t downloadedBytes_;
uint32_t downloadId_;
ErrorCode failedReason_;
std::string fileName_;
std::string filePath_;
PausedReason pausedReason_;
DownloadStatus status_;
std::string targetURI_;
std::string downloadTitle_;
std::string taskType_;
uint32_t downloadTotalBytes_;
bool enableMetered_ {false};
bool enableRoaming_ {false};
uint32_t networkType_ {0};
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_INFO_H */
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_INFO_H
#define DOWNLOAD_INFO_H
#include <map>
#include <string>
#include "constant.h"
namespace OHOS::Request::Download {
class DownloadInfo final {
public:
DownloadInfo();
void SetDescription(const std::string &description);
void SetDownloadedBytes(uint32_t downloadedBytes);
void SetDownloadId(uint32_t downloadId);
void SetFailedReason(ErrorCode failedReason);
void SetFileName(const std::string &fileName);
void SetFilePath(const std::string &filePath);
void SetPausedReason(PausedReason pausedReason);
void SetStatus(DownloadStatus status);
void SetTargetURI(const std::string &targetURI);
void SetDownloadTitle(const std::string & downloadTitle);
void SetDownloadTotalBytes(uint32_t downloadTotalBytes);
void SetNetworkType(uint32_t networkType);
void SetRoaming(bool enableRoaming);
void SetMetered(bool enableMetered);
[[nodiscard]] const std::string &GetDescription() const;
[[nodiscard]] uint32_t GetDownloadedBytes() const;
[[nodiscard]] uint32_t GetDownloadId() const;
[[nodiscard]] ErrorCode GetFailedReason() const;
[[nodiscard]] const std::string &GetFileName() const;
[[nodiscard]] const std::string &GetFilePath() const;
[[nodiscard]] PausedReason GetPausedReason() const;
[[nodiscard]] DownloadStatus GetStatus() const;
[[nodiscard]] const std::string &GetTargetURI() const;
[[nodiscard]] const std::string &GetDownloadTitle() const;
[[nodiscard]] uint32_t GetDownloadTotalBytes() const;
[[nodiscard]] uint32_t GetNetworkType() const;
[[nodiscard]] bool GetMetered() const;
[[nodiscard]] bool GetRoaming() const;
[[nodiscard]] std::string GetTaskType() const;
void Dump();
private:
std::string description_;
uint32_t downloadedBytes_;
uint32_t downloadId_;
ErrorCode failedReason_;
std::string fileName_;
std::string filePath_;
PausedReason pausedReason_;
DownloadStatus status_;
std::string targetURI_;
std::string downloadTitle_;
std::string taskType_;
uint32_t downloadTotalBytes_;
bool enableMetered_ {false};
bool enableRoaming_ {false};
uint32_t networkType_ {0};
};
} // namespace OHOS::Request::Download
#endif /* DOWNLOAD_INFO_H */

View File

@ -1,81 +1,81 @@
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_MANAGER_H
#define DOWNLOAD_MANAGER_H
#include <map>
#include <mutex>
#include <condition_variable>
#include "data_ability_helper.h"
#include "iremote_object.h"
#include "refbase.h"
#include "download_notify_stub.h"
#include "download_service_interface.h"
#include "download_config.h"
#include "download_info.h"
#include "download_task.h"
namespace OHOS::Request::Download {
class DownloadSaDeathRecipient : public IRemoteObject::DeathRecipient {
public:
explicit DownloadSaDeathRecipient();
~DownloadSaDeathRecipient() = default;
void OnRemoteDied(const wptr<IRemoteObject> &object) override;
};
class DownloadManager : public RefBase {
public:
DownloadManager();
~DownloadManager();
static sptr<DownloadManager> GetInstance();
DownloadTask *EnqueueTask(const DownloadConfig &config);
bool Pause(uint32_t taskId);
bool Query(uint32_t taskId, DownloadInfo &info);
bool QueryMimeType(uint32_t taskId, std::string &mimeType);
bool Remove(uint32_t taskId);
bool Resume(uint32_t taskId);
bool On(uint32_t taskId, const std::string &type, const sptr<DownloadNotifyInterface> &listener);
bool Off(uint32_t taskId, const std::string &type);
bool CheckPermission();
void OnRemoteSaDied(const wptr<IRemoteObject> &object);
void SetDataAbilityHelper(std::shared_ptr<OHOS::AppExecFwk::DataAbilityHelper> dataAbilityHelper);
bool LoadDownloadServer();
void LoadServerSuccess();
void LoadServerFail();
private:
sptr<DownloadServiceInterface> GetDownloadServiceProxy();
private:
static std::mutex instanceLock_;
static sptr<DownloadManager> instance_;
std::mutex downloadMutex_;
std::mutex conditionMutex_;
sptr<DownloadServiceInterface> downloadServiceProxy_;
sptr<DownloadSaDeathRecipient> deathRecipient_;
std::shared_ptr<OHOS::AppExecFwk::DataAbilityHelper> dataAbilityHelper_;
std::condition_variable downloadSyncCon_;
bool ready_ = false;
static constexpr int LOAD_SA_TIMEOUT_MS = 15000;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_MANAGER_H
/*
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_MANAGER_H
#define DOWNLOAD_MANAGER_H
#include <map>
#include <mutex>
#include <condition_variable>
#include "data_ability_helper.h"
#include "iremote_object.h"
#include "refbase.h"
#include "download_notify_stub.h"
#include "download_service_interface.h"
#include "download_config.h"
#include "download_info.h"
#include "download_task.h"
namespace OHOS::Request::Download {
class DownloadSaDeathRecipient : public IRemoteObject::DeathRecipient {
public:
explicit DownloadSaDeathRecipient();
~DownloadSaDeathRecipient() = default;
void OnRemoteDied(const wptr<IRemoteObject> &object) override;
};
class DownloadManager : public RefBase {
public:
DownloadManager();
~DownloadManager();
static sptr<DownloadManager> GetInstance();
DownloadTask *EnqueueTask(const DownloadConfig &config);
bool Pause(uint32_t taskId);
bool Query(uint32_t taskId, DownloadInfo &info);
bool QueryMimeType(uint32_t taskId, std::string &mimeType);
bool Remove(uint32_t taskId);
bool Resume(uint32_t taskId);
bool On(uint32_t taskId, const std::string &type, const sptr<DownloadNotifyInterface> &listener);
bool Off(uint32_t taskId, const std::string &type);
bool CheckPermission();
void OnRemoteSaDied(const wptr<IRemoteObject> &object);
void SetDataAbilityHelper(std::shared_ptr<OHOS::AppExecFwk::DataAbilityHelper> dataAbilityHelper);
bool LoadDownloadServer();
void LoadServerSuccess();
void LoadServerFail();
private:
sptr<DownloadServiceInterface> GetDownloadServiceProxy();
private:
static std::mutex instanceLock_;
static sptr<DownloadManager> instance_;
std::mutex downloadMutex_;
std::mutex conditionMutex_;
sptr<DownloadServiceInterface> downloadServiceProxy_;
sptr<DownloadSaDeathRecipient> deathRecipient_;
std::shared_ptr<OHOS::AppExecFwk::DataAbilityHelper> dataAbilityHelper_;
std::condition_variable downloadSyncCon_;
bool ready_ = false;
static constexpr int LOAD_SA_TIMEOUT_MS = 15000;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_MANAGER_H

View File

@ -1,33 +1,33 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_NOTIFY_STUB_H
#define DOWNLOAD_NOTIFY_STUB_H
#include <memory>
#include "iremote_stub.h"
#include "download_notify_interface.h"
namespace OHOS::Request::Download {
class DownloadNotifyStub : public IRemoteStub<DownloadNotifyInterface> {
public:
DownloadNotifyStub() = default;
virtual ~DownloadNotifyStub()
{
}
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_NOTIFY_STUB_H
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_NOTIFY_STUB_H
#define DOWNLOAD_NOTIFY_STUB_H
#include <memory>
#include "iremote_stub.h"
#include "download_notify_interface.h"
namespace OHOS::Request::Download {
class DownloadNotifyStub : public IRemoteStub<DownloadNotifyInterface> {
public:
DownloadNotifyStub() = default;
virtual ~DownloadNotifyStub()
{
}
int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_NOTIFY_STUB_H

View File

@ -1,61 +1,61 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_PAUSE_H
#define DOWNLOAD_PAUSE_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadPause final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadPause);
DownloadPause() = default;
~DownloadPause() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct PauseContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
PauseContext() : Context(nullptr, nullptr) {};
PauseContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~PauseContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_PAUSE_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_PAUSE_H
#define DOWNLOAD_PAUSE_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadPause final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadPause);
DownloadPause() = default;
~DownloadPause() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct PauseContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
PauseContext() : Context(nullptr, nullptr) {};
PauseContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~PauseContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_PAUSE_H

View File

@ -1,39 +1,39 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_PROGRESS_NOTIFY_H
#define DOWNLOAD_PROGRESS_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_base_notify.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadProgressNotify final : public DownloadBaseNotify {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadProgressNotify);
explicit DownloadProgressNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadProgressNotify();
void OnCallBack(MessageParcel &data) override;
};
} // namespace OHOS::Request::Download
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_PROGRESS_NOTIFY_H
#define DOWNLOAD_PROGRESS_NOTIFY_H
#include <string>
#include "async_call.h"
#include "download_base_notify.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadProgressNotify final : public DownloadBaseNotify {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadProgressNotify);
explicit DownloadProgressNotify(napi_env env, const std::string &type, const DownloadTask *task, napi_ref ref);
virtual ~DownloadProgressNotify();
void OnCallBack(MessageParcel &data) override;
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_PROGRESS_NOTIFY_H

View File

@ -1,62 +1,62 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_QUERY_H
#define DOWNLOAD_QUERY_H
#include "async_call.h"
#include "download_info.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadQuery final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadQuery);
DownloadQuery() = default;
~DownloadQuery() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct QueryContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
DownloadInfo info;
napi_status status = napi_generic_failure;
QueryContext() : Context(nullptr, nullptr) {};
QueryContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~QueryContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_QUERY_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_QUERY_H
#define DOWNLOAD_QUERY_H
#include "async_call.h"
#include "download_info.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadQuery final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadQuery);
DownloadQuery() = default;
~DownloadQuery() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct QueryContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
DownloadInfo info;
napi_status status = napi_generic_failure;
QueryContext() : Context(nullptr, nullptr) {};
QueryContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~QueryContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_QUERY_H

View File

@ -1,64 +1,64 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_QUERY_MIMETYPE_H
#define DOWNLOAD_QUERY_MIMETYPE_H
#include <string>
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadQueryMimeType final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadQueryMimeType);
DownloadQueryMimeType() = default;
~DownloadQueryMimeType() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct QueryMimeContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
std::string result = "";
napi_status status = napi_generic_failure;
QueryMimeContext() : Context(nullptr, nullptr) {};
QueryMimeContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~QueryMimeContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_QUERY_MIMETYPE_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_QUERY_MIMETYPE_H
#define DOWNLOAD_QUERY_MIMETYPE_H
#include <string>
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadQueryMimeType final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadQueryMimeType);
DownloadQueryMimeType() = default;
~DownloadQueryMimeType() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct QueryMimeContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
std::string result = "";
napi_status status = napi_generic_failure;
QueryMimeContext() : Context(nullptr, nullptr) {};
QueryMimeContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~QueryMimeContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_QUERY_MIMETYPE_H

View File

@ -1,62 +1,62 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_REMOVE_H
#define DOWNLOAD_REMOVE_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadRemove final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadRemove);
DownloadRemove() = default;
~DownloadRemove() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct RemoveContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
RemoveContext() : Context(nullptr, nullptr) {};
RemoveContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~RemoveContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_REMOVE_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_REMOVE_H
#define DOWNLOAD_REMOVE_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadRemove final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadRemove);
DownloadRemove() = default;
~DownloadRemove() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct RemoveContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
RemoveContext() : Context(nullptr, nullptr) {};
RemoveContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~RemoveContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_REMOVE_H

View File

@ -1,62 +1,62 @@
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_RESUME_H
#define DOWNLOAD_RESUME_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadResume final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadResume);
DownloadResume() = default;
~DownloadResume() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct ResumeContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
ResumeContext() : Context(nullptr, nullptr) {};
ResumeContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~ResumeContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_RESUME_H
/*
* Copyright (C) 2021-2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef DOWNLOAD_RESUME_H
#define DOWNLOAD_RESUME_H
#include "async_call.h"
#include "download_task.h"
#include "napi/native_api.h"
#include "noncopyable.h"
namespace OHOS::Request::Download {
class DownloadResume final {
public:
ACE_DISALLOW_COPY_AND_MOVE(DownloadResume);
DownloadResume() = default;
~DownloadResume() = default;
static napi_value Exec(napi_env env, napi_callback_info info);
private:
struct ResumeContext : public AsyncCall::Context {
DownloadTask *task_ = nullptr;
bool result = false;
napi_status status = napi_generic_failure;
ResumeContext() : Context(nullptr, nullptr) {};
ResumeContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
virtual ~ResumeContext() {};
napi_status operator()(napi_env env, size_t argc, napi_value *argv, napi_value self) override
{
NAPI_ASSERT_BASE(env, self != nullptr, "self is nullptr", napi_invalid_arg);
NAPI_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task_)), napi_invalid_arg);
NAPI_ASSERT_BASE(env, task_ != nullptr, "there is no native task", napi_invalid_arg);
return Context::operator()(env, argc, argv, self);
}
napi_status operator()(napi_env env, napi_value *result) override
{
if (status != napi_ok) {
return status;
}
return Context::operator()(env, result);
}
};
};
} // namespace OHOS::Request::Download
#endif // DOWNLOAD_RESUME_H

View File

@ -1,90 +1,90 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LEGACY_DOWNLOAD_MANAGER_H
#define LEGACY_DOWNLOAD_MANAGER_H
#include <atomic>
#include <functional>
#include <map>
#include <mutex>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "legacy/download_task.h"
namespace OHOS::Request::Download::Legacy {
class DownloadManager {
public:
static bool IsLegacy(napi_env env, napi_callback_info info);
static napi_value Download(napi_env env, napi_callback_info info);
static napi_value OnDownloadComplete(napi_env env, napi_callback_info info);
static void OnTaskDone(const std::string &token, bool successful, const std::string &errMsg);
private:
using ArgsGenerator = std::function<void(napi_env env, napi_ref *recv, int &argc, napi_value *argv)>;
struct DownloadDescriptor {
DownloadTask *task_ {};
std::string filename_;
napi_env env_ {};
napi_ref this_ {};
napi_ref successCb_ {};
napi_ref failCb_ {};
};
struct CallFunctionData {
napi_env env_ {};
napi_ref func_ {};
ArgsGenerator generator_;
};
static std::string GetTaskToken();
static std::string GetCacheDir(napi_env env);
static std::string GetFilenameFromUrl(std::string &url);
static bool IsPathValid(const std::string& dir, const std::string& filename);
static bool HasSameFilename(const std::string& filename);
static std::vector<std::string> ParseHeader(napi_env env, napi_value option);
static DownloadTask::DownloadOption ParseOption(napi_env env, napi_value option);
static void CallFailCallback(napi_env env, napi_value object, const std::string& msg);
static void CallSuccessCallback(napi_env env, napi_value object, const std::string& token);
static void CallFunctionAsync(napi_env env, napi_ref func, const ArgsGenerator &generator);
static std::atomic<uint32_t> taskId_;
static std::mutex lock_;
static std::map<std::string, DownloadManager::DownloadDescriptor> downloadDescriptors_;
static constexpr int DOWNLOAD_ARGC = 1;
static constexpr int SUCCESS_CB_ARGC = 1;
static constexpr int FAIL_CB_ARGC = 2;
static constexpr int FAIL_CB_DOWNLOAD_ERROR = 400;
static constexpr int FAIL_CB_TASK_NOT_EXIST = 401;
static constexpr int MAX_CB_ARGS = 2;
static inline const std::string URI_PREFIX = "internal://cache/";
};
}
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LEGACY_DOWNLOAD_MANAGER_H
#define LEGACY_DOWNLOAD_MANAGER_H
#include <atomic>
#include <functional>
#include <map>
#include <mutex>
#include "napi/native_api.h"
#include "napi/native_common.h"
#include "legacy/download_task.h"
namespace OHOS::Request::Download::Legacy {
class DownloadManager {
public:
static bool IsLegacy(napi_env env, napi_callback_info info);
static napi_value Download(napi_env env, napi_callback_info info);
static napi_value OnDownloadComplete(napi_env env, napi_callback_info info);
static void OnTaskDone(const std::string &token, bool successful, const std::string &errMsg);
private:
using ArgsGenerator = std::function<void(napi_env env, napi_ref *recv, int &argc, napi_value *argv)>;
struct DownloadDescriptor {
DownloadTask *task_ {};
std::string filename_;
napi_env env_ {};
napi_ref this_ {};
napi_ref successCb_ {};
napi_ref failCb_ {};
};
struct CallFunctionData {
napi_env env_ {};
napi_ref func_ {};
ArgsGenerator generator_;
};
static std::string GetTaskToken();
static std::string GetCacheDir(napi_env env);
static std::string GetFilenameFromUrl(std::string &url);
static bool IsPathValid(const std::string& dir, const std::string& filename);
static bool HasSameFilename(const std::string& filename);
static std::vector<std::string> ParseHeader(napi_env env, napi_value option);
static DownloadTask::DownloadOption ParseOption(napi_env env, napi_value option);
static void CallFailCallback(napi_env env, napi_value object, const std::string& msg);
static void CallSuccessCallback(napi_env env, napi_value object, const std::string& token);
static void CallFunctionAsync(napi_env env, napi_ref func, const ArgsGenerator &generator);
static std::atomic<uint32_t> taskId_;
static std::mutex lock_;
static std::map<std::string, DownloadManager::DownloadDescriptor> downloadDescriptors_;
static constexpr int DOWNLOAD_ARGC = 1;
static constexpr int SUCCESS_CB_ARGC = 1;
static constexpr int FAIL_CB_ARGC = 2;
static constexpr int FAIL_CB_DOWNLOAD_ERROR = 400;
static constexpr int FAIL_CB_TASK_NOT_EXIST = 401;
static constexpr int MAX_CB_ARGS = 2;
static inline const std::string URI_PREFIX = "internal://cache/";
};
}
#endif // LEGACY_DOWNLOAD_MANAGER_H

View File

@ -1,70 +1,70 @@
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LEGACY_DOWNLOAD_TASK_H
#define LEGACY_DOWNLOAD_TASK_H
#include <cstdio>
#include <functional>
#include <thread>
#include <vector>
#include "curl/curl.h"
namespace OHOS::Request::Download::Legacy {
class DownloadTask {
public:
struct DownloadOption {
std::string url_;
std::string filename_;
std::string fileDir_;
std::vector<std::string> header_;
};
using DoneFunc = std::function<void(const std::string&, bool, const std::string&)>;
DownloadTask(const std::string &token, const DownloadOption &option, const DoneFunc &callback);
~DownloadTask();
void Start();
void Run();
bool DoDownload();
void SetResumeFromLarge(CURL *curl, uint64_t pos);
private:
FILE *OpenDownloadFile() const;
uint32_t GetLocalFileSize();
bool SetOption(CURL *handle, curl_slist *&headers);
void NotifyDone(bool successful, const std::string& errMsg = "");
bool GetFileSize(uint32_t &result);
std::string taskId_;
DownloadOption option_;
DoneFunc callback_;
std::thread *thread_ {};
FILE *filp_ {};
char *errorBuffer_ {};
static bool isCurlGlobalInited_;
uint32_t totalSize_;
bool hasFileSize_;
};
}
/*
* Copyright (c) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LEGACY_DOWNLOAD_TASK_H
#define LEGACY_DOWNLOAD_TASK_H
#include <cstdio>
#include <functional>
#include <thread>
#include <vector>
#include "curl/curl.h"
namespace OHOS::Request::Download::Legacy {
class DownloadTask {
public:
struct DownloadOption {
std::string url_;
std::string filename_;
std::string fileDir_;
std::vector<std::string> header_;
};
using DoneFunc = std::function<void(const std::string&, bool, const std::string&)>;
DownloadTask(const std::string &token, const DownloadOption &option, const DoneFunc &callback);
~DownloadTask();
void Start();
void Run();
bool DoDownload();
void SetResumeFromLarge(CURL *curl, uint64_t pos);
private:
FILE *OpenDownloadFile() const;
uint32_t GetLocalFileSize();
bool SetOption(CURL *handle, curl_slist *&headers);
void NotifyDone(bool successful, const std::string& errMsg = "");
bool GetFileSize(uint32_t &result);
std::string taskId_;
DownloadOption option_;
DoneFunc callback_;
std::thread *thread_ {};
FILE *filp_ {};
char *errorBuffer_ {};
static bool isCurlGlobalInited_;
uint32_t totalSize_;
bool hasFileSize_;
};
}
#endif // LEGACY_DOWNLOAD_TASK_H