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_data_manager.h" | 5 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
6 | 6 |
| 7 #include "base/guid.h" |
7 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
8 #include "content/browser/background_fetch/background_fetch_context.h" | 9 #include "content/browser/background_fetch/background_fetch_context.h" |
9 #include "content/browser/background_fetch/background_fetch_request_info.h" | |
10 | 10 |
11 namespace content { | 11 namespace content { |
12 | 12 |
13 BackgroundFetchDataManager::BackgroundFetchDataManager( | 13 BackgroundFetchDataManager::BackgroundFetchDataManager() = default; |
14 BackgroundFetchContext* background_fetch_context) | |
15 : background_fetch_context_(background_fetch_context) { | |
16 DCHECK(background_fetch_context_); | |
17 // TODO(harkness) Read from persistent storage and recreate requests. | |
18 } | |
19 | 14 |
20 BackgroundFetchDataManager::~BackgroundFetchDataManager() = default; | 15 BackgroundFetchDataManager::~BackgroundFetchDataManager() = default; |
21 | 16 |
22 std::unique_ptr<BackgroundFetchJobData> | 17 void BackgroundFetchDataManager::CreateRegistration( |
23 BackgroundFetchDataManager::CreateRequest( | 18 const BackgroundFetchRegistrationId& registration_id, |
24 std::unique_ptr<BackgroundFetchJobInfo> job_info, | 19 const std::vector<ServiceWorkerFetchRequest>& requests, |
25 BackgroundFetchRequestInfos request_infos) { | 20 const BackgroundFetchOptions& options, |
26 BackgroundFetchRegistrationId registration_id( | 21 CreateRegistrationCallback callback) { |
27 job_info->service_worker_registration_id(), job_info->origin(), | 22 if (registrations_.find(registration_id) != registrations_.end()) { |
28 job_info->tag()); | 23 callback.Run(blink::mojom::BackgroundFetchError::DUPLICATED_TAG, |
29 | 24 nullptr /* job_data */); |
30 // Ensure that this is not a duplicate request. | 25 return; |
31 if (known_registrations_.find(registration_id) != | |
32 known_registrations_.end()) { | |
33 DVLOG(1) << "Origin " << job_info->origin() | |
34 << " has already created a batch request with tag " | |
35 << job_info->tag(); | |
36 // TODO(harkness) Figure out how to return errors like this. | |
37 return nullptr; | |
38 } | 26 } |
39 | 27 |
40 // Add the request to our maps and return a JobData to track the individual | 28 // Create a new BackgroundFetchJobData instance based on the available data. |
41 // files in the request. | 29 std::unique_ptr<BackgroundFetchJobData> job_data = |
42 const std::string job_guid = job_info->guid(); | 30 base::MakeUnique<BackgroundFetchJobData>(BackgroundFetchRequestInfos()); |
43 known_registrations_.insert(std::move(registration_id)); | |
44 WriteJobToStorage(std::move(job_info), std::move(request_infos)); | |
45 // TODO(harkness): Remove data when the job is complete. | |
46 | 31 |
47 return base::MakeUnique<BackgroundFetchJobData>( | 32 callback.Run(blink::mojom::BackgroundFetchError::NONE, std::move(job_data)); |
48 ReadRequestsFromStorage(job_guid)); | |
49 } | 33 } |
50 | 34 |
51 void BackgroundFetchDataManager::WriteJobToStorage( | 35 void BackgroundFetchDataManager::DeleteRegistration( |
52 std::unique_ptr<BackgroundFetchJobInfo> job_info, | 36 const BackgroundFetchRegistrationId& registration_id, |
53 BackgroundFetchRequestInfos request_infos) { | 37 DeleteRegistrationCallback callback) { |
54 // TODO(harkness): Replace these maps with actually writing to storage. | 38 auto iter = registrations_.find(registration_id); |
55 // TODO(harkness): Check for job_guid clash. | 39 if (iter == registrations_.end()) { |
56 const std::string job_guid = job_info->guid(); | 40 callback.Run(blink::mojom::BackgroundFetchError::INVALID_TAG); |
57 job_map_[job_guid] = std::move(job_info); | 41 return; |
58 request_map_[job_guid] = std::move(request_infos); | 42 } |
59 } | |
60 | 43 |
61 // TODO(harkness): This should be changed to read (and cache) small numbers of | 44 registrations_.erase(iter); |
62 // the RequestInfos instead of returning all of them. | 45 |
63 BackgroundFetchRequestInfos& | 46 callback.Run(blink::mojom::BackgroundFetchError::NONE); |
64 BackgroundFetchDataManager::ReadRequestsFromStorage( | |
65 const std::string& job_guid) { | |
66 return request_map_[job_guid]; | |
67 } | 47 } |
68 | 48 |
69 } // namespace content | 49 } // namespace content |
OLD | NEW |