• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_file_cache_core.h"
17 
18 #include <memory>
19 
20 #include "cloud_sync_manager.h"
21 #include "dfs_error.h"
22 #include "utils_log.h"
23 
24 namespace OHOS::FileManagement::CloudSync {
25 using namespace ModuleFileIO;
Constructor()26 FsResult<CloudFileCacheCore *> CloudFileCacheCore::Constructor()
27 {
28     std::unique_ptr<CloudFileCacheCore> cloudFileCachePtr = std::make_unique<CloudFileCacheCore>();
29     return FsResult<CloudFileCacheCore *>::Success(cloudFileCachePtr.release());
30 }
31 
DoOn(const string & event,const shared_ptr<CloudFileCacheCallbackImplAni> callback)32 FsResult<void> CloudFileCacheCore::DoOn(const string &event, const shared_ptr<CloudFileCacheCallbackImplAni> callback)
33 {
34     return FsResult<void>::Success();
35 }
36 
DoOff(const string & event,const optional<shared_ptr<CloudFileCacheCallbackImplAni>> & callback)37 FsResult<void> CloudFileCacheCore::DoOff(const string &event,
38                                          const optional<shared_ptr<CloudFileCacheCallbackImplAni>> &callback)
39 {
40     return FsResult<void>::Success();
41 }
42 
GetCallbackImpl(const std::string & eventType,bool isInit)43 std::shared_ptr<CloudFileCacheCallbackImplAni> CloudFileCacheCore::GetCallbackImpl(const std::string &eventType,
44                                                                                    bool isInit)
45 {
46     std::shared_ptr<CloudFileCacheCallbackImplAni> callbackImpl = nullptr;
47     std::lock_guard<std::mutex> lock(registerMutex_);
48     auto iter = registerMap_.find(eventType);
49     if (iter == registerMap_.end() || iter->second == nullptr) {
50         if (isInit) {
51             callbackImpl = std::make_shared<CloudFileCacheCallbackImplAni>();
52             registerMap_.insert(make_pair(eventType, callbackImpl));
53         }
54     } else {
55         callbackImpl = iter->second;
56     }
57     return callbackImpl;
58 }
59 
DoStart(const string & uri)60 FsResult<void> CloudFileCacheCore::DoStart(const string &uri)
61 {
62     auto callbackImpl = GetCallbackImpl(PROGRESS, true);
63     int32_t ret = callbackImpl->StartDownloadInner(uri, FieldKey::FIELDKEY_CONTENT);
64     if (ret != E_OK) {
65         LOGE("Stop Download failed! ret = %{public}d", ret);
66         return FsResult<void>::Error(Convert2ErrNum(ret));
67     }
68 
69     return FsResult<void>::Success();
70 }
71 
DoStart(const std::vector<std::string> & uriList,int32_t fieldKey)72 FsResult<int64_t> CloudFileCacheCore::DoStart(const std::vector<std::string> &uriList, int32_t fieldKey)
73 {
74     auto callbackImpl = GetCallbackImpl(MULTI_PROGRESS, true);
75     int64_t downloadId = 0;
76     int32_t ret = callbackImpl->StartDownloadInner(uriList, downloadId, fieldKey);
77     if (ret != E_OK) {
78         LOGE("Stop Download failed! ret = %{public}d", ret);
79         return FsResult<int64_t>::Error(Convert2ErrNum(ret));
80     }
81 
82     return FsResult<int64_t>::Success(downloadId);
83 }
84 
DoStop(const string & uri,bool needClean)85 FsResult<void> CloudFileCacheCore::DoStop(const string &uri, bool needClean)
86 {
87     auto callbackImpl = GetCallbackImpl(PROGRESS, false);
88     if (callbackImpl == nullptr) {
89         LOGE("Failed to stop download, callback is null!");
90         return FsResult<void>::Error(E_INVAL_ARG);
91     }
92     int32_t ret = callbackImpl->StopDownloadInner(uri, needClean);
93     if (ret != E_OK) {
94         LOGE("Stop Download failed! ret = %{public}d", ret);
95         return FsResult<void>::Error(Convert2ErrNum(ret));
96     }
97 
98     return FsResult<void>::Success();
99 }
100 
DoStop(int64_t downloadId,bool needClean)101 FsResult<void> CloudFileCacheCore::DoStop(int64_t downloadId, bool needClean)
102 {
103     auto callbackImpl = GetCallbackImpl(MULTI_PROGRESS, false);
104     if (callbackImpl == nullptr) {
105         LOGE("Failed to stop batch download, callback is null!");
106         return FsResult<void>::Error(EINVAL);
107     }
108     int32_t ret = callbackImpl->StopDownloadInner(downloadId, needClean);
109     if (ret != E_OK) {
110         LOGE("Stop batch download failed! ret = %{public}d", ret);
111         return FsResult<void>::Error(Convert2ErrNum(ret));
112     }
113 
114     return FsResult<void>::Success();
115 }
116 
CleanCache(const string & uri)117 FsResult<void> CloudFileCacheCore::CleanCache(const string &uri)
118 {
119     LOGI("CleanCache start");
120 
121     int32_t ret = CloudSyncManager::GetInstance().CleanCache(uri);
122     if (ret != E_OK) {
123         LOGE("Clean Cache failed! ret = %{public}d", ret);
124         return FsResult<void>::Error(Convert2ErrNum(ret));
125     }
126 
127     return FsResult<void>::Success();
128 }
129 } // namespace OHOS::FileManagement::CloudSync