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_job_info.h" | |
10 #include "content/browser/background_fetch/background_fetch_request_info.h" | 9 #include "content/browser/background_fetch/background_fetch_request_info.h" |
11 | 10 |
12 namespace content { | 11 namespace content { |
13 | 12 |
14 BackgroundFetchDataManager::BackgroundFetchDataManager( | 13 BackgroundFetchDataManager::BackgroundFetchDataManager( |
15 BackgroundFetchContext* background_fetch_context) | 14 BackgroundFetchContext* background_fetch_context) |
16 : background_fetch_context_(background_fetch_context) { | 15 : background_fetch_context_(background_fetch_context) { |
17 DCHECK(background_fetch_context_); | 16 DCHECK(background_fetch_context_); |
18 // TODO(harkness) Read from persistent storage and recreate requests. | 17 // TODO(harkness) Read from persistent storage and recreate requests. |
19 } | 18 } |
20 | 19 |
21 BackgroundFetchDataManager::~BackgroundFetchDataManager() = default; | 20 BackgroundFetchDataManager::~BackgroundFetchDataManager() = default; |
22 | 21 |
23 BackgroundFetchJobData* BackgroundFetchDataManager::CreateRequest( | 22 BackgroundFetchJobData* BackgroundFetchDataManager::CreateRequest( |
24 const BackgroundFetchJobInfo& job_info, | 23 const BackgroundFetchJobInfo& job_info, |
25 BackgroundFetchRequestInfos request_infos) { | 24 BackgroundFetchRequestInfos& request_infos) { |
Peter Beverloo
2017/03/08 14:27:04
nit: why?
harkness
2017/03/09 13:33:25
I'm still trying to understand exactly what semant
Peter Beverloo
2017/03/09 15:18:57
Please just fall back to const& if you can't wrap
harkness
2017/03/09 18:35:31
As discussed in person, we're in an awkward positi
| |
26 JobIdentifier id(job_info.service_worker_registration_id(), job_info.tag()); | 25 JobIdentifier id(job_info.service_worker_registration_id(), job_info.tag()); |
27 // Ensure that this is not a duplicate request. | 26 // Ensure that this is not a duplicate request. |
28 if (service_worker_tag_map_.find(id) != service_worker_tag_map_.end()) { | 27 if (service_worker_tag_map_.find(id) != service_worker_tag_map_.end()) { |
29 DVLOG(1) << "Origin " << job_info.origin() | 28 DVLOG(1) << "Origin " << job_info.origin() |
30 << " has already created a batch request with tag " | 29 << " has already created a batch request with tag " |
31 << job_info.tag(); | 30 << job_info.tag(); |
32 // TODO(harkness) Figure out how to return errors like this. | 31 // TODO(harkness) Figure out how to return errors like this. |
33 return nullptr; | 32 return nullptr; |
34 } | 33 } |
35 if (batch_map_.find(job_info.guid()) != batch_map_.end()) { | |
36 DVLOG(1) << "Job with UID " << job_info.guid() << " already exists."; | |
37 // TODO(harkness) Figure out how to return errors like this. | |
38 return nullptr; | |
39 } | |
40 | 34 |
41 // Add the request to our maps and return a JobData to track the individual | 35 // Add the request to our maps and return a JobData to track the individual |
42 // files in the request. | 36 // files in the request. |
43 service_worker_tag_map_[id] = job_info.guid(); | 37 service_worker_tag_map_[id] = job_info.guid(); |
44 // TODO(harkness): When a job is complete, remove the JobData from the map. | 38 WriteJobToStorage(job_info, request_infos); |
45 batch_map_[job_info.guid()] = | 39 // TODO(harkness): Remove data when the job is complete. |
46 base::MakeUnique<BackgroundFetchJobData>(std::move(request_infos)); | 40 |
47 return batch_map_[job_info.guid()].get(); | 41 return new BackgroundFetchJobData(ReadRequestsFromStorage(job_info.guid())); |
42 } | |
43 | |
44 void BackgroundFetchDataManager::WriteJobToStorage( | |
45 const BackgroundFetchJobInfo& job_info, | |
46 BackgroundFetchRequestInfos& request_infos) { | |
47 // TODO(harkness): Replace these maps with actually writing to storage. | |
48 // TODO(harkness): Check for job_guid clash. | |
49 job_map_[job_info.guid()] = job_info; | |
50 request_map_[job_info.guid()] = std::move(request_infos); | |
51 } | |
52 | |
53 // TODO(harkness): This should be changed to read (and cache) small numbers of | |
54 // the RequestInfos instead of returning all of them. | |
55 BackgroundFetchRequestInfos BackgroundFetchDataManager::ReadRequestsFromStorage( | |
56 const std::string& job_guid) { | |
57 BackgroundFetchRequestInfos infos = std::move(request_map_[job_guid]); | |
58 request_map_.erase(job_guid); | |
Peter Beverloo
2017/03/08 14:27:04
I don't really know what moving from a map entry m
harkness
2017/03/09 13:33:25
That's a good point. I'll just return a reference
| |
59 return infos; | |
48 } | 60 } |
49 | 61 |
50 } // namespace content | 62 } // namespace content |
OLD | NEW |