summaryrefslogtreecommitdiff
path: root/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc
blob: 5907b706d0998c0fcf53f9d8828dd0b58d1241f8 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h"

#include <cmath>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/sparse_histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/trace_event/trace_event.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/data_reduction_proxy/core/browser/data_usage_store.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_features.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_switches.h"
#include "components/data_reduction_proxy/proto/data_store.pb.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "net/base/mime_util.h"

#if defined(OS_ANDROID)
#include "base/android/application_status_listener.h"
#endif

namespace data_reduction_proxy {

namespace {

#define CONCAT(a, b) a##b
// CONCAT1 provides extra level of indirection so that __LINE__ macro expands.
#define CONCAT1(a, b) CONCAT(a, b)
#define UNIQUE_VARNAME CONCAT1(var_, __LINE__)
// We need to use a macro instead of a method because UMA_HISTOGRAM_COUNTS
// requires its first argument to be an inline string and not a variable.
#define RECORD_INT64PREF_TO_HISTOGRAM(pref, uma)     \
  int64_t UNIQUE_VARNAME = GetInt64(pref);           \
  if (UNIQUE_VARNAME > 0) {                          \
    UMA_HISTOGRAM_COUNTS(uma, UNIQUE_VARNAME >> 10); \
  }

const double kSecondsPerWeek =
    base::Time::kMicrosecondsPerWeek / base::Time::kMicrosecondsPerSecond;

// Returns the value at |index| of |list_value| as an int64_t.
int64_t GetInt64PrefValue(const base::ListValue& list_value, size_t index) {
  int64_t val = 0;
  std::string pref_value;
  bool rv = list_value.GetString(index, &pref_value);
  DCHECK(rv);
  if (rv) {
    rv = base::StringToInt64(pref_value, &val);
    DCHECK(rv);
  }
  return val;
}

// Ensure list has exactly |length| elements, either by truncating at the
// front, or appending "0"'s to the back.
void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t length) {
  // Remove data for old days from the front.
  while (list->GetSize() > length)
    list->Remove(0, nullptr);
  // Newly added lists are empty. Add entries to back to fill the window,
  // each initialized to zero.
  while (list->GetSize() < length)
    list->AppendString(base::Int64ToString(0));
  DCHECK_EQ(length, list->GetSize());
}

// Increments an int64_t, stored as a string, in a ListPref at the specified
// index.  The value must already exist and be a string representation of a
// number.
void AddInt64ToListPref(size_t index,
                        int64_t length,
                        base::ListValue* list_update) {
  int64_t value = GetInt64PrefValue(*list_update, index) + length;
  list_update->Set(index,
                   std::make_unique<base::Value>(base::Int64ToString(value)));
}

// DailyContentLengthUpdate maintains a data saving pref. The pref is a list
// of |kNumDaysInHistory| elements of daily total content lengths for the past
// |kNumDaysInHistory| days.
void RecordDailyContentLengthHistograms(
    int64_t original_length,
    int64_t received_length,
    int64_t original_length_with_data_reduction_enabled,
    int64_t received_length_with_data_reduction_enabled,
    int64_t original_length_via_data_reduction_proxy,
    int64_t received_length_via_data_reduction_proxy,
    int64_t https_length_with_data_reduction_enabled,
    int64_t short_bypass_length_with_data_reduction_enabled,
    int64_t long_bypass_length_with_data_reduction_enabled,
    int64_t unknown_length_with_data_reduction_enabled) {
  // Report daily UMA only for days having received content.
  if (original_length <= 0 || received_length <= 0)
    return;

  // Record metrics in KB.
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyOriginalContentLength", original_length >> 10);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength", received_length >> 10);

  int percent = 0;
  // UMA percentage cannot be negative.
  if (original_length > received_length) {
    percent = (100 * (original_length - received_length)) / original_length;
  }
  UMA_HISTOGRAM_PERCENTAGE("Net.DailyContentSavingPercent", percent);

  if (original_length_with_data_reduction_enabled <= 0 ||
      received_length_with_data_reduction_enabled <= 0) {
    return;
  }

  UMA_HISTOGRAM_COUNTS(
      "Net.DailyOriginalContentLength_DataReductionProxyEnabled",
      original_length_with_data_reduction_enabled >> 10);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_DataReductionProxyEnabled",
      received_length_with_data_reduction_enabled >> 10);

  int percent_data_reduction_proxy_enabled = 0;
  // UMA percentage cannot be negative.
  if (original_length_with_data_reduction_enabled >
      received_length_with_data_reduction_enabled) {
    percent_data_reduction_proxy_enabled =
        100 * (original_length_with_data_reduction_enabled -
               received_length_with_data_reduction_enabled) /
        original_length_with_data_reduction_enabled;
  }
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentSavingPercent_DataReductionProxyEnabled",
      percent_data_reduction_proxy_enabled);

  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_DataReductionProxyEnabled",
      (100 * received_length_with_data_reduction_enabled) / received_length);

  DCHECK_GE(https_length_with_data_reduction_enabled, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_DataReductionProxyEnabled_Https",
      https_length_with_data_reduction_enabled >> 10);
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_DataReductionProxyEnabled_Https",
      (100 * https_length_with_data_reduction_enabled) / received_length);

  DCHECK_GE(short_bypass_length_with_data_reduction_enabled, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_DataReductionProxyEnabled_ShortBypass",
      short_bypass_length_with_data_reduction_enabled >> 10);
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_DataReductionProxyEnabled_ShortBypass",
      ((100 * short_bypass_length_with_data_reduction_enabled) /
       received_length));

  DCHECK_GE(long_bypass_length_with_data_reduction_enabled, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_DataReductionProxyEnabled_LongBypass",
      long_bypass_length_with_data_reduction_enabled >> 10);
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_DataReductionProxyEnabled_LongBypass",
      ((100 * long_bypass_length_with_data_reduction_enabled) /
       received_length));

  DCHECK_GE(unknown_length_with_data_reduction_enabled, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_DataReductionProxyEnabled_UnknownBypass",
      unknown_length_with_data_reduction_enabled >> 10);
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_DataReductionProxyEnabled_Unknown",
      ((100 * unknown_length_with_data_reduction_enabled) /
       received_length));

  DCHECK_GE(original_length_via_data_reduction_proxy, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyOriginalContentLength_ViaDataReductionProxy",
      original_length_via_data_reduction_proxy >> 10);
  DCHECK_GE(received_length_via_data_reduction_proxy, 0);
  UMA_HISTOGRAM_COUNTS(
      "Net.DailyContentLength_ViaDataReductionProxy",
      received_length_via_data_reduction_proxy >> 10);
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentPercent_ViaDataReductionProxy",
      (100 * received_length_via_data_reduction_proxy) / received_length);

  if (original_length_via_data_reduction_proxy <= 0)
    return;
  int percent_savings_via_data_reduction_proxy = 0;
  if (original_length_via_data_reduction_proxy >
      received_length_via_data_reduction_proxy) {
    percent_savings_via_data_reduction_proxy =
        100 * (original_length_via_data_reduction_proxy -
               received_length_via_data_reduction_proxy) /
        original_length_via_data_reduction_proxy;
  }
  UMA_HISTOGRAM_PERCENTAGE(
      "Net.DailyContentSavingPercent_ViaDataReductionProxy",
      percent_savings_via_data_reduction_proxy);
}

