Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / public / browser / push_messaging_service.cc
bloba1918ac0cbd5eaf8238d2b1090958010e82c52ea
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"
14 namespace content {
16 namespace {
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);
47 void GetUserDataOnIO(
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->GetRegistrationUserData(
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->StoreRegistrationUserData(
65 service_worker_registration_id, origin,
66 kNotificationsShownServiceWorkerKey, data,
67 base::Bind(&CallResultCallbackFromIO, callback));
70 void ClearPushSubscriptionIDOnIO(
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->ClearRegistrationUserData(
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
93 // static
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,
103 wrapper,
104 service_worker_registration_id,
105 kNotificationsShownServiceWorkerKey,
106 callback));
109 // static
110 void PushMessagingService::SetNotificationsShownByLastFewPushes(
111 ServiceWorkerContext* service_worker_context,
112 int64 service_worker_registration_id,
113 const GURL& origin,
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,
121 wrapper,
122 service_worker_registration_id,
123 origin,
124 notifications_shown,
125 callback));
128 // static
129 void PushMessagingService::GetSenderId(BrowserContext* browser_context,
130 const GURL& origin,
131 int64 service_worker_registration_id,
132 const StringCallback& callback) {
133 DCHECK_CURRENTLY_ON(BrowserThread::UI);
134 BrowserThread::PostTask(
135 BrowserThread::IO,
136 FROM_HERE,
137 base::Bind(&GetUserDataOnIO,
138 GetServiceWorkerContext(browser_context, origin),
139 service_worker_registration_id,
140 kPushSenderIdServiceWorkerKey,
141 callback));
144 // static
145 void PushMessagingService::ClearPushSubscriptionID(
146 BrowserContext* browser_context,
147 const GURL& origin,
148 int64 service_worker_registration_id,
149 const base::Closure& callback) {
150 DCHECK_CURRENTLY_ON(BrowserThread::UI);
151 BrowserThread::PostTask(
152 BrowserThread::IO,
153 FROM_HERE,
154 base::Bind(&ClearPushSubscriptionIDOnIO,
155 GetServiceWorkerContext(browser_context, origin),
156 service_worker_registration_id,
157 callback));
160 } // namespace content