• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_JS_ABILITY_CONTEXT_H
17 #define OHOS_ABILITY_RUNTIME_JS_ABILITY_CONTEXT_H
18 
19 #include <algorithm>
20 #include <memory>
21 #include <native_engine/native_value.h>
22 
23 #include "ability_connect_callback.h"
24 #include "foundation/ability/ability_runtime/interfaces/kits/native/ability/ability_runtime/ability_context.h"
25 #include "js_runtime.h"
26 #include "event_handler.h"
27 
28 class NativeObject;
29 class NativeReference;
30 class NativeValue;
31 
32 namespace OHOS {
33 namespace AbilityRuntime {
34 class JsAbilityContext final {
35 public:
JsAbilityContext(const std::shared_ptr<AbilityContext> & context)36     JsAbilityContext(const std::shared_ptr<AbilityContext>& context) : context_(context) {}
37     ~JsAbilityContext() = default;
38 
39     static void Finalizer(NativeEngine* engine, void* data, void* hint);
40 
41     static NativeValue* StartAbility(NativeEngine* engine, NativeCallbackInfo* info);
42     static NativeValue* StartRecentAbility(NativeEngine* engine, NativeCallbackInfo* info);
43     static NativeValue* StartAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
44     static NativeValue* StartAbilityByCall(NativeEngine* engine, NativeCallbackInfo* info);
45     static NativeValue* StartAbilityForResult(NativeEngine* engine, NativeCallbackInfo* info);
46     static NativeValue* StartAbilityForResultWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
47     static NativeValue* StartServiceExtensionAbility(NativeEngine* engine, NativeCallbackInfo* info);
48     static NativeValue* StartServiceExtensionAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
49     static NativeValue* StopServiceExtensionAbility(NativeEngine* engine, NativeCallbackInfo* info);
50     static NativeValue* StopServiceExtensionAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
51     static NativeValue* ConnectAbility(NativeEngine* engine, NativeCallbackInfo* info);
52     static NativeValue* ConnectAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
53     static NativeValue* DisconnectAbility(NativeEngine* engine, NativeCallbackInfo* info);
54     static NativeValue* TerminateSelf(NativeEngine* engine, NativeCallbackInfo* info);
55     static NativeValue* TerminateSelfWithResult(NativeEngine* engine, NativeCallbackInfo* info);
56     static NativeValue* RequestPermissionsFromUser(NativeEngine* engine, NativeCallbackInfo* info);
57     static NativeValue* RestoreWindowStage(NativeEngine* engine, NativeCallbackInfo* info);
58     static NativeValue* RequestDialogService(NativeEngine* engine, NativeCallbackInfo* info);
59     static NativeValue* IsTerminating(NativeEngine* engine, NativeCallbackInfo* info);
60 
61     static void ConfigurationUpdated(NativeEngine* engine, std::shared_ptr<NativeReference> &jsContext,
62         const std::shared_ptr<AppExecFwk::Configuration> &config);
63 
GetAbilityContext()64     std::shared_ptr<AbilityContext> GetAbilityContext()
65     {
66         return context_.lock();
67     }
68 
69 #ifdef SUPPORT_GRAPHICS
70 public:
71     static NativeValue* SetMissionLabel(NativeEngine* engine, NativeCallbackInfo* info);
72     static NativeValue* SetMissionIcon(NativeEngine* engine, NativeCallbackInfo* info);
73 
74 private:
75     NativeValue* OnSetMissionLabel(NativeEngine& engine, NativeCallbackInfo& info);
76     NativeValue* OnSetMissionIcon(NativeEngine& engine, NativeCallbackInfo& info);
77 #endif
78 
79 private:
80     NativeValue* OnStartAbility(NativeEngine& engine, NativeCallbackInfo& info, bool isStartRecent = false);
81     NativeValue* OnStartAbilityWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
82     NativeValue* OnStartAbilityByCall(NativeEngine& engine, NativeCallbackInfo& info);
83     NativeValue* OnStartAbilityForResult(NativeEngine& engine, NativeCallbackInfo& info);
84     NativeValue* OnStartAbilityForResultWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
85     NativeValue* OnStartExtensionAbility(NativeEngine& engine, NativeCallbackInfo& info);
86     NativeValue* OnStartExtensionAbilityWithAccount(NativeEngine& engine, const NativeCallbackInfo& info);
87     NativeValue* OnStopExtensionAbility(NativeEngine& engine, const NativeCallbackInfo& info);
88     NativeValue* OnStopExtensionAbilityWithAccount(NativeEngine& engine, const NativeCallbackInfo& info);
89     NativeValue* OnTerminateSelfWithResult(NativeEngine& engine, NativeCallbackInfo& info);
90     NativeValue* OnConnectAbility(NativeEngine& engine, NativeCallbackInfo& info);
91     NativeValue* OnConnectAbilityWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
92     NativeValue* OnDisconnectAbility(NativeEngine& engine, NativeCallbackInfo& info);
93     NativeValue* OnTerminateSelf(NativeEngine& engine, NativeCallbackInfo& info);
94     NativeValue* OnRequestPermissionsFromUser(NativeEngine& engine, NativeCallbackInfo& info);
95     NativeValue* OnRestoreWindowStage(NativeEngine& engine, NativeCallbackInfo& info);
96     NativeValue* OnRequestDialogService(NativeEngine& engine, NativeCallbackInfo& info);
97     NativeValue* OnIsTerminating(NativeEngine& engine, NativeCallbackInfo& info);
98 
99     static bool UnWrapWant(NativeEngine& engine, NativeValue* argv, AAFwk::Want& want);
100     static NativeValue* WrapWant(NativeEngine& engine, const AAFwk::Want& want);
101     static bool UnWrapAbilityResult(NativeEngine& engine, NativeValue* argv, int& resultCode, AAFwk::Want& want);
102     static NativeValue* WrapAbilityResult(NativeEngine& engine, const int& resultCode, const AAFwk::Want& want);
103     static NativeValue* WrapPermissionRequestResult(NativeEngine& engine,
104         const std::vector<std::string> &permissions, const std::vector<int> &grantResults);
105     void InheritWindowMode(AAFwk::Want &want);
106     static NativeValue* WrapRequestDialogResult(NativeEngine& engine, int32_t resultCode);
107 
108     std::weak_ptr<AbilityContext> context_;
109     int curRequestCode_ = 0;
110 };
111 
112 NativeValue* CreateJsAbilityContext(NativeEngine& engine, std::shared_ptr<AbilityContext> context,
113                                     DetachCallback detach, AttachCallback attach);
114 
115 struct ConnectCallback {
116     std::unique_ptr<NativeReference> jsConnectionObject_ = nullptr;
117 };
118 
119 class JSAbilityConnection : public AbilityConnectCallback {
120 public:
121     explicit JSAbilityConnection(NativeEngine& engine);
122     ~JSAbilityConnection();
123     void OnAbilityConnectDone(
124         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
125     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
126     void HandleOnAbilityConnectDone(
127         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode);
128     void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode);
129     void SetJsConnectionObject(NativeValue* jsConnectionObject);
130     void CallJsFailed(int32_t errorCode);
131     void SetConnectionId(int64_t id);
132 private:
133     NativeValue* ConvertElement(const AppExecFwk::ElementName &element);
134     NativeEngine& engine_;
135     std::unique_ptr<NativeReference> jsConnectionObject_ = nullptr;
136     int64_t connectionId_ = -1;
137 };
138 
139 struct ConnectionKey {
140     AAFwk::Want want;
141     int64_t id;
142 };
143 
144 struct KeyCompare {
operatorKeyCompare145     bool operator()(const ConnectionKey &key1, const ConnectionKey &key2) const
146     {
147         if (key1.id < key2.id) {
148             return true;
149         }
150         return false;
151     }
152 };
153 
154 static std::map<ConnectionKey, sptr<JSAbilityConnection>, KeyCompare> abilityConnects_;
155 static int64_t g_serialNumber = 0;
156 static std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
157 }  // namespace AbilityRuntime
158 }  // namespace OHOS
159 #endif  // OHOS_ABILITY_RUNTIME_JS_ABILITY_CONTEXT_H
160