summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/resumption/resumption_data_processor.cc
blob: 508c86ea941de576a7f002ec98c2c2a16c9753e4 (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
/*
 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 <algorithm>

#include "application_manager/resumption/resumption_data_processor.h"
#include "application_manager/application_manager.h"
#include "application_manager/smart_object_keys.h"
#include "application_manager/message_helper.h"
#include "application_manager/event_engine/event_observer.h"

namespace resumption {

using app_mngr::ApplicationSharedPtr;
using app_mngr::AppFile;
using app_mngr::MessageHelper;
using app_mngr::ChoiceSetMap;
using app_mngr::ButtonSubscriptions;
namespace strings = app_mngr::strings;
namespace event_engine = app_mngr::event_engine;

CREATE_LOGGERPTR_GLOBAL(logger_, "Resumption")

ResumptionDataProcessor::ResumptionDataProcessor(
    app_mngr::ApplicationManager& application_manager)
    : event_engine::EventObserver(application_manager.event_dispatcher())
    , application_manager_(application_manager) {}

ResumptionDataProcessor::~ResumptionDataProcessor() {}

void ResumptionDataProcessor::Restore(ApplicationSharedPtr application,
                                      smart_objects::SmartObject& saved_app,
                                      ResumeCtrl::ResumptionCallBack callback) {
  LOG4CXX_AUTO_TRACE(logger_);
  AddFiles(application, saved_app);
  AddSubmenues(application, saved_app);
  AddCommands(application, saved_app);
  AddChoicesets(application, saved_app);
  SetGlobalProperties(application, saved_app);
  AddSubscriptions(application, saved_app);
  AddWayPointsSubscription(application, saved_app);
  register_callbacks_[application->app_id()] = callback;
}

bool ResumptionRequestIDs::operator<(const ResumptionRequestIDs& other) const {
  LOG4CXX_AUTO_TRACE(logger_);
  return correlation_id < other.correlation_id ||
         function_id < other.function_id;
}

void ResumptionDataProcessor::on_event(const event_engine::Event& event) {
  LOG4CXX_AUTO_TRACE(logger_);
  const smart_objects::SmartObject& response = event.smart_object();

  ResumptionRequestIDs request_ids;
  request_ids.function_id = event.id();
  request_ids.correlation_id = event.smart_object_correlation_id();
  //TODO i suppose it can be optimised with moving app id into
  //ResumptionRequest struct, so that we don't have to perform
  //basically the same search twice
  auto predicate =
      [&event](const std::pair<ResumptionRequestIDs, std::uint32_t>& item) {
        return item.first.function_id == event.id() &&
               item.first.correlation_id == event.smart_object_correlation_id();
      };

  auto app_id_ptr =
      std::find_if(request_app_ids_.begin(),
                   request_app_ids_.end(),
                   predicate);

  if (app_id_ptr == request_app_ids_.end()) {
    LOG4CXX_ERROR(logger_,
                  "application id for correlation id "
                      << event.smart_object_correlation_id()
                      << " and function id: " << event.id()
                      << " was not found.");
    return;
  }

  const uint32_t app_id = app_id_ptr->second;

  LOG4CXX_DEBUG(logger_, "app_id is: " << app_id);

  LOG4CXX_DEBUG(logger_,
                "Found function id: " << app_id_ptr->first.function_id
                                      << " correlation id: "
                                      << app_id_ptr->first.correlation_id);

  LOG4CXX_DEBUG(logger_,
                "Now processing event with function id: "
                    << request_ids.function_id
                    << " correlation id: " << request_ids.correlation_id);

  ApplicationResumptionStatus& status = resumption_status_[app_id];
  std::vector<ResumptionRequest>& list_of_sent_requests =
      status.list_of_sent_requests;

  auto request_ptr =
      std::find_if(list_of_sent_requests.begin(),
                   list_of_sent_requests.end(),
                   [&event](const ResumptionRequest& request) {
                     return request.request_ids.correlation_id ==
                                event.smart_object_correlation_id() &&
                            request.request_ids.function_id == event.id();
                   });

  if (list_of_sent_requests.end() == request_ptr) {
    LOG4CXX_ERROR(logger_, "Request not found");
    return;
  }

  const hmi_apis::Common_Result::eType result_code =
      static_cast<hmi_apis::Common_Result::eType>(
      response[strings::params][application_manager::hmi_response::code]
              .asInt());

  if (result_code == hmi_apis::Common_Result::SUCCESS) {
    status.successful_requests.push_back(*request_ptr);
  } else {
    status.error_requests.push_back(*request_ptr);
  }
  list_of_sent_requests.erase(request_ptr);

  if (!list_of_sent_requests.empty()) {
    LOG4CXX_DEBUG(logger_, "is not the last response for this application");
    return;
  }

  auto it = register_callbacks_.find(app_id);
  DCHECK_OR_RETURN_VOID(it != register_callbacks_.end());
  auto callback = it->second;
  if (status.error_requests.empty()) {
    LOG4CXX_DEBUG(logger_, "Resumption for app " << app_id << "successful");
    callback(mobile_apis::Result::SUCCESS, "Data resumption succesful");
  }
  if (!status.error_requests.empty()) {
    LOG4CXX_ERROR(logger_, "Resumption for app " << app_id << "failed");
    RevertRestoredData(app_id);
    callback(mobile_apis::Result::RESUME_FAILED, "Data resumption failed");
  }
  resumption_status_.erase(app_id);
  request_app_ids_.erase(app_id_ptr);
}

void ResumptionDataProcessor::RevertRestoredData(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  DeleteFiles(app_id);
  DeleteSubmenues(app_id);
  DeleteCommands(app_id);
  DeleteChoicesets(app_id);
  DeleteGlobalProperties(app_id);
  DeleteSubscriptions(app_id);
  DeleteWayPointsSubscription(app_id);
}

void ResumptionDataProcessor::WaitForResponse(
    const int32_t app_id, const ResumptionRequest& request) {
  LOG4CXX_AUTO_TRACE(logger_);
  LOG4CXX_DEBUG(logger_,
                "app " << app_id << "Subscribe on "
                       << request.request_ids.function_id << " "
                       << request.request_ids.correlation_id);
  subscribe_on_event(request.request_ids.function_id,
                     request.request_ids.correlation_id);
  resumption_status_[app_id].list_of_sent_requests.push_back(request);
  request_app_ids_.insert(std::make_pair(request.request_ids, app_id));
}

void ResumptionDataProcessor::ProcessHMIRequest(
    smart_objects::SmartObjectSPtr request, bool subscribe_on_response) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (subscribe_on_response) {
    auto function_id = static_cast<hmi_apis::FunctionID::eType>(
        (*request)[strings::params][strings::function_id].asInt());

    const int32_t hmi_correlation_id =
        (*request)[strings::params][strings::correlation_id].asInt();

    const int32_t app_id =
        (*request)[strings::msg_params][strings::app_id].asInt();

    ResumptionRequest wait_for_response;
    wait_for_response.request_ids.correlation_id = hmi_correlation_id;
    wait_for_response.request_ids.function_id = function_id;
    wait_for_response.message = *request;

    WaitForResponse(app_id, wait_for_response);
  }
  if (!application_manager_.GetRPCService().ManageHMICommand(request)) {
    LOG4CXX_ERROR(logger_, "Unable to send request");
  }
}

void ResumptionDataProcessor::ProcessHMIRequests(
    const smart_objects::SmartObjectList& requests) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (requests.empty()) {
    LOG4CXX_DEBUG(logger_, "requests list is empty");
    return;
  }

  for (const auto& it : requests) {
    ProcessHMIRequest(it, /*subscribe_on_response = */ true);
  }
}

