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 #ifndef RESOURCE_MANAGER_DATA_H
16 #define RESOURCE_MANAGER_DATA_H
17 #include "napi/native_api.h"
18 #include "napi/native_node_api.h"
19 #include "resource_manager.h"
20 #include "resource_manager_addon.h"
21 #include "hilog_wrapper.h"
22 namespace OHOS {
23 namespace Global {
24 namespace Resource {
25 struct ResMgrDataContext {
26     napi_async_work work_;
27 
28     std::string bundleName_;
29     int32_t resId_;
30     int32_t param_;
31 
32     std::string path_;
33     std::string resName_;
34     int iValue_;
35     float fValue_;
36     bool bValue_;
37     uint32_t colorValue_;
38 
39     typedef napi_value (*CreateNapiValue)(napi_env env, ResMgrDataContext &context);
40     CreateNapiValue createValueFunc_;
41     std::string value_;
42     std::vector<std::string> arrayValue_;
43     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams_;
44 
45     std::unique_ptr<uint8_t[]> mediaData;
46     size_t len_;
47 
48     napi_deferred deferred_;
49     napi_ref callbackRef_;
50 
51     std::string errMsg_;
52     int success_;
53     int errCode_;
54     uint32_t density_;
55     uint32_t iconType_;
56     uint32_t symbolValue_;
57 
58     ResourceManager::RawFileDescriptor descriptor_;
59     std::shared_ptr<ResourceManagerAddon> addon_;
60     std::shared_ptr<ResourceManager> resMgr_;
61     std::shared_ptr<ResourceManager::Resource> resource_;
62     std::shared_ptr<ResConfig> overrideResConfig_;
63 
ResMgrDataContextResMgrDataContext64     ResMgrDataContext() : work_(nullptr), resId_(0), param_(0), iValue_(0), fValue_(0.0f), bValue_(false),
65         colorValue_(0), createValueFunc_(nullptr), len_(0), deferred_(nullptr), callbackRef_(nullptr), success_(true),
66         errCode_(0), density_(0), iconType_(0), symbolValue_(0) {}
67 
68     void SetErrorMsg(const std::string &msg, bool withResId = false, int32_t errCode = 0)
69     {
70         errMsg_ = msg;
71         success_ = false;
72         errCode_ = errCode;
73         if (withResId) {
74             RESMGR_HILOGE(RESMGR_JS_TAG, "%{public}s id = %{public}d", msg.c_str(), resId_);
75         } else {
76             RESMGR_HILOGE(RESMGR_JS_TAG, "%{public}s name = %{public}s", msg.c_str(), resName_.c_str());
77         }
78     }
79 
GetResourceManagerAddonResMgrDataContext80     static std::shared_ptr<ResourceManagerAddon> GetResourceManagerAddon(napi_env env, napi_callback_info info)
81     {
82         GET_PARAMS(env, info, 2); // 2 means get two params
83 
84         std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
85         napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&addonPtr));
86         if (status != napi_ok) {
87             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to unwrap");
88             return nullptr;
89         }
90         if (addonPtr == nullptr) {
91             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to unwrap, addonPtr is null");
92             return nullptr;
93         }
94         return *addonPtr;
95     }
96 };
97 } // namespace Resource
98 } // namespace Global
99 } // namespace OHOS
100 #endif