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_CONTEXT_WRAPPER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/common/content_export.h"
15 #include "content/public/browser/service_worker_context.h"
19 class SequencedTaskRunner
;
20 class SingleThreadTaskRunner
;
24 class QuotaManagerProxy
;
25 class SpecialStoragePolicy
;
31 class ServiceWorkerContextCore
;
32 class ServiceWorkerContextObserver
;
33 class StoragePartitionImpl
;
35 // A refcounted wrapper class for our core object. Higher level content lib
36 // classes keep references to this class on mutliple threads. The inner core
37 // instance is strictly single threaded and is not refcounted, the core object
38 // is what is used internally in the service worker lib.
39 class CONTENT_EXPORT ServiceWorkerContextWrapper
40 : NON_EXPORTED_BASE(public ServiceWorkerContext
),
41 public base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
> {
43 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
44 ServiceWorkerContextWrapper(BrowserContext
* browser_context
);
46 // Init and Shutdown are for use on the UI thread when the profile,
47 // storagepartition is being setup and torn down.
48 void Init(const base::FilePath
& user_data_directory
,
49 storage::QuotaManagerProxy
* quota_manager_proxy
,
50 storage::SpecialStoragePolicy
* special_storage_policy
);
53 // Deletes all files on disk and restarts the system asynchronously. This
54 // leaves the system in a disabled state until it's done. This should be
55 // called on the IO thread.
56 void DeleteAndStartOver();
58 // The core context is only for use on the IO thread.
59 // Can be null before/during init, during/after shutdown, and after
60 // DeleteAndStartOver fails.
61 ServiceWorkerContextCore
* context();
63 // The StoragePartition should only be used on the UI thread.
64 // Can be null before/during init and during/after shutdown.
65 StoragePartitionImpl
* storage_partition() const;
67 void set_storage_partition(StoragePartitionImpl
* storage_partition
);
69 // The process manager can be used on either UI or IO.
70 ServiceWorkerProcessManager
* process_manager() {
71 return process_manager_
.get();
74 // ServiceWorkerContext implementation:
75 void RegisterServiceWorker(const GURL
& pattern
,
76 const GURL
& script_url
,
77 const ResultCallback
& continuation
) override
;
78 void UnregisterServiceWorker(const GURL
& pattern
,
79 const ResultCallback
& continuation
) override
;
80 void CanHandleMainResourceOffline(
82 const GURL
& first_party
,
83 const net::CompletionCallback
& callback
) override
;
84 void GetAllOriginsInfo(const GetUsageInfoCallback
& callback
) override
;
85 void DeleteForOrigin(const GURL
& origin_url
) override
;
86 void CheckHasServiceWorker(
88 const GURL
& other_url
,
89 const CheckHasServiceWorkerCallback
& callback
) override
;
91 // DeleteForOrigin with completion callback. Does not exit early, and returns
92 // false if one or more of the deletions fail.
93 virtual void DeleteForOrigin(const GURL
& origin_url
,
94 const ResultCallback
& done
);
96 void StartServiceWorker(const GURL
& pattern
, const StatusCallback
& callback
);
97 void AddObserver(ServiceWorkerContextObserver
* observer
);
98 void RemoveObserver(ServiceWorkerContextObserver
* observer
);
100 bool is_incognito() const { return is_incognito_
; }
103 friend class BackgroundSyncManagerTest
;
104 friend class base::RefCountedThreadSafe
<ServiceWorkerContextWrapper
>;
105 friend class EmbeddedWorkerTestHelper
;
106 friend class ServiceWorkerProcessManager
;
107 friend class MockServiceWorkerContextWrapper
;
109 ~ServiceWorkerContextWrapper() override
;
112 const base::FilePath
& user_data_directory
,
113 scoped_ptr
<ServiceWorkerDatabaseTaskManager
> database_task_manager
,
114 const scoped_refptr
<base::SingleThreadTaskRunner
>& disk_cache_thread
,
115 storage::QuotaManagerProxy
* quota_manager_proxy
,
116 storage::SpecialStoragePolicy
* special_storage_policy
);
119 void DidDeleteAndStartOver(ServiceWorkerStatusCode status
);
121 void DidGetAllRegistrationsForGetAllOrigins(
122 const GetUsageInfoCallback
& callback
,
123 const std::vector
<ServiceWorkerRegistrationInfo
>& registrations
);
125 void DidFindRegistrationForCheckHasServiceWorker(
126 const GURL
& other_url
,
127 const CheckHasServiceWorkerCallback
& callback
,
128 ServiceWorkerStatusCode status
,
129 const scoped_refptr
<ServiceWorkerRegistration
>& registration
);
131 const scoped_refptr
<ObserverListThreadSafe
<ServiceWorkerContextObserver
> >
133 const scoped_ptr
<ServiceWorkerProcessManager
> process_manager_
;
134 // Cleared in Shutdown():
135 scoped_ptr
<ServiceWorkerContextCore
> context_core_
;
137 // Initialized in Init(); true if the user data directory is empty.
140 // Raw pointer to the StoragePartitionImpl owning |this|.
141 StoragePartitionImpl
* storage_partition_
;
144 } // namespace content
146 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_