From 471b4131195a0c9c653d087056bdb6825634739c Mon Sep 17 00:00:00 2001 From: jacobkeeler Date: Tue, 23 Apr 2019 13:37:17 -0400 Subject: Prevent apps from publishing several services of the same type --- .../application_manager/app_service_manager.h | 14 +++++++++-- .../commands/hmi/as_publish_app_service_request.cc | 12 ++++++++- .../commands/mobile/publish_app_service_request.cc | 10 ++++++-- .../application_manager/src/app_service_manager.cc | 29 +++++++++++++++++++--- 4 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/components/application_manager/include/application_manager/app_service_manager.h b/src/components/application_manager/include/application_manager/app_service_manager.h index 9690d7cd46..9f096af536 100644 --- a/src/components/application_manager/include/application_manager/app_service_manager.h +++ b/src/components/application_manager/include/application_manager/app_service_manager.h @@ -81,7 +81,8 @@ class AppServiceManager { * app. False, if published by the embedded system. * @param connection_key - If mobile_service is true, the connection key of * the app publishing this service. - * @return The app service record of the published app service + * @return The app service record of the published app service on success, a + * Null SmartObject value on failure */ virtual smart_objects::SmartObject PublishAppService( const smart_objects::SmartObject& manifest, @@ -197,7 +198,7 @@ class AppServiceManager { /** * @brief Get the service with a given service ID. - * @param service_type - The service ID + * @param service_id - The service ID * @return A pointer to requested service on success, NULL on failure */ virtual AppService* FindServiceByID(const std::string service_id); @@ -247,6 +248,15 @@ class AppServiceManager { std::string DefaultServiceByType(const std::string service_type); AppService* FindServiceByPolicyAppID(const std::string policy_app_id, const std::string type); + + /** + * @brief Get the service of a given type published by a given provider. + * @param connection_key - The connection key of the service provider + * @param service_type - The service ID + * @return A pointer to requested service on success, NULL on failure + */ + virtual AppService* FindServiceByProvider(const uint32_t connection_key, + const std::string service_type); std::string GetPolicyAppID(AppService service); }; diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_publish_app_service_request.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_publish_app_service_request.cc index 637240666d..d60f5d9a88 100644 --- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_publish_app_service_request.cc +++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/hmi/as_publish_app_service_request.cc @@ -63,13 +63,23 @@ void ASPublishAppServiceRequest::Run() { smart_objects::SmartObject service_record = application_manager_.GetAppServiceManager().PublishAppService(manifest, false); + if (smart_objects::SmartType_Map != service_record.getType()) { + SendErrorResponse( + (*message_)[strings::params][strings::correlation_id].asUInt(), + hmi_apis::FunctionID::AppService_PublishAppService, + hmi_apis::Common_Result::REJECTED, + "Failed to publish service", + application_manager::commands::Command::SOURCE_SDL_TO_HMI); + return; + } response_params[strings::app_service_record] = service_record; SendResponse(true, (*message_)[strings::params][strings::correlation_id].asUInt(), hmi_apis::FunctionID::AppService_PublishAppService, hmi_apis::Common_Result::SUCCESS, - &response_params); + &response_params, + application_manager::commands::Command::SOURCE_SDL_TO_HMI); } } // namespace commands diff --git a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc index bb0855d42f..65e3ffb3da 100644 --- a/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc +++ b/src/components/application_manager/rpc_plugins/app_service_rpc_plugin/src/commands/mobile/publish_app_service_request.cc @@ -99,8 +99,7 @@ void PublishAppServiceRequest::Run() { if (!result) { SendResponse(false, mobile_apis::Result::DISALLOWED, - "Service disallowed by policies", - NULL); + "Service disallowed by policies"); return; } @@ -111,6 +110,13 @@ void PublishAppServiceRequest::Run() { smart_objects::SmartObject service_record = application_manager_.GetAppServiceManager().PublishAppService( manifest, true, connection_key()); + + if (smart_objects::SmartType_Map != service_record.getType()) { + SendResponse( + false, mobile_apis::Result::REJECTED, "Failed to publish service"); + return; + } + if (app->IsFullscreen()) { // Service should be activated if app is in the foreground application_manager_.GetAppServiceManager().ActivateAppService( diff --git a/src/components/application_manager/src/app_service_manager.cc b/src/components/application_manager/src/app_service_manager.cc index 1711180980..b2b6984f06 100644 --- a/src/components/application_manager/src/app_service_manager.cc +++ b/src/components/application_manager/src/app_service_manager.cc @@ -70,6 +70,15 @@ smart_objects::SmartObject AppServiceManager::PublishAppService( std::string str_to_hash = ""; std::string service_id = ""; + std::string service_type = manifest[strings::service_type].asString(); + + AppService* existing_service = + FindServiceByProvider(connection_key, service_type); + if (existing_service) { + LOG4CXX_DEBUG(logger_, "Service already exists, rejecting"); + return smart_objects::SmartObject(); + } + published_services_lock_.Acquire(); do { str_to_hash = manifest[strings::service_type].asString() + @@ -88,8 +97,6 @@ smart_objects::SmartObject AppServiceManager::PublishAppService( service_record[strings::service_active] = false; app_service.record = service_record; - std::string service_type = manifest[strings::service_type].asString(); - std::string default_app_id = DefaultServiceByType(service_type); if (default_app_id.empty() && !mobile_service) { auto embedded_services = app_manager_.get_settings().embedded_services(); @@ -378,7 +385,7 @@ bool AppServiceManager::DeactivateAppService(const std::string service_id) { auto embedded_service = EmbeddedServiceForType(service_type); if (embedded_service && embedded_service->record[strings::service_id].asString() != - service[strings::service_id].asString()) { + service_id) { embedded_service->record[strings::service_active] = true; AppServiceUpdated(embedded_service->record, mobile_apis::ServiceUpdateReason::ACTIVATED, @@ -456,6 +463,22 @@ AppService* AppServiceManager::FindServiceByID(const std::string service_id) { 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 it = published_services_.begin(); it != published_services_.end(); + ++it) { + if (it->second.connection_key == connection_key && + it->second.record[strings::service_manifest][strings::service_type] + .asString() == service_type) { + return &(it->second); + } + } + return NULL; +} + std::string AppServiceManager::DefaultServiceByType( const std::string service_type) { LOG4CXX_AUTO_TRACE(logger_); -- cgit v1.2.1