1 // Copyright 2015 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_WATCHER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WATCHER_H_
10 #include "base/callback.h"
11 #include "base/containers/scoped_ptr_hash_map.h"
12 #include "content/browser/service_worker/service_worker_context_observer.h"
13 #include "content/browser/service_worker/service_worker_info.h"
17 class ServiceWorkerContextWrapper
;
19 // Used to monitor the status change of the ServiceWorker registrations and
20 // versions in the ServiceWorkerContext from UI thread.
21 class ServiceWorkerContextWatcher
22 : public ServiceWorkerContextObserver
,
23 public base::RefCountedThreadSafe
<ServiceWorkerContextWatcher
> {
25 typedef base::Callback
<void(
26 const std::vector
<ServiceWorkerRegistrationInfo
>&)>
27 WorkerRegistrationUpdatedCallback
;
28 typedef base::Callback
<void(const std::vector
<ServiceWorkerVersionInfo
>&)>
29 WorkerVersionUpdatedCallback
;
30 typedef base::Callback
<void(int64
/* registration_id */,
31 int64
/* version_id */,
32 const ErrorInfo
&)> WorkerErrorReportedCallback
;
34 ServiceWorkerContextWatcher(
35 scoped_refptr
<ServiceWorkerContextWrapper
> context
,
36 const WorkerRegistrationUpdatedCallback
& registration_callback
,
37 const WorkerVersionUpdatedCallback
& version_callback
,
38 const WorkerErrorReportedCallback
& error_callback
);
43 friend class base::RefCountedThreadSafe
<ServiceWorkerContextWatcher
>;
44 ~ServiceWorkerContextWatcher() override
;
46 void GetStoredRegistrationsOnIOThread();
47 void OnStoredRegistrationsOnIOThread(
48 const std::vector
<ServiceWorkerRegistrationInfo
>& stored_registrations
);
49 void StopOnIOThread();
51 void StoreRegistrationInfo(
52 const ServiceWorkerRegistrationInfo
& registration
,
53 base::ScopedPtrHashMap
<int64
, ServiceWorkerRegistrationInfo
>* info_map
);
54 void StoreVersionInfo(const ServiceWorkerVersionInfo
& version
);
56 void SendRegistrationInfo(
57 int64 registration_id
,
59 ServiceWorkerRegistrationInfo::DeleteFlag delete_flag
);
60 void SendVersionInfo(const ServiceWorkerVersionInfo
& version
);
62 // ServiceWorkerContextObserver implements
63 void OnNewLiveRegistration(int64 registration_id
,
64 const GURL
& pattern
) override
;
65 void OnNewLiveVersion(int64 version_id
,
66 int64 registration_id
,
67 const GURL
& script_url
) override
;
68 void OnRunningStateChanged(
70 content::ServiceWorkerVersion::RunningStatus running_status
) override
;
71 void OnVersionStateChanged(
73 content::ServiceWorkerVersion::Status status
) override
;
74 void OnMainScriptHttpResponseInfoSet(
76 base::Time script_response_time
,
77 base::Time script_last_modified
) override
;
78 void OnErrorReported(int64 version_id
,
81 const ErrorInfo
& info
) override
;
82 void OnReportConsoleMessage(int64 version_id
,
85 const ConsoleMessage
& message
) override
;
86 void OnRegistrationStored(int64 registration_id
,
87 const GURL
& pattern
) override
;
88 void OnRegistrationDeleted(int64 registration_id
,
89 const GURL
& pattern
) override
;
91 base::ScopedPtrHashMap
<int64
, ServiceWorkerVersionInfo
> version_info_map_
;
92 scoped_refptr
<ServiceWorkerContextWrapper
> context_
;
93 WorkerRegistrationUpdatedCallback registration_callback_
;
94 WorkerVersionUpdatedCallback version_callback_
;
95 WorkerErrorReportedCallback error_callback_
;
98 } // namespace content
100 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WATCHER_H_