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 |
19 // static | 21 // static |
20 void BackgroundFetchServiceImpl::Create( | 22 void BackgroundFetchServiceImpl::Create( |
| 23 int render_process_id, |
21 scoped_refptr<BackgroundFetchContext> background_fetch_context, | 24 scoped_refptr<BackgroundFetchContext> background_fetch_context, |
22 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, | |
23 blink::mojom::BackgroundFetchServiceRequest request) { | 25 blink::mojom::BackgroundFetchServiceRequest request) { |
24 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
25 mojo::MakeStrongBinding(base::MakeUnique<BackgroundFetchServiceImpl>( | 27 mojo::MakeStrongBinding( |
26 std::move(background_fetch_context), | 28 base::MakeUnique<BackgroundFetchServiceImpl>( |
27 std::move(service_worker_context)), | 29 render_process_id, std::move(background_fetch_context)), |
28 std::move(request)); | 30 std::move(request)); |
29 } | 31 } |
30 | 32 |
31 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( | 33 BackgroundFetchServiceImpl::BackgroundFetchServiceImpl( |
32 scoped_refptr<BackgroundFetchContext> background_fetch_context, | 34 int render_process_id, |
33 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) | 35 scoped_refptr<BackgroundFetchContext> background_fetch_context) |
34 : background_fetch_context_(std::move(background_fetch_context)), | 36 : render_process_id_(render_process_id), |
35 service_worker_context_(std::move(service_worker_context)) { | 37 background_fetch_context_(std::move(background_fetch_context)) { |
36 DCHECK(background_fetch_context_); | 38 DCHECK(background_fetch_context_); |
37 DCHECK(service_worker_context_); | |
38 } | 39 } |
39 | 40 |
40 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; | 41 BackgroundFetchServiceImpl::~BackgroundFetchServiceImpl() = default; |
41 | 42 |
42 void BackgroundFetchServiceImpl::Fetch(int64_t service_worker_registration_id, | 43 void BackgroundFetchServiceImpl::Fetch(int64_t service_worker_registration_id, |
43 const url::Origin& origin, | 44 const url::Origin& origin, |
44 const std::string& tag, | 45 const std::string& tag, |
45 const BackgroundFetchOptions& options, | 46 const BackgroundFetchOptions& options, |
46 const FetchCallback& callback) { | 47 const FetchCallback& callback) { |
47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 BackgroundFetchRegistrationId registration_id(service_worker_registration_id, |
| 50 origin, tag); |
48 | 51 |
49 // TODO(peter): Create a new job with the BackgroundFetchContext for the | 52 if (tag.empty()) { |
50 // given tag, requests and options. For now we return a registration that's | 53 bad_message::ReceivedBadMessage(render_process_id_, |
51 // based on the given |options|, to make sure round-trip is covered. | 54 bad_message::BFSI_INVALID_TAG); |
52 | 55 |
53 BackgroundFetchRegistration registration; | 56 // The |callback| must be run in order to not crash the browser process. |
54 registration.tag = tag; | 57 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
55 registration.icons = options.icons; | 58 base::nullopt /* registration */); |
56 registration.title = options.title; | 59 return; |
57 registration.total_download_size = options.total_download_size; | 60 } |
58 | 61 |
59 callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); | 62 // TODO(peter): Remove once https://codereview.chromium.org/2762303002/ lands. |
| 63 std::vector<ServiceWorkerFetchRequest> requests; |
| 64 requests.emplace_back(GURL("https://example.com/image.png"), "POST", |
| 65 ServiceWorkerHeaderMap(), Referrer(), |
| 66 false /* is_reload */); |
| 67 |
| 68 if (!requests.size()) { |
| 69 bad_message::ReceivedBadMessage(render_process_id_, |
| 70 bad_message::BFSI_INVALID_REQUESTS); |
| 71 |
| 72 // The |callback| must be run in order to not crash the browser process. |
| 73 callback.Run(blink::mojom::BackgroundFetchError::INVALID_ARGUMENT, |
| 74 base::nullopt /* registration */); |
| 75 return; |
| 76 } |
| 77 |
| 78 background_fetch_context_->StartFetch(registration_id, requests, options, |
| 79 callback); |
60 } | 80 } |
61 | 81 |
62 void BackgroundFetchServiceImpl::UpdateUI( | 82 void BackgroundFetchServiceImpl::UpdateUI( |
63 int64_t service_worker_registration_id, | 83 int64_t service_worker_registration_id, |
64 const url::Origin& origin, | 84 const url::Origin& origin, |
65 const std::string& tag, | 85 const std::string& tag, |
66 const std::string& title, | 86 const std::string& title, |
67 const UpdateUICallback& callback) { | 87 const UpdateUICallback& callback) { |
68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
69 | 89 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
106 | 126 |
107 // TODO(peter): Get the list of active Background Fetches associated with | 127 // TODO(peter): Get the list of active Background Fetches associated with |
108 // service_worker_registration_id and share their tags. | 128 // service_worker_registration_id and share their tags. |
109 | 129 |
110 callback.Run(blink::mojom::BackgroundFetchError::NONE, | 130 callback.Run(blink::mojom::BackgroundFetchError::NONE, |
111 std::vector<std::string>()); | 131 std::vector<std::string>()); |
112 } | 132 } |
113 | 133 |
114 } // namespace content | 134 } // namespace content |
OLD | NEW |