• 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 "file_access_service_client.h"
17 
18 #include "bundle_constants.h"
19 #include "user_access_tracer.h"
20 #include "file_access_framework_errno.h"
21 #include "file_access_service_ipc_interface_code.h"
22 #include "hilog_wrapper.h"
23 #include "hitrace_meter.h"
24 #include "ipc_skeleton.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS {
29 namespace FileAccessFwk {
30 constexpr int LOAD_SA_TIMEOUT_MS = 5000;
31 const std::string UID_TAG = "uid:";
GetInstance()32 sptr<IFileAccessServiceBase> FileAccessServiceClient::GetInstance()
33 {
34     HILOG_INFO("Getinstance");
35     std::unique_lock<std::mutex> lock(proxyMutex_);
36     if (serviceProxy_ != nullptr) {
37         return serviceProxy_;
38     }
39 
40     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (samgr == nullptr) {
42         HILOG_ERROR("Samgr is nullptr");
43         return nullptr;
44     }
45     sptr<ServiceProxyLoadCallback> loadCallback(new ServiceProxyLoadCallback());
46     if (loadCallback == nullptr) {
47         HILOG_ERROR("loadCallback is nullptr");
48         return nullptr;
49     }
50     int32_t ret = samgr->LoadSystemAbility(FILE_ACCESS_SERVICE_ID, loadCallback);
51     if (ret != ERR_OK) {
52         HILOG_ERROR("Failed to Load systemAbility, systemAbilityId:%{public}d, ret code:%{public}d",
53             FILE_ACCESS_SERVICE_ID, ret);
54         return nullptr;
55     }
56 
57     auto waitStatus = loadCallback->proxyConVar_.wait_for(
58         lock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS),
59         [loadCallback]() { return loadCallback->isLoadSuccess_.load(); });
60     if (!waitStatus) {
61         HILOG_ERROR("Load FileAccessService SA timeout");
62         return nullptr;
63     }
64     return serviceProxy_;
65 }
66 
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)67 void FileAccessServiceClient::ServiceProxyLoadCallback::OnLoadSystemAbilitySuccess(
68     int32_t systemAbilityId,
69     const sptr<IRemoteObject> &remoteObject)
70 {
71     HILOG_INFO("Load FileAccessService SA success,systemAbilityId:%{public}d, remoteObj result:%{private}s",
72         systemAbilityId, (remoteObject == nullptr ? "false" : "true"));
73     if (systemAbilityId != FILE_ACCESS_SERVICE_ID || remoteObject == nullptr) {
74         isLoadSuccess_.store(false);
75         proxyConVar_.notify_one();
76         return;
77     }
78     std::unique_lock<std::mutex> lock(proxyMutex_);
79     serviceProxy_ = iface_cast<IFileAccessServiceBase>(remoteObject);
80     if (!serviceProxy_) {
81         HILOG_ERROR("Failed to get remote object");
82         isLoadSuccess_.store(false);
83         proxyConVar_.notify_one();
84         return;
85     }
86     auto remoteObj = serviceProxy_->AsObject();
87     if (!remoteObj) {
88         HILOG_ERROR("Failed to get remote object");
89         serviceProxy_ = nullptr;
90         isLoadSuccess_.store(false);
91         proxyConVar_.notify_one();
92         return;
93     }
94 
95     auto callback = [](const wptr<IRemoteObject> &obj) {
96         FileAccessServiceClient::InvaildInstance();
97         HILOG_ERROR("service died");
98     };
99     sptr<ProxyDeathRecipient> deathRecipient = sptr(new ProxyDeathRecipient(callback));
100     remoteObj->AddDeathRecipient(deathRecipient);
101     isLoadSuccess_.store(true);
102     proxyConVar_.notify_one();
103 }
104 
OnLoadSystemAbilityFail(int32_t systemAbilityId)105 void FileAccessServiceClient::ServiceProxyLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
106 {
107     HILOG_INFO("Load FileAccessService SA failed,systemAbilityId:%{public}d", systemAbilityId);
108     std::unique_lock<std::mutex> lock(proxyMutex_);
109     serviceProxy_ = nullptr;
110     isLoadSuccess_.store(false);
111     proxyConVar_.notify_one();
112 }
113 
InvaildInstance()114 void FileAccessServiceClient::InvaildInstance()
115 {
116     HILOG_INFO("invalid instance");
117     std::unique_lock<std::mutex> lock(proxyMutex_);
118     serviceProxy_ = nullptr;
119 }
120 } // namespace FileAccessFwk
121 } // namespace OHOS
122