Home | History | Annotate | Download | only in service_worker
      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.
      4 
      5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_
      6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_
      7 
      8 #include "base/memory/weak_ptr.h"
      9 #include "base/strings/string16.h"
     10 #include "content/browser/service_worker/embedded_worker_instance.h"
     11 #include "content/browser/service_worker/service_worker_cache_storage.h"
     12 
     13 namespace content {
     14 
     15 struct ServiceWorkerBatchOperation;
     16 struct ServiceWorkerCacheQueryParams;
     17 struct ServiceWorkerFetchRequest;
     18 class ServiceWorkerVersion;
     19 
     20 // This class listens for requests on the Cache APIs, and sends response
     21 // messages to the renderer process. There is one instance per
     22 // ServiceWorkerVersion instance.
     23 class ServiceWorkerCacheListener : public EmbeddedWorkerInstance::Listener {
     24  public:
     25   ServiceWorkerCacheListener(ServiceWorkerVersion* version,
     26                              base::WeakPtr<ServiceWorkerContextCore> context);
     27   virtual ~ServiceWorkerCacheListener();
     28 
     29   // From EmbeddedWorkerInstance::Listener:
     30   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     31 
     32  private:
     33   // The message receiver functions for the CacheStorage API:
     34   void OnCacheStorageGet(int request_id, const base::string16& cache_name);
     35   void OnCacheStorageHas(int request_id, const base::string16& cache_name);
     36   void OnCacheStorageCreate(int request_id,
     37                           const base::string16& cache_name);
     38   void OnCacheStorageDelete(int request_id,
     39                            const base::string16& cache_name);
     40   void OnCacheStorageKeys(int request_id);
     41 
     42   // The message receiver functions for the Cache API:
     43   void OnCacheMatch(int request_id,
     44                     int cache_id,
     45                     const ServiceWorkerFetchRequest& request,
     46                     const ServiceWorkerCacheQueryParams& match_params);
     47   void OnCacheMatchAll(int request_id,
     48                        int cache_id,
     49                        const ServiceWorkerFetchRequest& request,
     50                        const ServiceWorkerCacheQueryParams& match_params);
     51   void OnCacheKeys(int request_id,
     52                    int cache_id,
     53                    const ServiceWorkerFetchRequest& request,
     54                    const ServiceWorkerCacheQueryParams& match_params);
     55   void OnCacheBatch(int request_id,
     56                     int cache_id,
     57                     const std::vector<ServiceWorkerBatchOperation>& operations);
     58   void OnCacheClosed(int cache_id);
     59 
     60  private:
     61   typedef int32_t CacheID;  // TODO(jkarlin): Bump to 64 bit.
     62   typedef std::map<CacheID, scoped_refptr<ServiceWorkerCache> > IDToCacheMap;
     63 
     64   void Send(const IPC::Message& message);
     65 
     66   void OnCacheStorageGetCallback(
     67       int request_id,
     68       const scoped_refptr<ServiceWorkerCache>& cache,
     69       ServiceWorkerCacheStorage::CacheStorageError error);
     70   void OnCacheStorageHasCallback(
     71       int request_id,
     72       bool has_cache,
     73       ServiceWorkerCacheStorage::CacheStorageError error);
     74   void OnCacheStorageCreateCallback(
     75       int request_id,
     76       const scoped_refptr<ServiceWorkerCache>& cache,
     77       ServiceWorkerCacheStorage::CacheStorageError error);
     78   void OnCacheStorageDeleteCallback(
     79       int request_id,
     80       bool deleted,
     81       ServiceWorkerCacheStorage::CacheStorageError error);
     82   void OnCacheStorageKeysCallback(
     83       int request_id,
     84       const std::vector<std::string>& strings,
     85       ServiceWorkerCacheStorage::CacheStorageError error);
     86 
     87   // Hangs onto a scoped_refptr for the cache if it isn't already doing so.
     88   // Returns a unique cache_id. Call DropCacheReference when the client is done
     89   // with this cache.
     90   CacheID StoreCacheReference(const scoped_refptr<ServiceWorkerCache>& cache);
     91   void DropCacheReference(CacheID cache_id);
     92 
     93   // The ServiceWorkerVersion to use for messaging back to the renderer thread.
     94   ServiceWorkerVersion* version_;
     95 
     96   // The ServiceWorkerContextCore should always outlive this.
     97   base::WeakPtr<ServiceWorkerContextCore> context_;
     98 
     99   IDToCacheMap id_to_cache_map_;
    100   CacheID next_cache_id_;
    101 
    102   base::WeakPtrFactory<ServiceWorkerCacheListener> weak_factory_;
    103 
    104   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerCacheListener);
    105 };
    106 
    107 }  // namespace content
    108 
    109 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CACHE_LISTENER_H_
    110