ce6a0b1154191160fa605986a86dd84d62665cf8
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / get_appinfo_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 "get_appinfo_request_handler.hh"
6
7 #include <string>
8
9 #include "appinfo_db_handler.hh"
10 #include "filter_parcelable.hh"
11 #include "parcelable_factory.hh"
12 #include "pkgmgrinfo_debug.h"
13 #include "utils/logging.hh"
14
15 namespace pcp = pkgmgr_common::parcel;
16 namespace psd = pkgmgr_server::database;
17
18 namespace pkgmgr_server {
19 namespace request_handler {
20
21 bool GetAppinfoRequestHandler::HandleRequest(unsigned char* data,
22     size_t size, const std::string& locale) {
23   auto abstract_parcel =
24       pcp::ParcelableFactory::GetInst().CreateParcel(data, size);
25
26   if (abstract_parcel == nullptr ||
27       abstract_parcel->GetType() != pcp::ParcelableType::Filter) {
28     LOG(ERROR) << "Invalid parcel or type";
29     result_ = std::make_shared<pcp::AppInfoParcelable>(
30         PMINFO_R_ERROR, std::vector<std::shared_ptr<application_x>>{});
31     return false;
32   }
33
34   auto* parcel = dynamic_cast<pcp::FilterParcelable*>(abstract_parcel.get());
35   if (parcel == nullptr) {
36     LOG(ERROR) << "Parcel is empty";
37     result_ = std::make_shared<pcp::AppInfoParcelable>(
38         PMINFO_R_ERROR, std::vector<std::shared_ptr<application_x>>{});
39     return false;
40   }
41
42   psd::AppInfoDBHandler db(parcel->GetUid(), GetPID());
43   db.SetLocale(locale);
44   db.SetFilter(const_cast<pkgmgrinfo_filter_x*>(parcel->GetFilter()));
45   int ret = db.Execute();
46
47   result_ = std::make_shared<pcp::AppInfoParcelable>(ret, db.DetachAppHandle());
48
49   return true;
50 }
51
52 }  // namespace request_handler
53 }  // namespace pkgmgr_server