1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
6#define WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
7
8#include <set>
9#include <vector>
10
11#include "base/memory/scoped_ptr.h"
12#include "net/disk_cache/disk_cache.h"
13#include "webkit/browser/appcache/appcache_response.h"
14#include "webkit/browser/webkit_storage_browser_export.h"
15
16namespace appcache {
17
18// An implementation of AppCacheDiskCacheInterface that
19// uses net::DiskCache as the backing store.
20class WEBKIT_STORAGE_BROWSER_EXPORT AppCacheDiskCache
21    : public AppCacheDiskCacheInterface {
22 public:
23  AppCacheDiskCache();
24  virtual ~AppCacheDiskCache();
25
26  // Initializes the object to use disk backed storage.
27  int InitWithDiskBackend(const base::FilePath& disk_cache_directory,
28                          int disk_cache_size, bool force,
29                          base::MessageLoopProxy* cache_thread,
30                          const net::CompletionCallback& callback);
31
32  // Initializes the object to use memory only storage.
33  // This is used for Chrome's incognito browsing.
34  int InitWithMemBackend(int disk_cache_size,
35                         const net::CompletionCallback& callback);
36
37  void Disable();
38  bool is_disabled() const { return is_disabled_; }
39
40  virtual int CreateEntry(int64 key, Entry** entry,
41                          const net::CompletionCallback& callback) OVERRIDE;
42  virtual int OpenEntry(int64 key, Entry** entry,
43                        const net::CompletionCallback& callback) OVERRIDE;
44  virtual int DoomEntry(int64 key,
45                        const net::CompletionCallback& callback) OVERRIDE;
46
47 private:
48  class CreateBackendCallbackShim;
49  class EntryImpl;
50
51  // PendingCalls allow CreateEntry, OpenEntry, and DoomEntry to be called
52  // immediately after construction, without waiting for the
53  // underlying disk_cache::Backend to be fully constructed. Early
54  // calls are queued up and serviced once the disk_cache::Backend is
55  // really ready to go.
56  enum PendingCallType {
57    CREATE,
58    OPEN,
59    DOOM
60  };
61  struct PendingCall {
62    PendingCallType call_type;
63    int64 key;
64    Entry** entry;
65    net::CompletionCallback callback;
66
67    PendingCall();
68
69    PendingCall(PendingCallType call_type, int64 key,
70                Entry** entry, const net::CompletionCallback& callback);
71
72    ~PendingCall();
73  };
74  typedef std::vector<PendingCall> PendingCalls;
75
76  class ActiveCall;
77  typedef std::set<ActiveCall*> ActiveCalls;
78  typedef std::set<EntryImpl*> OpenEntries;
79
80  bool is_initializing() const {
81    return create_backend_callback_.get() != NULL;
82  }
83  disk_cache::Backend* disk_cache() { return disk_cache_.get(); }
84  int Init(net::CacheType cache_type, const base::FilePath& directory,
85           int cache_size, bool force, base::MessageLoopProxy* cache_thread,
86           const net::CompletionCallback& callback);
87  void OnCreateBackendComplete(int rv);
88  void AddActiveCall(ActiveCall* call) { active_calls_.insert(call); }
89  void RemoveActiveCall(ActiveCall* call) { active_calls_.erase(call); }
90  void AddOpenEntry(EntryImpl* entry) { open_entries_.insert(entry); }
91  void RemoveOpenEntry(EntryImpl* entry) { open_entries_.erase(entry); }
92
93  bool is_disabled_;
94  net::CompletionCallback init_callback_;
95  scoped_refptr<CreateBackendCallbackShim> create_backend_callback_;
96  PendingCalls pending_calls_;
97  ActiveCalls active_calls_;
98  OpenEntries open_entries_;
99  scoped_ptr<disk_cache::Backend> disk_cache_;
100};
101
102}  // namespace appcache
103
104#endif  // WEBKIT_BROWSER_APPCACHE_APPCACHE_DISK_CACHE_H_
105