void ResumptionDataProcessor::AddFiles(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!saved_app.keyExists(strings::application_files)) {
    LOG4CXX_ERROR(logger_, "application_files section is not exists");
    return;
  }

  const smart_objects::SmartObject& application_files =
      saved_app[strings::application_files];

  for (size_t i = 0; i < application_files.length(); ++i) {
    const smart_objects::SmartObject& file_data = application_files[i];
    const bool is_persistent = file_data.keyExists(strings::persistent_file) &&
                               file_data[strings::persistent_file].asBool();
    if (is_persistent) {
      AppFile file;
      file.is_persistent = is_persistent;
      file.is_download_complete =
          file_data[strings::is_download_complete].asBool();
      file.file_name = file_data[strings::sync_file_name].asString();
      file.file_type = static_cast<mobile_apis::FileType::eType>(
          file_data[strings::file_type].asInt());
      application->AddFile(file);
    }
  }
}

void ResumptionDataProcessor::DeleteFiles(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  ApplicationSharedPtr application = application_manager_.application(app_id);
  while (!application->getAppFiles().empty()) {
    application->DeleteFile(application->getAppFiles().begin()->first);
  }
}

void ResumptionDataProcessor::AddSubmenues(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (!saved_app.keyExists(strings::application_submenus)) {
    LOG4CXX_ERROR(logger_, "application_submenus section is not exists");
    return;
  }

  const smart_objects::SmartObject& app_submenus =
      saved_app[strings::application_submenus];

  for (size_t i = 0; i < app_submenus.length(); ++i) {
    const smart_objects::SmartObject& submenu = app_submenus[i];
    application->AddSubMenu(submenu[strings::menu_id].asUInt(), submenu);
  }

  ProcessHMIRequests(MessageHelper::CreateAddSubMenuRequestsToHMI(
      application, application_manager_.GetNextHMICorrelationID()));
}

