1 /*
2  * Copyright (c) 2023 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_ABILITY_RUNTIME_JS_INSIGHT_INTENT_EXECUTOR_H
17 #define OHOS_ABILITY_RUNTIME_JS_INSIGHT_INTENT_EXECUTOR_H
18 
19 #include "insight_intent_executor.h"
20 
21 #include "native_reference.h"
22 
23 class NativeReference;
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27     struct InsightIntentExecuteResult;
28 } // namespace AAFwk
29 namespace AbilityRuntime {
30 class JsRuntime;
31 
32 class JsInsightIntentExecutor final : public InsightIntentExecutor {
33 public:
34     static std::shared_ptr<JsInsightIntentExecutor> Create(JsRuntime& runtime);
35     enum class State {
36         INVALID,
37         CREATED,
38         INITIALIZED,
39         EXECUTING,
40         EXECUTATION_DONE,
41         DESTROYED
42     };
43 private:
44     static constexpr std::array<const char*,
45         static_cast<size_t>(InsightIntentExecuteMode::Count)> JS_FUNC_NAME_FOR_MODE {
46             "onExecuteInUIAbilityForegroundMode",
47             "onExecuteInUIAbilityBackgroundMode",
48             "onExecuteInUIExtensionAbility",
49             "onExecuteInServiceExtensionAbility"
50         };
51     static constexpr std::array<size_t,
52         static_cast<size_t>(InsightIntentExecuteMode::Count)> JS_ARGC_FOR_MODE {
53             3, 2, 3, 2 };
54 
55     explicit JsInsightIntentExecutor(JsRuntime& runtime);
56 public:
57     JsInsightIntentExecutor(const JsInsightIntentExecutor&) = delete;
58     JsInsightIntentExecutor(const JsInsightIntentExecutor&&) = delete;
59     JsInsightIntentExecutor& operator=(const JsInsightIntentExecutor&) = delete;
60     JsInsightIntentExecutor& operator=(const JsInsightIntentExecutor&&) = delete;
61     ~JsInsightIntentExecutor() override;
62 
63     /**
64      * @brief Init the intent executor and intent context.
65      *
66      * @param
67      */
68     bool Init(const InsightIntentExecutorInfo& insightIntentInfo) override;
69 
70     /**
71      * @brief Handling the life cycle execute intent.
72      *
73      * @param
74      *
75      */
76     bool HandleExecuteIntent(
77         InsightIntentExecuteMode mode,
78         const std::string& name,
79         const AAFwk::WantParams& param,
80         const std::shared_ptr<NativeReference>& pageLoader,
81         std::unique_ptr<InsightIntentExecutorAsyncCallback> callback,
82         bool& isAsync) override;
83 
GetState()84     inline State GetState() const
85     {
86         return state_;
87     }
88 
89 private:
90     static std::unique_ptr<NativeReference> LoadJsCode(
91         const InsightIntentExecutorInfo& insightIntentInfo,
92         JsRuntime& runtime);
93 
94     static bool CallJsFunctionWithResult(
95         napi_env env,
96         napi_value obj,
97         const char* funcName,
98         size_t argc,
99         const napi_value* argv,
100         napi_value& result
101     );
102 
103     bool CallJsFunctionWithResultInner(
104         const char* funcName,
105         size_t argc,
106         const napi_value* argv,
107         napi_value& result
108     );
109 
110     static std::shared_ptr<AppExecFwk::InsightIntentExecuteResult> GetResultFromJs(napi_env env,
111         napi_value resultJs);
112 
113     static napi_value ResolveCbCpp(napi_env env, napi_callback_info info);
114     static napi_value RejectCbCpp(napi_env env, napi_callback_info info);
115 
116     static void ReplyFailed(InsightIntentExecutorAsyncCallback* callback,
117         InsightIntentInnerErr innerErr = InsightIntentInnerErr::INSIGHT_INTENT_EXECUTE_REPLY_FAILED);
118     static void ReplySucceeded(InsightIntentExecutorAsyncCallback* callback,
119         std::shared_ptr<AppExecFwk::InsightIntentExecuteResult> resultCpp);
120     void ReplyFailedInner(InsightIntentInnerErr innerErr = InsightIntentInnerErr::INSIGHT_INTENT_EXECUTE_REPLY_FAILED);
121     void ReplySucceededInner(std::shared_ptr<AppExecFwk::InsightIntentExecuteResult> resultCpp);
122     bool ExecuteIntentCheckError();
123 
124     bool HandleResultReturnedFromJsFunc(napi_value resultJs);
125 
126     static bool CheckParametersUIAbilityForeground(const std::shared_ptr<NativeReference>& windowStage);
127     bool ExecuteInsightIntentUIAbilityForeground(
128         const std::string& name,
129         const AAFwk::WantParams& param,
130         const std::shared_ptr<NativeReference>& windowStage);
131 
132     static bool CheckParametersUIAbilityBackground();
133     bool ExecuteInsightIntentUIAbilityBackground(
134         const std::string& name,
135         const AAFwk::WantParams& param);
136 
137     static bool CheckParametersUIExtension(const std::shared_ptr<NativeReference>& UIExtensionContentSession);
138     bool ExecuteInsightIntentUIExtension(
139         const std::string& name,
140         const AAFwk::WantParams& param,
141         const std::shared_ptr<NativeReference>& UIExtensionContentSession);
142 
143     static bool CheckParametersServiceExtension();
144     bool ExecuteInsightIntentServiceExtension(
145         const std::string& name,
146         const AAFwk::WantParams& param);
147 
148     JsRuntime& runtime_;
149     State state_ = State::CREATED;
150     std::unique_ptr<NativeReference> jsObj_ = nullptr;
151     std::unique_ptr<NativeReference> contextObj_ = nullptr;
152     std::unique_ptr<InsightIntentExecutorAsyncCallback> callback_;
153     bool isAsync_ = false;
154 };
155 } // namespace AbilityRuntime
156 } // namespace OHOS
157 #endif // OHOS_ABILITY_RUNTIME_JS_INSIGHT_INTENT_EXECUTOR_H
158