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_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
11 #include "content/common/service_worker/service_worker_types.h" | |
12 #include "net/base/completion_callback.h" | |
13 #include "net/disk_cache/disk_cache.h" | |
11 | 14 |
12 namespace net { | 15 namespace net { |
13 class URLRequestContext; | 16 class URLRequestContext; |
17 class IOBufferWithSize; | |
14 } | 18 } |
15 | 19 |
16 namespace webkit_blob { | 20 namespace webkit_blob { |
21 class BlobData; | |
22 class BlobDataHandle; | |
17 class BlobStorageContext; | 23 class BlobStorageContext; |
18 } | 24 } |
19 | 25 |
20 namespace content { | 26 namespace content { |
27 class ChromeBlobStorageContext; | |
21 | 28 |
22 // TODO(jkarlin): Fill this in with a real Cache implementation as | |
23 // specified in | |
24 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | |
25 // TODO(jkarlin): Unload cache backend from memory once the cache object is no | 29 // TODO(jkarlin): Unload cache backend from memory once the cache object is no |
26 // longer referenced in javascript. | 30 // longer referenced in javascript. |
27 | 31 |
28 // Represents a ServiceWorker Cache as seen in | 32 // Represents a ServiceWorker Cache as seen in |
29 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. | 33 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html. |
30 // InitializeIfNeeded must be called before calling the other public members. | 34 // InitializeIfNeeded must be called before calling the other public members. |
31 class ServiceWorkerCache { | 35 class CONTENT_EXPORT ServiceWorkerCache { |
32 public: | 36 public: |
37 enum ErrorType { | |
38 ErrorTypeOK = 0, | |
39 ErrorTypeExists, | |
40 ErrorTypeStorage, | |
41 ErrorTypeNotFound | |
42 }; | |
43 enum EntryIndex { INDEX_HEADERS = 0, INDEX_RESPONSE_BODY }; | |
44 typedef base::Callback<void(ErrorType)> ErrorCallback; | |
45 typedef base::Callback<void(ErrorType, | |
46 scoped_ptr<ServiceWorkerResponse>, | |
47 scoped_ptr<webkit_blob::BlobDataHandle>)> | |
48 ResponseCallback; | |
33 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( | 49 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache( |
34 const std::string& name, | 50 const std::string& name, |
35 net::URLRequestContext* request_context, | 51 net::URLRequestContext* request_context, |
36 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 52 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
37 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( | 53 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache( |
38 const base::FilePath& path, | 54 const base::FilePath& path, |
39 const std::string& name, | 55 const std::string& name, |
40 net::URLRequestContext* request_context, | 56 net::URLRequestContext* request_context, |
41 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 57 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
42 | 58 |
43 virtual ~ServiceWorkerCache(); | 59 virtual ~ServiceWorkerCache(); |
44 | 60 |
45 // Loads the backend and calls the callback with the result (true for | 61 // Loads the backend and calls the callback with the result (true for |
46 // success). This must be called before member functions that require a | 62 // success). This must be called before member functions that require a |
47 // backend are called. | 63 // backend are called. The callback will always be called. |
48 void CreateBackend(const base::Callback<void(bool)>& callback); | 64 void CreateBackend(const ErrorCallback& callback); |
49 | 65 |
66 // Returns ErrorTypeNotFound if not found. The callback will always be called. | |
67 // |request| must remain valid until the callback is called. | |
68 void Match(ServiceWorkerFetchRequest* request, | |
69 const ResponseCallback& callback); | |
70 | |
71 // Puts the request and response object in the cache. The response body (if | |
72 // present) is stored in the cache, but not the request body. Returns | |
73 // ErrorTypeOK on success. The callback will always be called. |request| and | |
74 // |response| must remain valid until the callback is called. | |
75 void Put(ServiceWorkerFetchRequest* request, | |
76 ServiceWorkerResponse* response, | |
77 const ErrorCallback& callback); | |
78 | |
79 // Returns ErrorNotFound if not found. Otherwise deletes and returns | |
80 // ErrorTypeOK. The callback will always be called. |request| must remain vali d | |
michaeln
2014/08/25 21:16:48
nit: linelength
jkarlin
2014/08/25 23:51:46
Done.
| |
81 // until the callback is called. | |
82 void Delete(ServiceWorkerFetchRequest* request, | |
83 const ErrorCallback& callback); | |
84 | |
85 // Call to determine if CreateBackend must be called. | |
86 bool HasCreatedBackend() const; | |
87 | |
88 void set_backend(scoped_ptr<disk_cache::Backend> backend) { | |
89 backend_ = backend.Pass(); | |
90 } | |
50 void set_name(const std::string& name) { name_ = name; } | 91 void set_name(const std::string& name) { name_ = name; } |
51 const std::string& name() const { return name_; } | 92 const std::string& name() const { return name_; } |
52 int32 id() const { return id_; } | 93 int32 id() const { return id_; } |
53 void set_id(int32 id) { id_ = id; } | 94 void set_id(int32 id) { id_ = id; } |
54 | 95 |
55 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); | 96 base::WeakPtr<ServiceWorkerCache> AsWeakPtr(); |
56 | 97 |
57 private: | 98 private: |
58 ServiceWorkerCache( | 99 ServiceWorkerCache( |
59 const base::FilePath& path, | 100 const base::FilePath& path, |
60 const std::string& name, | 101 const std::string& name, |
61 net::URLRequestContext* request_context, | 102 net::URLRequestContext* request_context, |
62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); | 103 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context); |
63 | 104 |
105 scoped_ptr<disk_cache::Backend> backend_; | |
64 base::FilePath path_; | 106 base::FilePath path_; |
65 std::string name_; | 107 std::string name_; |
66 net::URLRequestContext* request_context_; | 108 net::URLRequestContext* request_context_; |
67 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; | 109 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_; |
68 int32 id_; | 110 int32 id_; |
69 | 111 |
70 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; | 112 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_; |
71 | 113 |
72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); | 114 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache); |
73 }; | 115 }; |
74 | 116 |
75 } // namespace content | 117 } // namespace content |
76 | 118 |
77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ | 119 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_ |
OLD | NEW |