1 /*
2  * Copyright (c) 2024 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_sync_manager_impl_lite.h"
17 #include "cloud_sync_service_proxy.h"
18 #include "dfs_error.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 #include "utils_log.h"
22 
23 namespace OHOS::FileManagement::CloudSync {
24 constexpr int32_t MIN_USER_ID = 100;
GetInstance()25 CloudSyncManagerImplLite &CloudSyncManagerImplLite::GetInstance()
26 {
27     static CloudSyncManagerImplLite instance;
28     return instance;
29 }
30 
TriggerSync(const std::string & bundleName,const int32_t & userId)31 int32_t CloudSyncManagerImplLite::TriggerSync(const std::string &bundleName, const int32_t &userId)
32 {
33     if (bundleName.empty() || userId < MIN_USER_ID) {
34         LOGE("Trigger Sync parameter is invalid");
35         return E_INVAL_ARG;
36     }
37     auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
38     if (!CloudSyncServiceProxy) {
39         LOGE("proxy is null");
40         return E_SA_LOAD_FAILED;
41     }
42     SetDeathRecipient(CloudSyncServiceProxy->AsObject());
43     return CloudSyncServiceProxy->TriggerSyncInner(bundleName, userId);
44 }
45 
SetDeathRecipient(const sptr<IRemoteObject> & remoteObject)46 void CloudSyncManagerImplLite::SetDeathRecipient(const sptr<IRemoteObject> &remoteObject)
47 {
48     if (!isFirstCall_.test_and_set()) {
49         auto deathCallback = [this](const wptr<IRemoteObject> &obj) {
50             LOGE("service dead");
51             CloudSyncServiceProxy::InvaildInstance();
52             isFirstCall_.clear();
53         };
54         deathRecipient_ = sptr(new SvcDeathRecipientLite(deathCallback));
55         remoteObject->AddDeathRecipient(deathRecipient_);
56     }
57 }
58 } // namespace OHOS::FileManagement::CloudSync
59