• 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 #include "service_proxy.h"
16 
17 #include <sstream>
18 
19 #include "dfs_error.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "utils_log.h"
23 
24 namespace OHOS::FileManagement::CloudSync {
25 using namespace std;
26 
27 constexpr int LOAD_SA_TIMEOUT_MS = 2000;
28 
GetInstance()29 sptr<ICloudSyncService> ServiceProxy::GetInstance()
30 {
31     LOGD("getinstance");
32     unique_lock<mutex> lock(instanceMutex_);
33     if (serviceProxy_ != nullptr) {
34         if (serviceProxy_->AsObject() != nullptr && !serviceProxy_->AsObject()->IsObjectDead()) {
35             return serviceProxy_;
36         }
37     }
38 
39     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40     if (samgr == nullptr) {
41         LOGE("Samgr is nullptr");
42         return nullptr;
43     }
44     sptr<ServiceProxyLoadCallback> cloudSyncLoadCallback = new ServiceProxyLoadCallback();
45     if (cloudSyncLoadCallback == nullptr) {
46         LOGE("cloudSyncLoadCallback is nullptr");
47         return nullptr;
48     }
49     int32_t ret = samgr->LoadSystemAbility(FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, cloudSyncLoadCallback);
50     if (ret != E_OK) {
51         LOGE("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
52              FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID, ret);
53         return nullptr;
54     }
55     unique_lock<mutex> proxyLock(proxyMutex_);
56     auto waitStatus = cloudSyncLoadCallback->proxyConVar_.wait_for(
57         proxyLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
58         [cloudSyncLoadCallback]() { return cloudSyncLoadCallback->isLoadSuccess_.load(); });
59     if (!waitStatus) {
60         LOGE("Load CloudSynd SA timeout");
61         return nullptr;
62     }
63     return serviceProxy_;
64 }
65 
InvaildInstance()66 void ServiceProxy::InvaildInstance()
67 {
68     LOGI("invalid instance");
69     unique_lock<mutex> lock(instanceMutex_);
70     serviceProxy_ = nullptr;
71 }
72 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)73 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
74     int32_t systemAbilityId,
75     const sptr<IRemoteObject> &remoteObject)
76 {
77     LOGI("Load CloudSync SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s", systemAbilityId,
78          (remoteObject == nullptr ? "false" : "true"));
79     unique_lock<mutex> lock(proxyMutex_);
80     if (serviceProxy_ != nullptr) {
81         LOGE("CloudSync SA proxy has been loaded");
82     } else {
83         serviceProxy_ = iface_cast<ICloudSyncService>(remoteObject);
84     }
85     isLoadSuccess_.store(true);
86     proxyConVar_.notify_one();
87 }
88 
OnLoadSystemAbilityFail(int32_t systemAbilityId)89 void ServiceProxy::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
90 {
91     LOGI("Load CloudSync SA failed,systemAbilityId:%{public}d", systemAbilityId);
92     unique_lock<mutex> lock(proxyMutex_);
93     serviceProxy_ = nullptr;
94     isLoadSuccess_.store(false);
95     proxyConVar_.notify_one();
96 }
97 } // namespace OHOS::FileManagement::CloudSync
98