summaryrefslogtreecommitdiff
path: root/chromium/content/browser/appcache/appcache_disk_cache.h
blob: 5eba6053397f596fc0320af3c7abc9c03e3ac3f7 (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
// 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.

#ifndef CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
#define CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_

#include <stdint.h>

#include <memory>
#include <set>
#include <vector>

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "content/browser/appcache/appcache_response.h"
#include "content/common/content_export.h"
#include "net/disk_cache/disk_cache.h"

namespace content {

// An implementation of AppCacheDiskCacheInterface that
// uses net::DiskCache as the backing store.
class CONTENT_EXPORT AppCacheDiskCache
    : public AppCacheDiskCacheInterface {
 public:
  AppCacheDiskCache();
  ~AppCacheDiskCache() override;

  // Initializes the object to use disk backed storage.
  int InitWithDiskBackend(const base::FilePath& disk_cache_directory,
                          int disk_cache_size,
                          bool force,
                          base::OnceClosure post_cleanup_callback,
                          const net::CompletionCallback& callback);

  // Initializes the object to use memory only storage.
  // This is used for Chrome's incognito browsing.
  int InitWithMemBackend(int disk_cache_size,
                         const net::CompletionCallback& callback);

  void Disable();
  bool is_disabled() const { return is_disabled_; }

  int CreateEntry(int64_t key,
                  Entry** entry,
                  const net::CompletionCallback& callback) override;
  int OpenEntry(int64_t key,
                Entry** entry,
                const net::CompletionCallback& callback) override;
  int DoomEntry(int64_t key, const net::CompletionCallback& callback) override;

  void set_is_waiting_to_initialize(bool is_waiting_to_initialize) {
    is_waiting_to_initialize_ = is_waiting_to_initialize;
  }

 protected:
  explicit AppCacheDiskCache(bool use_simple_cache);
  disk_cache::Backend* disk_cache() { return disk_cache_.get(); }

 private:
  class CreateBackendCallbackShim;
  class EntryImpl;

  // PendingCalls allow CreateEntry, OpenEntry, and DoomEntry to be called
  // immediately after construction, without waiting for the
  // underlying disk_cache::Backend to be fully constructed. Early
  // calls are queued up and serviced once the disk_cache::Backend is
  // really ready to go.
  enum PendingCallType {
    CREATE,
    OPEN,
    DOOM
  };
  struct PendingCall {
    PendingCallType call_type;
    int64_t key;
    Entry** entry;
    net::CompletionCallback callback;

    PendingCall();

    PendingCall(PendingCallType call_type,
                int64_t key,
                Entry** entry,
                const net::CompletionCallback& callback);

    PendingCall(const PendingCall& other);

    ~PendingCall();
  };
  using PendingCalls = std::vector<PendingCall>;

  class ActiveCall;
  using ActiveCalls = std::set<ActiveCall*>;
  using OpenEntries = std::set<EntryImpl*>;

  bool is_initializing_or_waiting_to_initialize() const {
    return create_backend_callback_.get() != NULL || is_waiting_to_initialize_;
  }

  int Init(net::CacheType cache_type,
           const base::FilePath& directory,
           int cache_size,
           bool force,
           base::OnceClosure post_cleanup_callback,
           const net::CompletionCallback& callback);
  void OnCreateBackendComplete(int rv);
  void AddOpenEntry(EntryImpl* entry) { open_entries_.insert(entry); }
  void RemoveOpenEntry(EntryImpl* entry) { open_entries_.erase(entry); }

  bool use_simple_cache_;
  bool is_disabled_;
  bool is_waiting_to_initialize_;
  net::CompletionCallback init_callback_;
  scoped_refptr<CreateBackendCallbackShim> create_backend_callback_;
  PendingCalls pending_calls_;
  OpenEntries open_entries_;
  std::unique_ptr<disk_cache::Backend> disk_cache_;

  base::WeakPtrFactory<AppCacheDiskCache> weak_factory_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_