summaryrefslogtreecommitdiffstats
path: root/net/http/infinite_cache.cc
blob: 4ebbd1e5c8c5000308727cef5ca5b29dba88e728 (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
// Copyright (c) 2012 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 "net/http/infinite_cache.h"

#include <algorithm>

#include "base/compiler_specific.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/hash.h"
#include "base/hash_tables.h"
#include "base/location.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/histogram.h"
#include "base/pickle.h"
#include "base/platform_file.h"
#include "base/rand_util.h"
#include "base/sha1.h"
#include "base/time.h"
#include "base/threading/sequenced_worker_pool.h"
#include "net/base/net_errors.h"
#include "net/http/http_cache_transaction.h"
#include "net/http/http_request_info.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_response_info.h"
#include "net/http/http_util.h"
#include "third_party/zlib/zlib.h"

using base::PlatformFile;
using base::Time;
using base::TimeDelta;

namespace {

// Flags to use with a particular resource.
enum Flags {
  NO_CACHE = 1 << 0,
  NO_STORE = 1 << 1,
  EXPIRED = 1 << 2,
  TRUNCATED = 1 << 3,
  RESUMABLE = 1 << 4,
  REVALIDATEABLE = 1 << 5,
  DOOM_METHOD = 1 << 6,
  CACHED = 1 << 7
};

const int kKeySizeBytes = 20;
COMPILE_ASSERT(base::kSHA1Length == static_cast<unsigned>(kKeySizeBytes),
               invalid_key_length);
struct Key {
  char value[kKeySizeBytes];
};

// The actual data that we store for every resource.
struct Details {
  int32 expiration;
  int32 last_access;
  uint16 flags;
  uint8 use_count;
  uint8 update_count;
  uint32 vary_hash;
  int32 headers_size;
  int32 response_size;
  uint32 headers_hash;
  uint32 response_hash;
};
const size_t kRecordSize = sizeof(Key) + sizeof(Details);

// Some constants related to the database file.
uint32 kMagicSignature = 0x1f00cace;
uint32 kCurrentVersion = 0x10001;

// Basic limits for the experiment.
int kMaxNumEntries = 200 * 1000;
int kMaxTrackingSize = 40 * 1024 * 1024;

// Settings that control how we generate histograms.
int kTimerMinutes = 5;
int kReportSizeStep = 100 * 1024 * 1024;

// Buffer to read and write the file.
const size_t kBufferSize = 1024 * 1024;
const size_t kMaxRecordsToRead = kBufferSize / kRecordSize;
COMPILE_ASSERT(kRecordSize * kMaxRecordsToRead < kBufferSize, wrong_buffer);

// Functor for operator <.
struct Key_less {
  bool operator()(const Key& left, const Key& right) const {
    // left < right.
    return (memcmp(left.value, right.value, kKeySizeBytes) < 0);
  }
};

// Functor for operator ==.
struct Key_eq {
  bool operator()(const Key& left, const Key& right) const {
    return (memcmp(left.value, right.value, kKeySizeBytes) == 0);
  }
};

// Simple adaptor for the sha1 interface.
void CryptoHash(std::string source, Key* destination) {
  base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(source.data()),
                      source.size(),
                      reinterpret_cast<unsigned char*>(destination->value));
}

// Simple adaptor for base::ReadPlatformFile.
bool ReadPlatformFile(PlatformFile file, size_t offset,
                      void* buffer, size_t buffer_len) {
  DCHECK_LE(offset, static_cast<size_t>(kuint32max));
  int bytes = base::ReadPlatformFile(file, static_cast<int64>(offset),
                                     reinterpret_cast<char*>(buffer),
                                     static_cast<int>(buffer_len));
  return (bytes == static_cast<int>(buffer_len));
}

// Simple adaptor for base::WritePlatformFile.
bool WritePlatformFile(PlatformFile file, size_t offset,
                       const void* buffer, size_t buffer_len) {
  DCHECK_LE(offset, static_cast<size_t>(kuint32max));
  int bytes = base::WritePlatformFile(file, static_cast<int64>(offset),
                                      reinterpret_cast<const char*>(buffer),
                                      static_cast<int>(buffer_len));
  return (bytes == static_cast<int>(buffer_len));
}

// 1 second resolution, +- 68 years from the baseline.
int32 TimeToInt(Time time) {
  int64 seconds = (time - Time::UnixEpoch()).InSeconds();
  if (seconds > kint32max)
    seconds = kint32max;
  if (seconds < kint32min)
    seconds = kint32min;
  return static_cast<int32>(seconds);
}

Time IntToTime(int32 time) {
  return Time::UnixEpoch() + TimeDelta::FromSeconds(time);
}

int32 GetExpiration(const net::HttpResponseInfo* response) {
  TimeDelta freshness =
      response->headers->GetFreshnessLifetime(response->response_time);

  // Avoid overflow when adding to current time.
  if (freshness.InDays() > 365 * 10)
    freshness = TimeDelta::FromDays(365 * 10);
  return TimeToInt(response->response_time + freshness);
}

uint32 GetCacheability(const net::HttpResponseInfo* response) {
  uint32 cacheability = 0;
  const net::HttpResponseHeaders* headers = response->headers;
  if (headers->HasHeaderValue("cache-control", "no-cache") ||
      headers->HasHeaderValue("pragma", "no-cache") ||
      headers->HasHeaderValue("vary", "*")) {
    cacheability |= NO_CACHE;
  }

  if (headers->HasHeaderValue("cache-control", "no-store"))
    cacheability |= NO_STORE;

  TimeDelta max_age;
  if (headers->GetMaxAgeValue(&max_age) && max_age.InSeconds() <= 0)
    cacheability |= NO_CACHE;

  return cacheability;
}

uint32 GetRevalidationFlags(const net::HttpResponseInfo* response) {
  uint32 revalidation = 0;
  std::string etag;
  response->headers->EnumerateHeader(NULL, "etag", &etag);

  std::string last_modified;
  response->headers->EnumerateHeader(NULL, "last-modified", &last_modified);

  if (!etag.empty() || !last_modified.empty())
    revalidation = REVALIDATEABLE;

  if (response->headers->HasStrongValidators())
    revalidation = RESUMABLE;

  return revalidation;
}


uint32 GetVaryHash(const net::HttpResponseInfo* response) {
  if (!response->vary_data.is_valid())
    return 0;

  uint32 hash = adler32(0, Z_NULL, 0);
  Pickle pickle;
  response->vary_data.Persist(&pickle);
  return adler32(hash, reinterpret_cast<const Bytef*>(pickle.data()),
                 pickle.size());
}

// Adaptor for PostTaskAndReply.
void OnComplete(const net::CompletionCallback& callback, int* result) {
  callback.Run(*result);
}

}  // namespace

