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"
13 class URLRequestContext;
16 namespace webkit_blob {
17 class BlobStorageContext;
22 // TODO(jkarlin): Fill this in with a real Cache implementation as
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
26 // longer referenced in javascript.
28 // Represents a ServiceWorker Cache as seen in
29 // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html.
30 // InitializeIfNeeded must be called before calling the other public members.
31 class ServiceWorkerCache {
33 static scoped_ptr<ServiceWorkerCache> CreateMemoryCache(
34 const std::string& name,
35 net::URLRequestContext* request_context,
36 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
37 static scoped_ptr<ServiceWorkerCache> CreatePersistentCache(
38 const base::FilePath& path,
39 const std::string& name,
40 net::URLRequestContext* request_context,
41 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
43 virtual ~ServiceWorkerCache();
45 // Loads the backend and calls the callback with the result (true for
46 // success). This must be called before member functions that require a
47 // backend are called.
48 void CreateBackend(const base::Callback<void(bool)>& callback);
50 void set_name(const std::string& name) { name_ = name; }
51 const std::string& name() const { return name_; }
52 int32 id() const { return id_; }
53 void set_id(int32 id) { id_ = id; }
55 base::WeakPtr<ServiceWorkerCache> AsWeakPtr();
59 const base::FilePath& path,
60 const std::string& name,
61 net::URLRequestContext* request_context,
62 base::WeakPtr<webkit_blob::BlobStorageContext> blob_context);
66 net::URLRequestContext* request_context_;
67 base::WeakPtr<webkit_blob::BlobStorageContext> blob_storage_context_;
70 base::WeakPtrFactory<ServiceWorkerCache> weak_ptr_factory_;
72 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCache);
75 } // namespace content
77 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_H_