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