Improve handling of cache
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / command_request_handler.cc
1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "command_request_handler.hh"
6
7 #include <string>
8
9 #include "db_handle_provider.hh"
10 #include "parcelable_factory.hh"
11 #include "pkginfo_parcelable.hh"
12 #include "remove_cache_db_handler.hh"
13 #include "utils/logging.hh"
14
15 #include "pkgmgrinfo_debug.h"
16 #include "pkgmgrinfo_type.h"
17
18 namespace pcp = pkgmgr_common::parcel;
19
20 namespace pkgmgr_server {
21 namespace request_handler {
22
23 bool CommandRequestHandler::HandleRequest(unsigned char* data, size_t size,
24     const std::string& locale) {
25   auto abstract_parcel =
26       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
27
28   if (abstract_parcel == nullptr ||
29       abstract_parcel->GetType() != pcp::ParcelableType::Command) {
30     LOG(ERROR) << "Invalid parcel or type";
31     result_ = std::make_shared<pcp::ResultParcelable>(
32         PMINFO_R_ERROR, std::vector<pcp::StrArgs>{});
33     return false;
34   }
35
36   auto* parcel = dynamic_cast<pcp::CommandParcelable*>(abstract_parcel.get());
37   if (parcel == nullptr) {
38     LOG(ERROR) << "Parcel is empty";
39     result_ = std::make_shared<pcp::ResultParcelable>(
40         PMINFO_R_ERROR, std::vector<pcp::StrArgs>{});
41     return false;
42   }
43
44   if (parcel->GetCmd() == CommandType::RemoveCache) {
45     database::RemoveCacheDBHandler db(parcel->GetUid(), GetPID());
46     db.SetLocale(locale);
47     int ret = db.Execute();
48     result_ = std::make_shared<pcp::ResultParcelable>(
49         ret, std::vector<pcp::StrArgs>{});
50     return true;
51   }
52
53   return true;
54 }
55
56 }  // namespace request_handler
57 }  // namespace pkgmgr_server