| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "content/browser/service_worker/embedded_worker_instance.h" | 13 #include "content/browser/service_worker/cache_storage_dispatcher_host.h" |
| 14 #include "content/browser/service_worker/service_worker_cache.h" | 14 #include "content/browser/service_worker/service_worker_cache.h" |
| 15 #include "content/browser/service_worker/service_worker_cache_storage.h" | 15 #include "content/browser/service_worker/service_worker_cache_storage.h" |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 | 18 |
| 19 struct ServiceWorkerBatchOperation; | 19 struct ServiceWorkerBatchOperation; |
| 20 struct ServiceWorkerCacheQueryParams; | 20 struct ServiceWorkerCacheQueryParams; |
| 21 struct ServiceWorkerFetchRequest; | 21 struct ServiceWorkerFetchRequest; |
| 22 class ServiceWorkerVersion; | |
| 23 | 22 |
| 24 // This class listens for requests on the Cache APIs, and sends response | 23 // This class listens for requests on the Cache APIs, and sends response |
| 25 // messages to the renderer process. There is one instance per | 24 // messages to the renderer process. There is one instance per |
| 26 // ServiceWorkerVersion instance. | 25 // CacheStorageDispatcherHost instance. |
| 27 class ServiceWorkerCacheListener : public EmbeddedWorkerInstance::Listener { | 26 class ServiceWorkerCacheListener { |
| 28 public: | 27 public: |
| 29 ServiceWorkerCacheListener(ServiceWorkerVersion* version, | 28 ServiceWorkerCacheListener(CacheStorageDispatcherHost* dispatcher, |
| 30 base::WeakPtr<ServiceWorkerContextCore> context); | 29 CacheStorageContextImpl* context); |
| 31 ~ServiceWorkerCacheListener() override; | 30 ~ServiceWorkerCacheListener(); |
| 32 | 31 |
| 33 // From EmbeddedWorkerInstance::Listener: | 32 bool OnMessageReceived(const IPC::Message& message); |
| 34 bool OnMessageReceived(const IPC::Message& message) override; | |
| 35 | 33 |
| 36 private: | 34 private: |
| 37 // The message receiver functions for the CacheStorage API: | 35 // The message receiver functions for the CacheStorage API: |
| 38 void OnCacheStorageGet(int request_id, const base::string16& cache_name); | 36 void OnCacheStorageGet(int thread_id, |
| 39 void OnCacheStorageHas(int request_id, const base::string16& cache_name); | 37 int request_id, |
| 40 void OnCacheStorageCreate(int request_id, const base::string16& cache_name); | 38 const GURL& origin, |
| 41 void OnCacheStorageOpen(int request_id, const base::string16& cache_name); | 39 const base::string16& cache_name); |
| 42 void OnCacheStorageDelete(int request_id, | 40 void OnCacheStorageHas(int thread_id, |
| 43 const base::string16& cache_name); | 41 int request_id, |
| 44 void OnCacheStorageKeys(int request_id); | 42 const GURL& origin, |
| 45 void OnCacheStorageMatch(int request_id, | 43 const base::string16& cache_name); |
| 44 void OnCacheStorageCreate(int thread_id, |
| 45 int request_id, |
| 46 const GURL& origin, |
| 47 const base::string16& cache_name); |
| 48 void OnCacheStorageOpen(int thread_id, |
| 49 int request_id, |
| 50 const GURL& origin, |
| 51 const base::string16& cache_name); |
| 52 void OnCacheStorageDelete(int thread_id, |
| 53 int request_id, |
| 54 const GURL& origin, |
| 55 const base::string16& cache_name); |
| 56 void OnCacheStorageKeys(int thread_id, int request_id, const GURL& origin); |
| 57 void OnCacheStorageMatch(int thread_id, |
| 58 int request_id, |
| 59 const GURL& origin, |
| 46 const ServiceWorkerFetchRequest& request, | 60 const ServiceWorkerFetchRequest& request, |
| 47 const ServiceWorkerCacheQueryParams& match_params); | 61 const ServiceWorkerCacheQueryParams& match_params); |
| 48 | 62 |
| 49 // The message receiver functions for the Cache API: | 63 // The message receiver functions for the Cache API: |
| 50 void OnCacheMatch(int request_id, | 64 void OnCacheMatch(int thread_id, |
| 65 int request_id, |
| 51 int cache_id, | 66 int cache_id, |
| 52 const ServiceWorkerFetchRequest& request, | 67 const ServiceWorkerFetchRequest& request, |
| 53 const ServiceWorkerCacheQueryParams& match_params); | 68 const ServiceWorkerCacheQueryParams& match_params); |
| 54 void OnCacheMatchAll(int request_id, | 69 void OnCacheMatchAll(int thread_id, |
| 70 int request_id, |
| 55 int cache_id, | 71 int cache_id, |
| 56 const ServiceWorkerFetchRequest& request, | 72 const ServiceWorkerFetchRequest& request, |
| 57 const ServiceWorkerCacheQueryParams& match_params); | 73 const ServiceWorkerCacheQueryParams& match_params); |
| 58 void OnCacheKeys(int request_id, | 74 void OnCacheKeys(int thread_id, |
| 75 int request_id, |
| 59 int cache_id, | 76 int cache_id, |
| 60 const ServiceWorkerFetchRequest& request, | 77 const ServiceWorkerFetchRequest& request, |
| 61 const ServiceWorkerCacheQueryParams& match_params); | 78 const ServiceWorkerCacheQueryParams& match_params); |
| 62 void OnCacheBatch(int request_id, | 79 void OnCacheBatch(int thread_id, |
| 80 int request_id, |
| 63 int cache_id, | 81 int cache_id, |
| 64 const std::vector<ServiceWorkerBatchOperation>& operations); | 82 const std::vector<ServiceWorkerBatchOperation>& operations); |
| 65 void OnCacheClosed(int cache_id); | 83 void OnCacheClosed(int cache_id); |
| 66 void OnBlobDataHandled(const std::string& uuid); | 84 void OnBlobDataHandled(const std::string& uuid); |
| 67 | 85 |
| 68 private: | 86 private: |
| 69 typedef int32_t CacheID; // TODO(jkarlin): Bump to 64 bit. | 87 typedef int32_t CacheID; // TODO(jkarlin): Bump to 64 bit. |
| 70 typedef std::map<CacheID, scoped_refptr<ServiceWorkerCache>> IDToCacheMap; | 88 typedef std::map<CacheID, scoped_refptr<ServiceWorkerCache>> IDToCacheMap; |
| 71 typedef std::map<std::string, std::list<storage::BlobDataHandle>> | 89 typedef std::map<std::string, std::list<storage::BlobDataHandle>> |
| 72 UUIDToBlobDataHandleList; | 90 UUIDToBlobDataHandleList; |
| 73 | 91 |
| 74 void Send(const IPC::Message& message); | 92 void Send(IPC::Message* message); |
| 75 | 93 |
| 76 // CacheStorageManager callbacks | 94 // CacheStorageManager callbacks |
| 77 void OnCacheStorageGetCallback( | 95 void OnCacheStorageGetCallback( |
| 96 int thread_id, |
| 78 int request_id, | 97 int request_id, |
| 79 const scoped_refptr<ServiceWorkerCache>& cache, | 98 const scoped_refptr<ServiceWorkerCache>& cache, |
| 80 ServiceWorkerCacheStorage::CacheStorageError error); | 99 ServiceWorkerCacheStorage::CacheStorageError error); |
| 81 void OnCacheStorageHasCallback( | 100 void OnCacheStorageHasCallback( |
| 101 int thread_id, |
| 82 int request_id, | 102 int request_id, |
| 83 bool has_cache, | 103 bool has_cache, |
| 84 ServiceWorkerCacheStorage::CacheStorageError error); | 104 ServiceWorkerCacheStorage::CacheStorageError error); |
| 85 void OnCacheStorageCreateCallback( | 105 void OnCacheStorageCreateCallback( |
| 106 int thread_id, |
| 86 int request_id, | 107 int request_id, |
| 87 const scoped_refptr<ServiceWorkerCache>& cache, | 108 const scoped_refptr<ServiceWorkerCache>& cache, |
| 88 ServiceWorkerCacheStorage::CacheStorageError error); | 109 ServiceWorkerCacheStorage::CacheStorageError error); |
| 89 void OnCacheStorageOpenCallback( | 110 void OnCacheStorageOpenCallback( |
| 111 int thread_id, |
| 90 int request_id, | 112 int request_id, |
| 91 const scoped_refptr<ServiceWorkerCache>& cache, | 113 const scoped_refptr<ServiceWorkerCache>& cache, |
| 92 ServiceWorkerCacheStorage::CacheStorageError error); | 114 ServiceWorkerCacheStorage::CacheStorageError error); |
| 93 void OnCacheStorageDeleteCallback( | 115 void OnCacheStorageDeleteCallback( |
| 116 int thread_id, |
| 94 int request_id, | 117 int request_id, |
| 95 bool deleted, | 118 bool deleted, |
| 96 ServiceWorkerCacheStorage::CacheStorageError error); | 119 ServiceWorkerCacheStorage::CacheStorageError error); |
| 97 void OnCacheStorageKeysCallback( | 120 void OnCacheStorageKeysCallback( |
| 121 int thread_id, |
| 98 int request_id, | 122 int request_id, |
| 99 const std::vector<std::string>& strings, | 123 const std::vector<std::string>& strings, |
| 100 ServiceWorkerCacheStorage::CacheStorageError error); | 124 ServiceWorkerCacheStorage::CacheStorageError error); |
| 101 void OnCacheStorageMatchCallback( | 125 void OnCacheStorageMatchCallback( |
| 126 int thread_id, |
| 102 int request_id, | 127 int request_id, |
| 103 ServiceWorkerCache::ErrorType error, | 128 ServiceWorkerCache::ErrorType error, |
| 104 scoped_ptr<ServiceWorkerResponse> response, | 129 scoped_ptr<ServiceWorkerResponse> response, |
| 105 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 130 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 106 | 131 |
| 107 // Cache callbacks | 132 // Cache callbacks |
| 108 void OnCacheMatchCallback( | 133 void OnCacheMatchCallback( |
| 134 int thread_id, |
| 109 int request_id, | 135 int request_id, |
| 110 const scoped_refptr<ServiceWorkerCache>& cache, | 136 const scoped_refptr<ServiceWorkerCache>& cache, |
| 111 ServiceWorkerCache::ErrorType error, | 137 ServiceWorkerCache::ErrorType error, |
| 112 scoped_ptr<ServiceWorkerResponse> response, | 138 scoped_ptr<ServiceWorkerResponse> response, |
| 113 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 139 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 114 void OnCacheKeysCallback(int request_id, | 140 void OnCacheKeysCallback(int thread_id, |
| 141 int request_id, |
| 115 const scoped_refptr<ServiceWorkerCache>& cache, | 142 const scoped_refptr<ServiceWorkerCache>& cache, |
| 116 ServiceWorkerCache::ErrorType error, | 143 ServiceWorkerCache::ErrorType error, |
| 117 scoped_ptr<ServiceWorkerCache::Requests> requests); | 144 scoped_ptr<ServiceWorkerCache::Requests> requests); |
| 118 void OnCacheDeleteCallback(int request_id, | 145 void OnCacheDeleteCallback(int thread_id, |
| 146 int request_id, |
| 119 const scoped_refptr<ServiceWorkerCache>& cache, | 147 const scoped_refptr<ServiceWorkerCache>& cache, |
| 120 ServiceWorkerCache::ErrorType error); | 148 ServiceWorkerCache::ErrorType error); |
| 121 void OnCachePutCallback(int request_id, | 149 void OnCachePutCallback(int thread_id, |
| 150 int request_id, |
| 122 const scoped_refptr<ServiceWorkerCache>& cache, | 151 const scoped_refptr<ServiceWorkerCache>& cache, |
| 123 ServiceWorkerCache::ErrorType error, | 152 ServiceWorkerCache::ErrorType error, |
| 124 scoped_ptr<ServiceWorkerResponse> response, | 153 scoped_ptr<ServiceWorkerResponse> response, |
| 125 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 154 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 126 | 155 |
| 127 // Hangs onto a scoped_refptr for the cache if it isn't already doing so. | 156 // Hangs onto a scoped_refptr for the cache if it isn't already doing so. |
| 128 // Returns a unique cache_id. Call DropCacheReference when the client is done | 157 // Returns a unique cache_id. Call DropCacheReference when the client is done |
| 129 // with this cache. | 158 // with this cache. |
| 130 CacheID StoreCacheReference(const scoped_refptr<ServiceWorkerCache>& cache); | 159 CacheID StoreCacheReference(const scoped_refptr<ServiceWorkerCache>& cache); |
| 131 void DropCacheReference(CacheID cache_id); | 160 void DropCacheReference(CacheID cache_id); |
| 132 | 161 |
| 133 // Stores blob handles while waiting for acknowledgement of receipt from the | 162 // Stores blob handles while waiting for acknowledgement of receipt from the |
| 134 // renderer. | 163 // renderer. |
| 135 void StoreBlobDataHandle( | 164 void StoreBlobDataHandle( |
| 136 scoped_ptr<storage::BlobDataHandle> blob_data_handle); | 165 scoped_ptr<storage::BlobDataHandle> blob_data_handle); |
| 137 void DropBlobDataHandle(std::string uuid); | 166 void DropBlobDataHandle(std::string uuid); |
| 138 | 167 |
| 139 // The ServiceWorkerVersion to use for messaging back to the renderer thread. | 168 // Pointer to the context that owns this instance. |
| 140 ServiceWorkerVersion* version_; | 169 CacheStorageDispatcherHost* dispatcher_; |
| 141 | 170 |
| 142 // The ServiceWorkerContextCore should always outlive this. | 171 scoped_refptr<CacheStorageContextImpl> context_; |
| 143 base::WeakPtr<ServiceWorkerContextCore> context_; | |
| 144 | 172 |
| 145 IDToCacheMap id_to_cache_map_; | 173 IDToCacheMap id_to_cache_map_; |
| 146 CacheID next_cache_id_; | 174 CacheID next_cache_id_ = 0; |
| 147 | 175 |
| 148 UUIDToBlobDataHandleList blob_handle_store_; | 176 UUIDToBlobDataHandleList blob_handle_store_; |
| 149 | 177 |
| 150 base::WeakPtrFactory<ServiceWorkerCacheListener> weak_factory_; | 178 base::WeakPtrFactory<ServiceWorkerCacheListener> weak_factory_; |
| 151 | 179 |
| 152 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheListener); | 180 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheListener); |
| 153 }; | 181 }; |
| 154 | 182 |
| 155 } // namespace content | 183 } // namespace content |
| 156 | 184 |
| 157 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ | 185 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_ |
| OLD | NEW |