1 /*
2  * Copyright (c) 2022 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 #ifndef OHOS_FILEMGMT_CLOUD_SYNC_ASSET_MANAGER_IMPL_H
17 #define OHOS_FILEMGMT_CLOUD_SYNC_ASSET_MANAGER_IMPL_H
18 
19 #include <atomic>
20 #include <mutex>
21 
22 #include "cloud_sync_asset_manager.h"
23 #include "download_asset_callback_client.h"
24 #include "nocopyable.h"
25 #include "svc_death_recipient.h"
26 
27 namespace OHOS::FileManagement::CloudSync {
28 class CloudSyncAssetManagerImpl final : public CloudSyncAssetManager, public NoCopyable {
29 public:
30     static CloudSyncAssetManagerImpl &GetInstance();
31 
32     int32_t UploadAsset(const int32_t userId, const std::string &request, std::string &result) override;
33     int32_t DownloadFile(const int32_t userId, const std::string &bundleName, AssetInfo &assetInfo) override;
34     int32_t DeleteAsset(const int32_t userId, const std::string &uri) override;
35     int32_t DownloadFile(const int32_t userId,
36                          const std::string &bundleName,
37                          const std::string &networkId,
38                          AssetInfo &assetInfo,
39                          ResultCallback resultCallback) override;
40     int32_t DownloadFiles(const int32_t userId,
41                           const std::string &bundleName,
42                           const std::vector<AssetInfo> &assetInfo,
43                           std::vector<bool> &assetResultMap) override;
44 
45 private:
46     CloudSyncAssetManagerImpl() = default;
47     void SetDeathRecipient(const sptr<IRemoteObject> &remoteObject);
48 
49     sptr<DownloadAssetCallbackClient> downloadAssetCallback_;
50     std::atomic_flag isCallbackRegistered_{false};
51     std::atomic_flag isFirstCall_{false};
52     sptr<SvcDeathRecipient> deathRecipient_;
53     std::mutex callbackInitMutex_;
54 };
55 } // namespace OHOS::FileManagement::CloudSync
56 
57 #endif // OHOS_FILEMGMT_CLOUD_SYNC_ASSET_MANAGER_IMPL_H
58