Home | History | Annotate | Download | only in service_worker
      1 // Copyright 2013 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_STORAGE_H_
      6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
      7 
      8 #include <map>
      9 
     10 #include "base/bind.h"
     11 #include "base/files/file_path.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "content/browser/service_worker/service_worker_registration_status.h"
     15 #include "content/common/content_export.h"
     16 #include "url/gurl.h"
     17 
     18 namespace quota {
     19 class QuotaManagerProxy;
     20 }
     21 
     22 namespace content {
     23 
     24 class ServiceWorkerRegistration;
     25 class ServiceWorkerRegisterJob;
     26 
     27 // This class provides an interface to load registration data and
     28 // instantiate ServiceWorkerRegistration objects. Any asynchronous
     29 // operations are run through instances of ServiceWorkerRegisterJob.
     30 class CONTENT_EXPORT ServiceWorkerStorage {
     31  public:
     32   ServiceWorkerStorage(const base::FilePath& path,
     33                        quota::QuotaManagerProxy* quota_manager_proxy);
     34   ~ServiceWorkerStorage();
     35 
     36   typedef base::Callback<void(ServiceWorkerRegistrationStatus status,
     37                               const scoped_refptr<ServiceWorkerRegistration>&
     38                                   registration)> RegistrationCallback;
     39   typedef base::Callback<
     40       void(ServiceWorkerRegistrationStatus status)> UnregistrationCallback;
     41 
     42   // `found` is only valid if status == REGISTRATION_OK.
     43   typedef base::Callback<void(bool found,
     44                               ServiceWorkerRegistrationStatus status,
     45                               const scoped_refptr<ServiceWorkerRegistration>&
     46                                   registration)> FindRegistrationCallback;
     47 
     48   void FindRegistrationForDocument(const GURL& document_url,
     49                                    const FindRegistrationCallback& callback);
     50   void FindRegistrationForPattern(const GURL& pattern,
     51                                   const FindRegistrationCallback& callback);
     52 
     53   void Register(const GURL& pattern,
     54                 const GURL& script_url,
     55                 const RegistrationCallback& callback);
     56 
     57   void Unregister(const GURL& pattern, const UnregistrationCallback& callback);
     58 
     59  private:
     60   friend class ServiceWorkerRegisterJob;
     61   FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStorageTest, PatternMatches);
     62 
     63   typedef std::map<GURL, scoped_refptr<ServiceWorkerRegistration> >
     64       PatternToRegistrationMap;
     65   typedef ScopedVector<ServiceWorkerRegisterJob> RegistrationJobList;
     66 
     67   // TODO(alecflett): These are temporary internal methods providing
     68   // synchronous in-memory registration. Eventually these will be
     69   // replaced by asynchronous methods that persist registration to disk.
     70   scoped_refptr<ServiceWorkerRegistration> RegisterInternal(
     71       const GURL& pattern,
     72       const GURL& script_url);
     73   void UnregisterInternal(const GURL& pattern);
     74   static bool PatternMatches(const GURL& pattern, const GURL& script_url);
     75 
     76   // Jobs are removed whenever they are finished or canceled.
     77   void EraseJob(ServiceWorkerRegisterJob* job);
     78 
     79   // Called at ServiceWorkerRegisterJob completion.
     80   void RegisterComplete(const RegistrationCallback& callback,
     81                         ServiceWorkerRegisterJob* job,
     82                         ServiceWorkerRegistrationStatus status,
     83                         ServiceWorkerRegistration* registration);
     84 
     85   // Called at ServiceWorkerRegisterJob completion.
     86   void UnregisterComplete(const UnregistrationCallback& callback,
     87                           ServiceWorkerRegisterJob* job,
     88                           ServiceWorkerRegistrationStatus status,
     89                           ServiceWorkerRegistration* registration);
     90 
     91   // This is the in-memory registration. Eventually the registration will be
     92   // persisted to disk.
     93   // A list of currently running jobs. This is a temporary structure until we
     94   // start managing overlapping registrations explicitly.
     95   RegistrationJobList registration_jobs_;
     96 
     97   // in-memory map, to eventually be replaced with persistence
     98   PatternToRegistrationMap registration_by_pattern_;
     99   scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_;
    100   base::FilePath path_;
    101   base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage);
    104 };
    105 
    106 }  // namespace content
    107 
    108 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
    109