2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "pkg_request.hh"
19 #include "utils/logging.hh"
21 #include "pkgmgrinfo_debug.h"
23 namespace pkgmgr_server {
25 PkgRequest::PkgRequest()
26 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1),
27 privilege_checked_(false) {}
29 PkgRequest::PkgRequest(int fd)
30 : request_type_(pkgmgr_common::REQ_TYPE_NONE), data_size_(-1),
31 privilege_checked_(false) {
32 socket_ = std::unique_ptr<pkgmgr_common::socket::DataSocket>(
33 new (std::nothrow) pkgmgr_common::socket::DataSocket(fd));
34 if (socket_ == nullptr)
35 LOG(ERROR) << "Out of memory";
38 PkgRequest::~PkgRequest() {
43 const unsigned char* PkgRequest::GetData() {
47 int PkgRequest::GetSize() {
51 int PkgRequest::GetFd() {
52 return socket_->GetFd();
55 pid_t PkgRequest::GetSenderPID() {
56 return socket_->GetPID();
59 uid_t PkgRequest::GetSenderUID() {
60 return socket_->GetUID();
63 pkgmgr_common::ReqType PkgRequest::GetRequestType() {
67 bool PkgRequest::ReceiveData() {
68 int ret = socket_->ReceiveData(&request_type_, sizeof(request_type_));
69 if (ret < 0 || request_type_ == pkgmgr_common::REQ_TYPE_NONE) {
70 LOG(ERROR) << "Failed to ReceiveData";
73 ret = socket_->ReceiveData(&data_size_, sizeof(data_size_));
75 LOG(ERROR) << "Failed to ReceiveData";
76 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
79 if (data_size_ <= 0) {
80 LOG(ERROR) << "Invalid data";
81 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
85 data_ = new (std::nothrow) unsigned char[data_size_];
86 if (data_ == nullptr) {
87 LOG(ERROR) << "Out of memory";
88 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
91 ret = socket_->ReceiveData(reinterpret_cast<void*>(data_), data_size_);
93 LOG(ERROR) << "Failed to ReceiveData";
96 request_type_ = pkgmgr_common::REQ_TYPE_NONE;
103 bool PkgRequest::SendData(tizen_base::Parcel parcel) {
104 const std::vector<uint8_t>& raw = parcel.GetRaw();
105 unsigned int size = raw.size();
106 if (socket_->SendData(&size, sizeof(size)) < 0)
109 return (socket_->SendData(raw.data(), size) == 0);
112 bool PkgRequest::GetPrivilegeChecked() {
113 return privilege_checked_;
116 void PkgRequest::SetPrivilegeChecked(bool checked) {
117 privilege_checked_ = checked;
120 } // namespace pkgmgr_server