Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cloud.cpp « map - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c177bd48c889822eb88d7acee3062b64652e139 (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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
#include "map/cloud.hpp"

#include "coding/file_reader.hpp"
#include "coding/file_writer.hpp"
#include "coding/internal/file_data.hpp"
#include "coding/sha1.hpp"

#include "platform/http_client.hpp"
#include "platform/http_payload.hpp"
#include "platform/http_uploader.hpp"
#include "platform/network_policy.hpp"
#include "platform/platform.hpp"
#include "platform/preferred_languages.hpp"
#include "platform/remote_file.hpp"
#include "platform/settings.hpp"

#include "base/assert.hpp"
#include "base/file_name_utils.hpp"
#include "base/logging.hpp"
#include "base/stl_helpers.hpp"

#include <algorithm>
#include <chrono>
#include <sstream>

#include "3party/Alohalytics/src/alohalytics.h"

#include "private.h"

using namespace std::chrono;

namespace
{
uint32_t constexpr kTaskTimeoutInSeconds = 1;
uint64_t constexpr kUpdateTimeoutInHours = 24;
uint32_t constexpr kRetryMaxAttempts = 3;
uint32_t constexpr kRetryTimeoutInSeconds = 5;
uint32_t constexpr kRetryDegradationFactor = 2;

uint64_t constexpr kMaxWwanUploadingSizeInBytes = 10 * 1024; // 10Kb
uint64_t constexpr kMaxUploadingFileSizeInBytes = 100 * 1024 * 1024; // 100Mb

uint32_t constexpr kRequestTimeoutInSec = 5;

std::string const kServerUrl = CLOUD_URL;
std::string const kServerVersion = "v1";
std::string const kServerCreateSnapshotMethod = "snapshot/create";
std::string const kServerFinishSnapshotMethod = "snapshot/finish";
std::string const kServerBestSnapshotMethod = "snapshot/best";
std::string const kServerUploadMethod = "file/upload/start";
std::string const kServerNotifyMethod = "file/upload/finish";
std::string const kServerDownloadMethod = "file/download";
std::string const kApplicationJson = "application/json";
std::string const kSnapshotFile = "snapshot.json";

std::string GetIndexFilePath(std::string const & indexName)
{
  return base::JoinPath(GetPlatform().SettingsDir(), indexName);
}

std::string GetRestoringFolder(std::string const & serverPathName)
{
  return base::JoinPath(GetPlatform().TmpDir(), serverPathName + "_restore");
}

std::string BuildMethodUrl(std::string const & serverPathName, std::string const & methodName)
{
  if (kServerUrl.empty())
    return {};

  std::ostringstream ss;
  ss << kServerUrl << "/"
     << kServerVersion << "/"
     << serverPathName << "/"
     << methodName << "/";
  return ss.str();
}

std::string BuildAuthenticationToken(std::string const & accessToken)
{
  return "Bearer " + accessToken;
}

std::string ExtractFileNameWithoutExtension(std::string const & filePath)
{
  std::string path = filePath;
  base::GetNameFromFullPath(path);
  base::GetNameWithoutExt(path);
  return path;
}

template<typename DataType>
std::string SerializeToJson(DataType const & data)
{
  std::string jsonStr;
  using Sink = MemWriter<std::string>;
  Sink sink(jsonStr);
  coding::SerializerJson<Sink> serializer(sink);
  serializer(data);
  return jsonStr;
}

template<typename DataType>
void DeserializeFromJson(std::string const & jsonStr, DataType & result)
{
  coding::DeserializerJson des(jsonStr);
  des(result);
}

bool IsSuccessfulResultCode(int resultCode)
{
  return resultCode >= 200 && resultCode < 300;
}

std::string GetDeviceName()
{
  return GetPlatform().DeviceName() + " (" + GetPlatform().DeviceModel() + ")";
}

bool CanUpload(uint64_t totalUploadingSize)
{
  auto const status = GetPlatform().ConnectionStatus();
  switch (status)
  {
  case Platform::EConnectionType::CONNECTION_NONE: return false;
  case Platform::EConnectionType::CONNECTION_WIFI: return true;
  case Platform::EConnectionType::CONNECTION_WWAN:
    return platform::GetCurrentNetworkPolicy().CanUse() &&
           totalUploadingSize <= kMaxWwanUploadingSizeInBytes;
  }
  UNREACHABLE();
}

struct SnapshotCreateRequestData
{
  std::string m_deviceId;
  std::string m_deviceName;
  std::vector<std::string> m_fileNames;

  explicit SnapshotCreateRequestData(std::vector<std::string> const & files = {})
    : m_deviceId(GetPlatform().UniqueClientId()), m_deviceName(GetDeviceName()), m_fileNames(files)
  {}

  DECLARE_VISITOR(visitor(m_deviceId, "device_id"), visitor(m_deviceName, "device_name"),
                  visitor(m_fileNames, "file_names"))
};

struct SnapshotRequestData
{
  std::string m_deviceId;

  SnapshotRequestData() : m_deviceId(GetPlatform().UniqueClientId()) {}

  DECLARE_VISITOR(visitor(m_deviceId, "device_id"))
};

struct UploadingRequestData
{
  std::string m_deviceId;
  std::string m_fileName;
  std::string m_locale;

  explicit UploadingRequestData(std::string const & filePath = {})
    : m_deviceId(GetPlatform().UniqueClientId())
    , m_fileName(ExtractFileNameWithoutExtension(filePath))
    , m_locale(languages::GetCurrentOrig())
  {}

  DECLARE_VISITOR(visitor(m_deviceId, "device_id"), visitor(m_fileName, "file_name"),
                  visitor(m_locale, "locale"))
};

struct NotifyRequestData
{
  std::string m_deviceId;
  std::string m_fileName;
  uint64_t m_fileSize = 0;

  NotifyRequestData(std::string const & filePath, uint64_t fileSize)
    : m_deviceId(GetPlatform().UniqueClientId())
    , m_fileName(ExtractFileNameWithoutExtension(filePath))
    , m_fileSize(fileSize)
  {}

  DECLARE_VISITOR(visitor(m_deviceId, "device_id"), visitor(m_fileName, "file_name"),
                  visitor(m_fileSize, "file_size"))
};

struct DownloadingRequestData
{
  std::string m_deviceId;
  std::string m_fileName;
  uint64_t m_datetime = 0;

  DownloadingRequestData(std::string const & deviceId, std::string const & fileName,
                         uint64_t datetime)
    : m_deviceId(deviceId), m_fileName(fileName), m_datetime(datetime)
  {}

  DECLARE_VISITOR(visitor(m_deviceId, "device_id"), visitor(m_fileName, "file_name"),
                  visitor(m_datetime, "datetime"))
};

struct DownloadingResponseData
{
  std::string m_url;
  std::string m_fallbackUrl;

  DECLARE_VISITOR(visitor(m_url, "url"), visitor(m_fallbackUrl, "fallback_url"))
};

struct DownloadingResult
{
  Cloud::RequestResult m_requestResult;
  bool m_isMalformed = false;
  DownloadingResponseData m_response;
};

using ResponseHandler = std::function<Cloud::RequestResult(
  int code, std::string const & response)>;

template <typename RequestDataType, typename... RequestDataArgs>
Cloud::RequestResult CloudRequestWithResult(std::string const & url,
                                            std::string const & accessToken,
                                            ResponseHandler const & responseHandler,
                                            RequestDataArgs const & ... args)
{
  ASSERT(responseHandler != nullptr, ());

  platform::HttpClient request(url);
  request.SetTimeout(kRequestTimeoutInSec);
  request.SetRawHeader("Accept", kApplicationJson);
  request.SetRawHeader("Authorization", BuildAuthenticationToken(accessToken));
  request.SetRawHeader("User-Agent", GetPlatform().GetAppUserAgent());
  request.SetBodyData(SerializeToJson(RequestDataType(args...)), kApplicationJson);

  if (request.RunHttpRequest() && !request.WasRedirected())
  {
    int const resultCode = request.ErrorCode();
    if (resultCode == 403)
    {
      LOG(LWARNING, ("Access denied for", url));
      return {Cloud::RequestStatus::Forbidden, request.ServerResponse()};
    }

    return responseHandler(resultCode, request.ServerResponse());
  }

  return {Cloud::RequestStatus::NetworkError, request.ServerResponse()};
}

template <typename RequestDataType, typename... RequestDataArgs>
Cloud::RequestResult CloudRequest(std::string const & url, std::string const & accessToken,
                                  RequestDataArgs const &... args)
{
  auto responseHandler = [](int code, std::string const & serverResponse) -> Cloud::RequestResult {
    if (IsSuccessfulResultCode(code))
      return {Cloud::RequestStatus::Ok, {}};
    return {Cloud::RequestStatus::NetworkError, serverResponse};
  };

  return CloudRequestWithResult<RequestDataType>(url, accessToken, responseHandler, args...);
}

template <typename ResultType>
void ParseRequestJsonResult(std::string const & url, std::string const & serverResponse,
                            ResultType & result)
{
  try
  {
    DeserializeFromJson(serverResponse, result.m_response);
  }
  catch (base::Json::Exception const & exception)
  {
    LOG(LWARNING, ("Malformed server response", "url:", url, "response:", serverResponse));
    result.m_response = {};
    result.m_isMalformed = true;
  }
}

template <typename RequestDataType, typename ResultType, typename... RequestDataArgs>
ResultType CloudRequestWithJsonResult(std::string const & url, std::string const & accessToken,
                                      RequestDataArgs const &... args)
{
  ResultType result;
  auto responseHandler = [&result, &url](int code,
    std::string const & serverResponse) -> Cloud::RequestResult
  {
    if (IsSuccessfulResultCode(code))
    {
      ParseRequestJsonResult(url, serverResponse, result);
      return {Cloud::RequestStatus::Ok, {}};
    }
    return {Cloud::RequestStatus::NetworkError, serverResponse};
  };
  result.m_requestResult =
      CloudRequestWithResult<RequestDataType>(url, accessToken, responseHandler, args...);
  return result;
}

Cloud::SnapshotResponseData ReadSnapshotFile(std::string const & filename)
{
  if (!GetPlatform().IsFileExistsByFullPath(filename))
    return {};

  std::string jsonStr;
  try
  {
    FileReader r(filename);
    r.ReadAsString(jsonStr);
  }
  catch (FileReader::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while reading file:", filename,
                   "reason:", exception.what()));
    return {};
  }

  if (jsonStr.empty())
    return {};

  try
  {
    Cloud::SnapshotResponseData data;
    DeserializeFromJson(jsonStr, data);
    return data;
  }
  catch (base::Json::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while parsing file:", filename,
                   "reason:", exception.what(), "json:", jsonStr));
  }
  return {};
}

