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_process_strategy.h"
17 #include "fms_log_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
ProcessCommandQueue(std::queue<FormCommand> & commandQueue,const int64_t formId)21 void RenderFormStrategy::ProcessCommandQueue(std::queue<FormCommand> &commandQueue, const int64_t formId)
22 {
23 if (commandQueue.empty()) {
24 HILOG_ERROR("empty commandQueue, formId :%{public}" PRId64 ". ", formId);
25 return;
26 }
27
28 std::queue<FormCommand> tempCommandQueue;
29 while (!commandQueue.empty()) {
30 auto frontCommand = commandQueue.front();
31 commandQueue.pop();
32 if (frontCommand.getEventMsg().first == TaskCommandType::RENDER_FORM) {
33 tempCommandQueue.push(frontCommand);
34 }
35 }
36
37 while (!tempCommandQueue.empty()) {
38 auto frontCommand = tempCommandQueue.front();
39 tempCommandQueue.pop();
40 commandQueue.push(frontCommand);
41 }
42 HILOG_DEBUG("HandleRenderCommand End, formId :%{public}" PRId64 ". ", formId);
43 }
44
ProcessCommandQueue(std::queue<FormCommand> & commandQueue,const int64_t formId)45 void SimilarStatusStrategy::ProcessCommandQueue(std::queue<FormCommand> &commandQueue, const int64_t formId)
46 {
47 if (commandQueue.empty()) {
48 HILOG_ERROR("empty commandQueue, formId :%{public}" PRId64 ". ", formId);
49 return;
50 }
51
52 std::queue<FormCommand> tempCommandQueue;
53 while (!commandQueue.empty()) {
54 auto frontCommand = commandQueue.front();
55 commandQueue.pop();
56 if (frontCommand.getEventMsg().first == TaskCommandType::RENDER_FORM) {
57 tempCommandQueue.push(frontCommand);
58 }
59 }
60
61 while (!tempCommandQueue.empty()) {
62 auto frontCommand = tempCommandQueue.front();
63 tempCommandQueue.pop();
64 commandQueue.push(frontCommand);
65 }
66 HILOG_DEBUG("HandleSimilarCommand End, formId :%{public}" PRId64 ". ", formId);
67 }
68
ProcessCommandQueue(std::queue<FormCommand> & commandQueue,const int64_t formId)69 void DifferentStatusStrategy::ProcessCommandQueue(std::queue<FormCommand> &commandQueue, const int64_t formId)
70 {
71 if (commandQueue.empty()) {
72 HILOG_ERROR("empty CommandQueue, formId :%{public}" PRId64 ". ", formId);
73 return;
74 }
75
76 auto backCommand = commandQueue.back();
77 std::queue<FormCommand> tempCommandQueue;
78 while (!commandQueue.empty()) {
79 auto frontCommand = commandQueue.front();
80 commandQueue.pop();
81 if (frontCommand.getEventMsg().first == TaskCommandType::RENDER_FORM) {
82 tempCommandQueue.push(frontCommand);
83 }
84 }
85
86 while (!tempCommandQueue.empty()) {
87 auto frontCommand = tempCommandQueue.front();
88 tempCommandQueue.pop();
89 commandQueue.push(frontCommand);
90 }
91 commandQueue.push(backCommand);
92 HILOG_DEBUG("HandleDifferentCommand End, formId :%{public}" PRId64 ". ", formId);
93 }
94
CreateStrategy(TaskCommandType taskCommandType,FormStatus currentFormStatus)95 std::unique_ptr<FormCommandProcessStrategy> CommandProcessStrategyFactory::CreateStrategy(
96 TaskCommandType taskCommandType, FormStatus currentFormStatus)
97 {
98 if (taskCommandType == TaskCommandType::RENDER_FORM) {
99 return std::make_unique<RenderFormStrategy>();
100 } else if (taskCommandType == TaskCommandType::RECOVER_FORM) {
101 switch (currentFormStatus) {
102 case FormStatus::UNPROCESSABLE:
103 return std::make_unique<SimilarStatusStrategy>();
104 case FormStatus::RECOVERED:
105 return std::make_unique<SimilarStatusStrategy>();
106 case FormStatus::RECOVERING:
107 return std::make_unique<SimilarStatusStrategy>();
108 case FormStatus::RECYCLED:
109 return std::make_unique<DifferentStatusStrategy>();
110 case FormStatus::RECYCLING:
111 return std::make_unique<DifferentStatusStrategy>();
112 default:
113 return nullptr;
114 }
115 } else if (taskCommandType == TaskCommandType::RECYCLE_FORM) {
116 switch (currentFormStatus) {
117 case FormStatus::UNPROCESSABLE:
118 return std::make_unique<DifferentStatusStrategy>();
119 case FormStatus::RECOVERED:
120 return std::make_unique<DifferentStatusStrategy>();
121 case FormStatus::RECOVERING:
122 return std::make_unique<DifferentStatusStrategy>();
123 case FormStatus::RECYCLED:
124 return std::make_unique<SimilarStatusStrategy>();
125 case FormStatus::RECYCLING:
126 return std::make_unique<SimilarStatusStrategy>();
127 default:
128 return nullptr;
129 }
130 }
131 return nullptr;
132 }
133 } // namespace AppExecFwk
134 } // namespace OHOS
135