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 #include "content/public/browser/push_messaging_service.h"
7 #include "content/browser/push_messaging/push_messaging_message_filter.h"
8 #include "content/browser/service_worker/service_worker_context_wrapper.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/storage_partition.h"
17 const char kNotificationsShownServiceWorkerKey
[] =
18 "notifications_shown_by_last_few_pushes";
20 void CallStringCallbackFromIO(
21 const PushMessagingService::StringCallback
& callback
,
22 const std::string
& data
,
23 ServiceWorkerStatusCode service_worker_status
) {
24 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
25 bool success
= service_worker_status
== SERVICE_WORKER_OK
;
26 bool not_found
= service_worker_status
== SERVICE_WORKER_ERROR_NOT_FOUND
;
27 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
28 base::Bind(callback
, data
, success
, not_found
));
31 void CallResultCallbackFromIO(
32 const ServiceWorkerContext::ResultCallback
& callback
,
33 ServiceWorkerStatusCode service_worker_status
) {
34 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
35 bool success
= service_worker_status
== SERVICE_WORKER_OK
;
36 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
37 base::Bind(callback
, success
));
41 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_wrapper
,
42 int64 service_worker_registration_id
,
43 const std::string
& key
,
44 const PushMessagingService::StringCallback
& callback
) {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
46 service_worker_context_wrapper
->context()->storage()->GetUserData(
47 service_worker_registration_id
, key
,
48 base::Bind(&CallStringCallbackFromIO
, callback
));
51 void SetNotificationsShownOnIO(
52 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_wrapper
,
53 int64 service_worker_registration_id
, const GURL
& origin
,
54 const std::string
& data
,
55 const PushMessagingService::ResultCallback
& callback
) {
56 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
57 service_worker_context_wrapper
->context()->storage()->StoreUserData(
58 service_worker_registration_id
, origin
,
59 kNotificationsShownServiceWorkerKey
, data
,
60 base::Bind(&CallResultCallbackFromIO
, callback
));
63 void OnClearPushRegistrationServiceWorkerKey(ServiceWorkerStatusCode status
) {
66 void ClearPushRegistrationIDOnIO(
67 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
68 int64 service_worker_registration_id
) {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
71 service_worker_context
->context()->storage()->ClearUserData(
72 service_worker_registration_id
,
73 kPushRegistrationIdServiceWorkerKey
,
74 base::Bind(&OnClearPushRegistrationServiceWorkerKey
));
77 scoped_refptr
<ServiceWorkerContextWrapper
> GetServiceWorkerContext(
78 BrowserContext
* browser_context
, const GURL
& origin
) {
79 StoragePartition
* partition
=
80 BrowserContext::GetStoragePartitionForSite(browser_context
, origin
);
81 return make_scoped_refptr(
82 static_cast<ServiceWorkerContextWrapper
*>(
83 partition
->GetServiceWorkerContext()));
86 } // anonymous namespace
89 void PushMessagingService::GetNotificationsShownByLastFewPushes(
90 ServiceWorkerContext
* service_worker_context
,
91 int64 service_worker_registration_id
,
92 const StringCallback
& callback
) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
94 scoped_refptr
<ServiceWorkerContextWrapper
> wrapper
=
95 static_cast<ServiceWorkerContextWrapper
*>(service_worker_context
);
96 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
97 base::Bind(&GetUserDataOnIO
,
99 service_worker_registration_id
,
100 kNotificationsShownServiceWorkerKey
,
105 void PushMessagingService::SetNotificationsShownByLastFewPushes(
106 ServiceWorkerContext
* service_worker_context
,
107 int64 service_worker_registration_id
,
109 const std::string
& notifications_shown
,
110 const ResultCallback
& callback
) {
111 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
112 scoped_refptr
<ServiceWorkerContextWrapper
> wrapper
=
113 static_cast<ServiceWorkerContextWrapper
*>(service_worker_context
);
114 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
115 base::Bind(&SetNotificationsShownOnIO
,
117 service_worker_registration_id
,
124 void PushMessagingService::GetSenderId(BrowserContext
* browser_context
,
126 int64 service_worker_registration_id
,
127 const StringCallback
& callback
) {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
129 BrowserThread::PostTask(
132 base::Bind(&GetUserDataOnIO
,
133 GetServiceWorkerContext(browser_context
, origin
),
134 service_worker_registration_id
,
135 kPushSenderIdServiceWorkerKey
,
140 void PushMessagingService::ClearPushRegistrationID(
141 BrowserContext
* browser_context
,
143 int64 service_worker_registration_id
) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
145 BrowserThread::PostTask(
148 base::Bind(&ClearPushRegistrationIDOnIO
,
149 GetServiceWorkerContext(browser_context
, origin
),
150 service_worker_registration_id
));
153 } // namespace content