void RecordSavingsClearedMetric(DataReductionProxySavingsClearedReason reason) {
  DCHECK_GT(DataReductionProxySavingsClearedReason::REASON_COUNT, reason);
  UMA_HISTOGRAM_ENUMERATION(
      "DataReductionProxy.SavingsCleared.Reason", reason,
      DataReductionProxySavingsClearedReason::REASON_COUNT);
}

// Returns the week number for the current time. The epoch time is treated as
// week=0.
int32_t GetCurrentWeekNumber(const base::Time& now) {
  double now_in_seconds = now.ToDoubleT();
  return now_in_seconds / kSecondsPerWeek;
}

// Adds |value| to the item at |key| in the preference dictionary found at
// |pref|. If |key| is not found it will be inserted.
void AddToDictionaryPref(PrefService* pref_service,
                         const std::string& pref,
                         int key,
                         int value) {
  DictionaryPrefUpdate pref_update(pref_service, pref);
  base::DictionaryValue* pref_dict = pref_update.Get();
  const std::string key_str = base::IntToString(key);
  base::Value* dict_value = pref_dict->FindKey(key_str);
  if (dict_value)
    value += dict_value->GetInt();
  pref_dict->SetKey(key_str, base::Value(value));
}

// Moves the dictionary stored in preference |pref_src| to |pref_dst|, and
// clears the preference |pref_src|.
void MoveAndClearDictionaryPrefs(PrefService* pref_service,
                                 const std::string& pref_dst,
                                 const std::string& pref_src) {
  DictionaryPrefUpdate pref_update_dst(pref_service, pref_dst);
  DictionaryPrefUpdate pref_update_src(pref_service, pref_src);
  pref_update_dst->Clear();
  pref_update_dst->Swap(pref_update_src.Get());
  DCHECK(pref_update_src->empty());
}

void MaybeInitWeeklyAggregateDataUsePrefs(const base::Time& now,
                                          PrefService* pref_service) {
  int saved_week = pref_service->GetInteger(prefs::kThisWeekNumber);
  int current_week = GetCurrentWeekNumber(now);

  if (saved_week == current_week)
    return;

  pref_service->SetInteger(prefs::kThisWeekNumber, current_week);
  if (current_week == saved_week + 1) {
    // The next week has just started. Move the data use aggregate prefs for
    // this week to last week, and clear the prefs for this week.
    MoveAndClearDictionaryPrefs(pref_service,
                                prefs::kLastWeekServicesDownstreamBackgroundKB,
                                prefs::kThisWeekServicesDownstreamBackgroundKB);
    MoveAndClearDictionaryPrefs(pref_service,
                                prefs::kLastWeekServicesDownstreamForegroundKB,
                                prefs::kThisWeekServicesDownstreamForegroundKB);
    MoveAndClearDictionaryPrefs(
        pref_service, prefs::kLastWeekUserTrafficContentTypeDownstreamKB,
        prefs::kThisWeekUserTrafficContentTypeDownstreamKB);
  } else {
    // Current week is too different than the last time data use aggregate prefs
    // were updated. This may happen if Chrome was opened after a long time, or
    // due to system clock being changed backward or forward. Clear all prefs in
    // this case.
    pref_service->ClearPref(prefs::kLastWeekServicesDownstreamBackgroundKB);
    pref_service->ClearPref(prefs::kLastWeekServicesDownstreamForegroundKB);
    pref_service->ClearPref(prefs::kLastWeekUserTrafficContentTypeDownstreamKB);
    pref_service->ClearPref(prefs::kThisWeekServicesDownstreamBackgroundKB);
    pref_service->ClearPref(prefs::kThisWeekServicesDownstreamForegroundKB);
    pref_service->ClearPref(prefs::kThisWeekUserTrafficContentTypeDownstreamKB);
  }
}

// Records the key-value pairs in the dictionary in a sparse histogram.
void RecordDictionaryToHistogram(const std::string& histogram_name,
                                 const base::DictionaryValue* dictionary) {
  base::HistogramBase* histogram = base::SparseHistogram::FactoryGet(
      histogram_name, base::HistogramBase::kUmaTargetedHistogramFlag);
  for (const auto& entry : *dictionary) {
    int key;
    int value = entry.second->GetInt();
    if (value > 0 && base::StringToInt(entry.first, &key)) {
      histogram->AddCount(key, value);
    }
  }
}

}  // namespace

class DataReductionProxyCompressionStats::DailyContentLengthUpdate {
 public:
  DailyContentLengthUpdate(
      DataReductionProxyCompressionStats* compression_stats,
      const char* pref_path)
      : update_(nullptr),
        compression_stats_(compression_stats),
        pref_path_(pref_path) {}

  void UpdateForDateChange(int days_since_last_update) {
    if (days_since_last_update) {
      MaybeInitialize();
      MaintainContentLengthPrefForDateChange(days_since_last_update);
    }
  }

  // Update the lengths for the current day.
  void Add(int64_t content_length) {
    if (content_length != 0) {
      MaybeInitialize();
      AddInt64ToListPref(kNumDaysInHistory - 1, content_length, update_);
    }
  }

  int64_t GetListPrefValue(size_t index) {
    MaybeInitialize();
    return GetInt64PrefValue(*update_, index);
  }

 private:
  void MaybeInitialize() {
    if (update_)
      return;

    update_ = compression_stats_->GetList(pref_path_);
    // New empty lists may have been created. Maintain the invariant that
    // there should be exactly |kNumDaysInHistory| days in the histories.
    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory);
  }

  // Update the list for date change and ensure the list has exactly |length|
  // elements. The last entry in the list will be for the current day after
  // the update.
  void MaintainContentLengthPrefForDateChange(int days_since_last_update) {
    if (days_since_last_update == -1) {
      // The system may go backwards in time by up to a day for legitimate
      // reasons, such as with changes to the time zone. In such cases, we
      // keep adding to the current day.
      // Note: we accept the fact that some reported data is shifted to
      // the adjacent day if users travel back and forth across time zones.
      days_since_last_update = 0;
    } else if (days_since_last_update < -1) {
      // Erase all entries if the system went backwards in time by more than
      // a day.
      update_->Clear();

      days_since_last_update = kNumDaysInHistory;
    }
    DCHECK_GE(days_since_last_update, 0);

    // Add entries for days since last update event. This will make the
    // lists longer than kNumDaysInHistory. The additional items will be cut off
    // from the head of the lists by |MaintainContentLengthPrefsWindow|, below.
    for (int i = 0;
         i < days_since_last_update && i < static_cast<int>(kNumDaysInHistory);
         ++i) {
      update_->AppendString(base::Int64ToString(0));
    }

    // Entries for new days may have been appended. Maintain the invariant that
    // there should be exactly |kNumDaysInHistory| days in the histories.
    MaintainContentLengthPrefsWindow(update_, kNumDaysInHistory);
  }

  // Non-owned. Lazily initialized, set to nullptr until initialized.
  base::ListValue* update_;
  // Non-owned pointer.
  DataReductionProxyCompressionStats* compression_stats_;
  // The path of the content length pref for |this|.
  const char* pref_path_;

  DISALLOW_COPY_AND_ASSIGN(DailyContentLengthUpdate);
};

