summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/app_service_manager.cc
blob: 3e7b4b99fef2c0b9e381a97de72c14dcc191bfea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * Copyright (c) 2018, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "application_manager/app_service_manager.h"

#include <algorithm>
#include <iterator>

#include "application_manager/application.h"
#include "application_manager/application_manager.h"
#include "application_manager/commands/command_impl.h"
#include "application_manager/message_helper.h"
#include "application_manager/smart_object_keys.h"
#include "encryption/hashing.h"
#include "resumption/last_state.h"
#include "smart_objects/enum_schema_item.h"
#include "utils/logger.h"

CREATE_LOGGERPTR_GLOBAL(logger_, "AppServiceManager")

namespace application_manager {

const char* kAppServiceSection = "AppServices";
const char* kDefaults = "defaults";

AppServiceManager::AppServiceManager(ApplicationManager& app_manager,
                                     resumption::LastStateWrapperPtr last_state)
    : app_manager_(app_manager)
    , last_state_(last_state)
    , rpc_passing_handler_(*this, app_manager_) {}

AppServiceManager::~AppServiceManager() {
  LOG4CXX_AUTO_TRACE(logger_);
}

smart_objects::SmartObject AppServiceManager::PublishAppService(
    const smart_objects::SmartObject& manifest,
    const bool mobile_service,
    const uint32_t connection_key) {
  LOG4CXX_AUTO_TRACE(logger_);
  std::string str_to_hash = "";
  std::string service_id = "";

  std::string service_type = manifest[strings::service_type].asString();

  AppService* found_service =
      FindServiceByProvider(connection_key, service_type);
  if (found_service) {
    // Check if there is a different existing service with the same updated
    // name.
    if (manifest.keyExists(strings::service_name)) {
      auto service_by_name =
          FindServiceByName(manifest[strings::service_name].asString());
      if (service_by_name) {
        auto service_by_name_id =
            service_by_name->record[strings::service_id].asString();
        auto found_service_id =
            found_service->record[strings::service_id].asString();
        if (service_by_name_id != found_service_id) {
          LOG4CXX_WARN(logger_,
                       "A service already exists with this name, rejecting");
          return smart_objects::SmartObject();
        }
      }
    }
    LOG4CXX_WARN(logger_, "Service already exists for this provider, updating");
    published_services_lock_.Acquire();
    found_service->record[strings::service_manifest] = manifest;
    found_service->record[strings::service_published] = true;
    smart_objects::SmartObject updated_service_record = found_service->record;
    published_services_lock_.Release();

    smart_objects::SmartObject msg_params;
    msg_params[strings::system_capability][strings::system_capability_type] =
        mobile_apis::SystemCapabilityType::APP_SERVICES;

    AppServiceUpdated(updated_service_record,
                      mobile_apis::ServiceUpdateReason::MANIFEST_UPDATE,
                      msg_params);

    MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);

    return updated_service_record;
  }

  if (manifest.keyExists(strings::service_name) &&
      FindServiceByName(manifest[strings::service_name].asString())) {
    LOG4CXX_WARN(logger_, "A service already exists with this name, rejecting");
    return smart_objects::SmartObject();
  }

  published_services_lock_.Acquire();
  do {
    str_to_hash = manifest[strings::service_type].asString() +
                  std::to_string(std::rand());
    service_id = encryption::MakeHash(str_to_hash);
  } while (published_services_.find(service_id) != published_services_.end());

  AppService app_service;
  app_service.connection_key = connection_key;
  app_service.mobile_service = mobile_service;

  smart_objects::SmartObject service_record;
  service_record[strings::service_manifest] = manifest;
  service_record[strings::service_id] = service_id;
  service_record[strings::service_published] = true;
  service_record[strings::service_active] = false;
  app_service.record = service_record;

  std::string default_app_id = DefaultServiceByType(service_type);
  if (default_app_id.empty() && !mobile_service) {
    auto embedded_services = app_manager_.get_settings().embedded_services();
    for (const auto& embedded_service : embedded_services) {
      if (embedded_service == service_type) {
        auto last_state_accessor = last_state_->get_accessor();
        Json::Value dictionary = last_state_accessor.GetData().dictionary();
        dictionary[kAppServiceSection][kDefaults][service_type] =
            kEmbeddedService;
        default_app_id = kEmbeddedService;
        last_state_accessor.GetMutableData().set_dictionary(dictionary);
      }
    }
  }
  app_service.default_service =
      !default_app_id.empty() && GetPolicyAppID(app_service) == default_app_id;

  auto ret = published_services_.insert(
      std::pair<std::string, AppService>(service_id, app_service));
  smart_objects::SmartObject& published_record = ret.first->second.record;
  published_services_lock_.Release();

