• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef OHOS_APPMGMT_DISTRIBUTED_EXTENSION_JS_H
17 #define OHOS_APPMGMT_DISTRIBUTED_EXTENSION_JS_H
18 
19 #include <vector>
20 
21 #include "distributed_extension.h"
22 #include "js_runtime.h"
23 #include "native_reference.h"
24 #include "native_value.h"
25 
26 namespace OHOS {
27 namespace DistributedSchedule {
28 using InputArgsParser = std::function<bool(napi_env, std::vector<napi_value> &)>;
29 using ResultValueParser = std::function<bool(napi_env, napi_value)>;
30 
31 struct CallJsParam {
32     std::mutex distributedOperateMutex;
33     std::condition_variable distributedOperateCondition;
34     std::atomic<bool> isReady {false};
35     std::string funcName;
36     AbilityRuntime::JsRuntime *jsRuntime;
37     NativeReference *jsObj;
38     InputArgsParser argParser;
39     ResultValueParser retParser;
40 
CallJsParamCallJsParam41     CallJsParam(const std::string &funcNameIn,
42                 AbilityRuntime::JsRuntime *jsRuntimeIn,
43                 NativeReference *jsObjIn,
44                 InputArgsParser &argParserIn,
45                 ResultValueParser &retParserIn)
46         : funcName(funcNameIn), jsRuntime(jsRuntimeIn), jsObj(jsObjIn), argParser(argParserIn), retParser(retParserIn)
47     {
48     }
49 };
50 
51 class DistributedExtensionJs : public DistributedExtension {
52 public:
53     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
54               const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
55               std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
56               const sptr<IRemoteObject> &token) override;
57 
58 public:
59     static DistributedExtensionJs *Create(const std::unique_ptr<AbilityRuntime::Runtime> &runtime);
60 
61     virtual int32_t TriggerOnCreate(AAFwk::Want& want) override;
62     virtual int32_t TriggerOnDestroy() override;
63     virtual int32_t TriggerOnCollaborate(AAFwk::WantParams &wantParam) override;
64 
65 public:
66     explicit DistributedExtensionJs(AbilityRuntime::JsRuntime &jsRuntime);
67     ~DistributedExtensionJs();
68 private:
69     int32_t CallJsMethod(const std::string &funcName,
70                      AbilityRuntime::JsRuntime &jsRuntime,
71                      NativeReference *jsObj,
72                      InputArgsParser argParser,
73                      ResultValueParser retParser);
74     int32_t CallJsOnCreate();
75     std::function<bool(napi_env env, std::vector<napi_value> &argv)> ParseCreateInfo();
76     int32_t CallJsOnDestroy();
77     int32_t CallJsOnCollaborate();
78     std::function<bool(napi_env env, std::vector<napi_value> &argv)> ParseCollabInfo();
79 
80     void ExportJsContext(void);
81 
82     AbilityRuntime::JsRuntime &jsRuntime_;
83     std::unique_ptr<NativeReference> jsObj_;
84 
85     OHOS::AAFwk::Want want_;
86     AAFwk::WantParams wantParam_;
87 };
88 }
89 }
90 
91 #endif // OHOS_APPMGMT_DISTRIBUTED_EXTENSION_JS_H
92