// DailyDataSavingUpdate maintains a pair of data saving prefs, original_update_
// and received_update_. pref_original is a list of |kNumDaysInHistory| elements
// of daily total original content lengths for the past |kNumDaysInHistory|
// days. pref_received is the corresponding list of the daily total received
// content lengths.
class DataReductionProxyCompressionStats::DailyDataSavingUpdate {
 public:
  DailyDataSavingUpdate(DataReductionProxyCompressionStats* compression_stats,
                        const char* original_pref_path,
                        const char* received_pref_path)
      : original_(compression_stats, original_pref_path),
        received_(compression_stats, received_pref_path) {}

  void UpdateForDateChange(int days_since_last_update) {
    original_.UpdateForDateChange(days_since_last_update);
    received_.UpdateForDateChange(days_since_last_update);
  }

  // Update the lengths for the current day.
  void Add(int64_t original_content_length, int64_t received_content_length) {
    original_.Add(original_content_length);
    received_.Add(received_content_length);
  }

  int64_t GetOriginalListPrefValue(size_t index) {
    return original_.GetListPrefValue(index);
  }
  int64_t GetReceivedListPrefValue(size_t index) {
    return received_.GetListPrefValue(index);
  }

 private:
  DailyContentLengthUpdate original_;
  DailyContentLengthUpdate received_;

  DISALLOW_COPY_AND_ASSIGN(DailyDataSavingUpdate);
};

DataReductionProxyCompressionStats::DataReductionProxyCompressionStats(
    DataReductionProxyService* service,
    PrefService* prefs,
    const base::TimeDelta& delay)
    : service_(service),
      pref_service_(prefs),
      delay_(delay),
      data_usage_map_is_dirty_(false),
      session_total_received_(0),
      session_total_original_(0),
      current_data_usage_load_status_(NOT_LOADED),
      weak_factory_(this) {
  DCHECK(service);
  DCHECK(prefs);
  DCHECK_GE(delay.InMilliseconds(), 0);
  Init();
}

DataReductionProxyCompressionStats::~DataReductionProxyCompressionStats() {
  DCHECK(thread_checker_.CalledOnValidThread());

  if (current_data_usage_load_status_ == LOADED)
    PersistDataUsage();

  WritePrefs();
}

void DataReductionProxyCompressionStats::Init() {
  DCHECK(thread_checker_.CalledOnValidThread());

  data_usage_reporting_enabled_.Init(
      prefs::kDataUsageReportingEnabled, pref_service_,
      base::Bind(
          &DataReductionProxyCompressionStats::OnDataUsageReportingPrefChanged,
          weak_factory_.GetWeakPtr()));

  if (data_usage_reporting_enabled_.GetValue()) {
    current_data_usage_load_status_ = LOADING;
    service_->LoadCurrentDataUsageBucket(base::Bind(
        &DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded,
        weak_factory_.GetWeakPtr()));
  }

  InitializeWeeklyAggregateDataUse(base::Time::Now());

  if (delay_.is_zero())
    return;

  // Init all int64_t prefs.
  InitInt64Pref(prefs::kDailyHttpContentLengthLastUpdateDate);
  InitInt64Pref(prefs::kHttpReceivedContentLength);
  InitInt64Pref(prefs::kHttpOriginalContentLength);

  InitInt64Pref(prefs::kDailyHttpOriginalContentLengthApplication);
  InitInt64Pref(prefs::kDailyHttpOriginalContentLengthVideo);
  InitInt64Pref(prefs::kDailyHttpOriginalContentLengthUnknown);
  InitInt64Pref(prefs::kDailyHttpReceivedContentLengthApplication);
  InitInt64Pref(prefs::kDailyHttpReceivedContentLengthVideo);
  InitInt64Pref(prefs::kDailyHttpReceivedContentLengthUnknown);

  InitInt64Pref(
      prefs::kDailyOriginalContentLengthViaDataReductionProxyApplication);
  InitInt64Pref(prefs::kDailyOriginalContentLengthViaDataReductionProxyVideo);
  InitInt64Pref(prefs::kDailyOriginalContentLengthViaDataReductionProxyUnknown);
  InitInt64Pref(prefs::kDailyContentLengthViaDataReductionProxyApplication);
  InitInt64Pref(prefs::kDailyContentLengthViaDataReductionProxyVideo);
  InitInt64Pref(prefs::kDailyContentLengthViaDataReductionProxyUnknown);

  InitInt64Pref(
      prefs::
          kDailyOriginalContentLengthWithDataReductionProxyEnabledApplication);
  InitInt64Pref(
      prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo);
  InitInt64Pref(
      prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabledUnknown);
  InitInt64Pref(
      prefs::kDailyContentLengthWithDataReductionProxyEnabledApplication);
  InitInt64Pref(prefs::kDailyContentLengthWithDataReductionProxyEnabledVideo);
  InitInt64Pref(prefs::kDailyContentLengthWithDataReductionProxyEnabledUnknown);

  // Init all list prefs.
  InitListPref(prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled);
  InitListPref(
      prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
  InitListPref(
      prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
  InitListPref(prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled);
  InitListPref(prefs::kDailyContentLengthViaDataReductionProxy);
  InitListPref(prefs::kDailyContentLengthWithDataReductionProxyEnabled);
  InitListPref(prefs::kDailyHttpOriginalContentLength);
  InitListPref(prefs::kDailyHttpReceivedContentLength);
  InitListPref(prefs::kDailyOriginalContentLengthViaDataReductionProxy);
  InitListPref(prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled);
}

void DataReductionProxyCompressionStats::RecordDataUseWithMimeType(
    int64_t data_used,
    int64_t original_size,
    bool data_saver_enabled,
    DataReductionProxyRequestType request_type,
    const std::string& mime_type,
    bool is_user_traffic,
    data_use_measurement::DataUseUserData::DataUseContentType content_type,
    int32_t service_hash_code) {
  DCHECK(thread_checker_.CalledOnValidThread());
  TRACE_EVENT0("loader",
               "DataReductionProxyCompressionStats::RecordDataUseWithMimeType")
  session_total_received_ += data_used;
  session_total_original_ += original_size;

  IncreaseInt64Pref(data_reduction_proxy::prefs::kHttpReceivedContentLength,
                    data_used);
  IncreaseInt64Pref(data_reduction_proxy::prefs::kHttpOriginalContentLength,
                    original_size);

  RecordRequestSizePrefs(data_used, original_size, data_saver_enabled,
                         request_type, mime_type, base::Time::Now());
  RecordWeeklyAggregateDataUse(
      base::Time::Now(), std::round(static_cast<double>(data_used) / 1024),
      is_user_traffic, content_type, service_hash_code);
}

void DataReductionProxyCompressionStats::InitInt64Pref(const char* pref) {
  int64_t pref_value = pref_service_->GetInt64(pref);
  pref_map_[pref] = pref_value;
}

void DataReductionProxyCompressionStats::InitListPref(const char* pref) {
  std::unique_ptr<base::ListValue> pref_value =
      std::unique_ptr<base::ListValue>(
          pref_service_->GetList(pref)->DeepCopy());
  list_pref_map_[pref] = std::move(pref_value);
}

int64_t DataReductionProxyCompressionStats::GetInt64(const char* pref_path) {
  if (delay_.is_zero())
    return pref_service_->GetInt64(pref_path);

  DataReductionProxyPrefMap::iterator iter = pref_map_.find(pref_path);
  return iter->second;
}

void DataReductionProxyCompressionStats::SetInt64(const char* pref_path,
                                                  int64_t pref_value) {
  if (delay_.is_zero()) {
    pref_service_->SetInt64(pref_path, pref_value);
    return;
  }

  DelayedWritePrefs();
  pref_map_[pref_path] = pref_value;
}

void DataReductionProxyCompressionStats::IncreaseInt64Pref(
    const char* pref_path,
    int64_t delta) {
  SetInt64(pref_path, GetInt64(pref_path) + delta);
}

base::ListValue* DataReductionProxyCompressionStats::GetList(
    const char* pref_path) {
  if (delay_.is_zero())
    return ListPrefUpdate(pref_service_, pref_path).Get();

  DelayedWritePrefs();
  auto it = list_pref_map_.find(pref_path);
  if (it == list_pref_map_.end())
    return nullptr;
  return it->second.get();
}

void DataReductionProxyCompressionStats::WritePrefs() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (delay_.is_zero())
    return;

  for (DataReductionProxyPrefMap::iterator iter = pref_map_.begin();
       iter != pref_map_.end(); ++iter) {
    pref_service_->SetInt64(iter->first, iter->second);
  }

  for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end();
       ++iter) {
    TransferList(*(iter->second.get()),
                 ListPrefUpdate(pref_service_, iter->first).Get());
  }
}