  smart_objects::SmartObject msg_params;
  msg_params[strings::system_capability][strings::system_capability_type] =
      mobile_apis::SystemCapabilityType::APP_SERVICES;
  AppServiceUpdated(
      service_record, mobile_apis::ServiceUpdateReason::PUBLISHED, msg_params);

  MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);

  // Activate the new service if it is the default for its service type, if
  // no service is active of its service type, or it is a mobile app in full.
  AppService* active_service = ActiveServiceForType(service_type);
  ApplicationSharedPtr app = NULL;
  if (mobile_service && connection_key) {
    app = app_manager_.application(connection_key);
  }

  if (!active_service || app_service.default_service ||
      (app && app->IsFullscreen())) {
    ActivateAppService(service_id);
  }

  return published_record;
}

bool AppServiceManager::UnpublishAppService(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
    return false;
  }
  LOG4CXX_DEBUG(logger_, "Unpublishing app service: " << service_id);

  SetServicePublished(service_id, false);
  smart_objects::SmartObject msg_params;
  msg_params[strings::system_capability][strings::system_capability_type] =
      mobile_apis::SystemCapabilityType::APP_SERVICES;

  auto& record = it->second.record;
  if (record[strings::service_active].asBool()) {
    record[strings::service_active] = false;

    // Activate embedded service, if available
    auto embedded_service = EmbeddedServiceForType(
        record[strings::service_manifest][strings::service_type].asString());
    if (embedded_service &&
        (embedded_service->record[strings::service_id].asString() !=
         service_id)) {
      embedded_service->record[strings::service_active] = true;
      AppServiceUpdated(embedded_service->record,
                        mobile_apis::ServiceUpdateReason::ACTIVATED,
                        msg_params);
    }
  }
  AppServiceUpdated(
      record, mobile_apis::ServiceUpdateReason::REMOVED, msg_params);
  MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);
  published_services_.erase(it);

  return true;
}

void AppServiceManager::UnpublishServices(const uint32_t connection_key) {
  LOG4CXX_AUTO_TRACE(logger_);
  LOG4CXX_DEBUG(logger_, "Unpublishing all app services: " << connection_key);

  std::list<std::string> app_published_services;
  {
    sync_primitives::AutoLock lock(published_services_lock_);
    for (auto it = published_services_.begin(); it != published_services_.end();
         ++it) {
      if (it->second.connection_key == connection_key) {
        app_published_services.push_back(it->first);
      }
    }
  }

  for (auto& service_id : app_published_services) {
    UnpublishAppService(service_id);
  }
}

void AppServiceManager::OnAppActivated(ApplicationConstSharedPtr app) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.begin();
  // Activate all services published by the app
  for (; it != published_services_.end(); ++it) {
    if (it->second.connection_key == app->app_id()) {
      ActivateAppService(it->first);
    }
  }
}

std::vector<smart_objects::SmartObject>
AppServiceManager::GetAllServiceRecords() {
  LOG4CXX_AUTO_TRACE(logger_);
  std::vector<smart_objects::SmartObject> services;
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto it = published_services_.begin(); it != published_services_.end();
       ++it) {
    services.push_back(it->second.record);
  }
  return services;
}

void AppServiceManager::GetProviderByType(const std::string& service_type,
                                          const bool mobile_consumer,
                                          ApplicationSharedPtr& app,
                                          bool& hmi_service) {
  LOG4CXX_AUTO_TRACE(logger_);
  auto active_service = ActiveServiceForType(service_type);
  if (!active_service) {
    LOG4CXX_ERROR(logger_,
                  "There is no active service for the given service type: "
                      << service_type);
    return;
  }

  LOG4CXX_DEBUG(logger_, "Found provider for service type: " << service_type);
  GetProviderFromService(*active_service, mobile_consumer, app, hmi_service);
}

void AppServiceManager::GetProviderByID(const std::string& service_id,
                                        const bool mobile_consumer,
                                        ApplicationSharedPtr& app,
                                        bool& hmi_service) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
    return;
  }

  LOG4CXX_DEBUG(logger_, "Found provider with service ID: " << service_id);
  GetProviderFromService(it->second, mobile_consumer, app, hmi_service);
}

void AppServiceManager::GetProviderFromService(const AppService& service,
                                               const bool mobile_consumer,
                                               ApplicationSharedPtr& app,
                                               bool& hmi_service) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (mobile_consumer &&
      !service.record[strings::service_manifest][strings::allow_app_consumers]
           .asBool()) {
    LOG4CXX_ERROR(logger_, "Service does not support app consumers");
    return;
  }
  bool mobile_service = service.mobile_service;
  if (mobile_service) {
    app = app_manager_.application(service.connection_key);
    hmi_service = false;
    return;
  }
  hmi_service = true;
}