bool CheckAndGetFileSize(std::string const & filePath, uint64_t & fileSize)
{
  if (!base::GetFileSize(filePath, fileSize))
    return false;

  // We do not work with files which size is more than kMaxUploadingFileSizeInBytes.
  return fileSize <=  kMaxUploadingFileSizeInBytes;
}
}  // namespace

Cloud::Cloud(CloudParams && params)
  : m_params(std::move(params))
{
  ASSERT(!m_params.m_indexName.empty(), ());
  ASSERT(!m_params.m_serverPathName.empty(), ());
  ASSERT(!m_params.m_settingsParamName.empty(), ());
  ASSERT(!m_params.m_restoredFileExtension.empty(), ());
  ASSERT(!m_params.m_restoringFolder.empty(), ());

  m_state = GetCloudState(m_params.m_settingsParamName);
  GetPlatform().RunTask(Platform::Thread::File, [this]() { ReadIndex(); });
}

Cloud::~Cloud()
{
  std::lock_guard<std::mutex> lock(m_mutex);
  SaveIndexImpl();
}

void Cloud::SetInvalidTokenHandler(InvalidTokenHandler && onInvalidToken)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_onInvalidToken = std::move(onInvalidToken);
}

void Cloud::SetSynchronizationHandlers(SynchronizationStartedHandler && onSynchronizationStarted,
                                       SynchronizationFinishedHandler && onSynchronizationFinished,
                                       RestoreRequestedHandler && onRestoreRequested,
                                       RestoredFilesPreparedHandler && onRestoredFilesPrepared)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_onSynchronizationStarted = std::move(onSynchronizationStarted);
  m_onSynchronizationFinished = std::move(onSynchronizationFinished);
  m_onRestoreRequested = std::move(onRestoreRequested);
  m_onRestoredFilesPrepared = std::move(onRestoredFilesPrepared);
}