void ResumptionDataProcessor::DeleteSubmenues(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  ApplicationResumptionStatus& status = resumption_status_[app_id];
  ApplicationSharedPtr application = application_manager_.application(app_id);
  for (auto request : status.successful_requests) {
    if (hmi_apis::FunctionID::UI_AddSubMenu ==
        request.request_ids.function_id) {
      smart_objects::SmartObjectSPtr ui_sub_menu =
          MessageHelper::CreateMessageForHMI(
              hmi_apis::messageType::request,
              application_manager_.GetNextHMICorrelationID());

      (*ui_sub_menu)[strings::params][strings::function_id] =
          hmi_apis::FunctionID::UI_DeleteSubMenu;

      smart_objects::SmartObject msg_params =
          smart_objects::SmartObject(smart_objects::SmartType_Map);

      msg_params[strings::menu_id] =
          request.message[strings::msg_params][strings::menu_id];
      msg_params[strings::app_id] =
          request.message[strings::msg_params][strings::app_id];

      (*ui_sub_menu)[strings::msg_params] = msg_params;

      application->RemoveSubMenu(msg_params[strings::menu_id].asInt());
      ProcessHMIRequest(ui_sub_menu, /*subscribe_on_response = */ false);
    }
  }
}

void ResumptionDataProcessor::AddCommands(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!saved_app.keyExists(strings::application_commands)) {
    LOG4CXX_ERROR(logger_, "application_commands section is not exists");
  }

  const smart_objects::SmartObject& app_commands =
      saved_app[strings::application_commands];

  for (size_t i = 0; i < app_commands.length(); ++i) {
    const smart_objects::SmartObject& command = app_commands[i];
    const uint32_t cmd_id = command[strings::cmd_id].asUInt();

    application->AddCommand(cmd_id, command);
    application->help_prompt_manager().OnVrCommandAdded(
        cmd_id, command, /*is_resumption =*/true);
  }

  ProcessHMIRequests(MessageHelper::CreateAddCommandRequestToHMI(
      application, application_manager_));
}

void ResumptionDataProcessor::DeleteCommands(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  ApplicationSharedPtr application = application_manager_.application(app_id);
  ApplicationResumptionStatus& status = resumption_status_[app_id];

  for (auto request : status.successful_requests) {
    const uint32_t cmd_id =
        request.message[strings::msg_params][strings::cmd_id].asUInt();

    if (hmi_apis::FunctionID::UI_AddCommand ==
        request.request_ids.function_id) {
      DeleteUICommands(request);
    }
    if (hmi_apis::FunctionID::VR_AddCommand ==
        request.request_ids.function_id) {
      DeleteVRCommands(request);
    }

    application->RemoveCommand(cmd_id);
    application->help_prompt_manager().OnVrCommandDeleted(
        cmd_id, /*is_resumption = */ true);
  }
}

void ResumptionDataProcessor::DeleteUICommands(
    const ResumptionRequest& request) {
  LOG4CXX_AUTO_TRACE(logger_);
  smart_objects::SmartObjectSPtr ui_command =
      MessageHelper::CreateMessageForHMI(
          hmi_apis::messageType::request,
          application_manager_.GetNextHMICorrelationID());

  (*ui_command)[strings::params][strings::function_id] =
      hmi_apis::FunctionID::UI_DeleteCommand;

  smart_objects::SmartObject msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);

  const uint32_t cmd_id =
      request.message[strings::msg_params][strings::cmd_id].asUInt();

  msg_params[strings::cmd_id] = cmd_id;
  msg_params[strings::app_id] =
      request.message[strings::msg_params][strings::app_id];

  (*ui_command)[strings::msg_params] = msg_params;

  ProcessHMIRequest(ui_command, /*subscribe_on_response = */ false);
}

