1 /*
2 * Copyright (c) 2023-2024 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 "form_command_queue.h"
17 #include "fms_log_wrapper.h"
18 #include "form_command_process_strategy.h"
19 #include "form_status_mgr.h"
20 #include "form_status_queue.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
FormCommandQueue(const int64_t & formId)24 FormCommandQueue::FormCommandQueue(const int64_t &formId) : formId_(formId)
25 {
26 HILOG_INFO("create FormCommandQueue, formId :%{public}" PRId64 ". ", formId_);
27 }
28
~FormCommandQueue()29 FormCommandQueue::~FormCommandQueue()
30 {
31 HILOG_INFO("destroy FormCommandQueue, formId :%{public}" PRId64 ". ", formId_);
32 }
33
PushFormCommand(FormCommand formCommand)34 void FormCommandQueue::PushFormCommand(FormCommand formCommand)
35 {
36 std::lock_guard<std::mutex> lock(formCommandQueueMutex_);
37 HILOG_INFO("PushFormCommand, formId :%{public}" PRId64 ". ", formId_);
38 CommandQueue.push(formCommand);
39
40 // Process the command queue after adding a new command.
41 ProcessCommandQueue();
42 }
43
PopFormCommand(FormCommand & formCommand)44 bool FormCommandQueue::PopFormCommand(FormCommand &formCommand)
45 {
46 std::lock_guard<std::mutex> lock(formCommandQueueMutex_);
47 if (CommandQueue.empty()) {
48 HILOG_INFO("empty CommandQueue, formId :%{public}" PRId64 ". ", formId_);
49 return false;
50 }
51 formCommand = CommandQueue.front();
52 CommandQueue.pop();
53 return true;
54 }
55
IsCommondQueueEmpty()56 bool FormCommandQueue::IsCommondQueueEmpty()
57 {
58 std::lock_guard<std::mutex> lock(formCommandQueueMutex_);
59 if (CommandQueue.empty()) {
60 HILOG_INFO("empty CommandQueue, formId :%{public}" PRId64 ". ", formId_);
61 return true;
62 }
63 HILOG_INFO("CommandQueue not empty, formId :%{public}" PRId64 ". ", formId_);
64 return false;
65 }
66
ProcessCommandQueue()67 void FormCommandQueue::ProcessCommandQueue()
68 {
69 if (CommandQueue.empty()) {
70 HILOG_ERROR("empty CommandQueue, formId :%{public}" PRId64 ". ", formId_);
71 return;
72 }
73
74 FormCommand latestFormCommand = CommandQueue.back();
75 FormStatus currentFormStatus;
76 if (!FormStatusMgr::GetInstance().GetFormStatus(latestFormCommand.getFormId(), currentFormStatus)) {
77 HILOG_ERROR("GetFormStatus fail, formId :%{public}" PRId64 ". ", formId_);
78 return;
79 }
80
81 auto strategy = CommandProcessStrategyFactory::CreateStrategy(
82 latestFormCommand.getEventMsg().first, currentFormStatus);
83 if (!strategy) {
84 HILOG_ERROR("strategy fail, formId :%{public}" PRId64 ". ", formId_);
85 return;
86 }
87
88 strategy->ProcessCommandQueue(CommandQueue, formId_);
89 }
90 } // namespace AppExecFwk
91 } // namespace OHOS
92