| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/browser/push_messaging_service.h" | 5 #include "content/public/browser/push_messaging_service.h" |
| 6 | 6 |
| 7 #include "content/browser/push_messaging/push_messaging_message_filter.h" | 7 #include "content/browser/push_messaging/push_messaging_message_filter.h" |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/public/browser/browser_context.h" | 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/storage_partition.h" | 11 #include "content/public/browser/storage_partition.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 const char kNotificationsShownServiceWorkerKey[] = | 17 const char kNotificationsShownServiceWorkerKey[] = |
| 18 "notifications_shown_by_last_few_pushes"; | 18 "notifications_shown_by_last_few_pushes"; |
| 19 | 19 |
| 20 void CallGetNotificationsShownCallbackFromIO( | 20 void CallStringCallbackFromIO( |
| 21 const PushMessagingService::GetNotificationsShownCallback& callback, | 21 const PushMessagingService::StringCallback& callback, |
| 22 const std::string& data, | 22 const std::string& data, |
| 23 ServiceWorkerStatusCode service_worker_status) { | 23 ServiceWorkerStatusCode service_worker_status) { |
| 24 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 24 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 25 bool success = service_worker_status == SERVICE_WORKER_OK; | 25 bool success = service_worker_status == SERVICE_WORKER_OK; |
| 26 bool not_found = service_worker_status == SERVICE_WORKER_ERROR_NOT_FOUND; | 26 bool not_found = service_worker_status == SERVICE_WORKER_ERROR_NOT_FOUND; |
| 27 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 27 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 28 base::Bind(callback, data, success, not_found)); | 28 base::Bind(callback, data, success, not_found)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 void CallResultCallbackFromIO( | 31 void CallResultCallbackFromIO( |
| 32 const ServiceWorkerContext::ResultCallback& callback, | 32 const ServiceWorkerContext::ResultCallback& callback, |
| 33 ServiceWorkerStatusCode service_worker_status) { | 33 ServiceWorkerStatusCode service_worker_status) { |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 34 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 35 bool success = service_worker_status == SERVICE_WORKER_OK; | 35 bool success = service_worker_status == SERVICE_WORKER_OK; |
| 36 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 36 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 37 base::Bind(callback, success)); | 37 base::Bind(callback, success)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 void GetNotificationsShownOnIO( | 40 void GetUserDataOnIO( |
| 41 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_wrapper, | 41 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_wrapper, |
| 42 int64 service_worker_registration_id, | 42 int64 service_worker_registration_id, |
| 43 const PushMessagingService::GetNotificationsShownCallback& callback) { | 43 const std::string& key, |
| 44 const PushMessagingService::StringCallback& callback) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 45 service_worker_context_wrapper->context()->storage()->GetUserData( | 46 service_worker_context_wrapper->context()->storage()->GetUserData( |
| 46 service_worker_registration_id, kNotificationsShownServiceWorkerKey, | 47 service_worker_registration_id, key, |
| 47 base::Bind(&CallGetNotificationsShownCallbackFromIO, callback)); | 48 base::Bind(&CallStringCallbackFromIO, callback)); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void SetNotificationsShownOnIO( | 51 void SetNotificationsShownOnIO( |
| 51 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_wrapper, | 52 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_wrapper, |
| 52 int64 service_worker_registration_id, const GURL& origin, | 53 int64 service_worker_registration_id, const GURL& origin, |
| 53 const std::string& data, | 54 const std::string& data, |
| 54 const PushMessagingService::ResultCallback& callback) { | 55 const PushMessagingService::ResultCallback& callback) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 56 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 56 service_worker_context_wrapper->context()->storage()->StoreUserData( | 57 service_worker_context_wrapper->context()->storage()->StoreUserData( |
| 57 service_worker_registration_id, origin, | 58 service_worker_registration_id, origin, |
| 58 kNotificationsShownServiceWorkerKey, data, | 59 kNotificationsShownServiceWorkerKey, data, |
| 59 base::Bind(&CallResultCallbackFromIO, callback)); | 60 base::Bind(&CallResultCallbackFromIO, callback)); |
| 60 } | 61 } |
| 61 | 62 |
| 62 void OnClearPushRegistrationServiceWorkerKey(ServiceWorkerStatusCode status) { | 63 void OnClearPushRegistrationServiceWorkerKey(ServiceWorkerStatusCode status) { |
| 63 } | 64 } |
| 64 | 65 |
| 65 void ClearPushRegistrationIDOnIO( | 66 void ClearPushRegistrationIDOnIO( |
| 66 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, | 67 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 67 int64 service_worker_registration_id) { | 68 int64 service_worker_registration_id) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 69 | 70 |
| 70 service_worker_context->context()->storage()->ClearUserData( | 71 service_worker_context->context()->storage()->ClearUserData( |
| 71 service_worker_registration_id, | 72 service_worker_registration_id, |
| 72 kPushRegistrationIdServiceWorkerKey, | 73 kPushRegistrationIdServiceWorkerKey, |
| 73 base::Bind(&OnClearPushRegistrationServiceWorkerKey)); | 74 base::Bind(&OnClearPushRegistrationServiceWorkerKey)); |
| 74 } | 75 } |
| 75 | 76 |
| 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())); |
| 84 } |
| 85 |
| 76 } // anonymous namespace | 86 } // anonymous namespace |
| 77 | 87 |
| 78 // static | 88 // static |
| 79 void PushMessagingService::GetNotificationsShownByLastFewPushes( | 89 void PushMessagingService::GetNotificationsShownByLastFewPushes( |
| 80 ServiceWorkerContext* service_worker_context, | 90 ServiceWorkerContext* service_worker_context, |
| 81 int64 service_worker_registration_id, | 91 int64 service_worker_registration_id, |
| 82 const GetNotificationsShownCallback& callback) { | 92 const StringCallback& callback) { |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 84 scoped_refptr<ServiceWorkerContextWrapper> wrapper = | 94 scoped_refptr<ServiceWorkerContextWrapper> wrapper = |
| 85 static_cast<ServiceWorkerContextWrapper*>(service_worker_context); | 95 static_cast<ServiceWorkerContextWrapper*>(service_worker_context); |
| 86 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 96 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 87 base::Bind(&GetNotificationsShownOnIO, | 97 base::Bind(&GetUserDataOnIO, |
| 88 wrapper, | 98 wrapper, |
| 89 service_worker_registration_id, | 99 service_worker_registration_id, |
| 100 kNotificationsShownServiceWorkerKey, |
| 90 callback)); | 101 callback)); |
| 91 } | 102 } |
| 92 | 103 |
| 93 // static | 104 // static |
| 94 void PushMessagingService::SetNotificationsShownByLastFewPushes( | 105 void PushMessagingService::SetNotificationsShownByLastFewPushes( |
| 95 ServiceWorkerContext* service_worker_context, | 106 ServiceWorkerContext* service_worker_context, |
| 96 int64 service_worker_registration_id, | 107 int64 service_worker_registration_id, |
| 97 const GURL& origin, | 108 const GURL& origin, |
| 98 const std::string& notifications_shown, | 109 const std::string& notifications_shown, |
| 99 const ResultCallback& callback) { | 110 const ResultCallback& callback) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 111 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 101 scoped_refptr<ServiceWorkerContextWrapper> wrapper = | 112 scoped_refptr<ServiceWorkerContextWrapper> wrapper = |
| 102 static_cast<ServiceWorkerContextWrapper*>(service_worker_context); | 113 static_cast<ServiceWorkerContextWrapper*>(service_worker_context); |
| 103 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 114 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 104 base::Bind(&SetNotificationsShownOnIO, | 115 base::Bind(&SetNotificationsShownOnIO, |
| 105 wrapper, | 116 wrapper, |
| 106 service_worker_registration_id, | 117 service_worker_registration_id, |
| 107 origin, | 118 origin, |
| 108 notifications_shown, | 119 notifications_shown, |
| 109 callback)); | 120 callback)); |
| 110 } | 121 } |
| 111 | 122 |
| 112 // static | 123 // static |
| 124 void PushMessagingService::GetSenderId(BrowserContext* browser_context, |
| 125 const GURL& origin, |
| 126 int64 service_worker_registration_id, |
| 127 const StringCallback& callback) { |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 129 BrowserThread::PostTask( |
| 130 BrowserThread::IO, |
| 131 FROM_HERE, |
| 132 base::Bind(&GetUserDataOnIO, |
| 133 GetServiceWorkerContext(browser_context, origin), |
| 134 service_worker_registration_id, |
| 135 kPushSenderIdServiceWorkerKey, |
| 136 callback)); |
| 137 } |
| 138 |
| 139 // static |
| 113 void PushMessagingService::ClearPushRegistrationID( | 140 void PushMessagingService::ClearPushRegistrationID( |
| 114 BrowserContext* browser_context, | 141 BrowserContext* browser_context, |
| 115 const GURL& origin, | 142 const GURL& origin, |
| 116 int64 service_worker_registration_id) { | 143 int64 service_worker_registration_id) { |
| 117 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 118 StoragePartition* partition = | |
| 119 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | |
| 120 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | |
| 121 static_cast<ServiceWorkerContextWrapper*>( | |
| 122 partition->GetServiceWorkerContext()); | |
| 123 | |
| 124 BrowserThread::PostTask( | 145 BrowserThread::PostTask( |
| 125 BrowserThread::IO, | 146 BrowserThread::IO, |
| 126 FROM_HERE, | 147 FROM_HERE, |
| 127 base::Bind(&ClearPushRegistrationIDOnIO, | 148 base::Bind(&ClearPushRegistrationIDOnIO, |
| 128 service_worker_context, | 149 GetServiceWorkerContext(browser_context, origin), |
| 129 service_worker_registration_id)); | 150 service_worker_registration_id)); |
| 130 } | 151 } |
| 131 | 152 |
| 132 } // namespace content | 153 } // namespace content |
| OLD | NEW |