void ResumptionDataProcessor::DeleteVRCommands(
    const ResumptionRequest& request) {
  LOG4CXX_AUTO_TRACE(logger_);
  smart_objects::SmartObjectSPtr vr_command =
      MessageHelper::CreateMessageForHMI(
          hmi_apis::messageType::request,
          application_manager_.GetNextHMICorrelationID());

  (*vr_command)[strings::params][strings::function_id] =
      hmi_apis::FunctionID::VR_DeleteCommand;

  smart_objects::SmartObject msg_params =
      smart_objects::SmartObject(smart_objects::SmartType_Map);

  const uint32_t cmd_id =
      request.message[strings::msg_params][strings::cmd_id].asUInt();

  msg_params[strings::cmd_id] = cmd_id;
  msg_params[strings::app_id] =
      request.message[strings::msg_params][strings::app_id];
  msg_params[strings::type] =
      request.message[strings::msg_params][strings::type];
  msg_params[strings::grammar_id] =
      request.message[strings::msg_params][strings::grammar_id];

  (*vr_command)[strings::msg_params] = msg_params;

  ProcessHMIRequest(vr_command, /*subscribe_on_response = */ false);
}

void ResumptionDataProcessor::AddChoicesets(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!saved_app.keyExists(strings::application_choice_sets)) {
    LOG4CXX_ERROR(logger_, "There is no any choicesets");
  }

  const smart_objects::SmartObject& app_choice_sets =
      saved_app[strings::application_choice_sets];

  for (size_t i = 0; i < app_choice_sets.length(); ++i) {
    const smart_objects::SmartObject& choice_set = app_choice_sets[i];
    const int32_t choice_set_id =
        choice_set[strings::interaction_choice_set_id].asInt();
    application->AddChoiceSet(choice_set_id, choice_set);
  }

  ProcessHMIRequests(MessageHelper::CreateAddVRCommandRequestFromChoiceToHMI(
      application, application_manager_));
}

void ResumptionDataProcessor::DeleteChoicesets(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  ApplicationSharedPtr application = application_manager_.application(app_id);

  const DataAccessor<ChoiceSetMap> accessor = application->choice_set_map();
  const ChoiceSetMap& choices = accessor.GetData();
  while (!choices.empty()) {
    application->RemoveChoiceSet(choices.begin()->first);
  }
}

void ResumptionDataProcessor::SetGlobalProperties(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (!saved_app.keyExists(strings::application_global_properties)) {
    LOG4CXX_DEBUG(logger_,
                  "application_global_properties section is not exists");
    return;
  }

  const smart_objects::SmartObject& properties_so =
      saved_app[strings::application_global_properties];
  application->load_global_properties(properties_so);

  ProcessHMIRequests(MessageHelper::CreateGlobalPropertiesRequestsToHMI(
      application, application_manager_));
}

void ResumptionDataProcessor::DeleteGlobalProperties(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);

  const auto application = application_manager_.application(app_id);

  const auto result =
      application_manager_.ResetAllApplicationGlobalProperties(app_id);

  if (result.HasUIPropertiesReset()) {
    smart_objects::SmartObjectSPtr ui_gl_props_reset_req =
        MessageHelper::CreateUIResetGlobalPropertiesRequest(result,
                                                            application);

    ProcessHMIRequest(ui_gl_props_reset_req, false);
  }
  if (result.HasTTSPropertiesReset()) {
    smart_objects::SmartObjectSPtr tts_gl_props_reset_req =
        MessageHelper::CreateUIResetGlobalPropertiesRequest(result,
                                                            application);

    ProcessHMIRequest(tts_gl_props_reset_req, false);
  }
}

void ResumptionDataProcessor::AddWayPointsSubscription(
    app_mngr::ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (saved_app.keyExists(strings::subscribed_for_way_points)) {
    LOG4CXX_DEBUG(logger_, "subscribed_for_way_points section is not exists");
    return;
  }

  const smart_objects::SmartObject& subscribed_for_way_points_so =
      saved_app[strings::subscribed_for_way_points];
  if (true == subscribed_for_way_points_so.asBool()) {
    application_manager_.SubscribeAppForWayPoints(application);
    auto subscribe_waypoints_msg =
        MessageHelper::CreateSubscribeWayPointsMessageToHMI(
            application_manager_.GetNextHMICorrelationID());
    application_manager_.GetRPCService().ManageHMICommand(
        subscribe_waypoints_msg);
  }
}