void Cloud::SetState(State state)
{
  std::lock_guard<std::mutex> lock(m_mutex);

  if (m_state == state)
    return;

  m_state = state;
  settings::Set(m_params.m_settingsParamName, static_cast<int>(m_state));

  switch (m_state)
  {
  case State::Enabled:
    GetPlatform().RunTask(Platform::Thread::File, [this]() { LoadIndex(); });
    break;

  case State::Disabled:
    // Delete index file and clear memory.
    base::DeleteFileX(GetIndexFilePath(m_params.m_indexName));
    m_index = Index();
    break;

  case State::Unknown: ASSERT(false, ("Unknown state can't be set up")); break;
  }
}

Cloud::State Cloud::GetState() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  return m_state;
}

void Cloud::Init(std::vector<std::string> const & filePaths)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  for (auto const & filePath : filePaths)
    m_files[ExtractFileNameWithoutExtension(filePath)] = filePath;

  if (m_state != State::Enabled)
    return;

  GetPlatform().RunTask(Platform::Thread::File, [this]() { LoadIndex(); });
}

uint64_t Cloud::GetLastSynchronizationTimestampInMs() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  if (m_state != State::Enabled)
    return 0;

  return m_index.m_lastSyncTimestamp * 1000; // in milliseconds.
}

std::unique_ptr<User::Subscriber> Cloud::GetUserSubscriber()
{
  auto s = std::make_unique<User::Subscriber>();
  s->m_onChangeToken = [this](std::string const & token)
  {
    SetAccessToken(token);
    ScheduleUploading();
  };
  return s;
}

void Cloud::LoadIndex()
{
  UpdateIndex(ReadIndex());
  ScheduleUploading();
}

bool Cloud::ReadIndex()
{
  auto const indexFilePath = GetIndexFilePath(m_params.m_indexName);
  if (!GetPlatform().IsFileExistsByFullPath(indexFilePath))
    return false;

  // Read index file.
  std::string jsonStr;
  try
  {
    FileReader r(indexFilePath);
    r.ReadAsString(jsonStr);
  }
  catch (FileReader::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while reading file:", indexFilePath,
                   "reason:", exception.what()));
    return false;
  }

  // Parse index file.
  if (jsonStr.empty())
    return false;

  try
  {
    Index index;
    DeserializeFromJson(jsonStr, index);
    std::lock_guard<std::mutex> lock(m_mutex);
    std::swap(m_index, index);
  }
  catch (base::Json::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while parsing file:", indexFilePath,
                   "reason:", exception.what(), "json:", jsonStr));
    return false;
  }
  return true;
}

void Cloud::UpdateIndex(bool indexExists)
{
  std::lock_guard<std::mutex> lock(m_mutex);

  // Now we process files ONLY if update time is out.
  auto const h = static_cast<uint64_t>(
    duration_cast<hours>(system_clock::now().time_since_epoch()).count());
  if (!indexExists || h >= m_index.m_lastUpdateInHours + kUpdateTimeoutInHours)
  {
    for (auto const & path : m_files)
      MarkModifiedImpl(path.second, true /* isOutdated */);

    // Erase disappeared files from index.
    base::EraseIf(m_index.m_entries, [this](EntryPtr const & entity) {
      return m_files.find(entity->m_name) == m_files.end();
    });

    // Index is outdated only if there is an entry.
    m_index.m_isOutdated = !m_index.m_entries.empty();
    if (m_index.m_isOutdated)
      m_index.m_lastUpdateInHours = h;

    SaveIndexImpl();
  }
  m_indexUpdated = true;
}

uint64_t Cloud::CalculateUploadingSizeImpl() const
{
  uint64_t sz = 0;
  for (auto const & entry : m_index.m_entries)
    sz += entry->m_sizeInBytes;
  return sz;
}

bool Cloud::CanUploadImpl() const
{
  return m_state == State::Enabled && m_index.CanBeUploaded() && !m_accessToken.empty() &&
         !m_uploadingStarted && m_indexUpdated && m_restoringState == RestoringState::None &&
         CanUpload(CalculateUploadingSizeImpl());
}

void Cloud::SortEntriesBeforeUploadingImpl()
{
  std::sort(m_index.m_entries.begin(), m_index.m_entries.end(),
            [](EntryPtr const & lhs, EntryPtr const & rhs)
  {
    return lhs->m_sizeInBytes < rhs->m_sizeInBytes;
  });
}

void Cloud::MarkModifiedImpl(std::string const & filePath, bool isOutdated)
{
  uint64_t fileSize = 0;
  if (!CheckAndGetFileSize(filePath, fileSize))
    return;

  auto const fileName = ExtractFileNameWithoutExtension(filePath);
  auto entryPtr = GetEntryImpl(fileName);
  if (entryPtr)
  {
    entryPtr->m_isOutdated = isOutdated;
    entryPtr->m_sizeInBytes = fileSize;
  }
  else
  {
    m_index.m_entries.emplace_back(
      std::make_shared<Entry>(fileName, fileSize, isOutdated));
  }
}

void Cloud::UpdateIndexByRestoredFilesImpl(RestoredFilesCollection const & files,
                                           uint64_t lastSyncTimestampInSec)
{
  m_index.m_isOutdated = false;
  m_index.m_lastUpdateInHours =
    static_cast<uint64_t>(duration_cast<hours>(system_clock::now().time_since_epoch()).count());
  m_index.m_lastSyncTimestamp = lastSyncTimestampInSec;
  m_index.m_entries.clear();
  for (auto const & f : files)
  {
    uint64_t fileSize = 0;
    if (!CheckAndGetFileSize(f.m_filename, fileSize))
      continue;

    m_index.m_entries.emplace_back(
      std::make_shared<Entry>(ExtractFileNameWithoutExtension(f.m_filename), fileSize,
                              false /* isOutdated */, f.m_hash));
  }
  SaveIndexImpl();
}

Cloud::EntryPtr Cloud::GetEntryImpl(std::string const & fileName) const
{
  auto it = std::find_if(m_index.m_entries.begin(), m_index.m_entries.end(),
                         [&fileName](EntryPtr ptr) { return ptr->m_name == fileName; });
  if (it != m_index.m_entries.end())
    return *it;
  return nullptr;
}

