Add LCOV remarkers to increase line coverage rate
[platform/core/api/maps-service.git] / src / session / command.cpp
1 /* Copyright (c) 2010-2014 Samsung Electronics Co., Ltd. All rights reserved.
2  *
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 "command.h"
18 #include "empty_module.h"
19 #include "maps_util.h"
20
21 extern plugin::plugin_s *__extract_plugin(maps_service_h maps);
22
23 volatile int session::command::command_request_id = 1;
24 session::command session::command::empty_instance; //LCOV_EXCL_LINE
25
26 session::command::command(maps_service_h ms)
27         : m(ms), my_req_id(0), error(MAPS_ERROR_NONE), is_merged(false)
28 {
29 }
30
31 //LCOV_EXCL_START
32 session::command::command(const command &src)
33 {
34         *this = src;
35 }
36
37 session::command::~command()
38 {
39 }
40
41 session::command &session::command::operator =(const command &src)
42 {
43         if (this != (&src)) {
44                 m = src.m;
45                 my_req_id = src.my_req_id;
46                 error = src.error;
47                 is_merged = src.is_merged;
48         }
49         return *this;
50 }
51
52 int session::command::run()
53 {
54         return MAPS_ERROR_NONE;
55 }
56 //LCOV_EXCL_STOP
57
58 void session::command::destroy()
59 {
60         if (this != empty_ptr())
61                 delete this;
62 }
63
64 plugin::interface_s *session::command::interface() const
65 {
66         if (!m)
67                 return plugin::get_empty_interface_ptr(); //LCOV_EXCL_LINE
68
69         plugin::plugin_s *p = __extract_plugin(m);
70         if (!p)
71                 return plugin::get_empty_interface_ptr(); //LCOV_EXCL_LINE
72
73         return &p->interface;
74 }
75
76 //LCOV_EXCL_START
77 maps_plugin_h session::command::handle() const
78 {
79         return (maps_plugin_h) plugin();
80 }
81
82 plugin::plugin_s *session::command::plugin() const
83 {
84         return __extract_plugin(m);
85 }
86
87 session::command_type_e session::command::get_type() const
88 {
89         /* Default command type */
90         return MAPS_DATA_COMMAND;
91 }
92
93 int session::command::get_priority() const
94 {
95         /* Default command priority */
96         return 1;
97 }
98
99 void session::command::merge(const command *c)
100 {
101         /* empty */
102 }
103
104 bool session::command::merged() const
105 {
106         return is_merged;
107 }
108
109
110 void session::command::set_merged()
111 {
112         is_merged = true;
113 }
114 //LCOV_EXCL_STOP
115 /*----------------------------------------------------------------------------*/
116
117 session::command_handler::command_handler(plugin::plugin_s *p, void *ud,
118                                           int urid)
119  : plg(p), user_data(ud), user_req_id(urid), plg_req_id(-1)
120 {
121 }
122
123 session::command_handler::~command_handler()
124 {
125 }
126
127 void session::command_handler::destroy(void *p)
128 {
129         if (!p)
130                 return;
131         command_handler *c = (command_handler *) p;
132         delete c;
133 }
134
135 /*----------------------------------------------------------------------------*/
136
137 session::pending_request::pending_request(plugin::plugin_s *p) : plg(p)
138 {
139 }
140
141 bool session::pending_request::add(const int user_req_id)
142 {
143         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
144                 return false;
145         plugin::scope_mutex(&plg->pending_request_mutex);
146
147         MAPS_LOGD("session::pending_request::add: %d", user_req_id);
148
149         if (contains(user_req_id))
150                 return false;   /* Attempt to add another instance of the id */
151         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
152                 NULL);
153         return true;
154 }
155
156 void session::pending_request::update(int user_req_id,
157                                       command_handler *handler)
158 {
159         if (!plg || !plg->pending_request_maps || (user_req_id < 0) || !handler)
160                 return;
161         plugin::scope_mutex(&plg->pending_request_mutex);
162
163         MAPS_LOGD("session::pending_request::update: %d, %d", user_req_id,
164                 handler->plg_req_id);
165
166         if (!contains(user_req_id)) {   /* Attempt to update not existing id */
167                 //LCOV_EXCL_START
168                 MAPS_LOGD("\t not updated session::pending_request: %d, %d",
169                         user_req_id, handler->plg_req_id);
170                 delete handler; /* This handler must be deleted */
171                 //LCOV_EXCL_STOP
172                 return;
173         }
174
175         g_hash_table_insert(plg->pending_request_maps, int_dup(user_req_id),
176                 handler);
177 }
178
179 void session::pending_request::remove(int user_req_id)
180 {
181         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
182                 return;
183         plugin::scope_mutex(&plg->pending_request_mutex);
184
185         MAPS_LOGD("session::pending_request::remove: %d", user_req_id);
186
187         if (!contains(user_req_id))
188                 return;         /* Attempt to remove not existing id */
189         g_hash_table_remove(plg->pending_request_maps, &user_req_id);
190 }
191
192 session::command_handler *session::pending_request::look_up(const int
193                                                             user_req_id)
194 {
195         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
196                 return NULL;
197         plugin::scope_mutex(&plg->pending_request_mutex);
198
199         MAPS_LOGD("session::pending_request::look_up: %d", user_req_id);
200
201         if (!contains(user_req_id))
202                 return NULL;    /* Attempt to query not existing id */
203         return (command_handler*) g_hash_table_lookup(plg->
204                 pending_request_maps, &user_req_id);
205 }
206
207 int session::pending_request::extract_plg_id(const int user_req_id)
208 {
209         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
210                 return -1;
211         plugin::scope_mutex(&plg->pending_request_mutex);
212
213         command_handler* ch = look_up(user_req_id);
214         if (!ch)
215                 return -1;
216         const int plg_req_id = ch->plg_req_id;
217         remove(user_req_id);
218         return plg_req_id;
219 }
220
221 bool session::pending_request::contains(const int user_req_id)
222 {
223         if (!plg || !plg->pending_request_maps || (user_req_id < 0))
224                 return false;
225         plugin::scope_mutex(&plg->pending_request_mutex);
226
227         return g_hash_table_contains(plg->pending_request_maps, &user_req_id);
228 }
229
230 //LCOV_EXCL_START
231 int *session::pending_request::int_dup(const int n)
232 {
233         int *clone = g_new0(int, 1);
234         *clone = n;
235         return clone;
236 }
237 //LCOV_EXCL_STOP