diff options
author | Jacob Keeler <jacob.keeler@livioradio.com> | 2019-03-07 22:43:04 -0500 |
---|---|---|
committer | jacobkeeler <jacob.keeler@livioradio.com> | 2019-03-07 22:45:21 -0500 |
commit | 9a60799a4f119e2411ee0a0a9d94ae1c8fa668f8 (patch) | |
tree | 730bd51dc8503a0d9c24f4e6f5c18ea5c56ddae9 /src/components/application_manager/src/application_manager_impl.cc | |
parent | 3be4d6a8384f5422a040121c11b448a7dd6901fc (diff) | |
parent | 3da1fc5cd3a1873400ea28cb9977926ffa56e34c (diff) | |
download | sdl_core-9a60799a4f119e2411ee0a0a9d94ae1c8fa668f8.tar.gz |
Merge branch 'feature/cloud_app_transport' into feature/base_app_services_implementation
Diffstat (limited to 'src/components/application_manager/src/application_manager_impl.cc')
-rw-r--r-- | src/components/application_manager/src/application_manager_impl.cc | 311 |
1 files changed, 293 insertions, 18 deletions
diff --git a/src/components/application_manager/src/application_manager_impl.cc b/src/components/application_manager/src/application_manager_impl.cc index f9da143720..9d16c366d5 100644 --- a/src/components/application_manager/src/application_manager_impl.cc +++ b/src/components/application_manager/src/application_manager_impl.cc @@ -73,6 +73,7 @@ #include "policy/usage_statistics/counter.h" #include "utils/custom_string.h" #include <time.h> +#include <boost/filesystem.hpp> namespace { int get_rand_from_range(uint32_t from = 0, int to = RAND_MAX) { @@ -241,6 +242,13 @@ DataAccessor<ApplicationSet> ApplicationManagerImpl::applications() const { return accessor; } +DataAccessor<AppsWaitRegistrationSet> +ApplicationManagerImpl::pending_applications() const { + DataAccessor<AppsWaitRegistrationSet> accessor( + apps_to_register_, apps_to_register_list_lock_ptr_); + return accessor; +} + ApplicationSharedPtr ApplicationManagerImpl::application( uint32_t app_id) const { AppIdPredicate finder(app_id); @@ -262,6 +270,20 @@ ApplicationSharedPtr ApplicationManagerImpl::application_by_policy_id( return FindApp(accessor, finder); } +ApplicationSharedPtr ApplicationManagerImpl::application_by_name( + const std::string& app_name) const { + AppNamePredicate finder(app_name); + DataAccessor<ApplicationSet> accessor = applications(); + return FindApp(accessor, finder); +} + +ApplicationSharedPtr ApplicationManagerImpl::pending_application_by_policy_id( + const std::string& policy_app_id) const { + PolicyAppIdPredicate finder(policy_app_id); + DataAccessor<AppsWaitRegistrationSet> accessor = pending_applications(); + return FindPendingApp(accessor, finder); +} + bool ActiveAppPredicate(const ApplicationSharedPtr app) { return app ? app->IsFullscreen() : false; } @@ -618,7 +640,7 @@ ApplicationSharedPtr ApplicationManagerImpl::RegisterApplication( // Set cloud app parameters application->set_cloud_app_endpoint((*it)->cloud_app_endpoint()); application->set_cloud_app_certificate((*it)->cloud_app_certificate()); - application->set_cloud_app_auth_token((*it)->cloud_app_auth_token()); + application->set_auth_token((*it)->auth_token()); application->set_cloud_app_transport_type( (*it)->cloud_app_transport_type()); application->set_hybrid_app_preference((*it)->hybrid_app_preference()); @@ -676,6 +698,10 @@ ApplicationSharedPtr ApplicationManagerImpl::RegisterApplication( apps_size_ = applications_.size(); applications_list_lock_ptr_->Release(); + // Update cloud app information, in case any pending apps are unable to be + // registered due to a mobile app taking precedence + RefreshCloudAppInformation(); + // It is possible that secondary transport of this app has been already // established. Make sure that the information is reflected to application // instance. @@ -811,21 +837,116 @@ void ApplicationManagerImpl::OnHMIStartedCooperation() { RefreshCloudAppInformation(); } +std::string ApplicationManagerImpl::PolicyIDByIconUrl(const std::string url) { + sync_primitives::AutoLock lock(app_icon_map_lock_ptr_); + for (auto& x : app_icon_map_) { + auto policy_id = x.first; + std::string icon_url = GetPolicyHandler().GetIconUrl(policy_id); + if (icon_url == url) { + LOG4CXX_DEBUG(logger_, "Matched icon url: " << url); + x.second.pending_request = false; + return policy_id; + } + } + return std::string(""); +} + +void ApplicationManagerImpl::SetIconFileFromSystemRequest( + const std::string policy_id) { + app_icon_map_lock_ptr_.Acquire(); + auto app_icon_it = app_icon_map_.find(policy_id); + if (app_icon_it != app_icon_map_.end()) { + app_icon_map_.erase(app_icon_it); + } + app_icon_map_lock_ptr_.Release(); + + // Find pending application and set icon path + auto app = pending_application_by_policy_id(policy_id); + if (!app) { + return; + } + const std::string app_icon_dir(settings_.app_icons_folder()); + const std::string full_icon_path(app_icon_dir + "/" + policy_id); + if (file_system::FileExists(full_icon_path)) { + LOG4CXX_DEBUG(logger_, "Set Icon Path: " << full_icon_path); + AppFile file; + file.is_persistent = true; + file.is_download_complete = true; + file.file_name = full_icon_path; + + std::string icon_url = GetPolicyHandler().GetIconUrl(policy_id); + std::string extension = boost::filesystem::extension(icon_url); + if (extension == "bmp" || extension == "BMP") { + file.file_type = mobile_apis::FileType::GRAPHIC_BMP; + } else if (extension == "JPEG" || extension == "jpeg" || + extension == "JPG" || extension == "jpg") { + file.file_type = mobile_apis::FileType::GRAPHIC_JPEG; + } else { + file.file_type = mobile_apis::FileType::GRAPHIC_PNG; + } + + app->AddFile(file); + app->set_app_icon_path(full_icon_path); + } + SendUpdateAppList(); +} + +void ApplicationManagerImpl::DisconnectCloudApp(ApplicationSharedPtr app) { + std::string endpoint; + std::string certificate; + std::string auth_token; + std::string cloud_transport_type; + std::string hybrid_app_preference; + bool enabled = true; + std::string policy_app_id = app->policy_app_id(); + GetPolicyHandler().GetCloudAppParameters(policy_app_id, + enabled, + endpoint, + certificate, + auth_token, + cloud_transport_type, + hybrid_app_preference); + if (app->IsRegistered() && app->is_cloud_app()) { + LOG4CXX_DEBUG(logger_, "Disabled app is registered, unregistering now"); + GetRPCService().ManageMobileCommand( + MessageHelper::GetOnAppInterfaceUnregisteredNotificationToMobile( + app->app_id(), + mobile_api::AppInterfaceUnregisteredReason::APP_UNAUTHORIZED), + commands::Command::SOURCE_SDL); + + OnAppUnauthorized(app->app_id()); + } + // Delete the cloud device + connection_handler().RemoveCloudAppDevice(app->device()); + + transport_manager::transport_adapter::CloudAppProperties properties{ + endpoint, + certificate, + enabled, + auth_token, + cloud_transport_type, + hybrid_app_preference}; + // Create device in pending state + LOG4CXX_DEBUG(logger_, "Re-adding the cloud app device"); + connection_handler().AddCloudAppDevice(policy_app_id, properties); +} + void ApplicationManagerImpl::RefreshCloudAppInformation() { LOG4CXX_AUTO_TRACE(logger_); std::vector<std::string> enabled_apps; GetPolicyHandler().GetEnabledCloudApps(enabled_apps); std::vector<std::string>::iterator enabled_it = enabled_apps.begin(); std::vector<std::string>::iterator enabled_end = enabled_apps.end(); - std::string endpoint = ""; - std::string certificate = ""; - std::string auth_token = ""; - std::string cloud_transport_type = ""; - std::string hybrid_app_preference = ""; + std::string endpoint; + std::string certificate; + std::string auth_token; + std::string cloud_transport_type; + std::string hybrid_app_preference_str; bool enabled = true; // Store old device map and clear the current map pending_device_map_lock_ptr_->Acquire(); + app_icon_map_lock_ptr_.Acquire(); std::map<std::string, std::string> old_device_map = pending_device_map_; pending_device_map_ = std::map<std::string, std::string>(); // Create a device for each newly enabled cloud app @@ -836,21 +957,82 @@ void ApplicationManagerImpl::RefreshCloudAppInformation() { certificate, auth_token, cloud_transport_type, - hybrid_app_preference); + hybrid_app_preference_str); + + mobile_apis::HybridAppPreference::eType hybrid_app_preference = + mobile_apis::HybridAppPreference::INVALID_ENUM; + smart_objects::EnumConversionHelper< + mobile_apis::HybridAppPreference::eType>:: + StringToEnum(hybrid_app_preference_str, &hybrid_app_preference); + + auto policy_id = *enabled_it; + policy::StringArray nicknames; + policy::StringArray app_hmi_types; + GetPolicyHandler().GetInitialAppData(policy_id, &nicknames, &app_hmi_types); + + if (nicknames.empty()) { + LOG4CXX_ERROR(logger_, "Cloud App missing nickname"); + continue; + } else if (mobile_apis::HybridAppPreference::MOBILE == + hybrid_app_preference) { + auto nickname_it = nicknames.begin(); + for (; nickname_it != nicknames.end(); ++nickname_it) { + auto app = application_by_name(*nickname_it); + if (app.use_count() != 0) { + LOG4CXX_ERROR( + logger_, + "Mobile app already registered for cloud app: " << *nickname_it); + continue; + } + } + } pending_device_map_.insert( - std::pair<std::string, std::string>(endpoint, *enabled_it)); + std::pair<std::string, std::string>(endpoint, policy_id)); // Determine which endpoints were disabled by erasing all enabled apps from // the old device list auto old_device_it = old_device_map.find(endpoint); if (old_device_it != old_device_map.end()) { old_device_map.erase(old_device_it); + } + + transport_manager::transport_adapter::CloudAppProperties properties{ + endpoint, + certificate, + enabled, + auth_token, + cloud_transport_type, + hybrid_app_preference_str}; + + // If the device was disconnected, this will reinitialize the device + connection_handler().AddCloudAppDevice(policy_id, properties); + + // Look for app icon url data and add to app_icon_url_map + std::string url = GetPolicyHandler().GetIconUrl(policy_id); + + if (url.empty()) { + LOG4CXX_DEBUG(logger_, "No Icon Url for cloud app"); + continue; + } + + auto app_icon_it = app_icon_map_.find(policy_id); + if (app_icon_it != app_icon_map_.end()) { + LOG4CXX_DEBUG(logger_, "Cloud App Already Exists in Icon Map"); continue; } - connection_handler().AddCloudAppDevice( - *enabled_it, endpoint, cloud_transport_type); + const std::string app_icon_dir(settings_.app_icons_folder()); + const std::string full_icon_path(app_icon_dir + "/" + policy_id); + if (!file_system::FileExists(full_icon_path)) { + int icon_map_size = app_icon_map_.size(); + AppIconInfo icon_info(endpoint, false); + LOG4CXX_DEBUG(logger_, + "Inserting cloud app into icon map: " << icon_map_size); + app_icon_map_.insert( + std::pair<std::string, AppIconInfo>(policy_id, icon_info)); + } } + app_icon_map_lock_ptr_.Release(); pending_device_map_lock_ptr_->Release(); int removed_app_count = 0; @@ -905,16 +1087,17 @@ void ApplicationManagerImpl::CreatePendingApplication( connection_handler::DeviceHandle device_id) { LOG4CXX_AUTO_TRACE(logger_); - std::string endpoint = ""; - std::string certificate = ""; - std::string auth_token = ""; - std::string cloud_transport_type = ""; - std::string hybrid_app_preference_str = ""; + std::string endpoint; + std::string certificate; + std::string auth_token; + std::string cloud_transport_type; + std::string hybrid_app_preference_str; bool enabled = true; std::string name = device_info.name(); pending_device_map_lock_ptr_->Acquire(); auto it = pending_device_map_.find(name); if (it == pending_device_map_.end()) { + pending_device_map_lock_ptr_->Release(); return; } pending_device_map_lock_ptr_->Release(); @@ -927,7 +1110,7 @@ void ApplicationManagerImpl::CreatePendingApplication( GetPolicyHandler().GetInitialAppData( policy_app_id, &nicknames, &app_hmi_types); - if (!nicknames.size()) { + if (nicknames.empty()) { LOG4CXX_ERROR(logger_, "Cloud App missing nickname"); return; } @@ -968,7 +1151,7 @@ void ApplicationManagerImpl::CreatePendingApplication( mobile_apis::HybridAppPreference::eType>:: StringToEnum(hybrid_app_preference_str, &hybrid_app_preference_enum); - if (!convert_result) { + if (!hybrid_app_preference_str.empty() && !convert_result) { LOG4CXX_ERROR( logger_, "Could not convert string to enum: " << hybrid_app_preference_str); @@ -977,7 +1160,7 @@ void ApplicationManagerImpl::CreatePendingApplication( application->set_hmi_application_id(GenerateNewHMIAppID()); application->set_cloud_app_endpoint(endpoint); - application->set_cloud_app_auth_token(auth_token); + application->set_auth_token(auth_token); application->set_cloud_app_transport_type(cloud_transport_type); application->set_hybrid_app_preference(hybrid_app_preference_enum); application->set_cloud_app_certificate(certificate); @@ -992,6 +1175,36 @@ void ApplicationManagerImpl::CreatePendingApplication( SendUpdateAppList(); } +void ApplicationManagerImpl::SetPendingApplicationState( + const transport_manager::ConnectionUID connection_id, + const transport_manager::DeviceInfo& device_info) { + std::string name = device_info.name(); + pending_device_map_lock_ptr_->Acquire(); + auto it = pending_device_map_.find(name); + if (it == pending_device_map_.end()) { + pending_device_map_lock_ptr_->Release(); + return; + } + pending_device_map_lock_ptr_->Release(); + + const std::string policy_app_id = it->second; + auto app = application_by_policy_id(policy_app_id); + + if (!app) { + return; + } + LOG4CXX_DEBUG(logger_, + "Unregister application and move into apps_to_register"); + { + sync_primitives::AutoLock lock(apps_to_register_list_lock_ptr_); + apps_to_register_.insert(app); + } + + UnregisterApplication( + app->app_id(), mobile_apis::Result::INVALID_ENUM, true, true); + app->MarkUnregistered(); +} + void ApplicationManagerImpl::OnConnectionStatusUpdated() { SendUpdateAppList(); } @@ -1246,6 +1459,7 @@ void ApplicationManagerImpl::OnDeviceListUpdated( so_to_send[jhs::S_PARAMS][jhs::S_CORRELATION_ID] = GetNextHMICorrelationID(); so_to_send[jhs::S_MSG_PARAMS] = *msg_params; rpc_service_->ManageHMICommand(update_list); + RefreshCloudAppInformation(); } void ApplicationManagerImpl::OnFindNewApplicationsRequest() { @@ -2741,6 +2955,7 @@ void ApplicationManagerImpl::UnregisterApplication( logger_, "There is no more SDL4 apps with device handle: " << handle); RemoveAppsWaitingForRegistration(handle); + RefreshCloudAppInformation(); SendUpdateAppList(); } } @@ -3625,7 +3840,15 @@ void ApplicationManagerImpl::OnPTUFinished(const bool ptu_result) { if (!ptu_result) { return; } + RefreshCloudAppInformation(); + + auto app_id = policy_handler_->GetAppIdForSending(); + auto app = application(app_id); + if (app) { + SendGetIconUrlNotifications(app->app_id(), app); + } + auto on_app_policy_updated = [](plugin_manager::RPCPlugin& plugin) { plugin.OnPolicyEvent(plugin_manager::kApplicationPolicyUpdated); }; @@ -3668,6 +3891,58 @@ void ApplicationManagerImpl::SendDriverDistractionState( } } +void ApplicationManagerImpl::SendGetIconUrlNotifications( + const uint32_t connection_key, ApplicationSharedPtr application) { + LOG4CXX_AUTO_TRACE(logger_); + std::vector<std::string> enabled_apps; + GetPolicyHandler().GetEnabledCloudApps(enabled_apps); + std::vector<std::string>::iterator enabled_it = enabled_apps.begin(); + std::vector<std::string>::iterator enabled_end = enabled_apps.end(); + sync_primitives::AutoLock lock(app_icon_map_lock_ptr_); + for (; enabled_it != enabled_end; ++enabled_it) { + auto app_icon_it = app_icon_map_.find(*enabled_it); + if (app_icon_it == app_icon_map_.end()) { + LOG4CXX_WARN(logger_, "Could not find cloud app in icon map"); + continue; + } + + std::string endpoint = app_icon_it->second.endpoint; + bool pending_request = app_icon_it->second.pending_request; + + if (pending_request) { + LOG4CXX_DEBUG(logger_, "Cloud app has already sent request"); + continue; + } + + std::string url = GetPolicyHandler().GetIconUrl(*enabled_it); + + if (url.empty()) { + LOG4CXX_DEBUG(logger_, "No Icon Url for cloud app"); + continue; + } + + LOG4CXX_DEBUG(logger_, "Creating Get Icon Request"); + + smart_objects::SmartObjectSPtr message = + std::make_shared<smart_objects::SmartObject>( + smart_objects::SmartType_Map); + (*message)[strings::params][strings::function_id] = + mobile_apis::FunctionID::OnSystemRequestID; + (*message)[strings::params][strings::connection_key] = connection_key; + (*message)[strings::params][strings::message_type] = + mobile_apis::messageType::notification; + (*message)[strings::params][strings::protocol_version] = + application->protocol_version(); + (*message)[strings::msg_params][strings::request_type] = + mobile_apis::RequestType::ICON_URL; + (*message)[strings::msg_params][strings::url] = url; + + app_icon_it->second.pending_request = true; + + rpc_service_->ManageMobileCommand(message, commands::Command::SOURCE_SDL); + } +} + protocol_handler::MajorProtocolVersion ApplicationManagerImpl::SupportedSDLVersion() const { LOG4CXX_AUTO_TRACE(logger_); |