void Cloud::SaveIndexImpl() const
{
  if (m_state != State::Enabled || m_index.m_entries.empty())
    return;

  auto const indexFilePath = GetIndexFilePath(m_params.m_indexName);
  try
  {
    auto jsonData = SerializeToJson(m_index);
    FileWriter w(indexFilePath);
    w.Write(jsonData.c_str(), jsonData.length());
  }
  catch (FileWriter::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while writing file:", indexFilePath,
                   "reason:", exception.what()));
  }
}

void Cloud::ScheduleUploading()
{
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (!CanUploadImpl())
      return;

    SortEntriesBeforeUploadingImpl();

    m_snapshotFiles.clear();
    m_snapshotFiles.reserve(m_index.m_entries.size());
    for (auto const & entry : m_index.m_entries)
      m_snapshotFiles.emplace_back(entry->m_name);

    m_uploadingStarted = true;
    m_isSnapshotCreated = false;
  }

  ThreadSafeCallback<SynchronizationStartedHandler>([this]() { return m_onSynchronizationStarted; },
                                                    SynchronizationType::Backup);

  auto entry = FindOutdatedEntry();
  if (entry != nullptr)
  {
    ScheduleUploadingTask(entry, kTaskTimeoutInSeconds);
  }
  else
  {
    ThreadSafeCallback<SynchronizationFinishedHandler>([this]() { return m_onSynchronizationFinished; },
                                                       SynchronizationType::Backup,
                                                       SynchronizationResult::Success, "");
  }
}

void Cloud::ScheduleUploadingTask(EntryPtr const & entry, uint32_t timeout)
{
  GetPlatform().RunDelayedTask(Platform::Thread::Network, seconds(timeout), [this, entry]()
  {
    std::string entryName;
    std::string entryHash;
    bool isInvalidToken;
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      // Uploading has finished.
      if (!m_uploadingStarted)
        return;

      ASSERT(entry->m_isOutdated, ());
      entryName = entry->m_name;
      entryHash = entry->m_hash;
      isInvalidToken = m_accessToken.empty();
    }
    
    if (!m_params.m_backupConverter)
    {
      FinishUploading(SynchronizationResult::InvalidCall, "Backup converter is not set");
      return;
    }

    if (kServerUrl.empty())
    {
      FinishUploading(SynchronizationResult::NetworkError, "Empty server url");
      return;
    }

    // Access token may become invalid between tasks.
    if (isInvalidToken)
    {
      FinishUploading(SynchronizationResult::AuthError, "Access token is empty");
      return;
    }

    // Prepare file to uploading.
    bool needSkip;
    std::string hash;
    auto const uploadedName = PrepareFileToUploading(entryName, hash, needSkip);
    auto deleteAfterUploading = [uploadedName]() {
      if (!uploadedName.empty())
        base::DeleteFileX(uploadedName);
    };
    SCOPE_GUARD(deleteAfterUploadingGuard, deleteAfterUploading);

    // This file must be skipped by some reason.
    if (needSkip)
      return;

    if (uploadedName.empty())
    {
      FinishUploading(SynchronizationResult::DiskError, "File preparation error");
      return;
    }

    // Upload only if calculated hash is not equal to previous one.
    if (entryHash != hash && !UploadFile(uploadedName))
      return;

    // Mark entry as not outdated.
    bool isSnapshotCreated;
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      entry->m_isOutdated = false;
      entry->m_hash = hash;
      SaveIndexImpl();
      isSnapshotCreated = m_isSnapshotCreated;
    }

    // Schedule next uploading task.
    auto nextEntry = FindOutdatedEntry();
    if (nextEntry != nullptr)
    {
      ScheduleUploadingTask(nextEntry, kTaskTimeoutInSeconds);
      return;
    }

    // Finish snapshot.
    if (isSnapshotCreated)
    {
      auto const result = FinishSnapshot();
      if (!result)
      {
        FinishUploadingOnRequestError(result);
        return;
      }
    }
    FinishUploading(SynchronizationResult::Success, {});
  });
}

bool Cloud::UploadFile(std::string const & uploadedName)
{
  uint64_t uploadedFileSize = 0;
  if (!base::GetFileSize(uploadedName, uploadedFileSize))
  {
    FinishUploading(SynchronizationResult::DiskError, "File size calculation error");
    return false;
  }

  // Create snapshot if it was not created early.
  bool snapshotCreated;
  std::vector<std::string> snapshotFiles;
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    snapshotCreated = m_isSnapshotCreated;
    std::swap(snapshotFiles, m_snapshotFiles);
  }
  if (!snapshotCreated)
  {
    auto const result = CreateSnapshot(snapshotFiles);
    if (!result)
    {
      FinishUploadingOnRequestError(result);
      return false;
    }

    std::lock_guard<std::mutex> lock(m_mutex);
    m_isSnapshotCreated = true;
  }

  // Request uploading.
  auto const result = RequestUploading(uploadedName);
  if (result.m_isMalformed)
  {
    FinishUploading(SynchronizationResult::NetworkError, "Malformed uploading response");
    return false;
  }
  if (!result.m_requestResult)
  {
    FinishUploadingOnRequestError(result.m_requestResult);
    return false;
  }

  // Execute uploading.
  auto const executeResult = ExecuteUploading(result.m_response, uploadedName);
  if (!executeResult)
  {
    FinishUploadingOnRequestError(executeResult);
    return false;
  }

  // Notify about successful uploading.
  auto const notificationResult = NotifyAboutUploading(uploadedName, uploadedFileSize);
  if (!notificationResult)
  {
    FinishUploadingOnRequestError(notificationResult);
    return false;
  }

  return true;
}

void Cloud::FinishUploadingOnRequestError(Cloud::RequestResult const & result)
{
  switch (result.m_status)
  {
  case RequestStatus::Ok:
    ASSERT(false, ("Possibly incorrect call"));
    return;
  case RequestStatus::Forbidden:
    FinishUploading(SynchronizationResult::AuthError, result.m_error);
    return;
  case RequestStatus::NetworkError:
    FinishUploading(SynchronizationResult::NetworkError, result.m_error);
    return;
  }
}