bool AppServiceManager::SetDefaultService(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
    return false;
  }
  AppService& service = it->second;

  std::string service_type =
      service.record[strings::service_manifest][strings::service_type]
          .asString();
  std::string default_app_id = DefaultServiceByType(service_type);
  if (!default_app_id.empty()) {
    auto default_service =
        FindServiceByPolicyAppID(default_app_id, service_type);
    if (default_service) {
      default_service->default_service = false;
    }
  }
  service.default_service = true;

  auto last_state_accessor = last_state_->get_accessor();
  Json::Value dictionary = last_state_accessor.GetData().dictionary();
  dictionary[kAppServiceSection][kDefaults][service_type] =
      GetPolicyAppID(service);
  last_state_accessor.GetMutableData().set_dictionary(dictionary);
  return true;
}

bool AppServiceManager::RemoveDefaultService(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
    return false;
  }

  AppService& service = it->second;
  if (!service.default_service) {
    LOG4CXX_ERROR(logger_, "Service was not default " << service_id);
    return false;
  }
  service.default_service = false;

  std::string service_type =
      service.record[strings::service_manifest][strings::service_type]
          .asString();

  auto last_state_accessor = last_state_->get_accessor();
  Json::Value dictionary = last_state_accessor.GetData().dictionary();
  dictionary[kAppServiceSection][kDefaults].removeMember(service_type);
  last_state_accessor.GetMutableData().set_dictionary(dictionary);
  return true;
}

bool AppServiceManager::ActivateAppService(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
    return false;
  }

  smart_objects::SmartObject& service = it->second.record;
  if (service[strings::service_active].asBool()) {
    LOG4CXX_DEBUG(logger_, "Service was already active " << service_id);
    return true;
  }

  smart_objects::SmartObject msg_params;
  msg_params[strings::system_capability][strings::system_capability_type] =
      mobile_apis::SystemCapabilityType::APP_SERVICES;

  const std::string service_type =
      service[strings::service_manifest][strings::service_type].asString();
  auto active_service = ActiveServiceForType(service_type);
  if (active_service) {
    active_service->record[strings::service_active] = false;
    AppServiceUpdated(active_service->record,
                      mobile_apis::ServiceUpdateReason::DEACTIVATED,
                      msg_params);
  }
  service[strings::service_active] = true;
  AppServiceUpdated(
      service, mobile_apis::ServiceUpdateReason::ACTIVATED, msg_params);

  MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);

  std::string navi_service_type;
  ns_smart_device_link::ns_smart_objects::
      EnumConversionHelper<mobile_apis::AppServiceType::eType>::EnumToString(
          mobile_apis::AppServiceType::NAVIGATION, &navi_service_type);
  if (service_type == navi_service_type) {
    smart_objects::SmartObject msg_params;
    msg_params[strings::system_capability][strings::system_capability_type] =
        mobile_apis::SystemCapabilityType::NAVIGATION;
    MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);
  }
  return true;
}

bool AppServiceManager::DeactivateAppService(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Unable to find published service " << service_id);
    return false;
  }
  smart_objects::SmartObject& service = it->second.record;
  smart_objects::SmartObject msg_params;
  bool send_update = false;

  if (service[strings::service_active].asBool()) {
    service[strings::service_active] = false;
    AppServiceUpdated(
        service, mobile_apis::ServiceUpdateReason::DEACTIVATED, msg_params);

    // Activate embedded service, if available
    const std::string service_type =
        service[strings::service_manifest][strings::service_type].asString();
    auto embedded_service = EmbeddedServiceForType(service_type);
    if (embedded_service &&
        embedded_service->record[strings::service_id].asString() !=
            service_id) {
      embedded_service->record[strings::service_active] = true;
      AppServiceUpdated(embedded_service->record,
                        mobile_apis::ServiceUpdateReason::ACTIVATED,
                        msg_params);
    }
    send_update = true;
  }

  if (send_update) {
    msg_params[strings::system_capability][strings::system_capability_type] =
        mobile_apis::SystemCapabilityType::APP_SERVICES;
    MessageHelper::BroadcastCapabilityUpdate(msg_params, app_manager_);
  }
  return true;
}

AppService* AppServiceManager::ActiveServiceForType(
    const std::string service_type) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto it = published_services_.begin(); it != published_services_.end();
       ++it) {
    if (it->second.record[strings::service_manifest][strings::service_type]
                .asString() == service_type &&
        it->second.record[strings::service_published].asBool() &&
        it->second.record[strings::service_active].asBool()) {
      return &(it->second);
    }
  }
  return NULL;
}

AppService* AppServiceManager::EmbeddedServiceForType(
    const std::string service_type) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto it = published_services_.begin(); it != published_services_.end();
       ++it) {
    if (it->second.record[strings::service_manifest][strings::service_type]
                .asString() == service_type &&
        !it->second.mobile_service) {
      return &(it->second);
    }
  }
  return NULL;
}

