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 #ifndef JS_WORKSCHED_EXTENSION_H
17 #define JS_WORKSCHED_EXTENSION_H
18 
19 #include "work_scheduler_extension.h"
20 
21 #include "js_runtime.h"
22 #include "work_info.h"
23 #include "work_sched_hilog.h"
24 
25 namespace OHOS {
26 namespace WorkScheduler {
27 class JsWorkSchedulerExtension : public WorkSchedulerExtension {
28 public:
29     explicit JsWorkSchedulerExtension(AbilityRuntime::JsRuntime &jsRuntime);
30     ~JsWorkSchedulerExtension() override;
31 
32     /**
33      * @brief Create JsWorkSchedulerExtension.
34      *
35      * @param runtime The runtime.
36      * @return The JsWorkSchedulerExtension instance.
37      */
38     static JsWorkSchedulerExtension* Create(const std::unique_ptr<AbilityRuntime::Runtime>& runtime);
39 
40     /**
41      * @brief The Init.
42      *
43      * @param record The record info.
44      * @param application The application info.
45      * @param handler The handler.
46      * @param token The remmote object token.
47      */
48     void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
49               const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
50               std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
51               const sptr<IRemoteObject> &token) override;
52 
53     /**
54      * @brief The OnStart callback.
55      *
56      * @param want The want.
57      */
58     void OnStart(const AAFwk::Want &want) override;
59 
60     /**
61      * @brief The OnConnect callback.
62      *
63      * @param want The want.
64      * @return Remote object.
65      */
66     sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
67 
68     /**
69      * @brief The OnDisconnect callback.
70      *
71      * @param want The want.
72      */
73     void OnDisconnect(const AAFwk::Want &want) override;
74 
75     /**
76      * @brief The OnStop callback.
77      */
78     void OnStop() override;
79 
80     /**
81      * @brief The OnWorkStart callback.
82      *
83      * @param workInfo The info of work.
84      */
85     void OnWorkStart(WorkInfo& workInfo) override;
86 
87     /**
88      * @brief The OnWorkStop callback.
89      *
90      * @param workInfo The info of work.
91      */
92     void OnWorkStop(WorkInfo& workInfo) override;
93 
94 private:
95     void BindContext(napi_env env, napi_value obj);
96     void GetSrcPath(std::string &srcPath);
97     bool GetExtrasJsonStr(const WorkInfo& workInfo, std::string& extrasStr);
98     AbilityRuntime::JsRuntime &jsRuntime_;
99     std::unique_ptr<NativeReference> jsObj_;
100     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
101 };
102 }  // namespace WorkScheduler
103 }  // namespace OHOS
104 #endif  // JS_WORKSCHED_EXTENSION_H