• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "asset/asset_adapter_sa_client.h"
17 
18 #include "dfs_error.h"
19 #include "distributed_file_daemon_manager_impl.h"
20 #include "distributed_file_daemon_proxy.h"
21 #include "if_system_ability_manager.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "utils_log.h"
25 
26 namespace OHOS {
27 namespace Storage {
28 namespace DistributedFile {
29 using namespace OHOS::FileManagement;
30 
GetInstance()31 AssetAdapterSaClient &AssetAdapterSaClient::GetInstance()
32 {
33     static AssetAdapterSaClient instance;
34     return instance;
35 }
36 
AssetAdapterSaClient()37 AssetAdapterSaClient::AssetAdapterSaClient()
38 {
39     LOGI("AssetAdapterSaClient create.");
40     if (!SubscribeAssetAdapterSA()) {
41         LOGE("AssetAdapterSaClient subscribe asset adapter SA fail.");
42     }
43 }
44 
SubscribeAssetAdapterSA()45 bool AssetAdapterSaClient::SubscribeAssetAdapterSA()
46 {
47     LOGI("SubscribeAssetAdapterSA");
48     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49     if (samgrProxy == nullptr) {
50         LOGE("Asset adapter Sa client get Sa manager proxy fail.");
51         return false;
52     }
53     sptr<AssetSystemAbilityStatusChange> callback (new AssetSystemAbilityStatusChange());
54     int32_t ret = samgrProxy->SubscribeSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, callback);
55     if (ret != E_OK) {
56         LOGE("SA manager subscribe system ability fail, SA Id %{public}d, ret %{public}d",
57              FILEMANAGEMENT_CLOUD_DAEMON_SERVICE_SA_ID, ret);
58         return false;
59     }
60     return true;
61 }
62 
AddListener(const sptr<IAssetRecvCallback> & listener)63 int32_t AssetAdapterSaClient::AddListener(const sptr<IAssetRecvCallback> &listener)
64 {
65     LOGI("Sa client add listener enter.");
66     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67     if (samgrProxy == nullptr) {
68         LOGE("Asset adapter Sa client get Sa manager proxy fail.");
69         return E_SA_LOAD_FAILED;
70     }
71 
72     std::lock_guard<std::mutex> lock(eventMutex_);
73     listeners_.emplace_back(listener);
74 
75     if (!CheckSystemAbilityStatus()) {
76         LOGI("Sa client add listener, check system ability status fail.");
77         return E_OK;
78     }
79 
80     auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
81     if (distributedFileDaemonProxy == nullptr) {
82         LOGE("proxy is null");
83         return OHOS::FileManagement::E_SA_LOAD_FAILED;
84     }
85 
86     int32_t ret = distributedFileDaemonProxy->RegisterAssetCallback(listener);
87     if (ret != E_OK) {
88         LOGE("Distributed file deamon proxy register listener fail, ret is %{public}d", ret);
89         return ret;
90     }
91     return E_OK;
92 }
93 
CheckSystemAbilityStatus()94 bool AssetAdapterSaClient::CheckSystemAbilityStatus()
95 {
96     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97     if (samgrProxy == nullptr) {
98         LOGE("Asset adapter Sa client get Sa manager proxy fail.");
99         return false;
100     }
101     if (!samgrProxy->CheckSystemAbility(FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID)) {
102         LOGI("SA manager proxy check system ability fail, SA Id %{public}d",
103              FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID);
104         return false;
105     }
106     return true;
107 }
108 
RemoveListener(const sptr<IAssetRecvCallback> & listener)109 int32_t AssetAdapterSaClient::RemoveListener(const sptr<IAssetRecvCallback> &listener)
110 {
111     LOGI("Sa client remove listener enter.");
112     std::lock_guard<std::mutex> lock(eventMutex_);
113     for (auto iter = listeners_.begin(); iter != listeners_.end();) {
114         if ((*iter)->AsObject() == listener->AsObject()) {
115             iter = listeners_.erase(iter);
116         } else {
117             iter++;
118         }
119     }
120 
121     auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
122     if (distributedFileDaemonProxy == nullptr) {
123         LOGE("proxy is null");
124         return OHOS::FileManagement::E_SA_LOAD_FAILED;
125     }
126 
127     int32_t ret = distributedFileDaemonProxy->UnRegisterAssetCallback(listener);
128     if (ret != E_OK) {
129         LOGE("Distributed file deamon proxy unRegister listener fail, ret is %{public}d", ret);
130         return ret;
131     }
132     return E_OK;
133 }
134 
OnAddSystemAbility()135 void AssetAdapterSaClient::OnAddSystemAbility()
136 {
137     LOGI("Sa client on add system ability enter.");
138     auto distributedFileDaemonProxy = DistributedFileDaemonProxy::GetInstance();
139     if (distributedFileDaemonProxy == nullptr) {
140         LOGE("proxy is null");
141         return;
142     }
143     std::lock_guard<std::mutex> lock(eventMutex_);
144     LOGI("Sa client on add system ability listener size %{public}d", (int32_t)listeners_.size());
145     for (auto iter = listeners_.begin(); iter != listeners_.end(); iter++) {
146         int32_t ret = distributedFileDaemonProxy->RegisterAssetCallback(*iter);
147         if (ret != E_OK) {
148             LOGE("Distributed file deamon proxy register listener fail, ret is %{public}d", ret);
149         }
150     }
151 }
152 
AssetSystemAbilityStatusChange()153 AssetSystemAbilityStatusChange::AssetSystemAbilityStatusChange()
154 {
155     LOGI("AssetSystemAbilityStatusChange create.");
156 }
157 
~AssetSystemAbilityStatusChange()158 AssetSystemAbilityStatusChange::~AssetSystemAbilityStatusChange()
159 {
160     LOGI("AssetSystemAbilityStatusChange delete.");
161 }
162 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)163 void AssetSystemAbilityStatusChange::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
164 {
165     LOGI("OnAddSystemAbility called, the systemAbilityId is %{public}d", systemAbilityId);
166     if (systemAbilityId != FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID) {
167         LOGE("systemAbilityId must be FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, but it is %{public}d",
168              systemAbilityId);
169     }
170 
171     AssetAdapterSaClient::GetInstance().OnAddSystemAbility();
172     LOGI("OnAddSystemAbility called end, the systemAbilityId is %{public}d", systemAbilityId);
173 }
174 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)175 void AssetSystemAbilityStatusChange::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
176 {
177     LOGI("OnRemoveSystemAbility called, the systemAbilityId is %{public}d", systemAbilityId);
178     if (systemAbilityId != FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID) {
179         LOGE("systemAbilityId must be FILEMANAGEMENT_DISTRIBUTED_FILE_DAEMON_SA_ID, but it is %{public}d",
180              systemAbilityId);
181     }
182 }
183 
184 } // namespace DistributedFile
185 } // namespace Storage
186 } // namespace OHOS
187