• 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 RESOURCE_MANAGER_ADDON_H
17 #define RESOURCE_MANAGER_ADDON_H
18 
19 #include <cstddef>
20 #include <memory>
21 #include <string>
22 
23 #include "foundation/ability/ability_runtime/interfaces/kits/native/appkit/ability_runtime/context/context.h"
24 #include "napi/native_api.h"
25 #include "napi/native_node_api.h"
26 #include "resource_manager.h"
27 
28 namespace OHOS {
29 namespace Global {
30 namespace Resource {
31 class ResourceManagerAddon {
32 public:
33     static napi_value Create(
34         napi_env env, const std::string& bundleName,
35             const std::shared_ptr<ResourceManager>& resMgr, std::shared_ptr<AbilityRuntime::Context> context);
36     static bool Init(napi_env env);
37 
38     static void Destructor(napi_env env, void *nativeObject, void *finalize_hint);
39 
40     ResourceManagerAddon(const std::string& bundleName, const std::shared_ptr<ResourceManager>& resMgr,
41         const std::shared_ptr<AbilityRuntime::Context>& context);
42 
43     ~ResourceManagerAddon();
44 
GetResMgr()45     inline std::shared_ptr<ResourceManager> GetResMgr()
46     {
47         return resMgr_;
48     }
49 
GetContext()50     inline std::shared_ptr<AbilityRuntime::Context> GetContext()
51     {
52         return context_;
53     }
54 
55     std::string GetLocale(std::unique_ptr<ResConfig> &cfg);
56 
57 private:
58     static int GetResId(napi_env env, size_t argc, napi_value *argv);
59 
60     static napi_value ProcessOnlyIdParam(napi_env env, napi_callback_info info, const std::string &name,
61         napi_async_execute_callback execute);
62 
63     static napi_value GetString(napi_env env, napi_callback_info info);
64 
65     static napi_value GetStringArray(napi_env env, napi_callback_info info);
66 
67     static napi_value GetMedia(napi_env env, napi_callback_info info);
68 
69     static napi_value GetMediaBase64(napi_env env, napi_callback_info info);
70 
71     static napi_value ProcessNoParam(napi_env env, napi_callback_info info, const std::string &name,
72         napi_async_execute_callback execute);
73 
74     static napi_value GetConfiguration(napi_env env, napi_callback_info info);
75 
76     static napi_value GetDeviceCapability(napi_env env, napi_callback_info info);
77 
78     static napi_value GetPluralString(napi_env env, napi_callback_info info);
79 
80     static napi_value New(napi_env env, napi_callback_info info);
81 
82     static napi_value GetRawFile(napi_env env, napi_callback_info info);
83 
84     static std::string GetResNameOrPath(napi_env env, size_t argc, napi_value *argv);
85 
86     static napi_value GetRawFileDescriptor(napi_env env, napi_callback_info info);
87 
88     static napi_value CloseRawFileDescriptor(napi_env env, napi_callback_info info);
89 
90     static napi_value Release(napi_env env, napi_callback_info info);
91 
92     static std::string GetResName(napi_env env, size_t argc, napi_value *argv);
93 
94     static napi_value GetMediaByName(napi_env env, napi_callback_info info);
95 
96     static napi_value GetMediaBase64ByName(napi_env env, napi_callback_info info);
97 
98     static napi_value GetStringByName(napi_env env, napi_callback_info info);
99 
100     static napi_value GetStringArrayByName(napi_env env, napi_callback_info info);
101 
102     static napi_value GetPluralStringByName(napi_env env, napi_callback_info info);
103 
104     static napi_value ProcessIdNameParam(napi_env env, napi_callback_info info, const std::string& name,
105         napi_async_execute_callback execute);
106 
107     static napi_value GetNumber(napi_env env, napi_callback_info info);
108 
109     static napi_value GetNumberByName(napi_env env, napi_callback_info info);
110 
111     static napi_value GetBoolean(napi_env env, napi_callback_info info);
112 
113     static napi_value GetBooleanByName(napi_env env, napi_callback_info info);
114 
115     static napi_value GetStringSync(napi_env env, napi_callback_info info);
116 
117     static napi_value GetStringByNameSync(napi_env env, napi_callback_info info);
118 
119     static int32_t GetResourceObject(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
120         napi_value &value);
121 
122     static napi_valuetype GetType(napi_env env, napi_value value);
123 
124     static bool GetResourceObjectName(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
125         napi_value &value, int32_t type);
126 
127     static bool GetResourceObjectId(napi_env env, std::shared_ptr<ResourceManager::Resource> &resourcePtr,
128         napi_value &value);
129 
130     static napi_value ProcessIdParamV9(napi_env env, napi_callback_info info, const std::string &name,
131         napi_async_execute_callback execute);
132 
133     static napi_value ProcessNameParamV9(napi_env env, napi_callback_info info, const std::string &name,
134         napi_async_execute_callback execute);
135 
136     static napi_value ProcessResourceParamV9(napi_env env, napi_callback_info info, const std::string &name,
137         napi_async_execute_callback execute);
138 
139     static napi_value GetStringValue(napi_env env, napi_callback_info info);
140 
141     static napi_value GetStringArrayValue(napi_env env, napi_callback_info info);
142 
143     static napi_value GetPluralStringValue(napi_env env, napi_callback_info info);
144 
145     static napi_value GetMediaContent(napi_env env, napi_callback_info info);
146 
147     static napi_value GetMediaContentBase64(napi_env env, napi_callback_info info);
148 
149     static napi_value GetRawFileContent(napi_env env, napi_callback_info info);
150 
151     static napi_value GetRawFd(napi_env env, napi_callback_info info);
152 
153     static napi_value CloseRawFd(napi_env env, napi_callback_info info);
154 
155     std::string bundleName_;
156     std::shared_ptr<ResourceManager> resMgr_;
157     std::shared_ptr<AbilityRuntime::Context> context_;
158 };
159 
160 struct ResMgrAsyncContext {
161     napi_async_work work_;
162 
163     std::string bundleName_;
164     int32_t resId_;
165     int32_t param_;
166 
167     std::string path_;
168     std::string resName_;
169     int iValue_;
170     float fValue_;
171     bool bValue_;
172 
173     typedef napi_value (*CreateNapiValue)(napi_env env, ResMgrAsyncContext &context);
174     CreateNapiValue createValueFunc_;
175     std::string value_;
176     std::vector<std::string> arrayValue_;
177 
178     std::unique_ptr<uint8_t[]> mediaData;
179     int len_;
180 
181     napi_deferred deferred_;
182     napi_ref callbackRef_;
183 
184     std::string errMsg_;
185     int success_;
186     int errCode_;
187 
188     std::shared_ptr<ResourceManagerAddon> addon_;
189     std::shared_ptr<ResourceManager> resMgr_;
190     std::shared_ptr<ResourceManager::Resource> resource_;
191 
ResMgrAsyncContextResMgrAsyncContext192     ResMgrAsyncContext() : work_(nullptr), resId_(0), param_(0), iValue_(0), fValue_(0.0f), bValue_(false),
193         createValueFunc_(nullptr), len_(0), deferred_(nullptr), callbackRef_(nullptr), success_(true), errCode_(0) {}
194 
195     void SetErrorMsg(const std::string &msg, bool withResId = false, int32_t errCode = 0);
196 
197     static void Complete(napi_env env, napi_status status, void* data);
198 
199     static bool GetHapResourceManager(const ResMgrAsyncContext* asyncContext, std::shared_ptr<ResourceManager> &resMgr,
200         int32_t &resId);
201 
202     static napi_value getResult(napi_env env, std::unique_ptr<ResMgrAsyncContext> &asyncContext,
203         const std::string &name, napi_async_execute_callback &execute);
204 
205     static void NapiThrow(napi_env env, int32_t errCode);
206 };
207 } // namespace Resource
208 } // namespace Global
209 } // namespace OHOS
210 #endif