1 /*
2 * Copyright (c) 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 "form_distributed_client.h"
17 #include "form_mgr_errors.h"
18 #include "hilog_wrapper.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
29 }
GetDmsServiceProxy()30 void FormDistributedClient::GetDmsServiceProxy()
31 {
32 HILOG_DEBUG("SHAREFORM:: func call");
33 if (dmsProxy_ != nullptr) {
34 HILOG_DEBUG("dms proxy already get.");
35 return;
36 }
37
38 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
39 if (samgrProxy == nullptr) {
40 HILOG_ERROR("fail to get samgr.");
41 return;
42 }
43 dmsProxy_ = samgrProxy->GetSystemAbility(DISTRIBUTED_SCHED_SA_ID);
44 }
45
ShareForm(const std::string & remoteDeviceId,const FormShareInfo & formShareInfo)46 int32_t FormDistributedClient::ShareForm(
47 const std::string &remoteDeviceId, const FormShareInfo &formShareInfo)
48 {
49 HILOG_DEBUG("SHAREFORM:: func call");
50 if (remoteDeviceId.empty()) {
51 HILOG_ERROR("fail to input deviceId.");
52 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
53 }
54
55 GetDmsServiceProxy();
56 if (dmsProxy_ == nullptr) {
57 HILOG_ERROR("fail to get dmsProxy.");
58 return ERR_APPEXECFWK_FORM_GET_DMS_PROXY_FAILED;
59 }
60
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option;
64 if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
65 HILOG_ERROR("WriteInterfaceToken failed");
66 return ERR_FLATTEN_OBJECT;
67 }
68
69 if (!data.WriteString(remoteDeviceId)) {
70 HILOG_ERROR("WriteString failed");
71 return ERR_FLATTEN_OBJECT;
72 }
73
74 if (!data.WriteParcelable(&formShareInfo)) {
75 HILOG_ERROR("WriteParcelable failed");
76 return ERR_FLATTEN_OBJECT;
77 }
78
79 int32_t error = dmsProxy_->SendRequest(START_REMOTE_SHARE_FORM, data, reply, option);
80 if (error != NO_ERROR) {
81 HILOG_ERROR("request failed, error: %{public}d", error);
82 return error;
83 }
84 int32_t result = reply.ReadInt32();
85 HILOG_DEBUG("get result from server data = %{public}d", result);
86 return result;
87 }
88
SetDmsProxy(const sptr<IRemoteObject> & dmsProxy)89 void FormDistributedClient::SetDmsProxy(const sptr<IRemoteObject> &dmsProxy)
90 {
91 dmsProxy_ = dmsProxy;
92 }
93 } // namespace AppExecFwk
94 } // namespace OHOS
95