• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "iservice_registry.h"
17 
18 #include <unistd.h>
19 
20 #include "errors.h"
21 #include "hilog/log.h"
22 #include "ipc_skeleton.h"
23 #include "ipc_types.h"
24 #include "iremote_broker.h"
25 #include "iremote_object.h"
26 #include "iremote_proxy.h"
27 #include "message_option.h"
28 #include "message_parcel.h"
29 #include "parcel_helper.h"
30 #include "refbase.h"
31 #include "sam_log.h"
32 #include "string_ex.h"
33 
34 namespace OHOS {
35 using namespace OHOS::HiviewDFX;
36 
37 namespace {
38     constexpr int32_t MASK = 0x100;
39     constexpr int32_t INTERFACE_TOKEN = 0;
40     constexpr int64_t SLEEP_TIME = 1;
41     constexpr int32_t RETRY_TIMES = 10;
42 }
43 
44 class ServiceRegistryProxy : public IRemoteProxy <IServiceRegistry> {
45 public:
ServiceRegistryProxy(const sptr<IRemoteObject> & object)46     explicit ServiceRegistryProxy(const sptr<IRemoteObject>& object)
47         : IRemoteProxy<IServiceRegistry>(object)
48     {
49     }
50 
51     ~ServiceRegistryProxy() = default;
52 
GetService(const std::u16string & name)53     virtual sptr<IRemoteObject> GetService(const std::u16string& name)
54     {
55         sptr<IRemoteObject> service = CheckService(name);
56         if (service != nullptr) {
57             return service;
58         }
59         int32_t retry = RETRY_TIMES;
60         HILOGI("Waiting for service %{public}s...", Str16ToStr8(name).data());
61         while (retry--) {
62             // Sleep and wait for 1 second;
63             sleep(SLEEP_TIME);
64             service = CheckService(name);
65             if (service != nullptr) {
66                 HILOGI("%{public}s:found service %{public}s", __func__, Str16ToStr8(name).data());
67                 return service;
68             }
69         }
70         HILOGE("Service %{public}s didn't start. Returning nullptr", Str16ToStr8(name).data());
71         return nullptr;
72     }
73 
CheckService(const std::u16string & name)74     virtual sptr<IRemoteObject> CheckService(const std::u16string& name)
75     {
76         HILOGI("%{public}s called", __func__);
77         MessageParcel data;
78         PARCEL_WRITE_HELPER_RET(data, Int32, MASK, nullptr);
79         PARCEL_WRITE_HELPER_RET(data, Int32, INTERFACE_TOKEN, nullptr);
80         PARCEL_WRITE_HELPER_RET(data, String16, IServiceRegistry::GetDescriptor(), nullptr);
81         PARCEL_WRITE_HELPER_RET(data, String16, name.data(), nullptr);
82         sptr<IRemoteObject> remote = Remote();
83         if (remote == nullptr) {
84             HILOGE("ServiceRegistryProxy::CheckService remote is nullptr !");
85             return nullptr;
86         }
87         MessageOption option;
88         MessageParcel reply;
89         int32_t err = remote->SendRequest(CHECK_SERVICE_TRANSACTION, data, reply, option);
90         HILOGI("%{public}s  available parcel size:%zd", __func__, reply.GetReadableBytes());
91         return (err == ERR_NONE) ? reply.ReadRemoteObject() : nullptr;
92     }
93 
AddService(const std::u16string & name,const sptr<IRemoteObject> & service,bool allowIsolated,int32_t dumpsysPriority)94     virtual int32_t AddService(const std::u16string& name,
95         const sptr<IRemoteObject>& service, bool allowIsolated, int32_t dumpsysPriority)
96     {
97         HILOGI("%s called", __func__);
98         MessageParcel data;
99         PARCEL_WRITE_HELPER_RET(data, Int32, MASK, ERR_FLATTEN_OBJECT);
100         PARCEL_WRITE_HELPER_RET(data, Int32, INTERFACE_TOKEN, ERR_FLATTEN_OBJECT);
101         PARCEL_WRITE_HELPER_RET(data, String16, IServiceRegistry::GetDescriptor(), ERR_FLATTEN_OBJECT);
102         PARCEL_WRITE_HELPER_RET(data, String16, name.data(), ERR_FLATTEN_OBJECT);
103         PARCEL_WRITE_HELPER_RET(data, RemoteObject, service, ERR_FLATTEN_OBJECT);
104         PARCEL_WRITE_HELPER_RET(data, Int32, (allowIsolated ? 1 : 0), ERR_FLATTEN_OBJECT);
105         PARCEL_WRITE_HELPER_RET(data, Int32, dumpsysPriority, ERR_FLATTEN_OBJECT);
106         sptr<IRemoteObject> remote = Remote();
107         if (remote == nullptr) {
108             HILOGE("ServiceRegistryProxy::AddService remote is nullptr !");
109             return ERR_INVALID_OPERATION;
110         }
111         MessageOption option;
112         MessageParcel reply;
113         int32_t err = remote->SendRequest(ADD_SERVICE_TRANSACTION, data, reply, option);
114 
115         HILOGI("%{public}s:add service %{public}s %{public}s, return %d",
116             __func__, Str16ToStr8(name).data(), err ? "fail" : "succ", err);
117 
118         return err;
119     }
120 private:
121     static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001800, "ServiceRegistry" };
122 };
123 
124 std::mutex ServiceRegistry::serviceRegistryLock_;
125 static inline BrokerDelegator<ServiceRegistryProxy> delegator_;
126 
GetInstance()127 sptr<IServiceRegistry> ServiceRegistry::GetInstance()
128 {
129     static sptr<IServiceRegistry> registryInstance;
130     std::lock_guard<std::mutex> lock(serviceRegistryLock_);
131     if (registryInstance == nullptr) {
132         sptr<IRemoteObject> registryObject = IPCSkeleton::GetContextObject();
133         if (registryObject == nullptr) {
134             return nullptr;
135         }
136         registryInstance = iface_cast<IServiceRegistry>(registryObject);
137     }
138     return registryInstance;
139 }
140 
GetInstance()141 SystemAbilityManagerClient& SystemAbilityManagerClient::GetInstance()
142 {
143     static auto instance = new SystemAbilityManagerClient();
144     return *instance;
145 }
146 
GetSystemAbilityManager()147 sptr<ISystemAbilityManager> SystemAbilityManagerClient::GetSystemAbilityManager()
148 {
149     std::lock_guard<std::mutex> lock(systemAbilityManagerLock_);
150     if (systemAbilityManager_ != nullptr) {
151         return systemAbilityManager_;
152     }
153     sptr<IRemoteObject> registryObject = IPCSkeleton::GetContextObject();
154     systemAbilityManager_ = iface_cast<ISystemAbilityManager>(registryObject);
155     return systemAbilityManager_;
156 }
157 
DestroySystemAbilityManagerObject()158 void SystemAbilityManagerClient::DestroySystemAbilityManagerObject()
159 {
160     HILOGI("%s called", __func__);
161     std::lock_guard<std::mutex> lock(systemAbilityManagerLock_);
162     systemAbilityManager_.clear();
163 }
164 } // namespace OHOS
165