blob: ac5aa4aee803bedba1d79ffdca33ce01801f766c [file] [log] [blame]
[email protected]49672df2009-08-26 23:10:091// Copyright (c) 2009 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_APPCACHE_APPCACHE_SERVICE_H_
6#define WEBKIT_APPCACHE_APPCACHE_SERVICE_H_
7
[email protected]d68a4fc62010-03-05 23:40:028#include <set>
9#include <vector>
10
[email protected]23f1ef12009-09-01 22:30:3011#include "base/file_path.h"
[email protected]d68a4fc62010-03-05 23:40:0212#include "base/ref_counted.h"
[email protected]e7dff7ba2009-10-09 03:25:5113#include "base/scoped_ptr.h"
[email protected]d68a4fc62010-03-05 23:40:0214#include "base/time.h"
15#include "net/base/completion_callback.h"
16#include "net/base/net_errors.h"
[email protected]e7dff7ba2009-10-09 03:25:5117#include "testing/gtest/include/gtest/gtest_prod.h"
18#include "webkit/appcache/appcache_storage.h"
[email protected]49672df2009-08-26 23:10:0919
[email protected]e2fc3d22009-09-22 19:50:3120class URLRequestContext;
21
[email protected]f5ad47a12010-06-07 22:42:0122namespace base {
23class MessageLoopProxy;
24}
25
[email protected]49672df2009-08-26 23:10:0926namespace appcache {
27
[email protected]49672df2009-08-26 23:10:0928class AppCacheBackendImpl;
[email protected]ea776d022010-02-17 22:20:3629class AppCachePolicy;
[email protected]49672df2009-08-26 23:10:0930
[email protected]d68a4fc62010-03-05 23:40:0231// Structure that contains basic info about an appcache.
32struct AppCacheInfo {
33 AppCacheInfo() {}
34 AppCacheInfo(const GURL& manifest_url,
35 int64 size,
36 base::Time creation_time,
37 base::Time last_access_time,
38 base::Time last_update_time)
39 : manifest_url(manifest_url),
40 size(size),
41 creation_time(creation_time),
42 last_access_time(last_access_time),
43 last_update_time(last_update_time) {
44 }
45 GURL manifest_url;
46 int64 size;
47 base::Time creation_time;
48 base::Time last_access_time;
49 base::Time last_update_time;
50};
51
52typedef std::vector<AppCacheInfo> AppCacheInfoVector;
53
54// Refcounted container to avoid copying the collection in callbacks.
55struct AppCacheInfoCollection
56 : public base::RefCountedThreadSafe<AppCacheInfoCollection> {
57 virtual ~AppCacheInfoCollection() {}
58 std::map<GURL, AppCacheInfoVector> infos_by_origin;
59};
60
[email protected]49672df2009-08-26 23:10:0961// Class that manages the application cache service. Sends notifications
[email protected]6c270d42009-09-15 20:00:1162// to many frontends. One instance per user-profile. Each instance has
63// exclusive access to it's cache_directory on disk.
[email protected]49672df2009-08-26 23:10:0964class AppCacheService {
65 public:
[email protected]6c270d42009-09-15 20:00:1166 AppCacheService();
[email protected]49672df2009-08-26 23:10:0967 virtual ~AppCacheService();
68
[email protected]f5ad47a12010-06-07 22:42:0169 void Initialize(const FilePath& cache_directory,
70 base::MessageLoopProxy* cache_thread);
[email protected]23f1ef12009-09-01 22:30:3071
[email protected]d68a4fc62010-03-05 23:40:0272 // Purges any memory not needed.
[email protected]520cdd72010-01-13 22:14:4673 void PurgeMemory() {
74 if (storage_.get())
75 storage_->PurgeMemory();
76 }
77
[email protected]d68a4fc62010-03-05 23:40:0278 // Populates 'collection' with info about all of the appcaches stored
79 // within the service, 'callback' is invoked upon completion. The service
80 // acquires a reference to the 'collection' until until completion.
81 // This method always completes asynchronously.
82 void GetAllAppCacheInfo(AppCacheInfoCollection* collection,
83 net::CompletionCallback* callback);
84
85 // Deletes the group identified by 'manifest_url', 'callback' is
86 // invoked upon completion. Upon completion, the cache group and
87 // any resources within the group are no longer loadable and all
88 // subresource loads for pages associated with a deleted group
89 // will fail. This method always completes asynchronously.
90 void DeleteAppCacheGroup(const GURL& manifest_url,
91 net::CompletionCallback* callback);
92
[email protected]6c270d42009-09-15 20:00:1193 // Context for use during cache updates, should only be accessed
[email protected]e2fc3d22009-09-22 19:50:3194 // on the IO thread. We do NOT add a reference to the request context,
95 // it is the callers responsibility to ensure that the pointer
96 // remains valid while set.
97 URLRequestContext* request_context() const { return request_context_; }
[email protected]6c270d42009-09-15 20:00:1198 void set_request_context(URLRequestContext* context) {
[email protected]e2fc3d22009-09-22 19:50:3199 request_context_ = context;
[email protected]6c270d42009-09-15 20:00:11100 }
101
[email protected]ea776d022010-02-17 22:20:36102 // The appcache policy, may be null, in which case access is always allowed.
103 // The service does NOT assume ownership of the policy, it is the callers
104 // responsibility to ensure that the pointer remains valid while set.
105 AppCachePolicy* appcache_policy() const { return appcache_policy_; }
106 void set_appcache_policy(AppCachePolicy* policy) {
107 appcache_policy_ = policy;
108 }
109
[email protected]d68a4fc62010-03-05 23:40:02110 // Each child process in chrome uses a distinct backend instance.
111 // See chrome/browser/AppCacheDispatcherHost.
[email protected]23f1ef12009-09-01 22:30:30112 void RegisterBackend(AppCacheBackendImpl* backend_impl);
113 void UnregisterBackend(AppCacheBackendImpl* backend_impl);
[email protected]e7dff7ba2009-10-09 03:25:51114 AppCacheBackendImpl* GetBackend(int id) const {
115 BackendMap::const_iterator it = backends_.find(id);
[email protected]23f1ef12009-09-01 22:30:30116 return (it != backends_.end()) ? it->second : NULL;
117 }
118
[email protected]e7dff7ba2009-10-09 03:25:51119 AppCacheStorage* storage() const { return storage_.get(); }
[email protected]23f1ef12009-09-01 22:30:30120
[email protected]e7dff7ba2009-10-09 03:25:51121 protected:
[email protected]d68a4fc62010-03-05 23:40:02122 class AsyncHelper;
123 class DeleteHelper;
124 class GetInfoHelper;
125
126 typedef std::set<AsyncHelper*> PendingAsyncHelpers;
127
[email protected]ea776d022010-02-17 22:20:36128 AppCachePolicy* appcache_policy_;
129
[email protected]e7dff7ba2009-10-09 03:25:51130 // Deals with persistence.
131 scoped_ptr<AppCacheStorage> storage_;
[email protected]6c270d42009-09-15 20:00:11132
[email protected]d68a4fc62010-03-05 23:40:02133 PendingAsyncHelpers pending_helpers_;
134
[email protected]49672df2009-08-26 23:10:09135 // Track current processes. One 'backend' per child process.
[email protected]23f1ef12009-09-01 22:30:30136 typedef std::map<int, AppCacheBackendImpl*> BackendMap;
137 BackendMap backends_;
[email protected]49672df2009-08-26 23:10:09138
[email protected]6c270d42009-09-15 20:00:11139 // Context for use during cache updates.
[email protected]e2fc3d22009-09-22 19:50:31140 URLRequestContext* request_context_;
[email protected]6c270d42009-09-15 20:00:11141
[email protected]49672df2009-08-26 23:10:09142 // TODO(jennb): service state: e.g. reached quota?
[email protected]e7dff7ba2009-10-09 03:25:51143
144 DISALLOW_COPY_AND_ASSIGN(AppCacheService);
[email protected]49672df2009-08-26 23:10:09145};
146
147} // namespace appcache
148
149#endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_