namespace BASE_HASH_NAMESPACE {
#if defined(COMPILER_MSVC)
inline size_t hash_value(const Key& key) {
  return base::Hash(key.value, kKeySizeBytes);
}
#elif defined(COMPILER_GCC)
template <>
struct hash<Key> {
  size_t operator()(const Key& key) const {
    return base::Hash(key.value, kKeySizeBytes);
  }
};
#endif

}  // BASE_HASH_NAMESPACE

namespace net {

struct InfiniteCacheTransaction::ResourceData {
  ResourceData() {
    memset(this, 0, sizeof(*this));
  }

  Key key;
  Details details;
};

InfiniteCacheTransaction::InfiniteCacheTransaction(InfiniteCache* cache)
    : cache_(cache->AsWeakPtr()), must_doom_entry_(false) {
}

InfiniteCacheTransaction::~InfiniteCacheTransaction() {
  Finish();
}

void InfiniteCacheTransaction::OnRequestStart(const HttpRequestInfo* request) {
  if (!cache_)
    return;

  std::string method = request->method;
  if (method == "POST" || method == "DELETE" || method == "PUT") {
    must_doom_entry_ = true;
  } else if (method != "GET") {
    cache_.reset();
    return;
  }

  resource_data_.reset(new ResourceData);
  CryptoHash(cache_->GenerateKey(request), &resource_data_->key);
}

void InfiniteCacheTransaction::OnResponseReceived(
    const HttpResponseInfo* response) {
  if (!cache_)
    return;

  Details& details = resource_data_->details;

  details.expiration = GetExpiration(response);
  details.last_access = TimeToInt(response->request_time);
  details.flags = GetCacheability(response);
  details.vary_hash = GetVaryHash(response);
  details.response_hash = adler32(0, Z_NULL, 0);  // Init the hash.

  if (!details.flags &&
      TimeToInt(response->response_time) == details.expiration) {
    details.flags = EXPIRED;
  }
  details.flags |= GetRevalidationFlags(response);

  if (must_doom_entry_)
    details.flags |= DOOM_METHOD;

  Pickle pickle;
  response->Persist(&pickle, true, false);  // Skip transient headers.
  details.headers_size = pickle.size();
  details.headers_hash = adler32(0, Z_NULL, 0);
  details.headers_hash = adler32(details.headers_hash,
                                 reinterpret_cast<const Bytef*>(pickle.data()),
                                 pickle.size());
}

void InfiniteCacheTransaction::OnDataRead(const char* data, int data_len) {
  if (!cache_)
    return;

  if (!data_len)
    return Finish();

  if (data_len < 0)
    return OnTruncatedResponse();

  resource_data_->details.response_size += data_len;

  resource_data_->details.response_hash =
    adler32(resource_data_->details.response_hash,
            reinterpret_cast<const Bytef*>(data), data_len);
}

void InfiniteCacheTransaction::OnTruncatedResponse() {
  if (!cache_)
    return;

  resource_data_->details.flags |= TRUNCATED;
}

void InfiniteCacheTransaction::OnServedFromCache() {
  if (!cache_)
    return;

  resource_data_->details.flags |= CACHED;
}

void InfiniteCacheTransaction::Finish() {
  if (!cache_ || !resource_data_.get())
    return;

  if (!resource_data_->details.headers_size)
    return;

  cache_->ProcessResource(resource_data_.Pass());
  cache_.reset();
}

// ----------------------------------------------------------------------------

// This is the object that performs the bulk of the work.
// InfiniteCacheTransaction posts the transaction data to the InfiniteCache, and
// the InfiniteCache basically just forward requests to the Worker for actual
// processing.
// The Worker lives on a worker thread (basically a dedicated worker pool with
// only one thread), and flushes data to disk once every five minutes, when it
// is notified by the InfiniteCache.
// In general, there are no callbacks on completion of tasks, and the Worker can
// be as behind as it has to when processing requests.
class InfiniteCache::Worker : public base::RefCountedThreadSafe<Worker> {
 public:
  Worker() : init_(false), flushed_(false) {}

