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 "base/callback.h"
8 #include "content/browser/push_messaging/push_messaging_message_filter.h"
9 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/storage_partition.h"
18 const char kNotificationsShownServiceWorkerKey
[] =
19 "notifications_shown_by_last_few_pushes";
21 void CallStringCallbackFromIO(
22 const PushMessagingService::StringCallback
& callback
,
23 const std::string
& data
,
24 ServiceWorkerStatusCode service_worker_status
) {
25 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
26 bool success
= service_worker_status
== SERVICE_WORKER_OK
;
27 bool not_found
= service_worker_status
== SERVICE_WORKER_ERROR_NOT_FOUND
;
28 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
29 base::Bind(callback
, data
, success
, not_found
));
32 void CallResultCallbackFromIO(
33 const ServiceWorkerContext::ResultCallback
& callback
,
34 ServiceWorkerStatusCode service_worker_status
) {
35 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
36 bool success
= service_worker_status
== SERVICE_WORKER_OK
;
37 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
38 base::Bind(callback
, success
));
41 void CallClosureFromIO(const base::Closure
& callback
,
42 ServiceWorkerStatusCode status
) {
43 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
44 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
, callback
);
48 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_wrapper
,
49 int64 service_worker_registration_id
,
50 const std::string
& key
,
51 const PushMessagingService::StringCallback
& callback
) {
52 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
53 service_worker_context_wrapper
->context()->storage()->GetUserData(
54 service_worker_registration_id
, key
,
55 base::Bind(&CallStringCallbackFromIO
, callback
));
58 void SetNotificationsShownOnIO(
59 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_wrapper
,
60 int64 service_worker_registration_id
, const GURL
& origin
,
61 const std::string
& data
,
62 const PushMessagingService::ResultCallback
& callback
) {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
64 service_worker_context_wrapper
->context()->storage()->StoreUserData(
65 service_worker_registration_id
, origin
,
66 kNotificationsShownServiceWorkerKey
, data
,
67 base::Bind(&CallResultCallbackFromIO
, callback
));
70 void ClearPushRegistrationIDOnIO(
71 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
72 int64 service_worker_registration_id
,
73 const base::Closure
& callback
) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
76 service_worker_context
->context()->storage()->ClearUserData(
77 service_worker_registration_id
,
78 kPushRegistrationIdServiceWorkerKey
,
79 base::Bind(&CallClosureFromIO
, callback
));
82 scoped_refptr
<ServiceWorkerContextWrapper
> GetServiceWorkerContext(
83 BrowserContext
* browser_context
, const GURL
& origin
) {
84 StoragePartition
* partition
=
85 BrowserContext::GetStoragePartitionForSite(browser_context
, origin
);
86 return make_scoped_refptr(
87 static_cast<ServiceWorkerContextWrapper
*>(
88 partition
->GetServiceWorkerContext()));
91 } // anonymous namespace
94 void PushMessagingService::GetNotificationsShownByLastFewPushes(
95 ServiceWorkerContext
* service_worker_context
,
96 int64 service_worker_registration_id
,
97 const StringCallback
& callback
) {
98 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
99 scoped_refptr
<ServiceWorkerContextWrapper
> wrapper
=
100 static_cast<ServiceWorkerContextWrapper
*>(service_worker_context
);
101 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
102 base::Bind(&GetUserDataOnIO
,
104 service_worker_registration_id
,
105 kNotificationsShownServiceWorkerKey
,
110 void PushMessagingService::SetNotificationsShownByLastFewPushes(
111 ServiceWorkerContext
* service_worker_context
,
112 int64 service_worker_registration_id
,
114 const std::string
& notifications_shown
,
115 const ResultCallback
& callback
) {
116 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
117 scoped_refptr
<ServiceWorkerContextWrapper
> wrapper
=
118 static_cast<ServiceWorkerContextWrapper
*>(service_worker_context
);
119 BrowserThread::PostTask(BrowserThread::IO
, FROM_HERE
,
120 base::Bind(&SetNotificationsShownOnIO
,
122 service_worker_registration_id
,
129 void PushMessagingService::GetSenderId(BrowserContext
* browser_context
,
131 int64 service_worker_registration_id
,
132 const StringCallback
& callback
) {
133 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
134 BrowserThread::PostTask(
137 base::Bind(&GetUserDataOnIO
,
138 GetServiceWorkerContext(browser_context
, origin
),
139 service_worker_registration_id
,
140 kPushSenderIdServiceWorkerKey
,
145 void PushMessagingService::ClearPushRegistrationID(
146 BrowserContext
* browser_context
,
148 int64 service_worker_registration_id
,
149 const base::Closure
& callback
) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
151 BrowserThread::PostTask(
154 base::Bind(&ClearPushRegistrationIDOnIO
,
155 GetServiceWorkerContext(browser_context
, origin
),
156 service_worker_registration_id
,
160 } // namespace content