| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/browser/background_fetch/background_fetch_service_impl.h" | 5 #include "content/browser/background_fetch/background_fetch_service_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/optional.h" | 9 #include "base/optional.h" |
| 10 #include "content/browser/background_fetch/background_fetch_context.h" | 10 #include "content/browser/background_fetch/background_fetch_context.h" |
| 11 #include "content/browser/background_fetch/background_fetch_job_controller.h" |
| 11 #include "content/browser/background_fetch/background_fetch_registration_id.h" | 12 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 12 #include "content/browser/bad_message.h" | 13 #include "content/browser/bad_message.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/common/background_fetch/background_fetch_types.h" | 15 #include "content/common/background_fetch/background_fetch_types.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "mojo/public/cpp/bindings/strong_binding.h" | 17 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 17 #include "url/origin.h" | 18 #include "url/origin.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 const url::Origin& origin, | 87 const url::Origin& origin, |
| 87 const std::string& tag, | 88 const std::string& tag, |
| 88 const std::string& title, | 89 const std::string& title, |
| 89 const UpdateUICallback& callback) { | 90 const UpdateUICallback& callback) { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 if (!ValidateTag(tag) || !ValidateTitle(title)) { | 92 if (!ValidateTag(tag) || !ValidateTitle(title)) { |
| 92 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); | 93 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); |
| 93 return; | 94 return; |
| 94 } | 95 } |
| 95 | 96 |
| 96 // TODO(peter): Get the BackgroundFetchJobController for the | 97 BackgroundFetchJobController* controller = |
| 97 // {service_worker_registration_id, tag} pair and call UpdateUI() on it. | 98 background_fetch_context_->GetActiveFetch(BackgroundFetchRegistrationId( |
| 99 service_worker_registration_id, origin, tag)); |
| 98 | 100 |
| 99 callback.Run(blink::mojom::BackgroundFetchError::NONE); | 101 if (controller) |
| 102 controller->UpdateUI(title); |
| 103 |
| 104 callback.Run(controller ? blink::mojom::BackgroundFetchError::NONE |
| 105 : blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 100 } | 106 } |
| 101 | 107 |
| 102 void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, | 108 void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, |
| 103 const url::Origin& origin, | 109 const url::Origin& origin, |
| 104 const std::string& tag, | 110 const std::string& tag, |
| 105 const AbortCallback& callback) { | 111 const AbortCallback& callback) { |
| 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 107 if (!ValidateTag(tag)) { | 113 if (!ValidateTag(tag)) { |
| 108 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); | 114 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); |
| 109 return; | 115 return; |
| 110 } | 116 } |
| 111 | 117 |
| 112 // TODO(peter): Get the BackgroundFetchJobController for the | 118 BackgroundFetchJobController* controller = |
| 113 // {service_worker_registration_id, tag} pair and call Abort() on it. | 119 background_fetch_context_->GetActiveFetch(BackgroundFetchRegistrationId( |
| 120 service_worker_registration_id, origin, tag)); |
| 114 | 121 |
| 115 callback.Run(blink::mojom::BackgroundFetchError::NONE); | 122 if (controller) |
| 123 controller->Abort(); |
| 124 |
| 125 callback.Run(controller ? blink::mojom::BackgroundFetchError::NONE |
| 126 : blink::mojom::BackgroundFetchError::INVALID_TAG); |
| 116 } | 127 } |
| 117 | 128 |
| 118 void BackgroundFetchServiceImpl::GetRegistration( | 129 void BackgroundFetchServiceImpl::GetRegistration( |
| 119 int64_t service_worker_registration_id, | 130 int64_t service_worker_registration_id, |
| 120 const url::Origin& origin, | 131 const url::Origin& origin, |
| 121 const std::string& tag, | 132 const std::string& tag, |
| 122 const GetRegistrationCallback& callback) { | 133 const GetRegistrationCallback& callback) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 134 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 if (!ValidateTag(tag)) { | 135 if (!ValidateTag(tag)) { |
| 125 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, | 136 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 126 base::nullopt /* registration */); | 137 base::nullopt /* registration */); |
| 127 return; | 138 return; |
| 128 } | 139 } |
| 129 | 140 |
| 130 // TODO(peter): Get the registration for {service_worker_registration_id, tag} | 141 BackgroundFetchJobController* controller = |
| 131 // and construct a BackgroundFetchRegistrationPtr for it. | 142 background_fetch_context_->GetActiveFetch(BackgroundFetchRegistrationId( |
| 143 service_worker_registration_id, origin, tag)); |
| 132 | 144 |
| 133 callback.Run(blink::mojom::BackgroundFetchError::NONE, | 145 if (!controller) { |
| 134 base::nullopt /* registration */); | 146 callback.Run(blink::mojom::BackgroundFetchError::INVALID_TAG, |
| 147 base::nullopt /* registration */); |
| 148 return; |
| 149 } |
| 150 |
| 151 // Compile the BackgroundFetchRegistration object that will be given to the |
| 152 // developer, representing the data associated with the |controller|. |
| 153 BackgroundFetchRegistration registration; |
| 154 registration.tag = controller->registration_id().tag(); |
| 155 registration.icons = controller->options().icons; |
| 156 registration.title = controller->options().title; |
| 157 registration.total_download_size = controller->options().total_download_size; |
| 158 |
| 159 callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); |
| 135 } | 160 } |
| 136 | 161 |
| 137 void BackgroundFetchServiceImpl::GetTags(int64_t service_worker_registration_id, | 162 void BackgroundFetchServiceImpl::GetTags(int64_t service_worker_registration_id, |
| 138 const url::Origin& origin, | 163 const url::Origin& origin, |
| 139 const GetTagsCallback& callback) { | 164 const GetTagsCallback& callback) { |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 141 | 166 callback.Run( |
| 142 // TODO(peter): Get the list of active Background Fetches associated with | 167 blink::mojom::BackgroundFetchError::NONE, |
| 143 // service_worker_registration_id and share their tags. | 168 background_fetch_context_->GetActiveTagsForServiceWorkerRegistration( |
| 144 | 169 service_worker_registration_id, origin)); |
| 145 callback.Run(blink::mojom::BackgroundFetchError::NONE, | |
| 146 std::vector<std::string>()); | |
| 147 } | 170 } |
| 148 | 171 |
| 149 bool BackgroundFetchServiceImpl::ValidateTag(const std::string& tag) { | 172 bool BackgroundFetchServiceImpl::ValidateTag(const std::string& tag) { |
| 150 if (tag.empty() || tag.size() > kMaxTagLength) { | 173 if (tag.empty() || tag.size() > kMaxTagLength) { |
| 151 bad_message::ReceivedBadMessage(render_process_id_, | 174 bad_message::ReceivedBadMessage(render_process_id_, |
| 152 bad_message::BFSI_INVALID_TAG); | 175 bad_message::BFSI_INVALID_TAG); |
| 153 return false; | 176 return false; |
| 154 } | 177 } |
| 155 | 178 |
| 156 return true; | 179 return true; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 171 if (title.empty() || title.size() > kMaxTitleLength) { | 194 if (title.empty() || title.size() > kMaxTitleLength) { |
| 172 bad_message::ReceivedBadMessage(render_process_id_, | 195 bad_message::ReceivedBadMessage(render_process_id_, |
| 173 bad_message::BFSI_INVALID_TITLE); | 196 bad_message::BFSI_INVALID_TITLE); |
| 174 return false; | 197 return false; |
| 175 } | 198 } |
| 176 | 199 |
| 177 return true; | 200 return true; |
| 178 } | 201 } |
| 179 | 202 |
| 180 } // namespace content | 203 } // namespace content |
| OLD | NEW |