Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_cache.h
blob42393a29e1ea10cfff90b9b8babb2f788bd89d3e
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_
8 #include "base/callback.h"
9 #include "base/files/file_path.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"
15 namespace net {
16 class URLRequestContext;
17 class IOBufferWithSize;
20 namespace storage {
21 class BlobData;
22 class BlobDataHandle;
23 class BlobStorageContext;
26 namespace content {
27 class ChromeBlobStorageContext;
29 // TODO(jkarlin): Unload cache backend from memory once the cache object is no
30 // longer referenced in javascript.
32 // Represents a ServiceWorker Cache as seen in
33 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
34 // InitializeIfNeeded must be called before calling the other public members.
35 class CONTENT_EXPORT ServiceWorkerCache {
36 public:
37 enum ErrorType {
38 ErrorTypeOK = 0,
39 ErrorTypeExists,
40 ErrorTypeStorage,
41 ErrorTypeNotFound
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<storage::BlobDataHandle>)>
48 ResponseCallback;
49 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache(
50 net::URLRequestContext* request_context,
51 base::WeakPtr<storage::BlobStorageContext> blob_context);
52 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache(
53 const base::FilePath& path,
54 net::URLRequestContext* request_context,
55 base::WeakPtr<storage::BlobStorageContext> blob_context);
57 virtual ~ServiceWorkerCache();
59 // Loads the backend and calls the callback with the result (true for
60 // success). This must be called before member functions that require a
61 // backend are called. The callback will always be called.
62 void CreateBackend(const ErrorCallback& callback);
64 // Returns ErrorTypeNotFound if not found. The callback will always be called.
65 // |request| must remain valid until the callback is called.
66 void Match(ServiceWorkerFetchRequest* request,
67 const ResponseCallback& callback);
69 // Puts the request and response object in the cache. The response body (if
70 // present) is stored in the cache, but not the request body. Returns
71 // ErrorTypeOK on success. The callback will always be called. |request| and
72 // |response| must remain valid until the callback is called.
73 void Put(ServiceWorkerFetchRequest* request,
74 ServiceWorkerResponse* response,
75 const ErrorCallback& callback);
77 // Returns ErrorNotFound if not found. Otherwise deletes and returns
78 // ErrorTypeOK. The callback will always be called. |request| must remain
79 // valid until the callback is called.
80 void Delete(ServiceWorkerFetchRequest* request,
81 const ErrorCallback& callback);
83 // Call to determine if CreateBackend must be called.
84 bool HasCreatedBackend() const;
86 void set_backend(scoped_ptr<disk_cache::Backend> backend) {
87 backend_ = backend.Pass();
90 base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
92 private:
93 ServiceWorkerCache(const base::FilePath& path,
94 net::URLRequestContext* request_context,
95 base::WeakPtr<storage::BlobStorageContext> blob_context);
97 scoped_ptr<disk_cache::Backend> backend_;
98 base::FilePath path_;
99 net::URLRequestContext* request_context_;
100 base::WeakPtr<storage::BlobStorageContext> blob_storage_context_;
102 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
104 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache);
107 } // namespace content
109 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_