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 
16 #include "applypatch/command_function.h"
17 #include "command_process.h"
18 
19 namespace Updater {
RegistBlockUpdateCommandFunction(void)20 extern "C" __attribute__((constructor)) void RegistBlockUpdateCommandFunction(void)
21 {
22     const std::unordered_map<std::string, std::function<std::unique_ptr<CommandFunction>()>> COMMANDFUNC = {
23         { "abort", []() { return std::make_unique<AbortCommandFn>(); } },
24         { "bsdiff", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
25         { "new", []() { return std::make_unique<NewCommandFn>(); } },
26         { "pkgdiff", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
27         { "zero", []() { return std::make_unique<ZeroAndEraseCommandFn>(); } },
28         { "erase", []() { return std::make_unique<ZeroAndEraseCommandFn>(); } },
29         { "free", []() { return std::make_unique<FreeCommandFn>(); } },
30         { "move", []() { return std::make_unique<DiffAndMoveCommandFn>(); } },
31         { "stash", []() { return std::make_unique<StashCommandFn>(); } }
32     };
33     for (auto &iter : COMMANDFUNC) {
34         CommandFunctionFactory::GetInstance().RegistCommandFunction(iter.first, iter.second());
35     }
36 }
37 
GetInstance()38 CommandFunctionFactory &CommandFunctionFactory::GetInstance()
39 {
40     static CommandFunctionFactory instance;
41     return instance;
42 }
43 
GetCommandFunction(std::string command)44 CommandFunction* CommandFunctionFactory::GetCommandFunction(std::string command)
45 {
46     auto iter = commandFunctionMap_.find(command);
47     if (iter != commandFunctionMap_.end()) {
48         return iter->second.get();
49     }
50     return nullptr;
51 }
52 
RegistCommandFunction(std::string command,std::unique_ptr<CommandFunction> commandFunction)53 void CommandFunctionFactory::RegistCommandFunction(std::string command,
54                                                    std::unique_ptr<CommandFunction> commandFunction)
55 {
56     commandFunctionMap_.emplace(command, std::move(commandFunction));
57 }
58 }