1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cloud_download_callback_stub.h"
17 #include "cloud_download_uri_manager.h"
18 #include "dfs_error.h"
19 #include "utils_log.h"
20 
21 namespace OHOS::FileManagement::CloudSync {
22 using namespace std;
23 
CloudDownloadCallbackStub()24 CloudDownloadCallbackStub::CloudDownloadCallbackStub()
25 {
26     opToInterfaceMap_[SERVICE_CMD_ON_PROCESS] = [this](MessageParcel &data, MessageParcel &reply) {
27             return this->HandleOnProcess(data, reply);
28         };
29 }
30 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int32_t CloudDownloadCallbackStub::OnRemoteRequest(uint32_t code,
32                                                    MessageParcel &data,
33                                                    MessageParcel &reply,
34                                                    MessageOption &option)
35 {
36     if (data.ReadInterfaceToken() != GetDescriptor()) {
37         return E_SERVICE_DESCRIPTOR_IS_EMPTY;
38     }
39     auto interfaceIndex = opToInterfaceMap_.find(code);
40     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
41         LOGE("Cannot response request %d: unknown tranction", code);
42         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
43     }
44     auto memberFunc = interfaceIndex->second;
45     return memberFunc(data, reply);
46 }
47 
HandleOnProcess(MessageParcel & data,MessageParcel & reply)48 int32_t CloudDownloadCallbackStub::HandleOnProcess(MessageParcel &data, MessageParcel &reply)
49 {
50     sptr<DownloadProgressObj> progress = data.ReadParcelable<DownloadProgressObj>();
51     if (!progress) {
52         LOGE("object of DownloadProgressObj is nullptr");
53         return E_INVAL_ARG;
54     }
55 
56     CloudDownloadUriManager& uriMgr = CloudDownloadUriManager::GetInstance();
57     std::string path = progress->path;
58     std::string uri = uriMgr.GetUri(path);
59     if (uri.empty()) {
60         LOGI("CloudDownloadCallbackStub path %{public}s trans to uri error, skip.", GetAnonyString(path).c_str());
61         return E_OK;
62     } else {
63         progress->path = uri;
64         // if download finished, call need call RemoveUri and CheckDownloadIdPathMap later
65     }
66 
67     OnDownloadProcess(*progress);
68     return E_OK;
69 }
70 
71 } // namespace OHOS::FileManagement::CloudSync
72