AppService* AppServiceManager::FindServiceByPolicyAppID(
    const std::string policy_app_id, const std::string type) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto it = published_services_.begin(); it != published_services_.end();
       ++it) {
    if (it->second.record[strings::service_manifest][strings::service_type]
            .asString() != type) {
      continue;
    }

    if (policy_app_id == GetPolicyAppID(it->second)) {
      return &(it->second);
    }
  }
  return NULL;
}

AppService* AppServiceManager::FindServiceByID(const std::string service_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);

  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
    return NULL;
  }
  return &(it->second);
}

AppService* AppServiceManager::FindServiceByProvider(
    const uint32_t connection_key, const std::string service_type) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto& service : published_services_) {
    if (service.second.connection_key == connection_key &&
        service.second.record[strings::service_manifest][strings::service_type]
                .asString() == service_type) {
      return &(service.second);
    }
  }
  return NULL;
}

AppService* AppServiceManager::FindServiceByName(std::string name) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  for (auto& service : published_services_) {
    if (service.second.record[strings::service_manifest][strings::service_name]
            .asString() == name) {
      return &(service.second);
    }
  }
  return NULL;
}

std::string AppServiceManager::DefaultServiceByType(
    const std::string service_type) {
  LOG4CXX_AUTO_TRACE(logger_);

  auto last_state_accessor = last_state_->get_accessor();
  Json::Value dictionary = last_state_accessor.GetData().dictionary();
  if (dictionary[kAppServiceSection][kDefaults].isMember(service_type)) {
    return dictionary[kAppServiceSection][kDefaults][service_type].asString();
  }
  return std::string();
}

void AppServiceManager::SetServicePublished(const std::string service_id,
                                            const bool service_published) {
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock lock(published_services_lock_);
  auto it = published_services_.find(service_id);
  if (it == published_services_.end()) {
    LOG4CXX_ERROR(logger_, "Service id does not exist in published services");
    return;
  }
  it->second.record[strings::service_published] = service_published;
}

std::string AppServiceManager::GetPolicyAppID(AppService service) {
  if (service.mobile_service) {
    auto app = app_manager_.application(service.connection_key);
    return app ? app->policy_app_id() : std::string();
  }
  return kEmbeddedService;
}

bool AppServiceManager::UpdateNavigationCapabilities(
    smart_objects::SmartObject& out_params) {
  LOG4CXX_AUTO_TRACE(logger_);
  std::string navi_service_type;
  ns_smart_device_link::ns_smart_objects::
      EnumConversionHelper<mobile_apis::AppServiceType::eType>::EnumToString(
          mobile_apis::AppServiceType::NAVIGATION, &navi_service_type);
  auto service = ActiveServiceForType(navi_service_type);
  if (!service) {
    return false;
  }

  if (!out_params.keyExists(strings::send_location_enabled)) {
    out_params[strings::send_location_enabled] = false;
  }
  if (!out_params.keyExists(strings::get_way_points_enabled)) {
    out_params[strings::get_way_points_enabled] = false;
  }

  if (!service->record[strings::service_manifest].keyExists(
          strings::handled_rpcs)) {
    return true;
  }

  smart_objects::SmartObject& handled_rpcs =
      service->record[strings::service_manifest][strings::handled_rpcs];
  for (size_t i = 0; i < handled_rpcs.length(); ++i) {
    if (handled_rpcs[i].asInt() == mobile_apis::FunctionID::SendLocationID) {
      out_params[strings::send_location_enabled] = true;
    } else if (handled_rpcs[i].asInt() ==
               mobile_apis::FunctionID::GetWayPointsID) {
      out_params[strings::get_way_points_enabled] = true;
    }
  }
  return true;
}

void AppServiceManager::AppServiceUpdated(
    const smart_objects::SmartObject& service_record,
    const mobile_apis::ServiceUpdateReason::eType update_reason,
    smart_objects::SmartObject& msg_params) {
  LOG4CXX_AUTO_TRACE(logger_);
  smart_objects::SmartObject& services =
      msg_params[strings::system_capability][strings::app_services_capabilities]
                [strings::app_services];
  smart_objects::SmartObject service(smart_objects::SmartType_Map);
  service[strings::update_reason] = update_reason;
  service[strings::updated_app_service_record] = service_record;
  services[-1] = service;
}

std::vector<AppService> AppServiceManager::GetActiveServices() {
  std::vector<AppService> active_services;
  for (auto it = published_services_.begin(); it != published_services_.end();
       ++it) {
    if (it->second.record[strings::service_active].asBool()) {
      active_services.push_back(it->second);
    }
  }
  return active_services;
}

RPCPassingHandler& AppServiceManager::GetRPCPassingHandler() {
  return rpc_passing_handler_;
}

}  //  namespace application_manager