  // Construction and destruction helpers.
  void Init(const FilePath& path);
  void Cleanup();

  // Deletes all tracked data.
  void DeleteData(int* result);

  // Deletes requests between |initial_time| and |end_time|.
  void DeleteDataBetween(base::Time initial_time,
                         base::Time end_time,
                         int* result);

  // Performs the actual processing of a new transaction. Takes ownership of
  // the transaction |data|.
  void Process(scoped_ptr<InfiniteCacheTransaction::ResourceData> data);

  // Test helpers.
  void Query(int* result);
  void Flush(int* result);

  // Timer notification.
  void OnTimer();

 private:
  friend class base::RefCountedThreadSafe<Worker>;
#if defined(COMPILER_MSVC)
  typedef BASE_HASH_NAMESPACE::hash_map<
      Key, Details, BASE_HASH_NAMESPACE::hash_compare<Key, Key_less> > KeyMap;
#elif defined(COMPILER_GCC)
  typedef BASE_HASH_NAMESPACE::hash_map<
      Key, Details, BASE_HASH_NAMESPACE::hash<Key>, Key_eq> KeyMap;
#endif

  // Header for the data file. The file starts with the header, followed by
  // all the records, and a data hash at the end (just of the records, not the
  // header). Note that the header has a dedicated hash.
  struct Header {
    uint32 magic;
    uint32 version;
    int32 num_entries;
    int32 generation;
    uint64 creation_time;
    uint64 update_time;
    int64 total_size;
    int64 size_last_report;
    int32 use_minutes;
    int32 num_hits;
    int32 num_bad_hits;
    int32 num_requests;
    int32 disabled;
    uint32 header_hash;
  };

