• 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 "js_continuation_state_manager_stub.h"
17 #include "napi_error_code.h"
18 #include "distributedsched_ipc_interface_code.h"
19 #include "base/continuationmgr_log.h"
20 
21 namespace OHOS {
22 namespace DistributedSchedule {
23 using namespace OHOS::AbilityRuntime;
24 using namespace OHOS::AppExecFwk;
25 namespace {
26     const std::string TAG = "JsContinuationStateManagerStub";
27     const std::u16string CONNECTION_CALLBACK_INTERFACE_TOKEN = u"ohos.abilityshell.DistributedConnection";
28     const int32_t CONTINUE_MANAGER_PERMISSION_ERR = -1;
29     const int32_t CALLBACK_PARAMS_NUM = 2;
30 }
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int32_t JsContinuationStateManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
33     MessageParcel &reply, MessageOption &option)
34 {
35     std::u16string token = data.ReadInterfaceToken();
36     if (CONNECTION_CALLBACK_INTERFACE_TOKEN != token) {
37         HILOGW("OnRemoteRequest interface token check failed!");
38         return CONTINUE_MANAGER_PERMISSION_ERR;
39     }
40     switch (code) {
41         case static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_STATE_CALLBACK):
42             return ContinueStateCallback(data, reply);
43         default:
44             return ERR_OK;
45     }
46 }
47 
ContinueStateCallback(MessageParcel & data,MessageParcel & reply)48 int32_t JsContinuationStateManagerStub::ContinueStateCallback(MessageParcel &data, MessageParcel &reply)
49 {
50     HILOGI("call");
51     int32_t state = data.ReadInt32();
52     std::string message = data.ReadString();
53     auto task = [this, state, message]() {
54         napi_env env = this->callbackData_.env;
55         napi_value callback = nullptr;
56         napi_get_reference_value(env, this->callbackData_.callbackRef, &callback);
57         napi_value undefined = nullptr;
58         napi_get_undefined(env, &undefined);
59 
60         napi_value result;
61         napi_create_object(env, &result);
62         napi_value resultState;
63         napi_create_int32(env, state, &resultState);
64         napi_set_named_property(env, result, "resultState", resultState);
65         napi_value resultInfo;
66         napi_create_string_utf8(env, message.c_str(), NAPI_AUTO_LENGTH, &resultInfo);
67         napi_set_named_property(env, result, "resultInfo", resultInfo);
68         napi_value callbackResult[2] = {NULL, result};
69 
70         HILOGI("callback result: %{public}d", state);
71         napi_value callbackReturn = nullptr;
72         napi_call_function(env, undefined, callback, CALLBACK_PARAMS_NUM, callbackResult, &callbackReturn);
73     };
74     napi_send_event(callbackData_.env, task, napi_eprio_high);
75     HILOGI("end");
76     return ERR_OK;
77 }
78 } // namespace DistributedSchedule
79 } // namespace OHOS
80 
81