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