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.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_
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/common/content_export.h"
15 #include "content/common/service_worker/service_worker_status_code.h"
19 class QuotaManagerProxy
;
24 class ServiceWorkerRegistration
;
26 // This class provides an interface to load registration data and
27 // instantiate ServiceWorkerRegistration objects.
28 class CONTENT_EXPORT ServiceWorkerStorage
{
30 typedef base::Callback
<void(ServiceWorkerStatusCode status
)> StatusCallback
;
31 typedef base::Callback
<void(ServiceWorkerStatusCode status
,
32 const scoped_refptr
<ServiceWorkerRegistration
>&
33 registration
)> FindRegistrationCallback
;
35 ServiceWorkerStorage(const base::FilePath
& path
,
36 quota::QuotaManagerProxy
* quota_manager_proxy
);
37 ~ServiceWorkerStorage();
39 // Finds registration for |document_url| or |pattern|.
40 // Returns SERVICE_WORKER_OK with non-null registration if registration
41 // is found, or returns SERVICE_WORKER_ERROR_NOT_FOUND if no matching
42 // registration is found.
43 void FindRegistrationForDocument(const GURL
& document_url
,
44 const FindRegistrationCallback
& callback
);
45 void FindRegistrationForPattern(const GURL
& pattern
,
46 const FindRegistrationCallback
& callback
);
48 // Stores |registration|. Returns SERVICE_WORKER_ERROR_EXISTS if
49 // conflicting registration (which has different script_url) is
50 // already registered for the |registration|->pattern().
51 void StoreRegistration(ServiceWorkerRegistration
* registration
,
52 const StatusCallback
& callback
);
54 // Deletes |registration|. This may return SERVICE_WORKER_ERROR_NOT_FOUND
55 // if no matching registration is found.
56 void DeleteRegistration(const GURL
& pattern
,
57 const StatusCallback
& callback
);
59 // Returns new registration ID which is guaranteed to be unique in
61 int64
NewRegistrationId();
64 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStorageTest
, PatternMatches
);
66 typedef std::map
<GURL
, scoped_refptr
<ServiceWorkerRegistration
> >
67 PatternToRegistrationMap
;
69 static bool PatternMatches(const GURL
& pattern
, const GURL
& script_url
);
71 // This is the in-memory registration. Eventually the registration will be
73 PatternToRegistrationMap registration_by_pattern_
;
75 int last_registration_id_
;
77 scoped_refptr
<quota::QuotaManagerProxy
> quota_manager_proxy_
;
80 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage
);
83 } // namespace content
85 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_