summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/app_launch/app_launch_data_json.cc
blob: 86d585f2156ad6b8267f640dbd2d3863ab7237c3 (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
/*
 * Copyright (c) 2017, 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_launch/app_launch_data_json.h"
#include <algorithm>
#include "application_manager/smart_object_keys.h"
#include "json/json.h"
#include "smart_objects/smart_object.h"
#include "utils/date_time.h"

namespace app_launch {

CREATE_LOGGERPTR_GLOBAL(logger_, "AppLaunch")

AppLaunchDataJson::AppLaunchDataJson(
    const AppLaunchSettings& settings,
    resumption::LastStateWrapperPtr last_state_wrapper)
    : AppLaunchDataImpl(settings), last_state_wrapper_(last_state_wrapper) {}

AppLaunchDataJson::~AppLaunchDataJson() {}

Json::Value& AppLaunchDataJson::GetSavedApplicationDataList(
    Json::Value& dictionary) const {
  using namespace application_manager;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);
  Json::Value& app_launch = GetApplicationData(dictionary);
  if (!app_launch.isMember(strings::app_launch_list)) {
    app_launch[strings::app_launch_list] = Json::Value(Json::arrayValue);
    LOG4CXX_WARN(logger_, "app_list section is missed");
  }
  Json::Value& app_launch_list = app_launch[strings::app_launch_list];
  if (!app_launch_list.isArray()) {
    LOG4CXX_ERROR(logger_, "app_launch_list type INVALID rewrite");
    app_launch_list = Json::Value(Json::arrayValue);
  }
  return app_launch_list;
}

Json::Value& AppLaunchDataJson::GetApplicationData(
    Json::Value& dictionary) const {
  using namespace application_manager;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);
  if (!dictionary.isMember(strings::app_launch)) {
    dictionary[strings::app_launch] = Json::Value(Json::objectValue);
    LOG4CXX_WARN(logger_, "app_launch section is missed");
  }
  Json::Value& app_launch = dictionary[strings::app_launch];
  if (!app_launch.isObject()) {
    LOG4CXX_ERROR(logger_, "resumption type INVALID rewrite");
    app_launch = Json::Value(Json::objectValue);
  }
  return app_launch;
}

Json::Value& AppLaunchDataJson::GetApplicationListAndIndex(
    const ApplicationData& app_data,
    int32_t& found_index,
    Json::Value& dictionary) const {
  using namespace application_manager;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);

  Json::Value& apps_list = GetSavedApplicationDataList(dictionary);
  const Json::ArrayIndex size = apps_list.size();

  for (Json::ArrayIndex idx = 0; idx != size; ++idx) {
    if (apps_list[idx].isMember(strings::device_id) &&
        apps_list[idx].isMember(strings::bundle_id) &&
        apps_list[idx].isMember(strings::app_id) &&
        apps_list[idx].isMember(strings::app_launch_last_session)) {
      const std::string deviceID =
          apps_list[idx][strings::device_id].asString();
      const std::string bundleID =
          apps_list[idx][strings::bundle_id].asString();
      const std::string appID = apps_list[idx][strings::app_id].asString();

      if (deviceID == app_data.device_mac_ && bundleID == app_data.bundle_id_ &&
          appID == app_data.mobile_app_id_) {
        found_index = idx;
      }
    }
  }

  return apps_list;
}

bool AppLaunchDataJson::IsAppDataAlreadyExisted(
    const ApplicationData& app_data) const {
  LOG4CXX_AUTO_TRACE(logger_);

  int32_t index = NotFound;

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  GetApplicationListAndIndex(app_data, index, dictionary);
  accessor.GetMutableData().set_dictionary(dictionary);
  return index == NotFound ? false : true;
}

bool AppLaunchDataJson::RefreshAppSessionTime(const ApplicationData& app_data) {
  using namespace application_manager;
  using namespace date_time;
  LOG4CXX_AUTO_TRACE(logger_);
  bool retVal = false;

  int32_t index = NotFound;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& json_data_list =
      GetApplicationListAndIndex(app_data, index, dictionary);
  if (index != NotFound) {
    if (json_data_list.empty() == false) {
      json_data_list[index][strings::app_launch_last_session] =
          static_cast<Json::Value::UInt64>(getSecs(getCurrentTime()));
      retVal = true;
    }
  }
  accessor.GetMutableData().set_dictionary(dictionary);
  return retVal;
}

bool AppLaunchDataJson::AddNewAppData(const ApplicationData& app_data) {
  using namespace application_manager;
  using namespace date_time;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);

  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& json_app_data =
      GetSavedApplicationDataList(dictionary).append(Json::Value());
  json_app_data[strings::device_id] = app_data.device_mac_;
  json_app_data[strings::app_id] = app_data.mobile_app_id_;
  json_app_data[strings::bundle_id] = app_data.bundle_id_;
  json_app_data[strings::app_launch_last_session] =
      static_cast<Json::Value::UInt64>(getSecs(getCurrentTime()));
  accessor.GetMutableData().set_dictionary(dictionary);

  LOG4CXX_DEBUG(logger_,
                "New application data saved. Detatils device_id: "
                    << app_data.device_mac_
                    << ", app_id: " << app_data.mobile_app_id_
                    << ", bundle_id: " << app_data.bundle_id_ << ".");
  return true;
}

std::vector<ApplicationDataPtr> AppLaunchDataJson::GetAppDataByDevMac(
    const std::string& dev_mac) const {
  using namespace application_manager;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);
  std::vector<ApplicationDataPtr> dev_apps;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const Json::Value& apps_list = GetSavedApplicationDataList(dictionary);
  const Json::ArrayIndex size = apps_list.size();

  for (Json::ArrayIndex idx = 0; idx != size; ++idx) {
    if (apps_list[idx].isMember(strings::device_id) &&
        apps_list[idx].isMember(strings::bundle_id) &&
        apps_list[idx].isMember(strings::app_id) &&
        apps_list[idx].isMember(strings::app_launch_last_session)) {
      const std::string deviceMac =
          apps_list[idx][strings::device_id].asString();
      const std::string bundleID =
          apps_list[idx][strings::bundle_id].asString();
      const std::string appID = apps_list[idx][strings::app_id].asString();

      if (deviceMac == dev_mac) {
        dev_apps.push_back(
            std::make_shared<ApplicationData>(appID, bundleID, deviceMac));
      }
    }
  }
  accessor.GetMutableData().set_dictionary(dictionary);
  return dev_apps;
}

bool AppLaunchDataJson::Clear() {
  LOG4CXX_AUTO_TRACE(logger_);
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  GetSavedApplicationDataList(dictionary).clear();
  accessor.GetMutableData().set_dictionary(dictionary);
  LOG4CXX_DEBUG(logger_,
                "Application launch JSON section successfully cleared.");

  return true;
}

uint32_t AppLaunchDataJson::GetCurentNumberOfAppData() const {
  LOG4CXX_AUTO_TRACE(logger_);
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  const uint32_t list_size = GetSavedApplicationDataList(dictionary).size();
  accessor.GetMutableData().set_dictionary(dictionary);
  LOG4CXX_DEBUG(logger_,
                "Successfully was gotten app_launch list. Size: " << list_size);

  return list_size;
}

bool AppLaunchDataJson::DeleteOldestAppData() {
  using namespace application_manager;
  LOG4CXX_AUTO_TRACE(logger_);
  sync_primitives::AutoLock autolock(app_launch_json_lock_);
  std::vector<uint64_t> temp_array;
  std::vector<Json::Value> temp_json_list;
  resumption::LastStateAccessor accessor = last_state_wrapper_->get_accessor();
  Json::Value dictionary = accessor.GetData().dictionary();
  Json::Value& apps_list = GetSavedApplicationDataList(dictionary);
  const Json::ArrayIndex size = apps_list.size();

  // Search oldest record in Json
  // for it collect all timestaps in vector
  for (Json::ArrayIndex idx = 0; idx != size; ++idx) {
    if (apps_list[idx].isMember(strings::device_id) &&
        apps_list[idx].isMember(strings::bundle_id) &&
        apps_list[idx].isMember(strings::app_id) &&
        apps_list[idx].isMember(strings::app_launch_last_session)) {
      temp_array.push_back(
          apps_list[idx][strings::app_launch_last_session].asUInt64());
    }
  }

  // Calc oldest one and found index of it in Json
  const int32_t oldest_index =
      (std::min_element(temp_array.begin(), temp_array.end()) -
       temp_array.begin());

  // Copy in temporary vector Json list without oldest record
  int32_t i = 0;
  for (auto it = apps_list.begin(); it != apps_list.end(); ++it, i++) {
    if ((*it).isMember(strings::device_id) &&
        (*it).isMember(strings::bundle_id) && (*it).isMember(strings::app_id) &&
        (*it).isMember(strings::app_launch_last_session)) {
      if (i == oldest_index) {
        continue;
      }
      temp_json_list.push_back((*it));
    }
  }

  // Clear Json list
  GetSavedApplicationDataList(dictionary).clear();

  // Copy to Json new list without oldest one
  for (const auto& item : temp_json_list) {
    GetSavedApplicationDataList(dictionary).append(item);
  }
  accessor.GetMutableData().set_dictionary(dictionary);
  LOG4CXX_DEBUG(
      logger_, "Oldest application launch data had been successfully deleted.");

  return true;
}

bool AppLaunchDataJson::Persist() {
  LOG4CXX_AUTO_TRACE(logger_);
  last_state_wrapper_->get_accessor().GetMutableData().SaveToFileSystem();
  return true;
}

}  // namespace app_launch