1 /*
2 * Copyright (c) 2022-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 "quick_fix_callback_with_record.h"
17
18 #include "hilog_tag_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
~QuickFixCallbackWithRecord()22 QuickFixCallbackWithRecord::~QuickFixCallbackWithRecord()
23 {
24 TAG_LOGD(AAFwkTag::APPMGR, "destroyed.");
25 }
26
OnLoadPatchDone(int32_t resultCode,int32_t recordId)27 void QuickFixCallbackWithRecord::OnLoadPatchDone(int32_t resultCode, int32_t recordId)
28 {
29 TAG_LOGD(AAFwkTag::APPMGR, "called");
30 ProcessCallback(resultCode, recordId);
31 if (IsRecordListEmpty() && callback_ != nullptr) {
32 callback_->OnLoadPatchDone(finalResult.load(), recordId);
33 }
34 }
35
OnUnloadPatchDone(int32_t resultCode,int32_t recordId)36 void QuickFixCallbackWithRecord::OnUnloadPatchDone(int32_t resultCode, int32_t recordId)
37 {
38 TAG_LOGD(AAFwkTag::APPMGR, "called");
39 ProcessCallback(resultCode, recordId);
40 if (IsRecordListEmpty() && callback_ != nullptr) {
41 callback_->OnUnloadPatchDone(finalResult.load(), recordId);
42 }
43 }
44
OnReloadPageDone(int32_t resultCode,int32_t recordId)45 void QuickFixCallbackWithRecord::OnReloadPageDone(int32_t resultCode, int32_t recordId)
46 {
47 TAG_LOGD(AAFwkTag::APPMGR, "called");
48 ProcessCallback(resultCode, recordId);
49 if (IsRecordListEmpty() && callback_ != nullptr) {
50 callback_->OnReloadPageDone(finalResult.load(), recordId);
51 }
52 }
53
ProcessCallback(int32_t resultCode,int32_t recordId)54 void QuickFixCallbackWithRecord::ProcessCallback(int32_t resultCode, int32_t recordId)
55 {
56 if (!IsRecordExist(recordId)) {
57 TAG_LOGD(AAFwkTag::APPMGR, "Record id %{public}d didn't exist.", recordId);
58 return;
59 }
60 RemoveRecordId(recordId);
61
62 if (resultCode != 0) {
63 finalResult.store(resultCode);
64 }
65 }
66
AddRecordId(const int32_t recordId)67 void QuickFixCallbackWithRecord::AddRecordId(const int32_t recordId)
68 {
69 std::lock_guard<std::mutex> lock(mutex_);
70 recordIds_.emplace_back(recordId);
71 }
72
RemoveRecordId(const int32_t recordId)73 void QuickFixCallbackWithRecord::RemoveRecordId(const int32_t recordId)
74 {
75 std::lock_guard<std::mutex> lock(mutex_);
76 for (auto it = recordIds_.begin(); it != recordIds_.end();) {
77 if (*it == recordId) {
78 it = recordIds_.erase(it);
79 return;
80 }
81 it++;
82 }
83 }
84
IsRecordExist(const int32_t recordId)85 bool QuickFixCallbackWithRecord::IsRecordExist(const int32_t recordId)
86 {
87 std::lock_guard<std::mutex> lock(mutex_);
88 auto it = std::find(recordIds_.begin(), recordIds_.end(), recordId);
89 return (it != recordIds_.end()) ? true : false;
90 }
91
IsRecordListEmpty()92 bool QuickFixCallbackWithRecord::IsRecordListEmpty()
93 {
94 std::lock_guard<std::mutex> lock(mutex_);
95 return recordIds_.empty();
96 }
97 } // namespace AAFwk
98 } // namespace OHOS
99