[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 1 | // 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 8 | #include <set> |
| 9 | #include <vector> |
| 10 | |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 11 | #include "base/file_path.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 12 | #include "base/ref_counted.h" |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 13 | #include "base/scoped_ptr.h" |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 14 | #include "base/time.h" |
| 15 | #include "net/base/completion_callback.h" |
| 16 | #include "net/base/net_errors.h" |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest_prod.h" |
| 18 | #include "webkit/appcache/appcache_storage.h" |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 19 | |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 20 | class URLRequestContext; |
| 21 | |
[email protected] | f5ad47a1 | 2010-06-07 22:42:01 | [diff] [blame^] | 22 | namespace base { |
| 23 | class MessageLoopProxy; |
| 24 | } |
| 25 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 26 | namespace appcache { |
| 27 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 28 | class AppCacheBackendImpl; |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 29 | class AppCachePolicy; |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 30 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 31 | // Structure that contains basic info about an appcache. |
| 32 | struct 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 | |
| 52 | typedef std::vector<AppCacheInfo> AppCacheInfoVector; |
| 53 | |
| 54 | // Refcounted container to avoid copying the collection in callbacks. |
| 55 | struct AppCacheInfoCollection |
| 56 | : public base::RefCountedThreadSafe<AppCacheInfoCollection> { |
| 57 | virtual ~AppCacheInfoCollection() {} |
| 58 | std::map<GURL, AppCacheInfoVector> infos_by_origin; |
| 59 | }; |
| 60 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 61 | // Class that manages the application cache service. Sends notifications |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 62 | // to many frontends. One instance per user-profile. Each instance has |
| 63 | // exclusive access to it's cache_directory on disk. |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 64 | class AppCacheService { |
| 65 | public: |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 66 | AppCacheService(); |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 67 | virtual ~AppCacheService(); |
| 68 | |
[email protected] | f5ad47a1 | 2010-06-07 22:42:01 | [diff] [blame^] | 69 | void Initialize(const FilePath& cache_directory, |
| 70 | base::MessageLoopProxy* cache_thread); |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 71 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 72 | // Purges any memory not needed. |
[email protected] | 520cdd7 | 2010-01-13 22:14:46 | [diff] [blame] | 73 | void PurgeMemory() { |
| 74 | if (storage_.get()) |
| 75 | storage_->PurgeMemory(); |
| 76 | } |
| 77 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 78 | // 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] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 93 | // Context for use during cache updates, should only be accessed |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 94 | // 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] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 98 | void set_request_context(URLRequestContext* context) { |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 99 | request_context_ = context; |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 102 | // 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] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 110 | // Each child process in chrome uses a distinct backend instance. |
| 111 | // See chrome/browser/AppCacheDispatcherHost. |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 112 | void RegisterBackend(AppCacheBackendImpl* backend_impl); |
| 113 | void UnregisterBackend(AppCacheBackendImpl* backend_impl); |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 114 | AppCacheBackendImpl* GetBackend(int id) const { |
| 115 | BackendMap::const_iterator it = backends_.find(id); |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 116 | return (it != backends_.end()) ? it->second : NULL; |
| 117 | } |
| 118 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 119 | AppCacheStorage* storage() const { return storage_.get(); } |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 120 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 121 | protected: |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 122 | class AsyncHelper; |
| 123 | class DeleteHelper; |
| 124 | class GetInfoHelper; |
| 125 | |
| 126 | typedef std::set<AsyncHelper*> PendingAsyncHelpers; |
| 127 | |
[email protected] | ea776d02 | 2010-02-17 22:20:36 | [diff] [blame] | 128 | AppCachePolicy* appcache_policy_; |
| 129 | |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 130 | // Deals with persistence. |
| 131 | scoped_ptr<AppCacheStorage> storage_; |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 132 | |
[email protected] | d68a4fc6 | 2010-03-05 23:40:02 | [diff] [blame] | 133 | PendingAsyncHelpers pending_helpers_; |
| 134 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 135 | // Track current processes. One 'backend' per child process. |
[email protected] | 23f1ef1 | 2009-09-01 22:30:30 | [diff] [blame] | 136 | typedef std::map<int, AppCacheBackendImpl*> BackendMap; |
| 137 | BackendMap backends_; |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 138 | |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 139 | // Context for use during cache updates. |
[email protected] | e2fc3d2 | 2009-09-22 19:50:31 | [diff] [blame] | 140 | URLRequestContext* request_context_; |
[email protected] | 6c270d4 | 2009-09-15 20:00:11 | [diff] [blame] | 141 | |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 142 | // TODO(jennb): service state: e.g. reached quota? |
[email protected] | e7dff7ba | 2009-10-09 03:25:51 | [diff] [blame] | 143 | |
| 144 | DISALLOW_COPY_AND_ASSIGN(AppCacheService); |
[email protected] | 49672df | 2009-08-26 23:10:09 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | } // namespace appcache |
| 148 | |
| 149 | #endif // WEBKIT_APPCACHE_APPCACHE_SERVICE_H_ |