std::unique_ptr<base::Value>
DataReductionProxyCompressionStats::HistoricNetworkStatsInfoToValue() {
  DCHECK(thread_checker_.CalledOnValidThread());
  int64_t total_received = GetInt64(prefs::kHttpReceivedContentLength);
  int64_t total_original = GetInt64(prefs::kHttpOriginalContentLength);

  auto dict = std::make_unique<base::DictionaryValue>();
  // Use strings to avoid overflow. base::Value only supports 32-bit integers.
  dict->SetString("historic_received_content_length",
                  base::Int64ToString(total_received));
  dict->SetString("historic_original_content_length",
                  base::Int64ToString(total_original));
  return std::move(dict);
}

std::unique_ptr<base::Value>
DataReductionProxyCompressionStats::SessionNetworkStatsInfoToValue() const {
  DCHECK(thread_checker_.CalledOnValidThread());

  auto dict = std::make_unique<base::DictionaryValue>();
  // Use strings to avoid overflow. base::Value only supports 32-bit integers.
  dict->SetString("session_received_content_length",
                  base::Int64ToString(session_total_received_));
  dict->SetString("session_original_content_length",
                  base::Int64ToString(session_total_original_));
  return std::move(dict);
}

int64_t DataReductionProxyCompressionStats::GetLastUpdateTime() {
  int64_t last_update_internal =
      GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
  base::Time last_update = base::Time::FromInternalValue(last_update_internal);
  return static_cast<int64_t>(last_update.ToJsTime());
}

void DataReductionProxyCompressionStats::ResetStatistics() {
  base::ListValue* original_update =
      GetList(prefs::kDailyHttpOriginalContentLength);
  base::ListValue* received_update =
      GetList(prefs::kDailyHttpReceivedContentLength);
  original_update->Clear();
  received_update->Clear();
  for (size_t i = 0; i < kNumDaysInHistory; ++i) {
    original_update->AppendString(base::Int64ToString(0));
    received_update->AppendString(base::Int64ToString(0));
  }
}

int64_t DataReductionProxyCompressionStats::GetHttpReceivedContentLength() {
  return GetInt64(prefs::kHttpReceivedContentLength);
}

int64_t DataReductionProxyCompressionStats::GetHttpOriginalContentLength() {
  return GetInt64(prefs::kHttpOriginalContentLength);
}

ContentLengthList DataReductionProxyCompressionStats::GetDailyContentLengths(
    const char* pref_name) {
  ContentLengthList content_lengths;
  const base::ListValue* list_value = GetList(pref_name);
  if (list_value->GetSize() == kNumDaysInHistory) {
    for (size_t i = 0; i < kNumDaysInHistory; ++i)
      content_lengths.push_back(GetInt64PrefValue(*list_value, i));
  }
  return content_lengths;
}

void DataReductionProxyCompressionStats::GetContentLengths(
    unsigned int days,
    int64_t* original_content_length,
    int64_t* received_content_length,
    int64_t* last_update_time) {
  DCHECK_LE(days, kNumDaysInHistory);

  const base::ListValue* original_list =
      GetList(prefs::kDailyHttpOriginalContentLength);
  const base::ListValue* received_list =
      GetList(prefs::kDailyHttpReceivedContentLength);

  if (original_list->GetSize() != kNumDaysInHistory ||
      received_list->GetSize() != kNumDaysInHistory) {
    *original_content_length = 0L;
    *received_content_length = 0L;
    *last_update_time = 0L;
    return;
  }

  int64_t orig = 0L;
  int64_t recv = 0L;
  // Include days from the end of the list going backwards.
  for (size_t i = kNumDaysInHistory - days;
       i < kNumDaysInHistory; ++i) {
    orig += GetInt64PrefValue(*original_list, i);
    recv += GetInt64PrefValue(*received_list, i);
  }
  *original_content_length = orig;
  *received_content_length = recv;
  *last_update_time = GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
}

void DataReductionProxyCompressionStats::GetHistoricalDataUsage(
    const HistoricalDataUsageCallback& get_data_usage_callback) {
  GetHistoricalDataUsageImpl(get_data_usage_callback, base::Time::Now());
}

void DataReductionProxyCompressionStats::DeleteBrowsingHistory(
    const base::Time& start,
    const base::Time& end) {
  DCHECK_NE(LOADING, current_data_usage_load_status_);

  if (!data_usage_map_last_updated_.is_null() &&
      DataUsageStore::BucketOverlapsInterval(data_usage_map_last_updated_,
                                             start, end)) {
    data_usage_map_.clear();
    data_usage_map_last_updated_ = base::Time();
    data_usage_map_is_dirty_ = false;
  }

  service_->DeleteBrowsingHistory(start, end);

  RecordSavingsClearedMetric(DataReductionProxySavingsClearedReason::
                                 USER_ACTION_DELETE_BROWSING_HISTORY);
}

void DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded(
    std::unique_ptr<DataUsageBucket> data_usage) {
  // Exit early if the pref was turned off before loading from storage
  // completed.
  if (!data_usage_reporting_enabled_.GetValue()) {
    DCHECK_EQ(NOT_LOADED, current_data_usage_load_status_);
    DCHECK(data_usage_map_.empty());
    current_data_usage_load_status_ = NOT_LOADED;
    return;
  } else {
    DCHECK_EQ(LOADING, current_data_usage_load_status_);
  }

  DCHECK(data_usage_map_last_updated_.is_null());
  DCHECK(data_usage_map_.empty());

  // We currently do not break down by connection type. However, we use a schema
  // that makes it easy to transition to a connection based breakdown without
  // requiring a data migration.
  DCHECK(data_usage->connection_usage_size() == 0 ||
         data_usage->connection_usage_size() == 1);
  for (const auto& connection_usage : data_usage->connection_usage()) {
    for (const auto& site_usage : connection_usage.site_usage()) {
      data_usage_map_[site_usage.hostname()] =
          std::make_unique<PerSiteDataUsage>(site_usage);
    }
  }

  data_usage_map_last_updated_ =
      base::Time::FromInternalValue(data_usage->last_updated_timestamp());
  // Record if there was a read error.
  if (data_usage->had_read_error()) {
    RecordSavingsClearedMetric(
        DataReductionProxySavingsClearedReason::PREFS_PARSE_ERROR);
  }

  current_data_usage_load_status_ = LOADED;
}

void DataReductionProxyCompressionStats::SetDataUsageReportingEnabled(
    bool enabled) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (data_usage_reporting_enabled_.GetValue() != enabled) {
    data_usage_reporting_enabled_.SetValue(enabled);
    OnDataUsageReportingPrefChanged();
  }
}

void DataReductionProxyCompressionStats::ClearDataSavingStatistics(
    DataReductionProxySavingsClearedReason reason) {
  DeleteHistoricalDataUsage();

  pref_service_->ClearPref(prefs::kDailyHttpContentLengthLastUpdateDate);
  pref_service_->ClearPref(prefs::kHttpReceivedContentLength);
  pref_service_->ClearPref(prefs::kHttpOriginalContentLength);

  pref_service_->ClearPref(prefs::kDailyHttpOriginalContentLengthApplication);
  pref_service_->ClearPref(prefs::kDailyHttpOriginalContentLengthVideo);
  pref_service_->ClearPref(prefs::kDailyHttpOriginalContentLengthUnknown);
  pref_service_->ClearPref(prefs::kDailyHttpReceivedContentLengthApplication);
  pref_service_->ClearPref(prefs::kDailyHttpReceivedContentLengthVideo);
  pref_service_->ClearPref(prefs::kDailyHttpReceivedContentLengthUnknown);

  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthViaDataReductionProxyApplication);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthViaDataReductionProxyVideo);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthViaDataReductionProxyUnknown);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthViaDataReductionProxyApplication);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthViaDataReductionProxyVideo);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthViaDataReductionProxyUnknown);

  pref_service_->ClearPref(
      prefs::
          kDailyOriginalContentLengthWithDataReductionProxyEnabledApplication);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabledUnknown);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthWithDataReductionProxyEnabledApplication);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthWithDataReductionProxyEnabledVideo);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthWithDataReductionProxyEnabledUnknown);

  pref_service_->ClearPref(
      prefs::kDailyContentLengthHttpsWithDataReductionProxyEnabled);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthUnknownWithDataReductionProxyEnabled);
  pref_service_->ClearPref(prefs::kDailyContentLengthViaDataReductionProxy);
  pref_service_->ClearPref(
      prefs::kDailyContentLengthWithDataReductionProxyEnabled);
  pref_service_->ClearPref(prefs::kDailyHttpOriginalContentLength);
  pref_service_->ClearPref(prefs::kDailyHttpReceivedContentLength);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthViaDataReductionProxy);
  pref_service_->ClearPref(
      prefs::kDailyOriginalContentLengthWithDataReductionProxyEnabled);

  for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end();
       ++iter) {
    iter->second->Clear();
  }

  RecordSavingsClearedMetric(reason);
}

void DataReductionProxyCompressionStats::DelayedWritePrefs() {
  if (pref_writer_timer_.IsRunning())
    return;

  pref_writer_timer_.Start(FROM_HERE, delay_, this,
                           &DataReductionProxyCompressionStats::WritePrefs);
}

void DataReductionProxyCompressionStats::TransferList(
    const base::ListValue& from_list,
    base::ListValue* to_list) {
  to_list->Clear();
  from_list.CreateDeepCopy()->Swap(to_list);
}