std::string Cloud::PrepareFileToUploading(std::string const & fileName, std::string & hash,
                                          bool & needSkip)
{
  // 1. Get path to the original uploading file.
  std::string filePath;
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    auto const it = m_files.find(fileName);
    if (it == m_files.end())
      return {};
    filePath = it->second;
  }
  if (!GetPlatform().IsFileExistsByFullPath(filePath))
    return {};

  // 2. Calculate SHA1 of the original uploading file.
  auto const originalSha1 = coding::SHA1::CalculateBase64(filePath);
  if (originalSha1.empty())
    return {};

  // 3. Create a temporary file from the original uploading file.
  auto name = ExtractFileNameWithoutExtension(filePath);
  auto const tmpPath = base::JoinPath(GetPlatform().TmpDir(), name + ".tmp");
  if (!base::CopyFileX(filePath, tmpPath))
    return {};

  SCOPE_GUARD(tmpFileGuard, std::bind(&base::DeleteFileX, std::cref(tmpPath)));

  // 4. Calculate SHA1 of the temporary file and compare with original one.
  // Original file can be modified during copying process, so we have to
  // compare original file with the temporary file after copying.
  auto const tmpSha1 = coding::SHA1::CalculateBase64(tmpPath);
  if (originalSha1 != tmpSha1)
    return {};

  auto const outputPath = base::JoinPath(GetPlatform().TmpDir(), name + ".uploaded");

  // 5. Convert temporary file and save to output path.
  CHECK(m_params.m_backupConverter, ());
  auto const convertionResult = m_params.m_backupConverter(tmpPath, outputPath);
  needSkip = convertionResult.m_needSkip;
  hash = convertionResult.m_hash;
  if (convertionResult.m_isSuccessful)
    return outputPath;
  
  return {};
}

Cloud::RequestResult Cloud::CreateSnapshot(std::vector<std::string> const & files) const
{
  ASSERT(!files.empty(), ());
  auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerCreateSnapshotMethod);
  return CloudRequest<SnapshotCreateRequestData>(url, GetAccessToken(), files);
}

Cloud::RequestResult Cloud::FinishSnapshot() const
{
  auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerFinishSnapshotMethod);
  return CloudRequest<SnapshotRequestData>(url, GetAccessToken());
}

Cloud::SnapshotResult Cloud::GetBestSnapshot() const
{
  auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerBestSnapshotMethod);

  SnapshotResult result;
  auto responseHandler = [&result, &url](
    int code, std::string const & serverResponse) -> Cloud::RequestResult
  {
    if (IsSuccessfulResultCode(code))
    {
      ParseRequestJsonResult(url, serverResponse, result);
      return {Cloud::RequestStatus::Ok, {}};
    }

    // Server return 404 in case of snapshot absence.
    if (code == 404)
      return {Cloud::RequestStatus::Ok, {}};

    return {Cloud::RequestStatus::NetworkError, serverResponse};
  };

  result.m_requestResult =
      CloudRequestWithResult<SnapshotRequestData>(url, GetAccessToken(), responseHandler);

  return result;
}

Cloud::UploadingResult Cloud::RequestUploading(std::string const & filePath) const
{
  auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerUploadMethod);
  return CloudRequestWithJsonResult<UploadingRequestData, UploadingResult>(
    url, GetAccessToken(), filePath);
}

Cloud::RequestResult Cloud::ExecuteUploading(UploadingResponseData const & responseData,
                                             std::string const & filePath)
{
  ASSERT(!responseData.m_url.empty(), ());
  ASSERT(!responseData.m_method.empty(), ());

  static std::string const kStatErrors[] = {"download_server", "fallback_server"};

  std::vector<std::string> urls;
  urls.push_back(responseData.m_url);
  if (!responseData.m_fallbackUrl.empty())
    urls.push_back(responseData.m_fallbackUrl);

  std::string errorStr;
  int code = 0;
  for (size_t i = 0; i < urls.size(); ++i)
  {
    platform::HttpPayload payload;
    payload.m_url = urls[i];
    payload.m_method = responseData.m_method;

    for (auto const & f : responseData.m_fields)
    {
      ASSERT_EQUAL(f.size(), 2, ());
      payload.m_params[f[0]] = f[1];
    }
    payload.m_filePath = filePath;

    platform::HttpUploader request(payload);

    auto const result = request.Upload();
    code = result.m_httpCode;
    if (IsSuccessfulResultCode(code))
      return {RequestStatus::Ok, {}};

    errorStr = strings::to_string(code) + " " + result.m_description;
    if (code >= 500 && code < 600)
    {
      ASSERT_LESS_OR_EQUAL(i, ARRAY_SIZE(kStatErrors), ());
      alohalytics::TStringMap details{
          {"service", m_params.m_serverPathName}, {"type", kStatErrors[i]}, {"error", errorStr}};
      alohalytics::Stats::Instance().LogEvent("Cloud_Backup_error", details);
      return {RequestStatus::NetworkError, errorStr};
    }
  }

  return {code == 403 ? RequestStatus::Forbidden : RequestStatus::NetworkError, errorStr};
}

Cloud::RequestResult Cloud::NotifyAboutUploading(std::string const & filePath,
                                                 uint64_t fileSize) const
{
  auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerNotifyMethod);
  return CloudRequest<NotifyRequestData>(url, GetAccessToken(), filePath, fileSize);
}

Cloud::EntryPtr Cloud::FindOutdatedEntry() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  for (auto const & entry : m_index.m_entries)
  {
    if (entry->m_isOutdated)
      return entry;
  }
  return nullptr;
}

void Cloud::FinishUploading(SynchronizationResult result, std::string const & errorStr)
{
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (!m_uploadingStarted)
      return;

    if (result == SynchronizationResult::UserInterrupted)
    {
      // If the user interrupts uploading, we consider all files as up-to-dated.
      // The files will be checked and uploaded (if necessary) next time.
      m_index.m_isOutdated = false;
      for (auto & entry : m_index.m_entries)
        entry->m_isOutdated = false;
    }
    else
    {
      if (result == SynchronizationResult::Success)
      {
        m_index.m_isOutdated = false;
        m_index.m_lastSyncTimestamp = static_cast<uint64_t>(
            duration_cast<seconds>(system_clock::now().time_since_epoch()).count());
      }
      else
      {
        m_index.m_isOutdated = true;
      }
    }
    m_uploadingStarted = false;
    SaveIndexImpl();
  }

  if (result == SynchronizationResult::AuthError)
    ThreadSafeCallback<InvalidTokenHandler>([this]() { return m_onInvalidToken; });

  ThreadSafeCallback<SynchronizationFinishedHandler>(
      [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Backup, result,
      errorStr);
}