  ~Worker() {}

  // Methods to load and store data on disk.
  void LoadData();
  void StoreData();
  void InitializeData();
  bool ReadData(PlatformFile file);
  bool WriteData(PlatformFile file);
  bool ReadAndVerifyHeader(PlatformFile file);

  // Book-keeping methods.
  void Add(const Details& details);
  void Remove(const Details& details);
  void UpdateSize(int old_size, int new_size);

  // Bulk of report generation methods.
  void RecordHit(const Details& old, Details* details);
  void RecordUpdate(const Details& old, Details* details);
  void GenerateHistograms();

  // Cache logic methods.
  bool CanReuse(const Details& old, const Details& current);
  bool DataChanged(const Details& old, const Details& current);
  bool HeadersChanged(const Details& old, const Details& current);

  KeyMap map_;
  bool init_;
  bool flushed_;
  scoped_ptr<Header> header_;
  FilePath path_;

  DISALLOW_COPY_AND_ASSIGN(Worker);
};

void InfiniteCache::Worker::Init(const FilePath& path) {
  path_ = path;
  LoadData();
}

void InfiniteCache::Worker::Cleanup() {
  if (init_)
    StoreData();

  map_.clear();
}

void InfiniteCache::Worker::DeleteData(int* result) {
  if (!init_)
    return;

  map_.clear();
  InitializeData();
  file_util::Delete(path_, false);
  *result = OK;
  UMA_HISTOGRAM_BOOLEAN("InfiniteCache.DeleteAll", true);
}

void InfiniteCache::Worker::DeleteDataBetween(base::Time initial_time,
                                              base::Time end_time,
                                              int* result) {
  if (!init_)
    return;

  for (KeyMap::iterator i = map_.begin(); i != map_.end();) {
    Time last_access = IntToTime(i->second.last_access);
    if (last_access >= initial_time && last_access <= end_time) {
      KeyMap::iterator next = i;
      ++next;
      Remove(i->second);
      map_.erase(i);
      i = next;
      continue;
    }
    ++i;
  }

  file_util::Delete(path_, false);
  StoreData();
  *result = OK;
  UMA_HISTOGRAM_BOOLEAN("InfiniteCache.DeleteRange", true);
}

void InfiniteCache::Worker::Process(
    scoped_ptr<InfiniteCacheTransaction::ResourceData> data) {
  if (!init_)
    return;

  if (data->details.response_size > kMaxTrackingSize)
    return;

  if (header_->num_entries == kMaxNumEntries)
    return;

  header_->num_requests++;
  KeyMap::iterator i = map_.find(data->key);
  if (i != map_.end()) {
    if (data->details.flags & DOOM_METHOD) {
      Remove(i->second);
      map_.erase(i);
      return;
    }
    data->details.use_count = i->second.use_count;
    data->details.update_count = i->second.update_count;
    if (data->details.flags & CACHED) {
      RecordHit(i->second, &data->details);
    } else {
      bool reused = CanReuse(i->second, data->details);
      bool data_changed = DataChanged(i->second, data->details);
      bool headers_changed = HeadersChanged(i->second, data->details);

      if (reused && data_changed)
        header_->num_bad_hits++;

      if (reused)
        RecordHit(i->second, &data->details);

      if (headers_changed)
        UMA_HISTOGRAM_BOOLEAN("InfiniteCache.HeadersChange", true);

      if (data_changed)
        RecordUpdate(i->second, &data->details);
    }

    if (data->details.flags & NO_STORE) {
      Remove(i->second);
      map_.erase(i);
      return;
    }

    map_[data->key] = data->details;
    return;
  }

  if (data->details.flags & NO_STORE)
    return;

  if (data->details.flags & DOOM_METHOD)
    return;

  map_[data->key] = data->details;
  Add(data->details);
}

void InfiniteCache::Worker::LoadData() {
  if (path_.empty())
    return InitializeData();;

  PlatformFile file = base::CreatePlatformFile(
      path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, NULL, NULL);
  if (file == base::kInvalidPlatformFileValue)
    return InitializeData();
  if (!ReadData(file))
    InitializeData();
  base::ClosePlatformFile(file);
  if (header_->disabled)
    map_.clear();
}

void InfiniteCache::Worker::StoreData() {
  if (!init_ || flushed_ || path_.empty())
    return;

  header_->update_time = Time::Now().ToInternalValue();
  header_->generation++;
  header_->header_hash = base::Hash(
      reinterpret_cast<char*>(header_.get()), offsetof(Header, header_hash));

  FilePath temp_file = path_.ReplaceExtension(FILE_PATH_LITERAL("tmp"));
  PlatformFile file = base::CreatePlatformFile(
      temp_file, base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
      NULL, NULL);
  if (file == base::kInvalidPlatformFileValue)
    return;
  bool success = WriteData(file);
  base::ClosePlatformFile(file);
  if (success) {
    if (!file_util::ReplaceFile(temp_file, path_))
      file_util::Delete(temp_file, false);
  } else {
    LOG(ERROR) << "Failed to write experiment data";
  }
}

void InfiniteCache::Worker::InitializeData() {
  header_.reset(new Header);
  memset(header_.get(), 0, sizeof(Header));
  header_->magic = kMagicSignature;
  header_->version = kCurrentVersion;
  header_->creation_time = Time::Now().ToInternalValue();

  UMA_HISTOGRAM_BOOLEAN("InfiniteCache.Initialize", true);
  init_ = true;
}

bool InfiniteCache::Worker::ReadData(PlatformFile file) {
  if (!ReadAndVerifyHeader(file))
    return false;

  scoped_array<char> buffer(new char[kBufferSize]);
  size_t offset = sizeof(Header);
  uint32 hash = adler32(0, Z_NULL, 0);

  for (int remaining_records = header_->num_entries; remaining_records;) {
    int num_records = std::min(header_->num_entries,
                               static_cast<int>(kMaxRecordsToRead));
    size_t num_bytes = num_records * kRecordSize;
    remaining_records -= num_records;
    if (!remaining_records)
      num_bytes += sizeof(uint32);  // Trailing hash.
    DCHECK_LE(num_bytes, kBufferSize);

    if (!ReadPlatformFile(file, offset, buffer.get(), num_bytes))
      return false;

    hash = adler32(hash, reinterpret_cast<const Bytef*>(buffer.get()),
                   num_records * kRecordSize);
    if (!remaining_records &&
        hash != *reinterpret_cast<uint32*>(buffer.get() +
                                           num_records * kRecordSize)) {
      return false;
    }

    for (int i = 0; i < num_records; i++) {
      char* record = buffer.get() + i * kRecordSize;
      Key key = *reinterpret_cast<Key*>(record);
      Details details = *reinterpret_cast<Details*>(record + sizeof(key));
      map_[key] = details;
    }
    offset += num_bytes;
  }
  if (header_->num_entries != static_cast<int>(map_.size())) {
    NOTREACHED();
    return false;
  }

  init_ = true;
  return true;
}

bool InfiniteCache::Worker::WriteData(PlatformFile file) {
  if (!base::TruncatePlatformFile(file, 0))
    return false;

  if (!WritePlatformFile(file, 0, header_.get(), sizeof(Header)))
    return false;

  scoped_array<char> buffer(new char[kBufferSize]);
  size_t offset = sizeof(Header);
  uint32 hash = adler32(0, Z_NULL, 0);

  DCHECK_EQ(header_->num_entries, static_cast<int32>(map_.size()));
  KeyMap::iterator iterator = map_.begin();
  for (int remaining_records = header_->num_entries; remaining_records;) {
    int num_records = std::min(header_->num_entries,
                               static_cast<int>(kMaxRecordsToRead));
    size_t num_bytes = num_records * kRecordSize;
    remaining_records -= num_records;

    for (int i = 0; i < num_records; i++) {
      if (iterator == map_.end()) {
        NOTREACHED();
        return false;
      }
      char* record = buffer.get() + i * kRecordSize;
      *reinterpret_cast<Key*>(record) = iterator->first;
      *reinterpret_cast<Details*>(record + sizeof(Key)) = iterator->second;
      ++iterator;
    }

    hash = adler32(hash, reinterpret_cast<const Bytef*>(buffer.get()),
                   num_bytes);

    if (!remaining_records) {
      num_bytes += sizeof(uint32);  // Trailing hash.
      *reinterpret_cast<uint32*>(buffer.get() +
                                 num_records * kRecordSize) = hash;
    }

    DCHECK_LE(num_bytes, kBufferSize);
    if (!WritePlatformFile(file, offset, buffer.get(), num_bytes))
      return false;

    offset += num_bytes;
  }
  base::FlushPlatformFile(file);  // Ignore return value.
  return true;
}

bool InfiniteCache::Worker::ReadAndVerifyHeader(PlatformFile file) {
  base::PlatformFileInfo info;
  if (!base::GetPlatformFileInfo(file, &info))
    return false;

  if (info.size < static_cast<int>(sizeof(Header)))
    return false;

  header_.reset(new Header);
  if (!ReadPlatformFile(file, 0, header_.get(), sizeof(Header)))
    return false;

  if (header_->magic != kMagicSignature)
    return false;

  if (header_->version != kCurrentVersion)
    return false;

  if (header_->num_entries > kMaxNumEntries)
    return false;

  size_t expected_size = kRecordSize * header_->num_entries +
                         sizeof(Header) + sizeof(uint32);  // Trailing hash.

  if (info.size < static_cast<int>(expected_size))
    return false;

  uint32 hash = base::Hash(reinterpret_cast<char*>(header_.get()),
                                 offsetof(Header, header_hash));
  if (hash != header_->header_hash)
    return false;

  return true;
}

void InfiniteCache::Worker::Query(int* result) {
  *result = static_cast<int>(map_.size());
}

void InfiniteCache::Worker::Flush(int* result) {
  flushed_ = false;
  StoreData();
  flushed_ = true;
  *result = OK;
}

void InfiniteCache::Worker::OnTimer() {
  header_->use_minutes += kTimerMinutes;
  GenerateHistograms();
  StoreData();
}

void InfiniteCache::Worker::Add(const Details& details) {
  UpdateSize(0, details.headers_size);
  UpdateSize(0, details.response_size);
  header_->num_entries = static_cast<int>(map_.size());
  if (header_->num_entries == kMaxNumEntries) {
    int use_hours = header_->use_minutes / 60;
    int age_hours = (Time::Now() -
                     Time::FromInternalValue(header_->creation_time)).InHours();
    UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxUseTime", use_hours);
    UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.MaxAge", age_hours);

    int entry_size = static_cast<int>(header_->total_size / kMaxNumEntries);
    UMA_HISTOGRAM_COUNTS("InfiniteCache.FinalAvgEntrySize", entry_size);
    header_->disabled = 1;
    map_.clear();
  }
}

void InfiniteCache::Worker::Remove(const Details& details) {
  UpdateSize(details.headers_size, 0);
  UpdateSize(details.response_size, 0);
  header_->num_entries--;
}

void InfiniteCache::Worker::UpdateSize(int old_size, int new_size) {
  header_->total_size += new_size - old_size;
  DCHECK_GE(header_->total_size, 0);
}

void InfiniteCache::Worker::RecordHit(const Details& old, Details* details) {
  header_->num_hits++;
  int access_delta = (IntToTime(details->last_access) -
                      IntToTime(old.last_access)).InMinutes();
  if (old.use_count)
    UMA_HISTOGRAM_COUNTS("InfiniteCache.ReuseAge", access_delta);
  else
    UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstReuseAge", access_delta);

  details->use_count = old.use_count;
  if (details->use_count < kuint8max)
    details->use_count++;
  UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UseCount", details->use_count, 0,
                              kuint8max, 25);
}