void DataReductionProxyCompressionStats::RecordRequestSizePrefs(
    int64_t data_used,
    int64_t original_size,
    bool with_data_saver_enabled,
    DataReductionProxyRequestType request_type,
    const std::string& mime_type,
    const base::Time& now) {
  // TODO(bengr): Remove this check once the underlying cause of
  // http://crbug.com/287821 is fixed. For now, only continue if the current
  // year is reported as being between 1972 and 2970.
  base::TimeDelta time_since_unix_epoch = now - base::Time::UnixEpoch();
  const int kMinDaysSinceUnixEpoch = 365 * 2;  // 2 years.
  const int kMaxDaysSinceUnixEpoch = 365 * 1000;  // 1000 years.
  if (time_since_unix_epoch.InDays() < kMinDaysSinceUnixEpoch ||
      time_since_unix_epoch.InDays() > kMaxDaysSinceUnixEpoch) {
    return;
  }

  // Determine how many days it has been since the last update.
  int64_t then_internal = GetInt64(
      data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate);

  // Local midnight could have been shifted due to time zone change.
  // If time is null then don't care if midnight will be wrong shifted due to
  // time zone change because it's still too much time ago.
  base::Time then_midnight = base::Time::FromInternalValue(then_internal);
  if (!then_midnight.is_null()) {
    then_midnight = then_midnight.LocalMidnight();
  }
  base::Time midnight = now.LocalMidnight();

  DailyDataSavingUpdate total(
      this, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength,
      data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
  DailyDataSavingUpdate proxy_enabled(
      this, data_reduction_proxy::prefs::
                kDailyOriginalContentLengthWithDataReductionProxyEnabled,
      data_reduction_proxy::prefs::
          kDailyContentLengthWithDataReductionProxyEnabled);
  DailyDataSavingUpdate via_proxy(
      this, data_reduction_proxy::prefs::
                kDailyOriginalContentLengthViaDataReductionProxy,
      data_reduction_proxy::prefs::kDailyContentLengthViaDataReductionProxy);
  DailyContentLengthUpdate https(
      this, data_reduction_proxy::prefs::
                kDailyContentLengthHttpsWithDataReductionProxyEnabled);
  DailyContentLengthUpdate short_bypass(
      this, data_reduction_proxy::prefs::
                kDailyContentLengthShortBypassWithDataReductionProxyEnabled);
  DailyContentLengthUpdate long_bypass(
      this, data_reduction_proxy::prefs::
                kDailyContentLengthLongBypassWithDataReductionProxyEnabled);
  DailyContentLengthUpdate unknown(
      this, data_reduction_proxy::prefs::
                kDailyContentLengthUnknownWithDataReductionProxyEnabled);

  int days_since_last_update = (midnight - then_midnight).InDays();
  if (days_since_last_update) {
    // Record the last update time in microseconds in UTC.
    SetInt64(data_reduction_proxy::prefs::kDailyHttpContentLengthLastUpdateDate,
             midnight.ToInternalValue());

    // A new day. Report the previous day's data if exists. We'll lose usage
    // data if the last time Chrome was run was more than a day ago.
    // Here, we prefer collecting less data but the collected data is
    // associated with an accurate date.
    if (days_since_last_update == 1) {
      RecordDailyContentLengthHistograms(
          total.GetOriginalListPrefValue(kNumDaysInHistory - 1),
          total.GetReceivedListPrefValue(kNumDaysInHistory - 1),
          proxy_enabled.GetOriginalListPrefValue(kNumDaysInHistory - 1),
          proxy_enabled.GetReceivedListPrefValue(kNumDaysInHistory - 1),
          via_proxy.GetOriginalListPrefValue(kNumDaysInHistory - 1),
          via_proxy.GetReceivedListPrefValue(kNumDaysInHistory - 1),
          https.GetListPrefValue(kNumDaysInHistory - 1),
          short_bypass.GetListPrefValue(kNumDaysInHistory - 1),
          long_bypass.GetListPrefValue(kNumDaysInHistory - 1),
          unknown.GetListPrefValue(kNumDaysInHistory - 1));

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyHttpOriginalContentLengthApplication,
          "Net.DailyOriginalContentLength_Application");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyHttpReceivedContentLengthApplication,
          "Net.DailyReceivedContentLength_Application");

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthVideo,
          "Net.DailyOriginalContentLength_Video");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthVideo,
          "Net.DailyContentLength_Video");

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthUnknown,
          "Net.DailyOriginalContentLength_UnknownMime");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthUnknown,
          "Net.DailyContentLength_UnknownMime");

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledApplication,
          "Net.DailyOriginalContentLength_DataReductionProxyEnabled_"
          "Application");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthWithDataReductionProxyEnabledApplication,
          "Net.DailyContentLength_DataReductionProxyEnabled_Application");

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo,
          "Net.DailyOriginalContentLength_DataReductionProxyEnabled_Video");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthWithDataReductionProxyEnabledVideo,
          "Net.DailyContentLength_DataReductionProxyEnabled_Video");
      int64_t original_length_with_data_reduction_enabled_video = GetInt64(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo);
      if (original_length_with_data_reduction_enabled_video > 0) {
        int64_t received_length_with_data_reduction_enabled_video =
            GetInt64(data_reduction_proxy::prefs::
                         kDailyContentLengthWithDataReductionProxyEnabledVideo);
        int percent_data_reduction_proxy_enabled_video = 0;
        // UMA percentage cannot be negative.
        // The DataReductionProxy server will only serve optimized video content
        // if the optimized content is smaller than the original content.
        // TODO(ryansturm): Track daily data inflation percents here and
        // elsewhere. http://crbug.com/595818
        if (original_length_with_data_reduction_enabled_video >
            received_length_with_data_reduction_enabled_video) {
          percent_data_reduction_proxy_enabled_video =
              100 * (original_length_with_data_reduction_enabled_video -
                     received_length_with_data_reduction_enabled_video) /
              original_length_with_data_reduction_enabled_video;
        }
        UMA_HISTOGRAM_PERCENTAGE(
            "Net.DailyContentSavingPercent_DataReductionProxyEnabled_Video",
            percent_data_reduction_proxy_enabled_video);
      }

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledUnknown,
          "Net.DailyOriginalContentLength_DataReductionProxyEnabled_"
          "UnknownMime");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthWithDataReductionProxyEnabledUnknown,
          "Net.DailyContentLength_DataReductionProxyEnabled_UnknownMime")

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthViaDataReductionProxyApplication,
          "Net.DailyOriginalContentLength_ViaDataReductionProxy_Application");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthViaDataReductionProxyApplication,
          "Net.DailyContentLength_ViaDataReductionProxy_Application");

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthViaDataReductionProxyVideo,
          "Net.DailyOriginalContentLength_ViaDataReductionProxy_Video");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthViaDataReductionProxyVideo,
          "Net.DailyContentLength_ViaDataReductionProxy_Video");
      int64_t original_length_via_data_reduction_proxy_video =
          GetInt64(data_reduction_proxy::prefs::
                       kDailyOriginalContentLengthViaDataReductionProxyVideo);
      if (original_length_via_data_reduction_proxy_video > 0) {
        int64_t received_length_via_data_reduction_proxy_video =
            GetInt64(data_reduction_proxy::prefs::
                         kDailyContentLengthViaDataReductionProxyVideo);
        int percent_via_data_reduction_proxy_video = 0;
        // UMA percentage cannot be negative.
        // The DataReductionProxy server will only serve optimized video content
        // if the optimized content is smaller than the original content.
        // TODO(ryansturm): Track daily data inflation percents here and
        // elsewhere. http://crbug.com/595818
        if (original_length_via_data_reduction_proxy_video >
            received_length_via_data_reduction_proxy_video) {
          percent_via_data_reduction_proxy_video =
              100 * (original_length_via_data_reduction_proxy_video -
                     received_length_via_data_reduction_proxy_video) /
              original_length_via_data_reduction_proxy_video;
        }
        UMA_HISTOGRAM_PERCENTAGE(
            "Net.DailyContentSavingPercent_ViaDataReductionProxy_Video",
            percent_via_data_reduction_proxy_video);
      }

      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthViaDataReductionProxyUnknown,
          "Net.DailyOriginalContentLength_ViaDataReductionProxy_UnknownMime");
      RECORD_INT64PREF_TO_HISTOGRAM(
          data_reduction_proxy::prefs::
              kDailyContentLengthViaDataReductionProxyUnknown,
          "Net.DailyContentLength_ViaDataReductionProxy_UnknownMime");
    }

    if (days_since_last_update < -1) {
      RecordSavingsClearedMetric(
          DataReductionProxySavingsClearedReason::SYSTEM_CLOCK_MOVED_BACK);
    }

    // The system may go backwards in time by up to a day for legitimate
    // reasons, such as with changes to the time zone. In such cases, we
    // keep adding to the current day which is why we check for
    // |days_since_last_update != -1|.
    // Note: we accept the fact that some reported data is shifted to
    // the adjacent day if users travel back and forth across time zones.
    if (days_since_last_update && (days_since_last_update != -1)) {
      if (days_since_last_update < -1) {
        pref_service_->SetInt64(
            prefs::kDataReductionProxySavingsClearedNegativeSystemClock,
            now.ToInternalValue());
      }
      SetInt64(data_reduction_proxy::prefs::
                   kDailyHttpOriginalContentLengthApplication,
               0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyHttpReceivedContentLengthApplication,
               0);

      SetInt64(
          data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthVideo, 0);
      SetInt64(
          data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthVideo, 0);

      SetInt64(
          data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthUnknown,
          0);
      SetInt64(
          data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthUnknown,
          0);

      SetInt64(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledApplication,
          0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthWithDataReductionProxyEnabledApplication,
               0);

      SetInt64(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo,
          0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthWithDataReductionProxyEnabledVideo,
               0);

      SetInt64(
          data_reduction_proxy::prefs::
              kDailyOriginalContentLengthWithDataReductionProxyEnabledUnknown,
          0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthWithDataReductionProxyEnabledUnknown,
               0);

      SetInt64(data_reduction_proxy::prefs::
                   kDailyOriginalContentLengthViaDataReductionProxyApplication,
               0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthViaDataReductionProxyApplication,
               0);

      SetInt64(data_reduction_proxy::prefs::
                   kDailyOriginalContentLengthViaDataReductionProxyVideo,
               0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthViaDataReductionProxyVideo,
               0);

      SetInt64(data_reduction_proxy::prefs::
                   kDailyOriginalContentLengthViaDataReductionProxyUnknown,
               0);
      SetInt64(data_reduction_proxy::prefs::
                   kDailyContentLengthViaDataReductionProxyUnknown,
               0);
    }
  }

  total.UpdateForDateChange(days_since_last_update);
  proxy_enabled.UpdateForDateChange(days_since_last_update);
  via_proxy.UpdateForDateChange(days_since_last_update);
  https.UpdateForDateChange(days_since_last_update);
  short_bypass.UpdateForDateChange(days_since_last_update);
  long_bypass.UpdateForDateChange(days_since_last_update);
  unknown.UpdateForDateChange(days_since_last_update);

  total.Add(original_size, data_used);
  if (with_data_saver_enabled) {
    proxy_enabled.Add(original_size, data_used);
    // Ignore data source cases, if exist, when
    // "with_data_saver_enabled == false"
    switch (request_type) {
      case VIA_DATA_REDUCTION_PROXY:
        via_proxy.Add(original_size, data_used);
        break;
      case HTTPS:
        https.Add(data_used);
        break;
      case SHORT_BYPASS:
        short_bypass.Add(data_used);
        break;
      case LONG_BYPASS:
        long_bypass.Add(data_used);
        break;
      case UPDATE:
      case DIRECT_HTTP:
        // Don't record any request level prefs. If this is an update, this data
        // was already recorded at the URLRequest level. Updates are generally
        // page load level optimizations and don't correspond to request types.
        return;
      case UNKNOWN_TYPE:
        unknown.Add(data_used);
        break;
      default:
        NOTREACHED();
    }
  }

  bool via_data_reduction_proxy = request_type == VIA_DATA_REDUCTION_PROXY;
  bool is_application = net::MatchesMimeType("application/*", mime_type);
  bool is_video = net::MatchesMimeType("video/*", mime_type);
  bool is_mime_type_empty = mime_type.empty();
  if (is_application) {
    IncrementDailyUmaPrefs(
        original_size, data_used,
        data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthApplication,
        data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthApplication,
        with_data_saver_enabled,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthWithDataReductionProxyEnabledApplication,
        data_reduction_proxy::prefs::
            kDailyContentLengthWithDataReductionProxyEnabledApplication,
        via_data_reduction_proxy,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthViaDataReductionProxyApplication,
        data_reduction_proxy::prefs::
            kDailyContentLengthViaDataReductionProxyApplication);
  } else if (is_video) {
    IncrementDailyUmaPrefs(
        original_size, data_used,
        data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthVideo,
        data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthVideo,
        with_data_saver_enabled,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthWithDataReductionProxyEnabledVideo,
        data_reduction_proxy::prefs::
            kDailyContentLengthWithDataReductionProxyEnabledVideo,
        via_data_reduction_proxy,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthViaDataReductionProxyVideo,
        data_reduction_proxy::prefs::
            kDailyContentLengthViaDataReductionProxyVideo);
  } else if (is_mime_type_empty) {
    IncrementDailyUmaPrefs(
        original_size, data_used,
        data_reduction_proxy::prefs::kDailyHttpOriginalContentLengthUnknown,
        data_reduction_proxy::prefs::kDailyHttpReceivedContentLengthUnknown,
        with_data_saver_enabled,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthWithDataReductionProxyEnabledUnknown,
        data_reduction_proxy::prefs::
            kDailyContentLengthWithDataReductionProxyEnabledUnknown,
        via_data_reduction_proxy,
        data_reduction_proxy::prefs::
            kDailyOriginalContentLengthViaDataReductionProxyUnknown,
        data_reduction_proxy::prefs::
            kDailyContentLengthViaDataReductionProxyUnknown);
  }
}

