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