Add LCOV remarkers to increase line coverage rate
[platform/core/api/maps-service.git] / src / plugin / module.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 <glib/gstdio.h>
18 #include "module.h"
19 #include "maps_util.h"
20 #include "thread.h"
21 #include "command.h"
22 #include "command_queue.h"
23 #include "empty_module.h"
24
25 plugin::scope_mutex::scope_mutex(GMutex *m) : mutex(m)
26 {
27         g_mutex_lock(mutex);
28 }
29
30 plugin::scope_mutex::~scope_mutex()
31 {
32         g_mutex_unlock(mutex);
33 }
34
35 plugin::binary_extractor::binary_extractor()
36 {
37 }
38
39 plugin::provider_info plugin::binary_extractor::get_plugin_info(const
40                                                         string &file_name) const
41 {
42         if (file_name.empty())
43                 return provider_info::empty_instance; //LCOV_EXCL_LINE
44
45         /* 1.Initialize plugin */
46         GMod *plugin = gmod_new(file_name, FALSE);
47         if (!plugin)
48                 return provider_info::empty_instance; //LCOV_EXCL_LINE
49
50         provider_info info;
51
52         /* 2. Get plugin info */
53         maps_plugin_get_info_f func =
54                 (maps_plugin_get_info_f) gmod_find_sym(plugin,
55                 "maps_plugin_get_info");
56         if (func) {
57                 maps_plugin_info_h i;
58                 if (func(&i) == MAPS_ERROR_NONE) {
59                         char *provider_name = NULL;
60                         maps_plugin_info_get_provider_name(i, &provider_name);
61
62                         /* Convert plugin info to provider info */
63                         info.construct(string(provider_name), file_name);
64
65                         g_free(provider_name);
66                 }
67                 maps_plugin_info_destroy(i);
68         }
69
70         /* 3. shutdown plugin */
71         gmod_free(plugin);
72
73         return info;
74 }
75
76 maps_plugin_h plugin::binary_extractor::init(const provider_info &info,
77                                              const char *module, int *init_error)
78 {
79         /* 1.Initialize plugin */
80         if (info.file.empty() || !init_error) {
81                 //LCOV_EXCL_START
82                 if (init_error)
83                         *init_error = MAPS_ERROR_NOT_SUPPORTED;
84                 return NULL;
85                 //LCOV_EXCL_STOP
86         }
87
88         *init_error = MAPS_ERROR_NONE;
89
90         GMod *plugin = gmod_new(info.file, TRUE);
91         if (!plugin) {
92                 //LCOV_EXCL_START
93                 MAPS_LOGE("Open Module Failed: %s", info.file.c_str());
94                 *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
95                 return NULL;
96                 //LCOV_EXCL_STOP
97         }
98
99         /* 2.1 Create new plugin interface */
100         plugin_s* new_plugin = g_slice_new0(plugin_s);
101
102         /* 2. Perform steps to completely initialize a plugin */
103         do {
104                 if (!new_plugin) {
105                         //LCOV_EXCL_START
106                         MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
107                         *init_error = MAPS_ERROR_OUT_OF_MEMORY;
108                         break;
109                         //LCOV_EXCL_STOP
110                 }
111
112                 /* 2.1 Set plugin module handle */
113                 new_plugin->module = plugin;
114
115                 /* 2.2 Set plugin interface */
116                 new_plugin->interface = get_empty_interface();
117
118                 /* Plugin dedicated functions */
119                 new_plugin->interface.maps_plugin_init =
120                         (maps_plugin_init_f) gmod_find_sym(plugin,
121                         "maps_plugin_init");
122                 new_plugin->interface.maps_plugin_init_module =
123                         (maps_plugin_init_module_f) gmod_find_sym(plugin,
124                         "maps_plugin_init_module");
125                 new_plugin->interface.maps_plugin_shutdown =
126                         (maps_plugin_shutdown_f) gmod_find_sym(plugin,
127                         "maps_plugin_shutdown");
128                 new_plugin->interface.maps_plugin_get_info =
129                         (maps_plugin_get_info_f) gmod_find_sym(plugin,
130                         "maps_plugin_get_info");
131                 new_plugin->interface.maps_plugin_request_user_consent =
132                         (maps_plugin_request_user_consent_f) gmod_find_sym(plugin,
133                         "maps_plugin_request_user_consent");
134
135                 /* Maps Provider access key, preference and capabilities */
136                 new_plugin->interface.maps_plugin_set_provider_key =
137                         (maps_plugin_set_provider_key_f) gmod_find_sym(plugin,
138                         "maps_plugin_set_provider_key");
139                 new_plugin->interface.maps_plugin_get_provider_key =
140                         (maps_plugin_get_provider_key_f) gmod_find_sym(plugin,
141                         "maps_plugin_get_provider_key");
142                 new_plugin->interface.maps_plugin_set_preference =
143                         (maps_plugin_set_preference_f) gmod_find_sym(plugin,
144                         "maps_plugin_set_preference");
145                 new_plugin->interface.maps_plugin_get_preference =
146                         (maps_plugin_get_preference_f) gmod_find_sym(plugin,
147                         "maps_plugin_get_preference");
148                 new_plugin->interface.maps_plugin_is_service_supported =
149                         (maps_plugin_is_service_supported_f) gmod_find_sym(plugin,
150                         "maps_plugin_is_service_supported");
151                 new_plugin->interface.maps_plugin_is_data_supported =
152                         (maps_plugin_is_data_supported_f) gmod_find_sym(plugin,
153                         "maps_plugin_is_data_supported");
154
155                 /* Geocode */
156                 new_plugin->interface.maps_plugin_geocode =
157                         (maps_plugin_geocode_f) gmod_find_sym(plugin,
158                         "maps_plugin_geocode");
159                 new_plugin->interface.maps_plugin_geocode_inside_area =
160                         (maps_plugin_geocode_inside_area_f) gmod_find_sym(plugin,
161                         "maps_plugin_geocode_inside_area");
162                 new_plugin->interface.maps_plugin_geocode_by_structured_address =
163                         (maps_plugin_geocode_by_structured_address_f) gmod_find_sym(plugin,
164                         "maps_plugin_geocode_by_structured_address");
165                 new_plugin->interface.maps_plugin_reverse_geocode =
166                         (maps_plugin_reverse_geocode_f) gmod_find_sym(plugin,
167                         "maps_plugin_reverse_geocode");
168                 new_plugin->interface.maps_plugin_multi_reverse_geocode =
169                         (maps_plugin_multi_reverse_geocode_f) gmod_find_sym(plugin,
170                         "maps_plugin_multi_reverse_geocode");
171
172                 /* Place */
173                 new_plugin->interface.maps_plugin_search_place =
174                         (maps_plugin_search_place_f) gmod_find_sym(plugin,
175                         "maps_plugin_search_place");
176                 new_plugin->interface.maps_plugin_search_place_by_area =
177                         (maps_plugin_search_place_by_area_f) gmod_find_sym(plugin,
178                         "maps_plugin_search_place_by_area");
179                 new_plugin->interface.maps_plugin_search_place_by_address =
180                         (maps_plugin_search_place_by_address_f) gmod_find_sym(plugin,
181                         "maps_plugin_search_place_by_address");
182                 new_plugin->interface.maps_plugin_search_place_list =
183                         (maps_plugin_search_place_list_f) gmod_find_sym(plugin,
184                         "maps_plugin_search_place_list");
185                 new_plugin->interface.maps_plugin_get_place_details =
186                         (maps_plugin_get_place_details_f) gmod_find_sym(plugin,
187                         "maps_plugin_get_place_details");
188
189                 /* Route */
190                 new_plugin->interface.maps_plugin_search_route =
191                         (maps_plugin_search_route_f) gmod_find_sym(plugin,
192                         "maps_plugin_search_route");
193                 new_plugin->interface.maps_plugin_search_route_waypoints =
194                         (maps_plugin_search_route_waypoints_f) gmod_find_sym(plugin,
195                         "maps_plugin_search_route_waypoints");
196
197                 /* Cancel Request */
198                 new_plugin->interface.maps_plugin_cancel_request =
199                         (maps_plugin_cancel_request_f) gmod_find_sym(plugin,
200                         "maps_plugin_cancel_request");
201
202                 /* Mapping */
203                 new_plugin->interface.maps_plugin_create_map_view =
204                         (maps_plugin_create_map_view_f) gmod_find_sym(plugin,
205                         "maps_plugin_create_map_view");
206                 new_plugin->interface.maps_plugin_destroy_map_view =
207                         (maps_plugin_destroy_map_view_f) gmod_find_sym(plugin,
208                         "maps_plugin_destroy_map_view");
209                 new_plugin->interface.maps_plugin_render_map =
210                         (maps_plugin_render_map_f) gmod_find_sym(plugin,
211                         "maps_plugin_render_map");
212                 new_plugin->interface.maps_plugin_move_center =
213                         (maps_plugin_move_center_f) gmod_find_sym(plugin,
214                         "maps_plugin_move_center");
215                 new_plugin->interface.maps_plugin_set_scalebar =
216                         (maps_plugin_set_scalebar_f) gmod_find_sym(plugin,
217                         "maps_plugin_set_scalebar");
218                 new_plugin->interface.maps_plugin_get_scalebar =
219                         (maps_plugin_get_scalebar_f) gmod_find_sym(plugin,
220                         "maps_plugin_get_scalebar");
221                 new_plugin->interface.maps_plugin_on_object =
222                         (maps_plugin_on_object_f) gmod_find_sym(plugin,
223                         "maps_plugin_on_object");
224                 new_plugin->interface.maps_plugin_screen_to_geography =
225                         (maps_plugin_screen_to_geography_f) gmod_find_sym(plugin,
226                         "maps_plugin_screen_to_geography");
227                 new_plugin->interface.maps_plugin_geography_to_screen =
228                         (maps_plugin_geography_to_screen_f) gmod_find_sym(plugin,
229                         "maps_plugin_geography_to_screen");
230                 new_plugin->interface.maps_plugin_get_min_zoom_level =
231                         (maps_plugin_get_min_zoom_level_f) gmod_find_sym(plugin,
232                         "maps_plugin_get_min_zoom_level");
233                 new_plugin->interface.maps_plugin_get_max_zoom_level =
234                         (maps_plugin_get_max_zoom_level_f) gmod_find_sym(plugin,
235                         "maps_plugin_get_max_zoom_level");
236                 new_plugin->interface.maps_plugin_get_center =
237                         (maps_plugin_get_center_f) gmod_find_sym(plugin,
238                         "maps_plugin_get_center");
239                 new_plugin->interface.maps_plugin_capture_snapshot =
240                         (maps_plugin_capture_snapshot_f) gmod_find_sym(plugin,
241                         "maps_plugin_capture_snapshot");
242                 new_plugin->interface.maps_plugin_get_view_scale_factor =
243                         (maps_plugin_get_view_scale_factor_f) gmod_find_sym(plugin,
244                         "maps_plugin_get_view_scale_factor");
245                 new_plugin->interface.maps_plugin_set_view_scale_factor =
246                         (maps_plugin_set_view_scale_factor_f) gmod_find_sym(plugin,
247                         "maps_plugin_set_view_scale_factor");
248
249                 /* 2.3 Check whether the plugin init function is valid */
250                 if (!new_plugin->interface.maps_plugin_init) {
251                         //LCOV_EXCL_START
252                         MAPS_LOGE("ERROR! Plugin initialization function is invalid");
253                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
254                         break;
255                         //LCOV_EXCL_STOP
256                 }
257
258                 /* 2.4 Call a plugin to initialize itself, send to the plugin
259                 *  its pointer */
260                 int ret;
261                 if (!module || !new_plugin->interface.maps_plugin_init_module)
262                         ret = new_plugin->interface.maps_plugin_init((maps_plugin_h *) (&new_plugin));
263                 else
264                         ret = new_plugin->interface.maps_plugin_init_module((maps_plugin_h *) (&new_plugin), module); //LCOV_EXCL_LINE
265
266                 if (ret != MAPS_ERROR_NONE) {
267                         //LCOV_EXCL_START
268                         MAPS_LOGE("ERROR! Plugin initialization function failed: %d", ret);
269                         *init_error = ret;
270                         break;
271                         //LCOV_EXCL_STOP
272                 }
273
274                 if (!new_plugin->interface.maps_plugin_set_provider_key) {
275                         //LCOV_EXCL_START
276                         MAPS_LOGE("ERROR! Plugin set_provider_key function is NULL");
277                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
278                         break;
279                         //LCOV_EXCL_STOP
280                 }
281
282                 if (!new_plugin->interface.maps_plugin_get_provider_key) {
283                         //LCOV_EXCL_START
284                         MAPS_LOGE("ERROR! Plugin set_provider_key function is NULL");
285                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
286                         break;
287                         //LCOV_EXCL_STOP
288                 }
289
290                 if (!new_plugin->interface.maps_plugin_set_preference) {
291                         //LCOV_EXCL_START
292                         MAPS_LOGE("ERROR! Plugin set_preference function is NULL");
293                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
294                         break;
295                 }
296
297                 if (!new_plugin->interface.maps_plugin_get_preference) {
298                         //LCOV_EXCL_START
299                         MAPS_LOGE("ERROR! Plugin get_preference function is NULL");
300                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
301                         break;
302                         //LCOV_EXCL_STOP
303                 }
304
305                 if (!new_plugin->interface.maps_plugin_is_data_supported) {
306                         //LCOV_EXCL_START
307                         MAPS_LOGE("ERROR! Plugin support_is_data_supported function is NULL");
308                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
309                         break;
310                         //LCOV_EXCL_STOP
311                 }
312
313                 if (!new_plugin->interface.maps_plugin_is_service_supported) {
314                         //LCOV_EXCL_START
315                         MAPS_LOGE("ERROR! Plugin support_is_service_supported function is NULL");
316                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
317                         break;
318                         //LCOV_EXCL_STOP
319                 }
320
321                 /* 2.7 Create a queue with asynchronous requests to plugin */
322                 if (session::command_queue::is_async())
323                         new_plugin->request_queue = g_async_queue_new();
324
325                 /* 2.8 Initialize the mutex for the map of pending requests */
326                 new_plugin->pending_request_maps =
327                         g_hash_table_new_full(g_int_hash, g_int_equal, g_free,
328                         session::command_handler::destroy);
329                 if (!new_plugin->pending_request_maps) {
330                         //LCOV_EXCL_START
331                         MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
332                         *init_error = MAPS_ERROR_OUT_OF_MEMORY;
333                         break;
334                         //LCOV_EXCL_STOP
335                 }
336                 g_mutex_init(&new_plugin->pending_request_mutex);
337
338                 /* DEBUG TRACE */
339                 trace_dbg(new_plugin);
340
341                 /* 2.5 Return newly initialized plugin */
342                 return new_plugin;
343         } while (FALSE);
344
345         //LCOV_EXCL_START
346         MAPS_LOGE("Shut down the plugin becuause of error");
347
348         /* 3. shutdown plugin in case of problem */
349         shutdown(new_plugin);
350         return NULL;
351         //LCOV_EXCL_STOP
352 }
353
354 void plugin::binary_extractor::shutdown(maps_plugin_h plugin_h)
355 {
356         if (!plugin_h)
357                 return;
358
359         plugin_s *plugin = (plugin_s *) plugin_h;
360         g_return_if_fail(plugin->module);
361
362         /* 0. shutdown plugin */
363         if (plugin->interface.maps_plugin_shutdown)
364                 plugin->interface.maps_plugin_shutdown(plugin);
365
366         /* 1. Stop the thread, processing the request queue */
367         session::thread().stop(plugin);
368
369         /* 2. Destroy the request queue */
370         if (plugin->request_queue)
371                 g_async_queue_unref(plugin->request_queue); //LCOV_EXCL_LINE
372
373         /* 3. Destroy the map of pending requests */
374         if (plugin->pending_request_maps) {
375                 g_hash_table_unref(plugin->pending_request_maps);
376
377                 /* Clear the mutex for the map of pending requests */
378                 g_mutex_clear(&plugin->pending_request_mutex);
379         }
380
381         /* 4. Unload plugin from memory */
382         gmod_free((GMod *) plugin->module);
383
384         /* 5. Destroying the table with plugin capabilities */
385         /*maps_int_hashtable_destroy(plugin->capabilities); */
386
387         /* 6. Release memory used by plugin structure */
388         g_slice_free(plugin_s, plugin);
389 }
390
391 /* Open the binary (which contains a plugin) */
392 plugin::GMod *plugin::binary_extractor::gmod_new(const string &module_file,
393                                                  gboolean is_resident) const
394 {
395         if (!g_module_supported()) {
396                 //LCOV_EXCL_START
397                 MAPS_LOGE("ERROR! g_module_supported is false\n\n");
398                 return NULL;
399                 //LCOV_EXCL_STOP
400         }
401
402         if (module_file.empty())
403                 return NULL;
404
405         GMod *gmod = g_new0(GMod, 1);
406
407         gmod->name = g_strdup(module_file.c_str());
408         if (!gmod->name) {
409                 //LCOV_EXCL_START
410                 g_free(gmod);
411                 return NULL;
412                 //LCOV_EXCL_STOP
413         }
414
415         gmod->path = g_strnfill(100, 0);
416         g_sprintf(gmod->path, "%s/%s", MAPS_PLUGINS_PATH_PREFIX, gmod->name);
417         if (!gmod->path) {
418                 //LCOV_EXCL_START
419                 g_free(gmod->name);
420                 g_free(gmod);
421                 return NULL;
422                 //LCOV_EXCL_STOP
423         }
424
425         gmod->module = g_module_open(gmod->path, G_MODULE_BIND_LAZY);
426         if (!gmod->module) {
427                 //LCOV_EXCL_START
428                 MAPS_LOGE("module path not found: %s", gmod->path);
429
430                 const gchar *last_error = g_module_error();
431                 MAPS_LOGE("last module error: %s", last_error);
432
433                 g_free(gmod->name);
434                 g_free(gmod->path);
435                 g_free(gmod);
436                 return NULL;
437                 //LCOV_EXCL_STOP
438         }
439         MAPS_LOGD("open module");
440         /*if (is_resident)
441                 g_module_make_resident(gmod->module);*/
442
443         return gmod;
444 }
445
446 /* Close the binary (which contains a plugin) */
447 void plugin::binary_extractor::gmod_free(GMod *gmod) const
448 {
449         /*g_return_if_fail(gmod); */
450         if (!gmod)
451                 return;
452
453         if (gmod->name)
454                 g_free(gmod->name);
455         if (gmod->path)
456                 g_free(gmod->path);
457         if (gmod->module)
458                 g_module_close(gmod->module);
459         g_free(gmod);
460
461         MAPS_LOGD("close module");
462         MAPS_LOGD("last module error: %s", g_module_error());
463 }
464
465 /* Find the address of a function in a binary (which contains a plugin) */
466 gpointer plugin::binary_extractor::gmod_find_sym(GMod *gmod,
467                                                  const gchar *func_name) const
468 {
469         g_return_val_if_fail(gmod, NULL);
470         g_return_val_if_fail(func_name, NULL);
471
472         gpointer func_ptr = NULL;
473         if (!g_module_symbol(gmod->module, func_name, &func_ptr)) {
474                 //LCOV_EXCL_START
475                 MAPS_LOGE("function symbol not found");
476                 MAPS_LOGE("%s", g_module_error());
477                 func_ptr = NULL;
478                 //LCOV_EXCL_STOP
479         }
480         return func_ptr;
481 }
482
483 void plugin::binary_extractor::trace_dbg(const plugin_s *plugin) const
484 {
485         MAPS_LOGD("*********************************************");
486         MAPS_LOGD("PLUGIN INFO");
487         if (!plugin) {
488                 //LCOV_EXCL_START
489                 MAPS_LOGD("PLUGIN is NULL");
490                 MAPS_LOGD("*********************************************");
491                 return;
492                 //LCOV_EXCL_STOP
493         }
494
495         const GMod *mod = (const GMod *) plugin->module;
496         if (!mod) {
497                 MAPS_LOGD("PLUGIN module is NULL"); //LCOV_EXCL_LINE
498         } else {
499                 MAPS_LOGD("module address:\t\t\t%p", mod->module);
500                 MAPS_LOGD("module name:\t\t\t%s", mod->name);
501                 MAPS_LOGD("module path:\t\t\t%s", mod->path);
502         }
503
504         if (!plugin->request_queue) {
505                 MAPS_LOGD("PLUGIN request queue is NULL");
506         } else {
507                 MAPS_LOGD("plugin request queue:\t\t\t%p", plugin->request_queue); //LCOV_EXCL_LINE
508         }
509
510         const interface_s *itf = &plugin->interface;
511         MAPS_LOGD("maps_plugin_init:\t\t\t%p", itf->maps_plugin_init);
512         MAPS_LOGD("maps_plugin_shutdown:\t\t\t%p", itf->maps_plugin_shutdown);
513         MAPS_LOGD("maps_plugin_get_info:\t\t\t%p", itf->maps_plugin_get_info);
514
515         /* Maps Provider access key */
516         MAPS_LOGD("maps_plugin_set_provider_key:\t\t%p",
517                 itf->maps_plugin_set_provider_key);
518         MAPS_LOGD("maps_plugin_get_provider_key:\t\t%p",
519                 itf->maps_plugin_get_provider_key);
520         MAPS_LOGD("maps_plugin_is_service_supported:\t%p",
521                 itf->maps_plugin_is_service_supported);
522         MAPS_LOGD("maps_plugin_is_data_supported:\t\t%p",
523                 itf->maps_plugin_is_data_supported);
524
525         MAPS_LOGD("maps_plugin_geocode:\t\t\t%p", itf->maps_plugin_geocode);
526         MAPS_LOGD("maps_plugin_geocode_inside_area:\t%p",
527                 itf->maps_plugin_geocode_inside_area);
528         MAPS_LOGD("maps_plugin_geocode_by_structured_address: %p",
529                 itf->maps_plugin_geocode_by_structured_address);
530         MAPS_LOGD("maps_plugin_reverse_geocode:\t\t%p",
531                 itf->maps_plugin_reverse_geocode);
532         MAPS_LOGD("maps_plugin_multi_reverse_geocode:\t%p",
533                 itf->maps_plugin_multi_reverse_geocode);
534
535         MAPS_LOGD("maps_plugin_search_place:\t\t%p",
536                 itf->maps_plugin_search_place);
537         MAPS_LOGD("maps_plugin_search_place_by_area:\t%p",
538                 itf->maps_plugin_search_place_by_area);
539         MAPS_LOGD("maps_plugin_search_place_by_address:\t%p",
540                 itf->maps_plugin_search_place_by_address);
541         MAPS_LOGD("maps_plugin_search_place_list:\t\t%p",
542                 itf->maps_plugin_search_place_list);
543         MAPS_LOGD("maps_plugin_get_place_details:\t%p",
544                 itf->maps_plugin_get_place_details);
545
546         MAPS_LOGD("maps_plugin_search_route:\t\t%p",
547                 itf->maps_plugin_search_route);
548         MAPS_LOGD("maps_plugin_search_route_waypoints:\t%p",
549                 itf->maps_plugin_search_route_waypoints);
550
551         MAPS_LOGD("maps_plugin_cancel_request:\t\t%p",
552                 itf->maps_plugin_cancel_request);
553
554         MAPS_LOGD("maps_plugin_create_map_view:\t\t%p",
555                 itf->maps_plugin_create_map_view);
556         MAPS_LOGD("maps_plugin_destroy_map_view:\t\t%p",
557                 itf->maps_plugin_destroy_map_view);
558         MAPS_LOGD("maps_plugin_capture_snapshot:\t\t%p",
559                 itf->maps_plugin_capture_snapshot);
560         MAPS_LOGD("*********************************************");
561 }
562
563 plugin::user_consent_checker::user_consent_checker()
564 {
565 }
566
567 maps_plugin_h plugin::user_consent_checker::init(const provider_info &info,
568                                              const char *module, int *init_error)
569 {
570         /* 1.Initialize plugin */
571         if (info.file.empty() || !init_error) {
572                 if (init_error)
573                         *init_error = MAPS_ERROR_NOT_SUPPORTED;
574                 return NULL;
575         }
576
577         *init_error = MAPS_ERROR_NONE;
578
579         GMod *plugin = gmod_new(info.file, TRUE);
580         if (!plugin) {
581                 //LCOV_EXCL_START
582                 MAPS_LOGE("Open Module Failed: %s", info.file.c_str());
583                 *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
584                 return NULL;
585                 //LCOV_EXCL_STOP
586         }
587
588         /* 2.1 Create new plugin interface */
589         plugin_s* new_plugin = g_slice_new0(plugin_s);
590
591         /* 2. Perform steps to completely initialize a plugin */
592         do {
593                 if (!new_plugin) {
594                         //LCOV_EXCL_START
595                         MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
596                         *init_error = MAPS_ERROR_OUT_OF_MEMORY;
597                         break;
598                         //LCOV_EXCL_STOP
599                 }
600
601                 /* 2.1 Set plugin module handle */
602                 new_plugin->module = plugin;
603
604                 /* 2.2 Set plugin interface */
605                 new_plugin->interface = get_empty_interface();
606
607                 /* Plugin dedicated functions */
608                 new_plugin->interface.maps_plugin_request_user_consent =
609                         (maps_plugin_request_user_consent_f) gmod_find_sym(plugin,
610                         "maps_plugin_request_user_consent");
611
612                 if (!new_plugin->interface.maps_plugin_request_user_consent) {
613                         MAPS_LOGE("ERROR! Plugin request_user_consent function is NULL");
614                         *init_error = MAPS_ERROR_SERVICE_NOT_AVAILABLE;
615                         break;
616                 }
617
618                 /* 2.5 Return newly initialized plugin */
619                 return new_plugin;
620         } while (FALSE);
621
622         MAPS_LOGE("Shut down the plugin becuause of error");
623
624         /* 3. shutdown plugin in case of problem */
625         shutdown(new_plugin);
626         return NULL;
627 }
628
629 void plugin::user_consent_checker::shutdown(maps_plugin_h plugin_h)
630 {
631         if (!plugin_h)
632                 return;
633         plugin_s *plugin = (plugin_s *) plugin_h;
634         g_return_if_fail(plugin->module);
635
636         /* 4. Unload plugin from memory */
637         gmod_free((GMod *) plugin->module);
638
639         /* 5. Destroying the table with plugin capabilities */
640         /*maps_int_hashtable_destroy(plugin->capabilities); */
641
642         /* 6. Release memory used by plugin structure */
643         g_slice_free(plugin_s, plugin);
644 }