void InfiniteCache::Worker::RecordUpdate(const Details& old, Details* details) {
  int access_delta = (IntToTime(details->last_access) -
                      IntToTime(old.last_access)).InMinutes();
  if (old.update_count)
    UMA_HISTOGRAM_COUNTS("InfiniteCache.UpdateAge", access_delta);
  else
    UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstUpdateAge", access_delta);

  details->update_count = old.update_count;
  if (details->update_count < kuint8max)
    details->update_count++;

  UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UpdateCount",
                              details->update_count, 0, kuint8max, 25);
  details->use_count = 0;
}

void InfiniteCache::Worker::GenerateHistograms() {
  bool new_size_step = (header_->total_size / kReportSizeStep !=
                        header_->size_last_report / kReportSizeStep);
  header_->size_last_report = header_->total_size;
  if (!new_size_step && (header_->use_minutes % 60 != 0))
    return;

  if (header_->disabled)
    return;

  int hit_ratio = header_->num_hits * 100;
  if (header_->num_requests)
    hit_ratio /= header_->num_requests;
  else
    hit_ratio = 0;

  // We'll be generating pairs of histograms that can be used to get the hit
  // ratio for any bucket of the paired histogram.
  bool report_second_stat = base::RandInt(0, 99) < hit_ratio;

  if (header_->use_minutes % 60 == 0) {
    int use_hours = header_->use_minutes / 60;
    int age_hours = (Time::Now() -
                     Time::FromInternalValue(header_->creation_time)).InHours();
    UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.UseTime", use_hours);
    UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.Age", age_hours);
    if (report_second_stat) {
      UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.HitRatioByUseTime", use_hours);
      UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.HitRatioByAge", age_hours);
    }
  }

  if (new_size_step) {
    int size_bucket = static_cast<int>(header_->total_size / kReportSizeStep);
    UMA_HISTOGRAM_ENUMERATION("InfiniteCache.Size", std::min(size_bucket, 50),
                              51);
    UMA_HISTOGRAM_ENUMERATION("InfiniteCache.SizeCoarse", size_bucket / 5, 51);
    UMA_HISTOGRAM_COUNTS("InfiniteCache.Entries", header_->num_entries);
    UMA_HISTOGRAM_COUNTS_10000("InfiniteCache.BadHits", header_->num_bad_hits);
    if (report_second_stat) {
      UMA_HISTOGRAM_ENUMERATION("InfiniteCache.HitRatioBySize",
                                std::min(size_bucket, 50), 51);
      UMA_HISTOGRAM_ENUMERATION("InfiniteCache.HitRatioBySizeCoarse",
                                size_bucket / 5, 51);
      UMA_HISTOGRAM_COUNTS("InfiniteCache.HitRatioByEntries",
                           header_->num_entries);
    }
    header_->num_hits = 0;
    header_->num_bad_hits = 0;
    header_->num_requests = 0;
  }
}

