| 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" |
| 12 #include "content/browser/bad_message.h" |
| 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 12 #include "content/common/background_fetch/background_fetch_types.h" | 14 #include "content/common/background_fetch/background_fetch_types.h" |
| 13 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" | 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 15 #include "url/origin.h" | 17 #include "url/origin.h" |
| 16 | 18 |
| 17 namespace content { | 19 namespace content { |
| 18 | 20 |
| 21 namespace { |
| 22 |
| 23 // Maximum length of a developer-provided tag for a Background Fetch. |
| 24 constexpr size_t kMaxTagLength = 1024 * 1024; |
| 25 |
| 26 // Maximum length of a developer-provided title for a Background Fetch. |
| 27 constexpr size_t kMaxTitleLength = 1024 * 1024; |
| 28 |
| 29 } // namespace |
| 30 |
| 19 // static | 31 // static |
| 20 void BackgroundFetchServiceImpl::Create( | 32 void BackgroundFetchServiceImpl::Create( |
| 33 int render_process_id, |
| 21 scoped_refptr<BackgroundFetchContext> background_fetch_context, | 34 scoped_refptr<BackgroundFetchContext> background_fetch_context, |
| 22 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, | |
| 23 blink::mojom::BackgroundFetchServiceRequest request) { | 35 blink::mojom::BackgroundFetchServiceRequest request) { |
| 24 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 36 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 25 mojo::MakeStrongBinding(base::MakeUnique<BackgroundFetchServiceImpl>( | 37 mojo::MakeStrongBinding( |
| 26 std::move(background_fetch_context), | 38 base::MakeUnique<BackgroundFetchServiceImpl>( |
| 27 std::move(service_worker_context)), | 39 render_process_id, std::move(background_fetch_context)), |
| 28 std::move(request)); | 40 std::move(request)); |
| 29 } | 41 } |
| 30 | 42 |
| 31 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( | 43 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( |
| 32 scoped_refptr<BackgroundFetchContext> background_fetch_context, | 44 int render_process_id, |
| 33 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 45 scoped_refptr<BackgroundFetchContext> background_fetch_context) |
| 34 : background_fetch_context_(std::move(background_fetch_context)), | 46 : render_process_id_(render_process_id), |
| 35 service_worker_context_(std::move(service_worker_context)) { | 47 background_fetch_context_(std::move(background_fetch_context)) { |
| 36 DCHECK(background_fetch_context_); | 48 DCHECK(background_fetch_context_); |
| 37 DCHECK(service_worker_context_); | |
| 38 } | 49 } |
| 39 | 50 |
| 40 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; | 51 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; |
| 41 | 52 |
| 42 void BackgroundFetchServiceImpl::Fetch(int64_t service_worker_registration_id, | 53 void BackgroundFetchServiceImpl::Fetch(int64_t service_worker_registration_id, |
| 43 const url::Origin& origin, | 54 const url::Origin& origin, |
| 44 const std::string& tag, | 55 const std::string& tag, |
| 45 const BackgroundFetchOptions& options, | 56 const BackgroundFetchOptions& options, |
| 46 const FetchCallback& callback) { | 57 const FetchCallback& callback) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 58 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 59 if (!ValidateTag(tag)) { |
| 60 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 61 base::nullopt /* registration */); |
| 62 return; |
| 63 } |
| 48 | 64 |
| 49 // TODO(peter): Create a new job with the BackgroundFetchContext for the | 65 BackgroundFetchRegistrationId registration_id(service_worker_registration_id, |
| 50 // given tag, requests and options. For now we return a registration that's | 66 origin, tag); |
| 51 // based on the given |options|, to make sure round-trip is covered. | |
| 52 | 67 |
| 53 BackgroundFetchRegistration registration; | 68 // TODO(peter): Remove once https://codereview.chromium.org/2762303002/ lands. |
| 54 registration.tag = tag; | 69 std::vector<ServiceWorkerFetchRequest> requests; |
| 55 registration.icons = options.icons; | 70 requests.emplace_back(GURL("https://example.com/image.png"), "POST", |
| 56 registration.title = options.title; | 71 ServiceWorkerHeaderMap(), Referrer(), |
| 57 registration.total_download_size = options.total_download_size; | 72 false /* is_reload */); |
| 58 | 73 |
| 59 callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); | 74 if (!ValidateRequests(requests)) { |
| 75 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 76 base::nullopt /* registration */); |
| 77 return; |
| 78 } |
| 79 |
| 80 background_fetch_context_->StartFetch(registration_id, requests, options, |
| 81 callback); |
| 60 } | 82 } |
| 61 | 83 |
| 62 void BackgroundFetchServiceImpl::UpdateUI( | 84 void BackgroundFetchServiceImpl::UpdateUI( |
| 63 int64_t service_worker_registration_id, | 85 int64_t service_worker_registration_id, |
| 64 const url::Origin& origin, | 86 const url::Origin& origin, |
| 65 const std::string& tag, | 87 const std::string& tag, |
| 66 const std::string& title, | 88 const std::string& title, |
| 67 const UpdateUICallback& callback) { | 89 const UpdateUICallback& callback) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 91 if (!ValidateTag(tag) || !ValidateTitle(title)) { |
| 92 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); |
| 93 return; |
| 94 } |
| 69 | 95 |
| 70 // TODO(peter): Get the BackgroundFetchJobController for the | 96 // TODO(peter): Get the BackgroundFetchJobController for the |
| 71 // {service_worker_registration_id, tag} pair and call UpdateUI() on it. | 97 // {service_worker_registration_id, tag} pair and call UpdateUI() on it. |
| 72 | 98 |
| 73 callback.Run(blink::mojom::BackgroundFetchError::NONE); | 99 callback.Run(blink::mojom::BackgroundFetchError::NONE); |
| 74 } | 100 } |
| 75 | 101 |
| 76 void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, | 102 void BackgroundFetchServiceImpl::Abort(int64_t service_worker_registration_id, |
| 77 const url::Origin& origin, | 103 const url::Origin& origin, |
| 78 const std::string& tag, | 104 const std::string& tag, |
| 79 const AbortCallback& callback) { | 105 const AbortCallback& callback) { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 107 if (!ValidateTag(tag)) { |
| 108 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT); |
| 109 return; |
| 110 } |
| 81 | 111 |
| 82 // TODO(peter): Get the BackgroundFetchJobController for the | 112 // TODO(peter): Get the BackgroundFetchJobController for the |
| 83 // {service_worker_registration_id, tag} pair and call Abort() on it. | 113 // {service_worker_registration_id, tag} pair and call Abort() on it. |
| 84 | 114 |
| 85 callback.Run(blink::mojom::BackgroundFetchError::NONE); | 115 callback.Run(blink::mojom::BackgroundFetchError::NONE); |
| 86 } | 116 } |
| 87 | 117 |
| 88 void BackgroundFetchServiceImpl::GetRegistration( | 118 void BackgroundFetchServiceImpl::GetRegistration( |
| 89 int64_t service_worker_registration_id, | 119 int64_t service_worker_registration_id, |
| 90 const url::Origin& origin, | 120 const url::Origin& origin, |
| 91 const std::string& tag, | 121 const std::string& tag, |
| 92 const GetRegistrationCallback& callback) { | 122 const GetRegistrationCallback& callback) { |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 124 if (!ValidateTag(tag)) { |
| 125 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 126 base::nullopt /* registration */); |
| 127 return; |
| 128 } |
| 94 | 129 |
| 95 // TODO(peter): Get the registration for {service_worker_registration_id, tag} | 130 // TODO(peter): Get the registration for {service_worker_registration_id, tag} |
| 96 // and construct a BackgroundFetchRegistrationPtr for it. | 131 // and construct a BackgroundFetchRegistrationPtr for it. |
| 97 | 132 |
| 98 callback.Run(blink::mojom::BackgroundFetchError::NONE, | 133 callback.Run(blink::mojom::BackgroundFetchError::NONE, |
| 99 base::nullopt /* registration */); | 134 base::nullopt /* registration */); |
| 100 } | 135 } |
| 101 | 136 |
| 102 void BackgroundFetchServiceImpl::GetTags(int64_t service_worker_registration_id, | 137 void BackgroundFetchServiceImpl::GetTags(int64_t service_worker_registration_id, |
| 103 const url::Origin& origin, | 138 const url::Origin& origin, |
| 104 const GetTagsCallback& callback) { | 139 const GetTagsCallback& callback) { |
| 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 106 | 141 |
| 107 // TODO(peter): Get the list of active Background Fetches associated with | 142 // TODO(peter): Get the list of active Background Fetches associated with |
| 108 // service_worker_registration_id and share their tags. | 143 // service_worker_registration_id and share their tags. |
| 109 | 144 |
| 110 callback.Run(blink::mojom::BackgroundFetchError::NONE, | 145 callback.Run(blink::mojom::BackgroundFetchError::NONE, |
| 111 std::vector<std::string>()); | 146 std::vector<std::string>()); |
| 112 } | 147 } |
| 113 | 148 |
| 149 bool BackgroundFetchServiceImpl::ValidateTag(const std::string& tag) { |
| 150 if (tag.empty() || tag.size() > kMaxTagLength) { |
| 151 bad_message::ReceivedBadMessage(render_process_id_, |
| 152 bad_message::BFSI_INVALID_TAG); |
| 153 return false; |
| 154 } |
| 155 |
| 156 return true; |
| 157 } |
| 158 |
| 159 bool BackgroundFetchServiceImpl::ValidateRequests( |
| 160 const std::vector<ServiceWorkerFetchRequest>& requests) { |
| 161 if (requests.empty()) { |
| 162 bad_message::ReceivedBadMessage(render_process_id_, |
| 163 bad_message::BFSI_INVALID_REQUESTS); |
| 164 return false; |
| 165 } |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
| 170 bool BackgroundFetchServiceImpl::ValidateTitle(const std::string& title) { |
| 171 if (title.empty() || title.size() > kMaxTitleLength) { |
| 172 bad_message::ReceivedBadMessage(render_process_id_, |
| 173 bad_message::BFSI_INVALID_TITLE); |
| 174 return false; |
| 175 } |
| 176 |
| 177 return true; |
| 178 } |
| 179 |
| 114 } // namespace content | 180 } // namespace content |
| OLD | NEW |