| 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_registration_id.h" | 11 #include "content/browser/background_fetch/background_fetch_registration_id.h" |
| 12 #include "content/browser/bad_message.h" | 12 #include "content/browser/bad_message.h" |
| 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 14 #include "content/common/background_fetch/background_fetch_types.h" | 14 #include "content/common/background_fetch/background_fetch_types.h" |
| 15 #include "content/common/service_worker/service_worker_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 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // Maximum length of a developer-provided tag for a Background Fetch. | 24 // Maximum length of a developer-provided tag for a Background Fetch. |
| 24 constexpr size_t kMaxTagLength = 1024 * 1024; | 25 constexpr size_t kMaxTagLength = 1024 * 1024; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( | 44 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( |
| 44 int render_process_id, | 45 int render_process_id, |
| 45 scoped_refptr<BackgroundFetchContext> background_fetch_context) | 46 scoped_refptr<BackgroundFetchContext> background_fetch_context) |
| 46 : render_process_id_(render_process_id), | 47 : render_process_id_(render_process_id), |
| 47 background_fetch_context_(std::move(background_fetch_context)) { | 48 background_fetch_context_(std::move(background_fetch_context)) { |
| 48 DCHECK(background_fetch_context_); | 49 DCHECK(background_fetch_context_); |
| 49 } | 50 } |
| 50 | 51 |
| 51 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; | 52 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; |
| 52 | 53 |
| 53 void BackgroundFetchServiceImpl::Fetch(int64_t service_worker_registration_id, | 54 void BackgroundFetchServiceImpl::Fetch( |
| 54 const url::Origin& origin, | 55 int64_t service_worker_registration_id, |
| 55 const std::string& tag, | 56 const url::Origin& origin, |
| 56 const BackgroundFetchOptions& options, | 57 const std::string& tag, |
| 57 const FetchCallback& callback) { | 58 const std::vector<ServiceWorkerFetchRequest>& requests, |
| 59 const BackgroundFetchOptions& options, |
| 60 const FetchCallback& callback) { |
| 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 59 if (!ValidateTag(tag)) { | 62 if (!ValidateTag(tag)) { |
| 60 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, | 63 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 61 base::nullopt /* registration */); | 64 base::nullopt /* registration */); |
| 62 return; | 65 return; |
| 63 } | 66 } |
| 64 | 67 |
| 65 BackgroundFetchRegistrationId registration_id(service_worker_registration_id, | |
| 66 origin, tag); | |
| 67 | |
| 68 // TODO(peter): Remove once https://codereview.chromium.org/2762303002/ lands. | |
| 69 std::vector<ServiceWorkerFetchRequest> requests; | |
| 70 requests.emplace_back(GURL("https://example.com/image.png"), "POST", | |
| 71 ServiceWorkerHeaderMap(), Referrer(), | |
| 72 false /* is_reload */); | |
| 73 | |
| 74 if (!ValidateRequests(requests)) { | 68 if (!ValidateRequests(requests)) { |
| 75 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, | 69 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 76 base::nullopt /* registration */); | 70 base::nullopt /* registration */); |
| 77 return; | 71 return; |
| 78 } | 72 } |
| 79 | 73 |
| 74 BackgroundFetchRegistrationId registration_id(service_worker_registration_id, |
| 75 origin, tag); |
| 76 |
| 80 background_fetch_context_->StartFetch(registration_id, requests, options, | 77 background_fetch_context_->StartFetch(registration_id, requests, options, |
| 81 callback); | 78 callback); |
| 82 } | 79 } |
| 83 | 80 |
| 84 void BackgroundFetchServiceImpl::UpdateUI( | 81 void BackgroundFetchServiceImpl::UpdateUI( |
| 85 int64_t service_worker_registration_id, | 82 int64_t service_worker_registration_id, |
| 86 const url::Origin& origin, | 83 const url::Origin& origin, |
| 87 const std::string& tag, | 84 const std::string& tag, |
| 88 const std::string& title, | 85 const std::string& title, |
| 89 const UpdateUICallback& callback) { | 86 const UpdateUICallback& callback) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 if (title.empty() || title.size() > kMaxTitleLength) { | 168 if (title.empty() || title.size() > kMaxTitleLength) { |
| 172 bad_message::ReceivedBadMessage(render_process_id_, | 169 bad_message::ReceivedBadMessage(render_process_id_, |
| 173 bad_message::BFSI_INVALID_TITLE); | 170 bad_message::BFSI_INVALID_TITLE); |
| 174 return false; | 171 return false; |
| 175 } | 172 } |
| 176 | 173 |
| 177 return true; | 174 return true; |
| 178 } | 175 } |
| 179 | 176 |
| 180 } // namespace content | 177 } // namespace content |
| OLD | NEW |