bool InfiniteCache::Worker::CanReuse(const Details& old,
                                     const Details& current) {
  enum ReuseStatus {
    REUSE_OK = 0,
    REUSE_NO_CACHE,
    REUSE_ALWAYS_EXPIRED,
    REUSE_EXPIRED,
    REUSE_TRUNCATED,
    REUSE_VARY,
    REUSE_DUMMY_VALUE,
    // Not an individual value; it's added to another reason.
    REUSE_REVALIDATEABLE = 7
  };
  int reason = REUSE_OK;

  if (old.flags & NO_CACHE)
    reason = REUSE_NO_CACHE;

  if (old.flags & EXPIRED)
    reason = REUSE_ALWAYS_EXPIRED;

  if (old.flags & TRUNCATED)
    reason = REUSE_TRUNCATED;

  Time expiration = IntToTime(old.expiration);
  if (expiration < Time::Now())
    reason = REUSE_EXPIRED;

  if (old.vary_hash != current.vary_hash)
    reason = REUSE_VARY;

  bool have_to_drop  = (old.flags & TRUNCATED) && !(old.flags & RESUMABLE);
  if (reason && (old.flags & REVALIDATEABLE) && !have_to_drop)
    reason += REUSE_REVALIDATEABLE;

  UMA_HISTOGRAM_ENUMERATION("InfiniteCache.ReuseFailure", reason, 15);
  return !reason;
}

