• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OHOS_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H
17 #define OHOS_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H
18 
19 #include <map>
20 #include <mutex>
21 
22 #include "ability_connect_callback_stub.h"
23 #include "ability_info.h"
24 #include "ability_manager_errors.h"
25 #include "application_info.h"
26 #include "feature_ability_common.h"
27 #include "js_runtime_utils.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 struct ConnectionKey {
32     Want want;
33     int64_t id;
34 };
35 struct KeyCompare {
operatorKeyCompare36     bool operator()(const ConnectionKey &key1, const ConnectionKey &key2) const
37     {
38         if (key1.id < key2.id) {
39             return true;
40         }
41         return false;
42     }
43 };
44 
45 class JsNapiCommon {
46 public:
47     JsNapiCommon();
48     virtual ~JsNapiCommon() = default;
49 
50     struct JsPermissionOptions {
51         bool uidFlag = false;
52         bool pidFlag = false;
53         int32_t uid = 0;
54         int32_t pid = 0;
55     };
56 
57     struct JsApplicationInfo {
58         ApplicationInfo appInfo;
59     };
60 
61     struct JsBundleName {
62         std::string name = "";
63     };
64     typedef JsBundleName JsProcessName;
65     typedef JsBundleName JsCallingBundleName;
66     typedef JsBundleName JsOrCreateLocalDir;
67     typedef JsBundleName JsFilesDir;
68     typedef JsBundleName JsCacheDir;
69     typedef JsBundleName JsCtxAppType;
70     typedef JsBundleName JsOrCreateDistributedDir;
71     typedef JsBundleName JsAppType;
72 
73     struct JsElementName {
74         std::string deviceId = "";
75         std::string bundleName = "";
76         std::string abilityName = "";
77         std::string uri = "";
78         std::string shortName = "";
79     };
80 
81     struct JsProcessInfo {
82         std::string processName = "";
83         pid_t pid = 0;
84     };
85 
86     struct JsConfigurations {
87         bool status;
88     };
89     typedef JsConfigurations JsDrawnCompleted;
90 
91     struct JsHapModuleInfo {
92         HapModuleInfo hapModInfo;
93     };
94 
95     struct JsAbilityInfoInfo {
96         AbilityInfo abilityInfo;
97     };
98 
99     struct JsWant {
100         Want want;
101     };
102 
103     NativeValue* JsConnectAbility(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
104     NativeValue* JsDisConnectAbility(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
105 
106     sptr<NAPIAbilityConnection> BuildWant(const Want &want, int64_t &id);
107     void ChangeAbilityConnection(napi_ref *callbackArray, const napi_env env, const napi_value &arg1);
108     NativeValue* JsGetContext(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
109     NativeValue* JsGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
110     NativeValue* JsIsUpdatingConfigurations(
111         NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
112     NativeValue* JsPrintDrawnCompleted(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
113     NativeValue* JsGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
114     NativeValue* JsGetCtxAppType(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
115     NativeValue* JsGetCtxHapModuleInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
116     NativeValue* JsGetAppVersionInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
117     NativeValue* JsGetApplicationContext(
118         NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
119     NativeValue* JsGetCtxAbilityInfo(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
120     NativeValue* JsGetOrCreateDistributedDir(
121         NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
122     NativeValue* JsGetDisplayOrientation(
123         NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
124     NativeValue* JsGetWant(NativeEngine &engine, NativeCallbackInfo &info, const AbilityType abilityType);
125 
126     NativeValue* CreateProcessInfo(NativeEngine &engine, const std::shared_ptr<JsProcessInfo> &processInfo);
127     NativeValue* CreateElementName(NativeEngine &engine, const std::shared_ptr<JsElementName> &elementName);
128     NativeValue* CreateHapModuleInfo(NativeEngine &engine, const std::shared_ptr<JsHapModuleInfo> &hapModInfo);
129     NativeValue* CreateModuleInfo(NativeEngine &engine, const ModuleInfo &modInfo);
130     NativeValue* CreateModuleInfos(NativeEngine &engine, const std::vector<ModuleInfo> &moduleInfos);
131     NativeValue* CreateAppInfo(NativeEngine &engine, const ApplicationInfo &appInfo);
132     NativeValue* CreateAppInfo(NativeEngine &engine, const std::shared_ptr<JsApplicationInfo> &appInfo);
133     NativeValue* CreateAbilityInfo(NativeEngine &engine, const AbilityInfo &abilityInfo);
134     NativeValue* CreateAbilityInfo(NativeEngine &engine, const std::shared_ptr<JsAbilityInfoInfo> &abilityInfo);
135     NativeValue* CreateAbilityInfos(NativeEngine &engine, const std::vector<AbilityInfo> &abilityInfos);
136     NativeValue* CreateAppVersionInfo(NativeEngine &engine, const std::shared_ptr<JsApplicationInfo> &appInfo);
137     NativeValue* CreateWant(NativeEngine &engine, const std::shared_ptr<JsWant> &want);
138     bool CheckAbilityType(const AbilityType typeWant);
139     bool UnwarpVerifyPermissionParams(NativeEngine &engine, NativeCallbackInfo &info, JsPermissionOptions &options);
140     bool GetStringsValue(NativeEngine &engine, NativeValue *object, std::vector<std::string> &strList);
141     bool GetPermissionOptions(NativeEngine &engine, NativeValue *object, JsPermissionOptions &options);
142     std::string ConvertErrorCode(int32_t errCode);
143     sptr<NAPIAbilityConnection> FindConnectionLocked(const Want &want, int64_t &id);
144     void RemoveAllCallbacksLocked();
145     bool CreateConnectionAndConnectAbilityLocked(
146         std::shared_ptr<ConnectionCallback> callback, const Want &want, int64_t &id);
147     void RemoveConnectionLocked(const Want &want);
148     Ability *ability_;
149     std::map<ConnectionKey, sptr<NAPIAbilityConnection>, KeyCompare> &connects_;
150     std::recursive_mutex &connectionsLock_;
151     int64_t &serialNumber_;
152 };
153 }  // namespace AppExecFwk
154 }  // namespace OHOS
155 #endif  // OHOS_ABILITY_RUNTIME_JS_NAPI_COMMON_ABILITY_H
156