• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 ABILITY_RUNTIME_JS_ABILITY_CONTEXT_H
17 #define 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/aafwk/standard/frameworks/kits/ability/ability_runtime/include/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* StartAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
43     static NativeValue* StartAbilityByCall(NativeEngine* engine, NativeCallbackInfo* info);
44     static NativeValue* StartAbilityForResult(NativeEngine* engine, NativeCallbackInfo* info);
45     static NativeValue* StartAbilityForResultWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
46     static NativeValue* ConnectAbility(NativeEngine* engine, NativeCallbackInfo* info);
47     static NativeValue* ConnectAbilityWithAccount(NativeEngine* engine, NativeCallbackInfo* info);
48     static NativeValue* DisconnectAbility(NativeEngine* engine, NativeCallbackInfo* info);
49     static NativeValue* TerminateSelf(NativeEngine* engine, NativeCallbackInfo* info);
50     static NativeValue* TerminateSelfWithResult(NativeEngine* engine, NativeCallbackInfo* info);
51     static NativeValue* RequestPermissionsFromUser(NativeEngine* engine, NativeCallbackInfo* info);
52     static NativeValue* RestoreWindowStage(NativeEngine* engine, NativeCallbackInfo* info);
53     static NativeValue* SetMissionLabel(NativeEngine* engine, NativeCallbackInfo* info);
54 
55 #ifdef SUPPORT_GRAPHICS
56     static NativeValue* SetMissionIcon(NativeEngine* engine, NativeCallbackInfo* info);
57 #endif
58 
59     static void ConfigurationUpdated(NativeEngine* engine, std::shared_ptr<NativeReference> &jsContext,
60         const std::shared_ptr<AppExecFwk::Configuration> &config);
61 
GetAbilityContext()62     std::shared_ptr<AbilityContext> GetAbilityContext()
63     {
64         return context_.lock();
65     }
66 
67 private:
68     NativeValue* OnStartAbility(NativeEngine& engine, NativeCallbackInfo& info);
69     NativeValue* OnStartAbilityWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
70     NativeValue* OnStartAbilityByCall(NativeEngine& engine, NativeCallbackInfo& info);
71     NativeValue* OnStartAbilityForResult(NativeEngine& engine, NativeCallbackInfo& info);
72     NativeValue* OnStartAbilityForResultWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
73     NativeValue* OnTerminateSelfWithResult(NativeEngine& engine, NativeCallbackInfo& info);
74     NativeValue* OnConnectAbility(NativeEngine& engine, NativeCallbackInfo& info);
75     NativeValue* OnConnectAbilityWithAccount(NativeEngine& engine, NativeCallbackInfo& info);
76     NativeValue* OnDisconnectAbility(NativeEngine& engine, NativeCallbackInfo& info);
77     NativeValue* OnTerminateSelf(NativeEngine& engine, NativeCallbackInfo& info);
78     NativeValue* OnRequestPermissionsFromUser(NativeEngine& engine, NativeCallbackInfo& info);
79     NativeValue* OnRestoreWindowStage(NativeEngine& engine, NativeCallbackInfo& info);
80     NativeValue* OnSetMissionLabel(NativeEngine& engine, NativeCallbackInfo& info);
81 
82 #ifdef SUPPORT_GRAPHICS
83     NativeValue* OnSetMissionIcon(NativeEngine& engine, NativeCallbackInfo& info);
84 #endif
85 
86     static bool UnWrapWant(NativeEngine& engine, NativeValue* argv, AAFwk::Want& want);
87     static NativeValue* WrapWant(NativeEngine& engine, const AAFwk::Want& want);
88     static bool UnWrapAbilityResult(NativeEngine& engine, NativeValue* argv, int& resultCode, AAFwk::Want& want);
89     static NativeValue* WrapAbilityResult(NativeEngine& engine, const int& resultCode, const AAFwk::Want& want);
90     static NativeValue* WrapPermissionRequestResult(NativeEngine& engine,
91         const std::vector<std::string> &permissions, const std::vector<int> &grantResults);
92     void InheritWindowMode(AAFwk::Want &want);
93 
94     std::weak_ptr<AbilityContext> context_;
95     int curRequestCode_ = 0;
96 };
97 
98 NativeValue* CreateJsAbilityContext(NativeEngine& engine, std::shared_ptr<AbilityContext> context);
99 
100 class JSAbilityConnection : public AbilityConnectCallback {
101 public:
102     explicit JSAbilityConnection(NativeEngine& engine);
103     ~JSAbilityConnection();
104     void OnAbilityConnectDone(
105         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
106     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
107     void HandleOnAbilityConnectDone(
108         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode);
109     void HandleOnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode);
110     void SetJsConnectionObject(NativeValue* jsConnectionObject);
111     void CallJsFailed(int32_t errorCode);
112 private:
113     NativeValue* ConvertElement(const AppExecFwk::ElementName &element);
114     NativeEngine& engine_;
115     std::unique_ptr<NativeReference> jsConnectionObject_ = nullptr;
116 };
117 
118 struct ConnectionKey {
119     AAFwk::Want want;
120     int64_t id;
121 };
122 
123 struct KeyCompare {
operatorKeyCompare124     bool operator()(const ConnectionKey &key1, const ConnectionKey &key2) const
125     {
126         if (key1.id < key2.id) {
127             return true;
128         }
129         return false;
130     }
131 };
132 
133 static std::map<ConnectionKey, sptr<JSAbilityConnection>, KeyCompare> abilityConnects_;
134 static int64_t g_serialNumber = 0;
135 static std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
136 }  // namespace AbilityRuntime
137 }  // namespace OHOS
138 #endif  // ABILITY_RUNTIME_JS_ABILITY_CONTEXT_H