• 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 "on_demand_start_stop_sa.h"
17 
18 #include "global.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 // LCOV_EXCL_START
25 std::atomic<uint32_t> OnDemandStartStopSa::processingIpcCount_ { 0 };
LoadInputMethodSystemAbility()26 sptr<IRemoteObject> OnDemandStartStopSa::LoadInputMethodSystemAbility()
27 {
28     std::unique_lock<std::mutex> lock(loadSaMtx_);
29     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
30     if (systemAbilityManager == nullptr) {
31         IMSA_HILOGE("get system ability manager fail");
32         return nullptr;
33     }
34 
35     auto remoteObject = systemAbilityManager->CheckSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID);
36     if (remoteObject != nullptr) {
37         return remoteObject;
38     }
39 
40     auto sharedThis = shared_from_this();
41     sptr<SaLoadCallback> callback = new (std::nothrow) SaLoadCallback(sharedThis);
42     if (callback == nullptr) {
43         IMSA_HILOGE("LoadCallback new fail.");
44         return nullptr;
45     }
46 
47     int32_t ret = systemAbilityManager->LoadSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID, callback);
48     if (ret != ERR_OK) {
49         IMSA_HILOGE("load input method system ability fail, ret: %{public}d", ret);
50         return nullptr;
51     }
52 
53     loadSaCv_.wait_for(lock, std::chrono::seconds(LOAD_SA_MAX_WAIT_TIME), [&sharedThis]() {
54         return sharedThis->remoteObj_ != nullptr;
55     });
56 
57     return remoteObj_;
58 }
59 
UnloadInputMethodSystemAbility()60 void OnDemandStartStopSa::UnloadInputMethodSystemAbility()
61 {
62     auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
63     if (systemAbilityManager == nullptr) {
64         IMSA_HILOGE("get system ability manager fail");
65         return;
66     }
67 
68     int32_t ret = systemAbilityManager->UnloadSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID);
69     if (ret != ERR_OK) {
70         IMSA_HILOGE("unload input method system ability fail, ret: %{public}d", ret);
71         return;
72     }
73     IMSA_HILOGI("unload input method system ability success");
74 }
75 
OnLoadSystemAbilitySuccess(int32_t said,const sptr<IRemoteObject> & object)76 void OnDemandStartStopSa::SaLoadCallback::OnLoadSystemAbilitySuccess(int32_t said, const sptr<IRemoteObject> &object)
77 {
78     IMSA_HILOGI("load inputmethod sa success");
79     if (onDemandObj_ == nullptr) {
80         IMSA_HILOGE("onDemandObj is null");
81         return;
82     }
83     std::unique_lock<std::mutex> lock(onDemandObj_->loadSaMtx_);
84     onDemandObj_->remoteObj_ = object;
85     onDemandObj_->loadSaCv_.notify_all();
86 }
87 
OnLoadSystemAbilityFail(int32_t said)88 void OnDemandStartStopSa::SaLoadCallback::OnLoadSystemAbilityFail(int32_t said)
89 {
90     IMSA_HILOGE("load inputmethod sa fail");
91     if (onDemandObj_ == nullptr) {
92         IMSA_HILOGE("onDemandObj is null");
93         return;
94     }
95     std::unique_lock<std::mutex> lock(onDemandObj_->loadSaMtx_);
96     onDemandObj_->remoteObj_ = nullptr;
97     onDemandObj_->loadSaCv_.notify_all();
98 }
99 
GetInputMethodSystemAbility(bool ifRetry)100 sptr<IRemoteObject> OnDemandStartStopSa::GetInputMethodSystemAbility(bool ifRetry)
101 {
102     sptr<ISystemAbilityManager> systemAbilityManager =
103         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
104     if (systemAbilityManager == nullptr) {
105         IMSA_HILOGE("system ability manager is nullptr!");
106         return nullptr;
107     }
108 
109     sptr<IRemoteObject> systemAbility = nullptr;
110     systemAbility = systemAbilityManager->CheckSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID);
111     if (systemAbility != nullptr) {
112         return systemAbility;
113     }
114     if (!ifRetry) {
115         IMSA_HILOGE("check system ability is nullptr!");
116         return nullptr;
117     }
118 
119     auto onDemandStartStopSa = std::make_shared<OnDemandStartStopSa>();
120     systemAbility = onDemandStartStopSa->LoadInputMethodSystemAbility();
121     if (systemAbility == nullptr) {
122         IMSA_HILOGE("load system ability fail");
123         return nullptr;
124     }
125     return systemAbility;
126 }
127 
IncreaseProcessingIpcCnt()128 void OnDemandStartStopSa::IncreaseProcessingIpcCnt()
129 {
130     processingIpcCount_.fetch_add(1);
131 }
DecreaseProcessingIpcCnt()132 void OnDemandStartStopSa::DecreaseProcessingIpcCnt()
133 {
134     processingIpcCount_.fetch_sub(1);
135 }
IsSaBusy()136 bool OnDemandStartStopSa::IsSaBusy()
137 {
138     return processingIpcCount_.load() > 0;
139 }
140 // LCOV_EXCL_STOP
141 } // namespace MiscServices
142 } // namespace OHOS