summaryrefslogtreecommitdiffstats
path: root/webkit/appcache/appcache_storage.h
blob: 47d1af8f0c9559ee964c5d0a8427a89e8cd1710d (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
// Copyright (c) 2009 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 WEBKIT_APPCACHE_APPCACHE_STORAGE_H_
#define WEBKIT_APPCACHE_APPCACHE_STORAGE_H_

#include <vector>

#include "base/basictypes.h"
#include "base/logging.h"
#include "webkit/appcache/appcache_working_set.h"

class GURL;

namespace appcache {

class AppCache;
class AppCacheGroup;
class AppCacheResponseInfo;
class AppCacheResponseReader;
class AppCacheResponseWriter;
class AppCacheService;

class AppCacheStorage {
 public:

  class Delegate {
   public:
    virtual ~Delegate() {}

    // If a load fails the 'cache' will be NULL.
    virtual void OnCacheLoaded(AppCache* cache, int64 cache_id) {}

    // If a load fails the 'group' will be NULL.
    virtual void OnGroupLoaded(
        AppCacheGroup* group, const GURL& manifest_url) {}

    // If successfully stored 'success' will be true.
    virtual void OnGroupAndNewestCacheStored(
        AppCacheGroup* group, bool success) {}

    // If the update fails, success will be false.
    virtual void OnGroupMarkedAsObsolete(GURL& manifest_url, bool success) {}

    // If a load fails the 'response_info' will be NULL.
    virtual void OnResponseInfoLoaded(
        AppCacheResponseInfo* response_info, int64 response_id) {}

    // If no response is found, response_id will be kNoResponseId.
    // If a response is found, the cache id and manifest url of the
    // containing cache and group are also returned.
    virtual void OnMainResponseFound(
        const GURL& url, int64 response_id, bool is_fallback,
        int64 cache_id, const GURL& mainfest_url) {}
  };

  explicit AppCacheStorage(AppCacheService* service)
      : last_cache_id_(kUnitializedId), last_group_id_(kUnitializedId),
        last_entry_id_(kUnitializedId), last_response_id_(kUnitializedId),
        service_(service)  {}
  virtual ~AppCacheStorage() {}

  // Schedules a cache to be loaded from storage. Upon load completion
  // the delegate will be called back. If the cache already resides in
  // memory, the delegate will be called back immediately without returning
  // to the message loop. If the load fails, the delegate will be called
  // back with a NULL cache pointer.
  virtual void LoadCache(int64 id, Delegate* delegate) = 0;

  // Schedules a group and its newest cache, if any, to be loaded from storage.
  // Upon load completion the delegate will be called back. If the group
  // and newest cache already reside in memory, the delegate will be called
  // back immediately without returning to the message loop. If the load fails,
  // the delegate will be called back with a NULL group pointer.
  virtual void LoadOrCreateGroup(
      const GURL& manifest_url, Delegate* delegate) = 0;

  // Schedules response info to be loaded from storage.
  // Upon load completion the delegate will be called back. If the data
  // already resides in memory, the delegate will be called back
  // immediately without returning to the message loop. If the load fails,
  // the delegate will be called back with a NULL pointer.
  virtual void LoadResponseInfo(
      const GURL& manifest_url, int64 response_id, Delegate* delegate) = 0;

  // Schedules a group and its newest complete cache to be initially stored or
  // incrementally updated with new changes. Upon completion the delegate
  // will be called back. A group without a newest cache cannot be stored.
  // It's a programming error to call this method with such a group. A
  // side effect of storing a new newest cache is the removal of the group's
  // old caches and responses from persistent storage (although they may still
  // linger in the in-memory working set until no longer needed).
  virtual void StoreGroupAndNewestCache(
      AppCacheGroup* group, Delegate* delegate) = 0;

  // Schedules a query to identify a response for a main request. Upon
  // completion the delegate will be called back.
  virtual void FindResponseForMainRequest(
      const GURL& url, Delegate* delegate) = 0;

  // Immediately updates in-memory storage, if the cache is in memory,
  // and schedules a task to update persistent storage. If the cache is
  // already scheduled to be loaded, upon loading completion the entry
  // will be marked. There is no delegate completion callback.
  virtual void MarkEntryAsForeign(const GURL& entry_url, int64 cache_id) = 0;

  // Schedules a task to update persistent storage and doom the group and all
  // related caches and responses for deletion. Upon completion the in-memory
  // instance is marked as obsolete and the delegate callback is called.
  virtual void MarkGroupAsObsolete(
      AppCacheGroup* group, Delegate* delegate) = 0;

  // Cancels all pending callbacks for the delegate. The delegate callbacks
  // will not be invoked after, however any scheduled operations will still
  // take place. The callbacks for subsequently scheduled operations are
  // unaffected.
  virtual void CancelDelegateCallbacks(Delegate* delegate) = 0;

  // Creates a reader to read a response from storage.
  virtual AppCacheResponseReader* CreateResponseReader(
      const GURL& manifest_url, int64 response_id) = 0;

  // Creates a writer to write a new response to storage. This call
  // establishes a new response id.
  virtual AppCacheResponseWriter* CreateResponseWriter(
      const GURL& manifest_url) = 0;

  // Schedules the deletion of many responses.
  virtual void DoomResponses(
      const GURL& manifest_url, const std::vector<int64>& response_ids) = 0;

  // Generates unique storage ids for different object types.
  int64 NewCacheId() {
    DCHECK(last_cache_id_ != kUnitializedId);
    return ++last_cache_id_;
  }
  int64 NewGroupId() {
    DCHECK(last_group_id_ != kUnitializedId);
    return ++last_group_id_;
  }
  int64 NewEntryId() {
    DCHECK(last_entry_id_ != kUnitializedId);
    return ++last_entry_id_;
  }

  // The working set of object instances currently in memory.
  AppCacheWorkingSet* working_set() { return &working_set_; }

  // Simple ptr back to the service object that owns us.
  AppCacheService* service() { return service_; }

 protected:
  // Should only be called when creating a new response writer.
  int64 NewResponseId() {
    DCHECK(last_response_id_ != kUnitializedId);
    return ++last_response_id_;
  }

  // The last storage id used for different object types.
  int64 last_cache_id_;
  int64 last_group_id_;
  int64 last_entry_id_;
  int64 last_response_id_;

  AppCacheWorkingSet working_set_;
  AppCacheService* service_;

  // The set of last ids must be retrieved from storage prior to being used.
  static const int64 kUnitializedId = -1;

  DISALLOW_COPY_AND_ASSIGN(AppCacheStorage);
};

}  // namespace appcache

#endif  // WEBKIT_APPCACHE_APPCACHE_STORAGE_H_