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 "core/common/form_manager.h"
17
18 #include "unistd.h"
19
20 #include "base/log/log.h"
21 #include "base/utils/utils.h"
22
23 namespace OHOS::Ace {
FormManager()24 FormManager::FormManager() {}
25
~FormManager()26 FormManager::~FormManager() {}
27
AddSubContainer(int64_t formId,const RefPtr<SubContainer> & subContainer)28 void FormManager::AddSubContainer(int64_t formId, const RefPtr<SubContainer>& subContainer)
29 {
30 std::lock_guard<std::mutex> lock(mutex_);
31 auto result = subContainerMap_.try_emplace(formId, subContainer);
32 if (!result.second) {
33 LOGW("already have subContainer of this instance");
34 }
35 }
36
RemoveSubContainer(int64_t formId)37 void FormManager::RemoveSubContainer(int64_t formId)
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 subContainerMap_.erase(formId);
41 }
42
GetSubContainer(int64_t formId)43 RefPtr<SubContainer> FormManager::GetSubContainer(int64_t formId)
44 {
45 std::lock_guard<std::mutex> lock(mutex_);
46 auto subContainer = subContainerMap_.find(formId);
47 if (subContainer != subContainerMap_.end()) {
48 return subContainer->second;
49 } else {
50 return nullptr;
51 }
52 }
53
AddNonmatchedContainer(const std::string & cardKey,const RefPtr<SubContainer> & subContainer)54 void FormManager::AddNonmatchedContainer(const std::string& cardKey, const RefPtr<SubContainer>& subContainer)
55 {
56 std::lock_guard<std::mutex> lock(nonmatchedContainerMutex_);
57 auto result = nonmatchedContainerMap_.try_emplace(cardKey, subContainer);
58 if (!result.second) {
59 LOGW("already have subContainer of this key: %{public}s", cardKey.c_str());
60 }
61 }
62
MatchSubContainerWithFormId(int64_t formId,const std::string & cardKey)63 RefPtr<SubContainer> FormManager::MatchSubContainerWithFormId(int64_t formId, const std::string& cardKey)
64 {
65 std::lock_guard<std::mutex> lock(nonmatchedContainerMutex_);
66 auto iter = nonmatchedContainerMap_.find(cardKey);
67 if (iter == nonmatchedContainerMap_.end()) {
68 LOGW("no subcontainer of key: %{private}s", cardKey.c_str());
69 return nullptr;
70 }
71 auto subContainer = iter->second;
72 AddSubContainer(formId, subContainer);
73 nonmatchedContainerMap_.erase(iter);
74 return subContainer;
75 }
Dump(const std::vector<std::string> & params)76 void FormManager::Dump(const std::vector<std::string>& params)
77 {
78 #ifdef FORM_SUPPORTED
79 std::unordered_map<std::string, RefPtr<SubContainer>> copied;
80 {
81 std::lock_guard<std::mutex> lock(mutex_);
82 copied = nonmatchedContainerMap_;
83 }
84 for (const auto& container : copied) {
85 auto pipelineContext = container.second->GetPipelineContext();
86 if (!pipelineContext) {
87 LOGW("the pipeline context is nullptr, pa container");
88 continue;
89 }
90 pipelineContext->GetTaskExecutor()->PostSyncTask(
91 [params, container = container.second]() { container->Dump(params); }, TaskExecutor::TaskType::UI);
92 }
93 #endif
94 }
95
SetFormUtils(const std::shared_ptr<FormUtils> & formUtils)96 void FormManager::SetFormUtils(const std::shared_ptr<FormUtils>& formUtils)
97 {
98 std::lock_guard<std::mutex> lock(formUtilsMutex_);
99 CHECK_NULL_VOID_NOLOG(formUtils);
100 formUtils_ = formUtils;
101 }
102
GetFormUtils()103 std::shared_ptr<FormUtils> FormManager::GetFormUtils()
104 {
105 std::lock_guard<std::mutex> lock(formUtilsMutex_);
106 CHECK_NULL_RETURN_NOLOG(formUtils_, nullptr);
107 return formUtils_;
108 }
109
110 } // namespace OHOS::Ace
111