bool InfiniteCache::Worker::DataChanged(const Details& old,
                                        const Details& current) {
  bool changed = false;
  if (old.response_size != current.response_size) {
    changed = true;
    UpdateSize(old.response_size, current.response_size);
  }

  if (old.response_hash != current.response_hash)
    changed = true;

  return changed;
}

bool InfiniteCache::Worker::HeadersChanged(const Details& old,
                                           const Details& current) {
  bool changed = false;
  if (old.headers_size != current.headers_size) {
    changed = true;
    UpdateSize(old.headers_size, current.headers_size);
  }

  if (old.headers_hash != current.headers_hash)
    changed = true;

  return changed;
}

// ----------------------------------------------------------------------------

InfiniteCache::InfiniteCache() {
}

InfiniteCache::~InfiniteCache() {
  if (!worker_)
    return;

  task_runner_->PostTask(FROM_HERE,
                         base::Bind(&InfiniteCache::Worker::Cleanup, worker_));
  worker_ = NULL;
}

void InfiniteCache::Init(const FilePath& path) {
  worker_pool_ = new base::SequencedWorkerPool(1, "Infinite cache thread");
  task_runner_ = worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior(
                     worker_pool_->GetSequenceToken(),
                     base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);

  worker_ = new Worker();
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(&InfiniteCache::Worker::Init, worker_,
                                    path));

  timer_.Start(FROM_HERE, TimeDelta::FromMinutes(kTimerMinutes), this,
               &InfiniteCache::OnTimer);
}