void Cloud::SetAccessToken(std::string const & token)
{
  std::lock_guard<std::mutex> lock(m_mutex);
  m_accessToken = token;
}

std::string Cloud::GetAccessToken() const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  return m_accessToken;
}

bool Cloud::IsRestoringEnabledCommonImpl(std::string & reason) const
{
  if (m_state != State::Enabled)
  {
    reason = "Cloud is not enabled";
    return false;
  }
  
  if (!m_indexUpdated)
  {
    reason = "Cloud is not initialized";
    return false;
  }
  
  if (m_accessToken.empty())
  {
    reason = "User is not authenticated";
    return false;
  }
  
  return true;
}

bool Cloud::IsRequestRestoringEnabled(std::string & reason) const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  if (!IsRestoringEnabledCommonImpl(reason))
    return false;
  
  if (m_restoringState != RestoringState::None)
  {
    reason = "Restoring process exists";
    return false;
  }
  
  return true;
}

bool Cloud::IsApplyRestoringEnabled(std::string & reason) const
{
  std::lock_guard<std::mutex> lock(m_mutex);
  if (!IsRestoringEnabledCommonImpl(reason))
    return false;
  
  if (m_restoringState != RestoringState::Requested)
  {
    reason = "Restoring process does not exist";
    return false;
  }
  
  if (m_bestSnapshotData.m_deviceId.empty())
  {
    reason = "Backup is absent";
    return false;
  }
  
  return true;
}

void Cloud::RequestRestoring()
{
  FinishUploading(SynchronizationResult::UserInterrupted, {});
  
  ThreadSafeCallback<SynchronizationStartedHandler>(
    [this]() { return m_onSynchronizationStarted; }, SynchronizationType::Restore);
  
  auto const status = GetPlatform().ConnectionStatus();
  if (status == Platform::EConnectionType::CONNECTION_NONE)
  {
    ThreadSafeCallback<SynchronizationFinishedHandler>(
      [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Restore,
      SynchronizationResult::InvalidCall, "No internet connection");
    return;
  }

  std::string reason;
  if (!IsRequestRestoringEnabled(reason))
  {
    ThreadSafeCallback<SynchronizationFinishedHandler>(
       [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Restore,
       SynchronizationResult::InvalidCall, reason);
    return;
  }
  
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_restoringState = RestoringState::Requested;
  }

  GetBestSnapshotTask(kTaskTimeoutInSeconds, 0 /* attemptIndex */);
}

void Cloud::ApplyRestoring()
{
  auto const status = GetPlatform().ConnectionStatus();
  if (status == Platform::EConnectionType::CONNECTION_NONE)
  {
    ThreadSafeCallback<SynchronizationFinishedHandler>(
      [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Restore,
      SynchronizationResult::InvalidCall, "No internet connection");
    return;
  }
  
  std::string reason;
  if (!IsApplyRestoringEnabled(reason))
  {
    ThreadSafeCallback<SynchronizationFinishedHandler>(
      [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Restore,
      SynchronizationResult::InvalidCall, reason);
    return;
  }

  {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_restoringState = RestoringState::Applying;
  }

  GetPlatform().RunTask(Platform::Thread::File, [this]()
  {
    // Create folder if it does not exist.
    auto const dirPath = GetRestoringFolder(m_params.m_serverPathName);
    if (!GetPlatform().IsFileExistsByFullPath(dirPath) && !Platform::MkDirChecked(dirPath))
    {
      FinishRestoring(SynchronizationResult::DiskError, "Unable create restoring directory");
      return;
    }

    // Get list of files to download.
    auto downloadingList = GetDownloadingList(dirPath);
    if (downloadingList.empty())
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      if (m_restoringState != RestoringState::Applying)
        return;

      // Try to complete restoring process.
      CompleteRestoring(dirPath);
    }
    else
    {
      // Start downloading.
      DownloadingTask(dirPath, false /* useFallbackUrl */, std::move(downloadingList));
    }
  });
}

void Cloud::CancelRestoring()
{
  FinishRestoring(SynchronizationResult::UserInterrupted, {});
}

void Cloud::GetBestSnapshotTask(uint32_t timeout, uint32_t attemptIndex)
{
  GetPlatform().RunDelayedTask(Platform::Thread::Network, seconds(timeout),
    [this, timeout, attemptIndex]()
  {
    bool isInvalidToken;
    {
      // Restoring state may be changed between tasks.
      std::lock_guard<std::mutex> lock(m_mutex);
      if (m_restoringState != RestoringState::Requested)
        return;

      isInvalidToken = m_accessToken.empty();
    }

    if (kServerUrl.empty())
    {
      FinishRestoring(SynchronizationResult::NetworkError, "Empty server url");
      return;
    }

    // Access token may become invalid between tasks.
    if (isInvalidToken)
    {
      FinishRestoring(SynchronizationResult::AuthError, "Access token is empty");
      return;
    }

    auto const result = GetBestSnapshot();
    if (result.m_isMalformed)
    {
      FinishRestoring(SynchronizationResult::NetworkError, "Malformed best snapshot response");
    }
    else if (result.m_requestResult.m_status == RequestStatus::Ok)
    {
      ProcessSuccessfulSnapshot(result);
    }
    else if (result.m_requestResult.m_status == RequestStatus::NetworkError)
    {
      // Retry request up to kRetryMaxAttempts times.
      if (attemptIndex + 1 == kRetryMaxAttempts)
      {
        FinishRestoring(SynchronizationResult::NetworkError, result.m_requestResult.m_error);
        return;
      }

      auto const retryTimeout = attemptIndex == 0 ? kRetryTimeoutInSeconds
                                                  : timeout * kRetryDegradationFactor;
      GetBestSnapshotTask(retryTimeout, attemptIndex + 1);
    }
    else if (result.m_requestResult.m_status == RequestStatus::Forbidden)
    {
      FinishRestoring(SynchronizationResult::AuthError, result.m_requestResult.m_error);
    }
  });
}

void Cloud::ProcessSuccessfulSnapshot(SnapshotResult const & result)
{
  // Check if the backup is empty.
  if (result.m_response.m_files.empty())
  {
    ThreadSafeCallback<RestoreRequestedHandler>([this]() { return m_onRestoreRequested; },
                                                RestoringRequestResult::NoBackup, "",
                                                result.m_response.m_datetime);
    FinishRestoring(SynchronizationResult::Success, {});
    return;
  }
  
  // Check if there is enough space to download backup.
  auto const totalSize = result.m_response.GetTotalSizeOfFiles();
  auto constexpr kSizeScalar = 10;
  if (totalSize * kSizeScalar >= GetPlatform().GetWritableStorageSpace())
  {
    ThreadSafeCallback<RestoreRequestedHandler>([this]() { return m_onRestoreRequested; },
                                                RestoringRequestResult::NotEnoughDiskSpace, "",
                                                result.m_response.m_datetime);
    FinishRestoring(SynchronizationResult::DiskError, {});
    return;
  }
  
  // Save snapshot data.
  bool isInterrupted;
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    m_bestSnapshotData = result.m_response;
    isInterrupted = (m_restoringState != RestoringState::Requested);
  }
  if (!isInterrupted)
  {
    ThreadSafeCallback<RestoreRequestedHandler>([this]() { return m_onRestoreRequested; },
                                                RestoringRequestResult::BackupExists,
                                                result.m_response.m_deviceName,
                                                result.m_response.m_datetime);
  }
}

void Cloud::FinishRestoring(Cloud::SynchronizationResult result, std::string const & errorStr)
{
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    if (m_restoringState == RestoringState::None)
      return;

    // We cannot interrupt restoring process on the finalizing step.
    if (result == Cloud::SynchronizationResult::UserInterrupted &&
        m_restoringState == RestoringState::Finalizing)
    {
      return;
    }

    m_bestSnapshotData = {};
    m_restoringState = RestoringState::None;
  }

  if (result == SynchronizationResult::AuthError)
    ThreadSafeCallback<InvalidTokenHandler>([this]() { return m_onInvalidToken; });

  ThreadSafeCallback<SynchronizationFinishedHandler>(
      [this]() { return m_onSynchronizationFinished; }, SynchronizationType::Restore, result,
      errorStr);
}