void ResumptionDataProcessor::DeleteWayPointsSubscription(
    const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  if (application_manager_.IsAppSubscribedForWayPoints(app_id)) {
    application_manager_.UnsubscribeAppFromWayPoints(app_id);
  }
}

void ResumptionDataProcessor::AddSubscriptions(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);

  AddButtonsSubscriptions(application, saved_app);
  AddPluginsSubscriptions(application, saved_app);
}

void ResumptionDataProcessor::AddButtonsSubscriptions(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);

  if (!saved_app.keyExists(strings::application_subscriptions)) {
    LOG4CXX_DEBUG(logger_, "application_subscriptions section is not exists");
    return;
  }

  const smart_objects::SmartObject& subscriptions =
      saved_app[strings::application_subscriptions];

  if (subscriptions.keyExists(strings::application_buttons)) {
    const smart_objects::SmartObject& subscriptions_buttons =
        subscriptions[strings::application_buttons];
    mobile_apis::ButtonName::eType btn;
    for (size_t i = 0; i < subscriptions_buttons.length(); ++i) {
      btn = static_cast<mobile_apis::ButtonName::eType>(
          (subscriptions_buttons[i]).asInt());
      application->SubscribeToButton(btn);
    }
  }

  MessageHelper::SendAllOnButtonSubscriptionNotificationsForApp(
      application, application_manager_);
}

void ResumptionDataProcessor::AddPluginsSubscriptions(
    ApplicationSharedPtr application,
    const smart_objects::SmartObject& saved_app) {
  LOG4CXX_AUTO_TRACE(logger_);

  for (auto& extension : application->Extensions()) {
    extension->ProcessResumption(
        saved_app,
        [this](const int32_t app_id, const ResumptionRequest request) {
          this->WaitForResponse(app_id, request);
        });
  }
}

void ResumptionDataProcessor::DeleteSubscriptions(const int32_t app_id) {
  LOG4CXX_AUTO_TRACE(logger_);
  ApplicationSharedPtr application = application_manager_.application(app_id);
  DeleteButtonsSubscriptions(application);
  DeletePluginsSubscriptions(application);
}

void ResumptionDataProcessor::DeleteButtonsSubscriptions(
    ApplicationSharedPtr application) {
  LOG4CXX_AUTO_TRACE(logger_);
  DataAccessor<ButtonSubscriptions> button_accessor =
      application->SubscribedButtons();
  ButtonSubscriptions button_subscriptions = button_accessor.GetData();
  while (!button_subscriptions.empty()) {
    auto btn = button_subscriptions.begin();
    MessageHelper::SendOnButtonSubscriptionNotification(
        application->hmi_app_id(),
        static_cast<hmi_apis::Common_ButtonName::eType>(*btn),
        /*is_subscribed = */ false,
        application_manager_);
    application->UnsubscribeFromButton(
        static_cast<mobile_apis::ButtonName::eType>(*btn));
  }

  MessageHelper::SendOnButtonSubscriptionNotification(
      application->hmi_app_id(),
      hmi_apis::Common_ButtonName::CUSTOM_BUTTON,
      /*is_subscribed = */ false,
      application_manager_);
  application->SubscribeToButton(mobile_apis::ButtonName::CUSTOM_BUTTON);
}

void ResumptionDataProcessor::DeletePluginsSubscriptions(
    application_manager::ApplicationSharedPtr application) {
  LOG4CXX_AUTO_TRACE(logger_);
  smart_objects::SmartObject extension_subscriptions;

  ApplicationResumptionStatus& status =
      resumption_status_[application->app_id()];
  for (auto request : status.successful_requests) {
    if (hmi_apis::FunctionID::VehicleInfo_SubscribeVehicleData ==
        request.request_ids.function_id) {
      extension_subscriptions[strings::application_vehicle_info] =
          request.message;
    }
  }

  for (auto& extension : application->Extensions()) {
    extension->RevertResumption(extension_subscriptions);
  }
}

}  // namespce resumption