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_context.h" | 5 #include "content/browser/background_fetch/background_fetch_context.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "content/browser/background_fetch/background_fetch_job_info.h" | 9 #include "content/browser/background_fetch/background_fetch_job_info.h" |
8 #include "content/browser/background_fetch/background_fetch_request_info.h" | 10 #include "content/browser/background_fetch/background_fetch_request_info.h" |
9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 11 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
10 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
11 #include "content/public/browser/download_manager.h" | 13 #include "content/public/browser/download_manager.h" |
12 #include "content/public/browser/storage_partition.h" | 14 #include "content/public/browser/storage_partition.h" |
13 | 15 |
14 namespace content { | 16 namespace content { |
15 | 17 |
16 BackgroundFetchContext::BackgroundFetchContext( | 18 BackgroundFetchContext::BackgroundFetchContext( |
17 BrowserContext* browser_context, | 19 BrowserContext* browser_context, |
18 StoragePartition* storage_partition, | 20 StoragePartition* storage_partition, |
19 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | 21 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) |
20 : browser_context_(browser_context), | 22 : browser_context_(browser_context), |
21 storage_partition_(storage_partition), | 23 storage_partition_(storage_partition), |
22 service_worker_context_(service_worker_context), | 24 service_worker_context_(service_worker_context), |
23 background_fetch_data_manager_(this) { | 25 background_fetch_data_manager_(this) { |
24 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 26 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
25 // TODO(harkness): BackgroundFetchContext should have | 27 // TODO(harkness): BackgroundFetchContext should have |
26 // ServiceWorkerContextObserver as a parent class and should register as an | 28 // ServiceWorkerContextObserver as a parent class and should register as an |
27 // observer here. | 29 // observer here. |
28 } | 30 } |
29 | 31 |
30 BackgroundFetchContext::~BackgroundFetchContext() { | 32 BackgroundFetchContext::~BackgroundFetchContext() { |
31 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 33 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
32 } | 34 } |
33 | 35 |
34 void BackgroundFetchContext::Init() { | |
35 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
36 } | |
37 | |
38 void BackgroundFetchContext::Shutdown() { | 36 void BackgroundFetchContext::Shutdown() { |
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 37 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
40 | 38 |
41 BrowserThread::PostTask( | 39 BrowserThread::PostTask( |
42 BrowserThread::IO, FROM_HERE, | 40 BrowserThread::IO, FROM_HERE, |
43 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); | 41 base::Bind(&BackgroundFetchContext::ShutdownOnIO, this)); |
44 } | 42 } |
45 | 43 |
46 void BackgroundFetchContext::ShutdownOnIO() { | 44 void BackgroundFetchContext::ShutdownOnIO() { |
47 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
48 | 46 |
49 // Call Shutdown on all pending job controllers to give them a chance to flush | 47 // Call Shutdown on all pending job controllers to give them a chance to flush |
50 // any status to the DataManager. | 48 // any status to the DataManager. |
51 for (auto& job : job_map_) | 49 for (auto& controller : controllers_) |
52 job.second->Shutdown(); | 50 controller->Shutdown(); |
53 | 51 |
54 job_map_.clear(); | 52 controllers_.clear(); |
55 } | 53 } |
56 | 54 |
57 void BackgroundFetchContext::CreateRequest( | 55 void BackgroundFetchContext::StartFetch( |
58 std::unique_ptr<BackgroundFetchJobInfo> job_info, | 56 const BackgroundFetchRegistrationId& registration_id, |
59 std::vector<BackgroundFetchRequestInfo>& request_infos) { | 57 const std::vector<ServiceWorkerFetchRequest>& requests, |
| 58 const BackgroundFetchOptions& options, |
| 59 const blink::mojom::BackgroundFetchService::FetchCallback& callback) { |
60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
61 DCHECK_GE(1U, request_infos.size()); | |
62 | 61 |
63 // Inform the data manager about the new download. | 62 // Registration IDs must be unique. When the |registration_id| already has an |
64 const std::string job_guid = job_info->guid(); | 63 // active job, it means that the developer is reusing the registration's tag. |
65 std::unique_ptr<BackgroundFetchJobData> job_data = | 64 if (GetActiveJobForRegistrationId(registration_id)) { |
66 background_fetch_data_manager_.CreateRequest(std::move(job_info), | 65 callback.Run(blink::mojom::BackgroundFetchError::DUPLICATED_TAG, |
67 request_infos); | 66 nullptr /* registration */); |
| 67 return; |
| 68 } |
68 | 69 |
69 // If job_data is null, the DataManager will have logged an error. | 70 background_fetch_data_manager_.CreateRegistration( |
70 if (job_data) { | 71 registration_id, requests, options, |
71 // Create a controller which drives the processing of the job. It will use | 72 base::BindOnce(&BackgroundFetchContext::DidCreateCreateRegistration, this, |
72 // the JobData to get information about individual requests for the job. | 73 registration_id, options, callback)); |
73 job_map_[job_guid] = base::MakeUnique<BackgroundFetchJobController>( | |
74 job_guid, browser_context_, storage_partition_, std::move(job_data), | |
75 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this, | |
76 job_guid)); | |
77 } | |
78 } | 74 } |
79 | 75 |
80 void BackgroundFetchContext::DidCompleteJob(const std::string& job_guid) { | 76 void BackgroundFetchContext::DidCreateCreateRegistration( |
81 DCHECK(job_map_.find(job_guid) != job_map_.end()); | 77 const BackgroundFetchRegistrationId& registration_id, |
| 78 const BackgroundFetchOptions& options, // TODO: kill |
| 79 const blink::mojom::BackgroundFetchService::FetchCallback& callback, |
| 80 blink::mojom::BackgroundFetchError error, |
| 81 std::unique_ptr<BackgroundFetchJobData> job_data) { |
| 82 if (error != blink::mojom::BackgroundFetchError::NONE) { |
| 83 callback.Run(error, nullptr /* registration */); |
| 84 return; |
| 85 } |
82 | 86 |
83 job_map_.erase(job_guid); | 87 DCHECK(job_data); |
| 88 |
| 89 controllers_.emplace_back( |
| 90 registration_id, browser_context_, storage_partition_, |
| 91 std::move(job_data), |
| 92 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); |
| 93 |
| 94 BackgroundFetchRegistration registration; |
| 95 registration.tag = tag; |
| 96 registration.icons = options.icons; |
| 97 registration.title = options.title; |
| 98 registration.total_download_size = options.total_download_size; |
| 99 |
| 100 callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); |
| 101 } |
| 102 |
| 103 BackgroundFetchJobController* |
| 104 BackgroundFetchContext::GetActiveJobForRegistrationId( |
| 105 const BackgroundFetchRegistrationId& registration_id) const { |
| 106 for (const auto& controller : controllers_) { |
| 107 if (controller->registration_id() == registration_id()) |
| 108 return controller.get(); |
| 109 } |
| 110 return nullptr; |
| 111 } |
| 112 |
| 113 void BackgroundFetchContext::DidCompleteJob( |
| 114 const BackgroundFetchRegistrationId& registration_id) { |
| 115 auto iter = std::find_if( |
| 116 controllers_.begin(), controllers_.end(), |
| 117 [®istration_id]( |
| 118 const std::unique_ptr<BackgroundFetchJobController>& controller) { |
| 119 return controller->registration_id() == registration_id; |
| 120 }); |
| 121 |
| 122 DCHECK(iter != controllers_.end()); |
84 | 123 |
85 // TODO(harkness): Once the caller receives the message, inform the | 124 // TODO(harkness): Once the caller receives the message, inform the |
86 // DataManager that it can clean up the pending job. | 125 // DataManager that it can clean up the pending job. |
| 126 |
| 127 controllers_.erase(iter); |
87 } | 128 } |
88 | 129 |
89 } // namespace content | 130 } // namespace content |
OLD | NEW |