void DataReductionProxyCompressionStats::IncrementDailyUmaPrefs(
    int64_t original_size,
    int64_t received_size,
    const char* original_size_pref,
    const char* received_size_pref,
    bool data_reduction_proxy_enabled,
    const char* original_size_with_proxy_enabled_pref,
    const char* recevied_size_with_proxy_enabled_pref,
    bool via_data_reduction_proxy,
    const char* original_size_via_proxy_pref,
    const char* received_size_via_proxy_pref) {
  IncreaseInt64Pref(original_size_pref, original_size);
  IncreaseInt64Pref(received_size_pref, received_size);

  if (data_reduction_proxy_enabled) {
    IncreaseInt64Pref(original_size_with_proxy_enabled_pref, original_size);
    IncreaseInt64Pref(recevied_size_with_proxy_enabled_pref, received_size);
  }

  if (via_data_reduction_proxy) {
    IncreaseInt64Pref(original_size_via_proxy_pref, original_size);
    IncreaseInt64Pref(received_size_via_proxy_pref, received_size);
  }
}

void DataReductionProxyCompressionStats::RecordDataUseByHost(
    const std::string& data_usage_host,
    int64_t data_used,
    int64_t original_size,
    const base::Time time) {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (current_data_usage_load_status_ != LOADED)
    return;

  DCHECK(data_usage_reporting_enabled_.GetValue());

  if (!DataUsageStore::AreInSameInterval(data_usage_map_last_updated_, time)) {
    PersistDataUsage();
    data_usage_map_.clear();
    data_usage_map_last_updated_ = base::Time();
  }

  std::string normalized_host = NormalizeHostname(data_usage_host);
  auto j = data_usage_map_.insert(
      std::make_pair(normalized_host, std::make_unique<PerSiteDataUsage>()));
  PerSiteDataUsage* per_site_usage = j.first->second.get();
  per_site_usage->set_hostname(normalized_host);
  per_site_usage->set_original_size(per_site_usage->original_size() +
                                    original_size);
  per_site_usage->set_data_used(per_site_usage->data_used() + data_used);

  data_usage_map_last_updated_ = time;
  data_usage_map_is_dirty_ = true;
}

