1 /*
2 * Copyright (c) 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 "process_cache_callback_host.h"
17
18 #include "app_log_wrapper.h"
19 #include "bundle_framework_core_ipc_interface_code.h"
20 #include "bundle_memory_guard.h"
21 #include "ipc_types.h"
22
23 namespace OHOS {
24 namespace AppExecFwk {
ProcessCacheCallbackHost()25 ProcessCacheCallbackHost::ProcessCacheCallbackHost()
26 {
27 APP_LOGI("process clean cache callback host instance");
28 }
29
~ProcessCacheCallbackHost()30 ProcessCacheCallbackHost::~ProcessCacheCallbackHost()
31 {
32 APP_LOGI("destroyclean process callback host instance");
33 }
34
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)35 int ProcessCacheCallbackHost::OnRemoteRequest(
36 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
37 {
38 BundleMemoryGuard memoryGuard;
39 APP_LOGD("process cache callback host onReceived message, the message code is %{public}u", code);
40 std::u16string descripter = ProcessCacheCallbackHost::GetDescriptor();
41 std::u16string remoteDescripter = data.ReadInterfaceToken();
42 if (descripter != remoteDescripter) {
43 APP_LOGE("fail to write reply message in process cache host due to the reply is nullptr");
44 return OBJECT_NULL;
45 }
46
47 switch (code) {
48 case static_cast<uint32_t>(ProcessCacheCallbackInterfaceCode::GET_ALL_BUNDLE_CACHE): {
49 uint64_t cacheStat = data.ReadUint64();
50 OnGetAllBundleCacheFinished(cacheStat);
51 break;
52 }
53 case static_cast<uint32_t>(ProcessCacheCallbackInterfaceCode::CLEAN_ALL_BUNDLE_CACHE): {
54 int32_t result = data.ReadInt32();
55 OnCleanAllBundleCacheFinished(result);
56 break;
57 }
58 default:
59 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 return NO_ERROR;
62 }
63
OnGetAllBundleCacheFinished(uint64_t cacheStat)64 void ProcessCacheCallbackHost::OnGetAllBundleCacheFinished(uint64_t cacheStat)
65 {
66 std::lock_guard<std::mutex> lock(getAllMutex_);
67 if (getAllcomplete_) {
68 return;
69 }
70 getAllcomplete_ = true;
71 getAllPromise_.set_value(cacheStat);
72 }
73
GetCacheStat()74 uint64_t ProcessCacheCallbackHost::GetCacheStat()
75 {
76 return getAllFuture_.get();
77 };
78
OnCleanAllBundleCacheFinished(int32_t result)79 void ProcessCacheCallbackHost::OnCleanAllBundleCacheFinished(int32_t result)
80 {
81 std::lock_guard<std::mutex> lock(cleanAllMutex_);
82 if (cleanAllcomplete_) {
83 return;
84 }
85 cleanAllcomplete_ = true;
86 cleanAllPromise_.set_value(result);
87 }
88
GetCleanRet()89 int32_t ProcessCacheCallbackHost::GetCleanRet()
90 {
91 return cleanAllFuture_.get();
92 };
93 } // namespace AppExecFwk
94 } // namespace OHOS
95