Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / ClientRequest.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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 <unistd.h>
18 #include <glib.h>
19 #include <app_manager.h>
20 #include <Types.h>
21 #include <DBusTypes.h>
22 #include "DBusServer.h"
23 #include "access_control/PeerCreds.h"
24 #include "ClientRequest.h"
25
26 ctx::ClientRequest::ClientRequest(int type, int reqId, const char *subj, const char *desc,
27                 ctx::Credentials *creds, const char *sender, GDBusMethodInvocation *inv) :
28         RequestInfo(type, reqId, subj, desc),
29         __credentials(creds),
30         __dbusSender(sender),
31         __invocation(inv)
32 {
33 }
34
35 ctx::ClientRequest::~ClientRequest()
36 {
37         if (__invocation)
38                 g_dbus_method_invocation_return_value(__invocation, g_variant_new("(iss)", ERR_OPERATION_FAILED, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
39
40         delete __credentials;
41 }
42
43 const ctx::Credentials* ctx::ClientRequest::getCredentials()
44 {
45         return __credentials;
46 }
47
48 const char* ctx::ClientRequest::getPackageId()
49 {
50         if (__credentials)
51                 return __credentials->packageId;
52
53         return NULL;
54 }
55
56 const char* ctx::ClientRequest::getClient()
57 {
58         if (__credentials)
59                 return __credentials->client;
60
61         return NULL;
62 }
63
64 bool ctx::ClientRequest::reply(int error)
65 {
66         IF_FAIL_RETURN(__invocation, true);
67
68         _I("Reply %#x", error);
69
70         g_dbus_method_invocation_return_value(__invocation, g_variant_new("(iss)", error, EMPTY_JSON_OBJECT, EMPTY_JSON_OBJECT));
71         __invocation = NULL;
72         return true;
73 }
74
75 bool ctx::ClientRequest::reply(int error, ctx::CtxJson1& requestResult)
76 {
77         IF_FAIL_RETURN(__invocation, true);
78         IF_FAIL_RETURN(error != ERR_NONE || __type != REQ_READ_SYNC, true);
79
80         std::string result = requestResult.str();
81         IF_FAIL_RETURN(!result.empty(), false);
82
83         _I("Reply %#x", error);
84         _SD("Result: %s", result.c_str());
85
86         g_dbus_method_invocation_return_value(__invocation, g_variant_new("(iss)", error, result.c_str(), EMPTY_JSON_OBJECT));
87         __invocation = NULL;
88
89         return true;
90 }
91
92 bool ctx::ClientRequest::reply(int error, ctx::CtxJson1& requestResult, ctx::CtxJson1& dataRead)
93 {
94         if (__invocation == NULL) {
95                 return publish(error, dataRead);
96         }
97
98         std::string result = requestResult.str();
99         std::string data = dataRead.str();
100         IF_FAIL_RETURN(!result.empty() && !data.empty(), false);
101
102         _I("Reply %#x", error);
103         _SD("Result: %s", result.c_str());
104         _SD("Data: %s", data.c_str());
105
106         g_dbus_method_invocation_return_value(__invocation, g_variant_new("(iss)", error, result.c_str(), data.c_str()));
107         __invocation = NULL;
108
109         return true;
110 }
111
112 bool ctx::ClientRequest::publish(int error, ctx::CtxJson1& data)
113 {
114         DBusServer::publish(__dbusSender, __reqId, __subject, error, data.str());
115         return true;
116 }