Remove the cache when language change event occurs
[platform/core/appfw/pkgmgr-info.git] / src / server / database / remove_all_cache_db_handler.cc
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "remove_all_cache_db_handler.hh"
18
19 #include "cache_flag.hh"
20 #include "db_handle_provider.hh"
21 #include "utils/logging.hh"
22
23 namespace {
24
25 uid_t globaluser_uid = -1;
26
27 uid_t GetGlobalUID() {
28   if (globaluser_uid == (uid_t)-1)
29     globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
30
31   return globaluser_uid;
32 }
33
34 }  // namespace
35
36 namespace pkgmgr_server {
37 namespace database {
38
39 RemoveAllCacheDBHandler::RemoveAllCacheDBHandler(uid_t uid, int pid)
40     : AbstractDBHandler(uid, pid), uid_(uid) {}
41
42 int RemoveAllCacheDBHandler::Execute() {
43   std::unique_lock<std::shared_mutex> u(lock_);
44
45   auto lock = CacheFlag::GetWriterLock();
46   if (CacheFlag::GetStatus() != CacheFlag::Status::PREPARED)
47     return PMINFO_R_OK;
48   CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
49   database::DBHandleProvider::GetInst(GetUID()).TrimCache();
50   database::DBHandleProvider::GetInst(GetGlobalUID()).TrimCache();
51
52   CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
53
54   return PMINFO_R_OK;
55 }
56
57 }  // namespace database
58 }  // namespace pkgmgr_server