1 /*
2 * Copyright (c) 2021-2022 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 "ui_service_mgr_client.h"
17
18 #include "dialog_callback.h"
19 #include "hilog_wrapper.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "string_ex.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Ace {
28 constexpr int32_t UI_MGR_SERVICE_SA_ID = 7001;
29 std::shared_ptr<UIServiceMgrClient> UIServiceMgrClient::instance_ = nullptr;
30 std::mutex UIServiceMgrClient::mutex_;
31
GetInstance()32 std::shared_ptr<UIServiceMgrClient> UIServiceMgrClient::GetInstance()
33 {
34 if (instance_ == nullptr) {
35 std::lock_guard<std::mutex> lock(mutex_);
36 if (instance_ == nullptr) {
37 instance_ = std::make_shared<UIServiceMgrClient>();
38 }
39 }
40 return instance_;
41 }
42
43 UIServiceMgrClient::UIServiceMgrClient() = default;
44
45 UIServiceMgrClient::~UIServiceMgrClient() =default;
46
RegisterCallBack(const AAFwk::Want & want,const sptr<IUIService> & uiService)47 ErrCode UIServiceMgrClient::RegisterCallBack(const AAFwk::Want& want, const sptr<IUIService>& uiService)
48 {
49 if (remoteObject_ == nullptr) {
50 ErrCode err = Connect();
51 if (err != ERR_OK) {
52 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
53 return UI_SERVICE_NOT_CONNECTED;
54 }
55 }
56 sptr<IUIServiceMgr> doms = iface_cast<IUIServiceMgr>(remoteObject_);
57 return doms->RegisterCallBack(want, uiService);
58 }
59
UnregisterCallBack(const AAFwk::Want & want)60 ErrCode UIServiceMgrClient::UnregisterCallBack(const AAFwk::Want& want)
61 {
62 if (remoteObject_ == nullptr) {
63 ErrCode err = Connect();
64 if (err != ERR_OK) {
65 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
66 return UI_SERVICE_NOT_CONNECTED;
67 }
68 }
69 sptr<IUIServiceMgr> doms = iface_cast<IUIServiceMgr>(remoteObject_);
70 return doms->UnregisterCallBack(want);
71 }
72
Push(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)73 ErrCode UIServiceMgrClient::Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
74 const std::string& data, const std::string& extraData)
75 {
76 if (remoteObject_ == nullptr) {
77 ErrCode err = Connect();
78 if (err != ERR_OK) {
79 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
80 return UI_SERVICE_NOT_CONNECTED;
81 }
82 }
83 sptr<IUIServiceMgr> doms = iface_cast<IUIServiceMgr>(remoteObject_);
84 return doms->Push(want, name, jsonPath, data, extraData);
85 }
86
Request(const AAFwk::Want & want,const std::string & name,const std::string & data)87 ErrCode UIServiceMgrClient::Request(const AAFwk::Want& want, const std::string& name, const std::string& data)
88 {
89 if (remoteObject_ == nullptr) {
90 ErrCode err = Connect();
91 if (err != ERR_OK) {
92 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
93 return UI_SERVICE_NOT_CONNECTED;
94 }
95 }
96 sptr<IUIServiceMgr> doms = iface_cast<IUIServiceMgr>(remoteObject_);
97 return doms->Request(want, name, data);
98 }
99
ReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)100 ErrCode UIServiceMgrClient::ReturnRequest(const AAFwk::Want& want, const std::string& source,
101 const std::string& data, const std::string& extraData)
102 {
103 if (remoteObject_ == nullptr) {
104 ErrCode err = Connect();
105 if (err != ERR_OK) {
106 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
107 return UI_SERVICE_NOT_CONNECTED;
108 }
109 }
110 sptr<IUIServiceMgr> doms = iface_cast<IUIServiceMgr>(remoteObject_);
111 return doms->ReturnRequest(want, source, data, extraData);
112 }
113
ShowDialog(const std::string & name,const std::string & params,OHOS::Rosen::WindowType windowType,int32_t x,int32_t y,int32_t width,int32_t height,DialogCallback callback,int32_t * id)114 ErrCode UIServiceMgrClient::ShowDialog(const std::string& name,
115 const std::string& params,
116 OHOS::Rosen::WindowType windowType,
117 int32_t x,
118 int32_t y,
119 int32_t width,
120 int32_t height,
121 DialogCallback callback,
122 int32_t* id)
123 {
124 HILOG_ERROR("Ui Service show dialog deprecated!");
125 return GET_UI_SERVICE_FAILED;
126 }
127
CancelDialog(int32_t id)128 ErrCode UIServiceMgrClient::CancelDialog(int32_t id)
129 {
130 HILOG_ERROR("Ui Service cancel dialog deprecated!");
131 return GET_UI_SERVICE_FAILED;
132 }
133
UpdateDialog(int32_t id,const std::string & data)134 ErrCode UIServiceMgrClient::UpdateDialog(int32_t id, const std::string& data)
135 {
136 HILOG_ERROR("Ui Service update dialog deprecated!");
137 return GET_UI_SERVICE_FAILED;
138 }
139
140 /**
141 * Connect ui_service manager service.
142 *
143 * @return Returns ERR_OK on success, others on failure.
144 */
Connect()145 ErrCode UIServiceMgrClient::Connect()
146 {
147 std::lock_guard<std::mutex> lock(mutex_);
148 if (remoteObject_ != nullptr) {
149 return ERR_OK;
150 }
151 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
152 if (systemManager == nullptr) {
153 HILOG_ERROR("%{private}s:fail to get Registry", __func__);
154 return GET_UI_SERVICE_FAILED;
155 }
156 remoteObject_ = systemManager->GetSystemAbility(UI_MGR_SERVICE_SA_ID);
157 if (remoteObject_ == nullptr) {
158 HILOG_ERROR("%{private}s:fail to connect UIMgrService", __func__);
159 return GET_UI_SERVICE_FAILED;
160 }
161 HILOG_DEBUG("connect UIMgrService success");
162 return ERR_OK;
163 }
164 } // namespace Ace
165 } // namespace OHOS
166