1 /*
2  * Copyright (c) 2021 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 USCRIPT_MANAGER_IMPL_H
16 #define USCRIPT_MANAGER_IMPL_H
17 
18 #include <map>
19 #include <memory>
20 #include <vector>
21 #include "pkg_manager.h"
22 #include "script_instruction.h"
23 #include "script_manager.h"
24 #include "thread_pool.h"
25 
26 namespace Uscript {
27 class ScriptInstructionHelper;
28 class ScriptManagerImpl : public ScriptManager {
29 public:
30     friend class ScriptInterpreter;
31     friend class ScriptInstructionHelper;
32 
ScriptManagerImpl(UScriptEnv * env)33     explicit ScriptManagerImpl(UScriptEnv *env) : scriptEnv_(env) {}
ScriptManagerImpl(UScriptEnv * env,const Hpackage::HashDataVerifier * verifier)34     explicit ScriptManagerImpl(UScriptEnv *env, const Hpackage::HashDataVerifier *verifier)
35         : scriptEnv_(env), scriptVerifier_(verifier) {}
36     virtual ~ScriptManagerImpl();
37     int32_t Init();
38     int32_t ExecuteScript(int32_t priority) override;
39 private:
40     int32_t ExtractAndExecuteScript(Hpackage::PkgManager::PkgManagerPtr manager,
41         const std::string &scriptName);
42     int32_t AddScript(const std::string &scriptName, int32_t priority);
43     int32_t AddInstruction(const std::string &instrName, const UScriptInstructionPtr instruction);
44     UScriptInstruction* FindInstruction(const std::string &instrName);
45     UScriptEnv* GetScriptEnv(const std::string &instrName) const;
46     int32_t RegisterInstruction(ScriptInstructionHelper &helper);
47 private:
48     static const int32_t MAX_THREAD_POOL = 4;
49     std::map<std::string, UScriptInstructionPtr> scriptInstructions_;
50     std::vector<std::string> scriptFiles_[MAX_PRIORITY] {};
51     ThreadPool *threadPool_ = nullptr;
52     UScriptEnv *scriptEnv_ = nullptr;
53     const Hpackage::HashDataVerifier *scriptVerifier_ = nullptr;
54 };
55 } // namespace Uscript
56 #endif
57