1 /*
2  * Copyright (c) 2022-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 
16 #ifndef OHOS_ABILITY_RUNTIME_JS_WORKER_H
17 #define OHOS_ABILITY_RUNTIME_JS_WORKER_H
18 
19 #include <string>
20 #include "bundle_mgr_proxy.h"
21 #include "js_environment_impl.h"
22 #include "native_engine/native_engine.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 void StartDebuggerInWorkerModule(bool isDebugApp, bool isNativeStart);
27 void InitWorkerFunc(NativeEngine* nativeEngine);
28 void OffWorkerFunc(NativeEngine* nativeEngine);
29 int32_t GetContainerId();
30 void UpdateContainerScope(int32_t id);
31 void RestoreContainerScope(int32_t id);
32 void SetJsFramework();
33 
34 class AssetHelper final {
35 public:
AssetHelper(std::shared_ptr<JsEnv::WorkerInfo> workerInfo)36     explicit AssetHelper(std::shared_ptr<JsEnv::WorkerInfo> workerInfo) : workerInfo_(workerInfo)
37     {
38         panda::panda_file::StringPacProtect codePath = panda::panda_file::StringPacProtect(workerInfo_->codePath);
39         if (!(codePath.GetOriginString()).empty() && (codePath.GetOriginString()).back() != '/') {
40             (workerInfo_->codePath).Append('/');
41         }
42     }
43 
44     virtual ~AssetHelper();
45 
46     void operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::vector<uint8_t>& content,
47         std::string& ami, bool& useSecureMem, bool isRestricted = false);
48 
49 private:
50     std::string NormalizedFileName(const std::string& fileName) const;
51 
52     bool ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, std::vector<uint8_t>& content,
53         bool& useSecureMem, bool isRestricted);
54 
55     bool ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, std::vector<uint8_t>& content,
56         bool& useSecureMem, bool isRestricted);
57 
58     void GetAmi(std::string& ami, const std::string& filePath);
59 
60     bool GetSafeData(const std::string& ami, uint8_t** buff, size_t* buffSize);
61 
62     bool GetIsStageModel();
63 
64     std::shared_ptr<JsEnv::WorkerInfo> workerInfo_ = nullptr;
65     int fd_ = -1;
66 };
67 
68 } // namespace AbilityRuntime
69 } // namespace OHOS
70 
71 #endif // OHOS_ABILITY_RUNTIME_JS_WORKER_H