std::list<Cloud::SnapshotFileData> Cloud::GetDownloadingList(std::string const & restoringDirPath)
{
  auto const snapshotFile = base::JoinPath(restoringDirPath, kSnapshotFile);
  auto const prevSnapshot = ReadSnapshotFile(snapshotFile);

  SnapshotResponseData currentSnapshot;
  {
    std::lock_guard<std::mutex> lock(m_mutex);
    currentSnapshot = m_bestSnapshotData;
  }

  // Save to use in the next sessions.
  try
  {
    auto jsonData = SerializeToJson(currentSnapshot);
    FileWriter w(snapshotFile);
    w.Write(jsonData.data(), jsonData.length());
  }
  catch (FileWriter::Exception const & exception)
  {
    LOG(LWARNING, ("Exception while writing file:", snapshotFile,
                   "reason:", exception.what()));
    FinishRestoring(SynchronizationResult::DiskError, "Could not save snapshot file");
    return {};
  }

  // If the snapshot from previous sessions is not valid, return files from new one.
  if (currentSnapshot.m_deviceId != prevSnapshot.m_deviceId ||
      currentSnapshot.m_datetime != prevSnapshot.m_datetime ||
      currentSnapshot.m_files.size() != prevSnapshot.m_files.size())
  {
    return std::list<Cloud::SnapshotFileData>(currentSnapshot.m_files.begin(),
                                              currentSnapshot.m_files.end());
  }

  // Check if some files were completely downloaded last time.
  std::list<Cloud::SnapshotFileData> result;
  for (auto & f : currentSnapshot.m_files)
  {
    auto const restoringFile = base::JoinPath(restoringDirPath, f.m_fileName);
    if (!GetPlatform().IsFileExistsByFullPath(restoringFile))
    {
      result.push_back(std::move(f));
      continue;
    }

    uint64_t fileSize = 0;
    if (!base::GetFileSize(restoringFile, fileSize) || fileSize != f.m_fileSize)
      result.push_back(std::move(f));
  }

  return result;
}

void Cloud::DownloadingTask(std::string const & dirPath, bool useFallbackUrl,
                            std::list<Cloud::SnapshotFileData> && files)
{
  GetPlatform().RunTask(Platform::Thread::Network,
    [this, dirPath, useFallbackUrl, files = std::move(files)]() mutable
  {
    std::string snapshotDeviceId;
    {
      // Check if the process was interrupted.
      std::lock_guard<std::mutex> lock(m_mutex);
      if (m_restoringState != RestoringState::Applying)
        return;
      snapshotDeviceId = m_bestSnapshotData.m_deviceId;
    }

    if (files.empty())
    {
      CompleteRestoring(dirPath);
      return;
    }

    auto const f = files.front();
    files.erase(files.begin());
    auto const filePath = base::JoinPath(dirPath, f.m_fileName);

    auto const url = BuildMethodUrl(m_params.m_serverPathName, kServerDownloadMethod);
    auto const result = CloudRequestWithJsonResult<DownloadingRequestData, DownloadingResult>(
        url, GetAccessToken(), snapshotDeviceId, f.m_fileName, f.m_datetime);

    if (result.m_isMalformed || result.m_response.m_url.empty())
    {
      FinishRestoring(SynchronizationResult::NetworkError, "Malformed downloading file response");
    }
    else if (result.m_requestResult.m_status == RequestStatus::Ok)
    {
      if (useFallbackUrl && result.m_response.m_fallbackUrl.empty())
      {
        FinishRestoring(SynchronizationResult::NetworkError, "Fallback url is absent");
        return;
      }

      platform::RemoteFile remoteFile(useFallbackUrl ? result.m_response.m_fallbackUrl
                                                     : result.m_response.m_url,
                                      {} /* accessToken */, {} /* defaultHeaders */,
                                      false /* allowRedirection */);
      auto const downloadResult = remoteFile.Download(filePath);

      if (downloadResult.m_status == platform::RemoteFile::Status::Ok)
      {
        // Download next file.
        DownloadingTask(dirPath, false /* useFallbackUrl */, std::move(files));
      }
      else if (downloadResult.m_status == platform::RemoteFile::Status::DiskError)
      {
        FinishRestoring(SynchronizationResult::DiskError, downloadResult.m_description);
      }
      else if (downloadResult.m_status == platform::RemoteFile::Status::Forbidden)
      {
        FinishRestoring(SynchronizationResult::AuthError, downloadResult.m_description);
      }
      else
      {
        alohalytics::TStringMap details{
          {"service", m_params.m_serverPathName},
          {"type", useFallbackUrl ? "fallback_server" : "download_server"},
          {"error", downloadResult.m_description}};
        alohalytics::Stats::Instance().LogEvent("Cloud_Restore_error", details);

        if (!useFallbackUrl)
        {
          // Retry to download by means of fallback url.
          files.push_front(std::move(f));
          DownloadingTask(dirPath, true /* useFallbackUrl */, std::move(files));
        }
        else
        {
          FinishRestoring(SynchronizationResult::NetworkError, downloadResult.m_description);
        }
      }
    }
    else if (result.m_requestResult.m_status == RequestStatus::NetworkError)
    {
      FinishRestoring(SynchronizationResult::NetworkError, result.m_requestResult.m_error);
    }
    else if (result.m_requestResult.m_status == RequestStatus::Forbidden)
    {
      FinishRestoring(SynchronizationResult::AuthError, result.m_requestResult.m_error);
    }
  });
}