InfiniteCacheTransaction* InfiniteCache::CreateInfiniteCacheTransaction() {
  if (!worker_)
    return NULL;
  return new InfiniteCacheTransaction(this);
}

int InfiniteCache::DeleteData(const CompletionCallback& callback) {
  if (!worker_)
    return OK;
  int* result = new int;
  task_runner_->PostTaskAndReply(
      FROM_HERE, base::Bind(&InfiniteCache::Worker::DeleteData, worker_,
                            result),
      base::Bind(&OnComplete, callback, base::Owned(result)));
  return ERR_IO_PENDING;
}

int InfiniteCache::DeleteDataBetween(base::Time initial_time,
                                     base::Time end_time,
                                     const CompletionCallback& callback) {
  if (!worker_)
    return OK;
  int* result = new int;
  task_runner_->PostTaskAndReply(
      FROM_HERE, base::Bind(&InfiniteCache::Worker::DeleteDataBetween, worker_,
                            initial_time, end_time, result),
      base::Bind(&OnComplete, callback, base::Owned(result)));
  return ERR_IO_PENDING;
}

std::string InfiniteCache::GenerateKey(const HttpRequestInfo* request) {
  // Don't add any upload data identifier.
  return HttpUtil::SpecForRequest(request->url);
}

void InfiniteCache::ProcessResource(
    scoped_ptr<InfiniteCacheTransaction::ResourceData> data) {
  if (!worker_)
    return;
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(&InfiniteCache::Worker::Process, worker_,
                                    base::Passed(&data)));
}

void InfiniteCache::OnTimer() {
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(&InfiniteCache::Worker::OnTimer, worker_));
}

int InfiniteCache::QueryItemsForTest(const CompletionCallback& callback) {
  DCHECK(worker_);
  int* result = new int;
  task_runner_->PostTaskAndReply(
      FROM_HERE, base::Bind(&InfiniteCache::Worker::Query, worker_, result),
      base::Bind(&OnComplete, callback, base::Owned(result)));
  return net::ERR_IO_PENDING;
}

int InfiniteCache::FlushDataForTest(const CompletionCallback& callback) {
  DCHECK(worker_);
  int* result = new int;
  task_runner_->PostTaskAndReply(
      FROM_HERE, base::Bind(&InfiniteCache::Worker::Flush, worker_, result),
      base::Bind(&OnComplete, callback, base::Owned(result)));
  return net::ERR_IO_PENDING;
}

}  // namespace net