void DataReductionProxyCompressionStats::PersistDataUsage() {
  DCHECK(current_data_usage_load_status_ == LOADED);

  if (data_usage_map_is_dirty_) {
    std::unique_ptr<DataUsageBucket> data_usage_bucket(new DataUsageBucket());
    data_usage_bucket->set_last_updated_timestamp(
        data_usage_map_last_updated_.ToInternalValue());
    PerConnectionDataUsage* connection_usage =
        data_usage_bucket->add_connection_usage();
    for (auto i = data_usage_map_.begin(); i != data_usage_map_.end(); ++i) {
        PerSiteDataUsage* per_site_usage = connection_usage->add_site_usage();
        per_site_usage->CopyFrom(*(i->second.get()));
    }
    service_->StoreCurrentDataUsageBucket(std::move(data_usage_bucket));
  }

  data_usage_map_is_dirty_ = false;
}

void DataReductionProxyCompressionStats::DeleteHistoricalDataUsage() {
  // This method does not support being called in |LOADING| status since this
  // means that the in-memory data usage will get populated when data usage
  // loads, which will undo the clear below. This method is called when users
  // click on the "Clear Data" button, or when user deletes the extension. In
  // both cases, enough time has passed since startup to load current data
  // usage. Technically, this could occur, and will have the effect of not
  // clearing data from the current bucket.
  // TODO(kundaji): Use cancellable tasks and remove this DCHECK.
  DCHECK(current_data_usage_load_status_ != LOADING);

  data_usage_map_.clear();
  data_usage_map_last_updated_ = base::Time();
  data_usage_map_is_dirty_ = false;

  service_->DeleteHistoricalDataUsage();
}

void DataReductionProxyCompressionStats::GetHistoricalDataUsageImpl(
    const HistoricalDataUsageCallback& get_data_usage_callback,
    const base::Time& now) {
#if !defined(OS_ANDROID)
  if (current_data_usage_load_status_ != LOADED) {
    // If current data usage has not yet loaded, we return an empty array. The
    // extension can retry after a slight delay.
    // This use case is unlikely to occur in practice since current data usage
    // should have sufficient time to load before user tries to view data usage.
    get_data_usage_callback.Run(
        std::make_unique<std::vector<DataUsageBucket>>());
    return;
  }
#endif

  if (current_data_usage_load_status_ == LOADED)
    PersistDataUsage();

  if (!data_usage_map_last_updated_.is_null() &&
      !DataUsageStore::AreInSameInterval(data_usage_map_last_updated_, now)) {
    data_usage_map_.clear();
    data_usage_map_last_updated_ = base::Time();

    // Force the last bucket to be for the current interval.
    std::unique_ptr<DataUsageBucket> data_usage_bucket(new DataUsageBucket());
    data_usage_bucket->set_last_updated_timestamp(now.ToInternalValue());
    service_->StoreCurrentDataUsageBucket(std::move(data_usage_bucket));
  }

  service_->LoadHistoricalDataUsage(get_data_usage_callback);
}

void DataReductionProxyCompressionStats::OnDataUsageReportingPrefChanged() {
  if (data_usage_reporting_enabled_.GetValue()) {
    if (current_data_usage_load_status_ == NOT_LOADED) {
      current_data_usage_load_status_ = LOADING;
      service_->LoadCurrentDataUsageBucket(base::Bind(
          &DataReductionProxyCompressionStats::OnCurrentDataUsageLoaded,
          weak_factory_.GetWeakPtr()));
    }
  } else {
// Don't delete the historical data on Android, but clear the map.
#if defined(OS_ANDROID)
    if (current_data_usage_load_status_ == LOADED)
      PersistDataUsage();

    data_usage_map_.clear();
    data_usage_map_last_updated_ = base::Time();
    data_usage_map_is_dirty_ = false;
#else
    DeleteHistoricalDataUsage();
#endif
    current_data_usage_load_status_ = NOT_LOADED;
  }
}

void DataReductionProxyCompressionStats::InitializeWeeklyAggregateDataUse(
    const base::Time& now) {
#if defined(OS_ANDROID) && defined(ARCH_CPU_X86)
  // TODO(rajendrant): Enable aggregate metrics recording in x86 Android.
  // http://crbug.com/865373
  return;
#endif

  MaybeInitWeeklyAggregateDataUsePrefs(now, pref_service_);
  // Record the histograms that will show up in the user feedback.
  RecordDictionaryToHistogram(
      "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Background",
      pref_service_->GetDictionary(
          prefs::kThisWeekServicesDownstreamBackgroundKB));
  RecordDictionaryToHistogram(
      "DataReductionProxy.ThisWeekAggregateKB.Services.Downstream.Foreground",
      pref_service_->GetDictionary(
          prefs::kThisWeekServicesDownstreamForegroundKB));
  RecordDictionaryToHistogram(
      "DataReductionProxy.ThisWeekAggregateKB.UserTraffic.Downstream."
      "ContentType",
      pref_service_->GetDictionary(
          prefs::kThisWeekUserTrafficContentTypeDownstreamKB));
  RecordDictionaryToHistogram(
      "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Background",
      pref_service_->GetDictionary(
          prefs::kLastWeekServicesDownstreamBackgroundKB));
  RecordDictionaryToHistogram(
      "DataReductionProxy.LastWeekAggregateKB.Services.Downstream.Foreground",
      pref_service_->GetDictionary(
          prefs::kLastWeekServicesDownstreamForegroundKB));
  RecordDictionaryToHistogram(
      "DataReductionProxy.LastWeekAggregateKB.UserTraffic.Downstream."
      "ContentType",
      pref_service_->GetDictionary(
          prefs::kLastWeekUserTrafficContentTypeDownstreamKB));
}

void DataReductionProxyCompressionStats::RecordWeeklyAggregateDataUse(
    const base::Time& now,
    int32_t data_used_kb,
    bool is_user_request,
    data_use_measurement::DataUseUserData::DataUseContentType content_type,
    int32_t service_hash_code) {
#if defined(OS_ANDROID) && defined(ARCH_CPU_X86)
  // TODO(rajendrant): Enable aggregate metrics recording in x86 Android.
  // http://crbug.com/865373
  return;
#endif
  // Update the prefs if this is a new week. This can happen when chrome is open
  // for weeks without being closed.
  MaybeInitWeeklyAggregateDataUsePrefs(now, pref_service_);
  if (is_user_request) {
    AddToDictionaryPref(pref_service_,
                        prefs::kThisWeekUserTrafficContentTypeDownstreamKB,
                        content_type, data_used_kb);
  } else {
    bool is_app_foreground = true;
#if defined(OS_ANDROID)
    is_app_foreground = base::android::ApplicationStatusListener::GetState() ==
                        base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES;
#endif
    if (is_app_foreground) {
      AddToDictionaryPref(pref_service_,
                          prefs::kThisWeekServicesDownstreamForegroundKB,
                          service_hash_code, data_used_kb);
    } else {
      AddToDictionaryPref(pref_service_,
                          prefs::kThisWeekServicesDownstreamBackgroundKB,
                          service_hash_code, data_used_kb);
    }
  }
}

// static
std::string DataReductionProxyCompressionStats::NormalizeHostname(
    const std::string& host) {
  size_t pos = host.find("://");
  if (pos != std::string::npos)
    return host.substr(pos + 3);

  return host;
}

}  // namespace data_reduction_proxy