• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     uint32_t resId_;
30     int32_t param_;
31     ResourceManager::Quantity quantity_;
32 
33     std::string path_;
34     std::string resName_;
35     int iValue_;
36     float fValue_;
37     bool bValue_;
38     uint32_t colorValue_;
39 
40     typedef napi_value (*CreateNapiValue)(napi_env env, ResMgrDataContext &context);
41     CreateNapiValue createValueFunc_;
42     std::string value_;
43     std::vector<std::string> arrayValue_;
44     std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> jsParams_;
45 
46     std::unique_ptr<uint8_t[]> mediaData;
47     size_t len_;
48 
49     napi_deferred deferred_;
50     napi_ref callbackRef_;
51 
52     std::string errMsg_;
53     int success_;
54     int errCode_;
55     uint32_t density_;
56     uint32_t iconType_;
57     uint32_t symbolValue_;
58 
59     ResourceManager::RawFileDescriptor descriptor_;
60     std::shared_ptr<ResourceManagerAddon> addon_;
61     std::shared_ptr<ResourceManager> resMgr_;
62     std::shared_ptr<ResourceManager::Resource> resource_;
63     std::shared_ptr<ResConfig> overrideResConfig_;
64 
ResMgrDataContextResMgrDataContext65     ResMgrDataContext() : work_(nullptr), resId_(0), param_(0), iValue_(0), fValue_(0.0f), bValue_(false),
66         colorValue_(0), createValueFunc_(nullptr), len_(0), deferred_(nullptr), callbackRef_(nullptr), success_(true),
67         errCode_(0), density_(0), iconType_(0), symbolValue_(0) {}
68 
69     void SetErrorMsg(const std::string &msg, bool withResId = false, int32_t errCode = 0)
70     {
71         errMsg_ = msg;
72         success_ = false;
73         errCode_ = errCode;
74         if (withResId) {
75             RESMGR_HILOGE(RESMGR_JS_TAG, "%{public}s id = %{public}d", msg.c_str(), resId_);
76         } else {
77             if (resName_.empty()) {
78                 RESMGR_HILOGW(RESMGR_JS_TAG, "%{public}s, name is empty.", msg.c_str());
79             }
80         }
81     }
82 
GetResourceManagerAddonResMgrDataContext83     static std::shared_ptr<ResourceManagerAddon> GetResourceManagerAddon(napi_env env, napi_callback_info info)
84     {
85         GET_PARAMS(env, info, 2); // 2 means get two params
86 
87         std::shared_ptr<ResourceManagerAddon> *addonPtr = nullptr;
88         napi_status status = napi_unwrap(env, thisVar, reinterpret_cast<void **>(&addonPtr));
89         if (status != napi_ok) {
90             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to unwrap");
91             return nullptr;
92         }
93         if (addonPtr == nullptr) {
94             RESMGR_HILOGE(RESMGR_JS_TAG, "Failed to unwrap, addonPtr is null");
95             return nullptr;
96         }
97         return *addonPtr;
98     }
99 };
100 } // namespace Resource
101 } // namespace Global
102 } // namespace OHOS
103 #endif