void Cloud::CompleteRestoring(std::string const & dirPath)
{
  GetPlatform().RunTask(Platform::Thread::File, [this, dirPath]()
  {
    if (!m_params.m_restoreConverter)
    {
      FinishRestoring(SynchronizationResult::InvalidCall, "Restore converter is not set");
      return;
    }
    
    // Check files and convert them to expected format.
    SnapshotResponseData currentSnapshot;
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      currentSnapshot = m_bestSnapshotData;
    }

    RestoredFilesCollection convertedFiles;
    convertedFiles.reserve(currentSnapshot.m_files.size());
    for (auto & f : currentSnapshot.m_files)
    {
      auto const restoringFile = base::JoinPath(dirPath, f.m_fileName);
      if (!GetPlatform().IsFileExistsByFullPath(restoringFile))
      {
        FinishRestoring(SynchronizationResult::DiskError, "Restored file is absent");
        return;
      }

      uint64_t fileSize = 0;
      if (!base::GetFileSize(restoringFile, fileSize) || fileSize != f.m_fileSize)
      {
        std::string const str = "Restored file has incorrect size. Expected size = " +
                                strings::to_string(f.m_fileSize) +
                                ". Server size =" + strings::to_string(fileSize);
        FinishRestoring(SynchronizationResult::DiskError, str);
        return;
      }

      auto const fn = f.m_fileName + ".converted";
      auto const convertedFile = base::JoinPath(dirPath, fn);
      auto const convertionResult = m_params.m_restoreConverter(restoringFile, convertedFile);

      // This file must be skipped by some reason.
      if (convertionResult.m_needSkip)
        continue;

      if (!convertionResult.m_isSuccessful)
      {
        FinishRestoring(SynchronizationResult::DiskError, "Restored file conversion error");
        return;
      }
      convertedFiles.emplace_back(fn, convertionResult.m_hash);
    }

    // Check if the process was interrupted and start finalizing.
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      if (m_restoringState != RestoringState::Applying)
        return;

      m_restoringState = RestoringState::Finalizing;
    }

    GetPlatform().RunTask(Platform::Thread::Gui,
                          [this, dirPath, convertedFiles = std::move(convertedFiles)]() mutable
    {
      ThreadSafeCallback<RestoredFilesPreparedHandler>([this]() { return m_onRestoredFilesPrepared; });
      ApplyRestoredFiles(dirPath, std::move(convertedFiles));
    });
  });
}

void Cloud::ApplyRestoredFiles(std::string const & dirPath, RestoredFilesCollection && files)
{
  GetPlatform().RunTask(Platform::Thread::File, [this, dirPath, files = std::move(files)]()
  {
    // Delete all files in the destination folder.
    if (GetPlatform().IsFileExistsByFullPath(m_params.m_restoringFolder) &&
        !GetPlatform().RmDirRecursively(m_params.m_restoringFolder))
    {
      FinishRestoring(SynchronizationResult::DiskError, "Could not delete restoring folder");
      return;
    }

    // Move files.
    if (!GetPlatform().MkDirChecked(m_params.m_restoringFolder))
    {
      FinishRestoring(SynchronizationResult::DiskError, "Could not create restoring folder");
      return;
    }

    RestoredFilesCollection readyFiles;
    readyFiles.reserve(files.size());
    for (auto const & f : files)
    {
      auto const restoredFile = base::JoinPath(dirPath, f.m_filename);
      auto const finalFilename =
          base::FilenameWithoutExt(f.m_filename) + m_params.m_restoredFileExtension;
      auto const readyFile = base::JoinPath(m_params.m_restoringFolder, finalFilename);
      if (!base::RenameFileX(restoredFile, readyFile))
      {
        if (!base::CopyFileX(restoredFile, readyFile))
        {
          FinishRestoring(SynchronizationResult::DiskError, "Restored file copying error");
          return;
        }
        UNUSED_VALUE(base::DeleteFileX(restoredFile));
      }
      readyFiles.emplace_back(readyFile, f.m_hash);
    }

    // Reset upload index to the restored state.
    {
      std::lock_guard<std::mutex> lock(m_mutex);
      m_files.clear();
      auto const lastSyncTimestampInSec = m_bestSnapshotData.m_datetime / 1000;
      UpdateIndexByRestoredFilesImpl(readyFiles, lastSyncTimestampInSec);
    }

    // Delete temporary directory.
    GetPlatform().RmDirRecursively(dirPath);
    FinishRestoring(SynchronizationResult::Success, {});
  });
}

//static
Cloud::State Cloud::GetCloudState(std::string const & paramName)
{
  int stateValue;
  if (!settings::Get(paramName, stateValue))
  {
    stateValue = static_cast<int>(State::Unknown);
    settings::Set(paramName, stateValue);
  }
  return static_cast<State>(stateValue);
}