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 "content/browser/background_fetch/background_fetch_context.h" | 7 #include "content/browser/background_fetch/background_fetch_context.h" |
8 #include "content/browser/background_fetch/batch_request.h" | |
8 #include "content/browser/background_fetch/fetch_request.h" | 9 #include "content/browser/background_fetch/fetch_request.h" |
9 | 10 |
10 namespace content { | 11 namespace content { |
11 | 12 |
12 BackgroundFetchDataManager::BackgroundFetchDataManager( | 13 BackgroundFetchDataManager::BackgroundFetchDataManager( |
13 BackgroundFetchContext* background_fetch_context) | 14 BackgroundFetchContext* background_fetch_context) |
14 : background_fetch_context_(background_fetch_context) { | 15 : background_fetch_context_(background_fetch_context) { |
15 DCHECK(background_fetch_context_); | 16 DCHECK(background_fetch_context_); |
16 // TODO(harkness) Read from persistent storage and recreate requests. | 17 // TODO(harkness) Read from persistent storage and recreate requests. |
17 } | 18 } |
18 | 19 |
19 BackgroundFetchDataManager::~BackgroundFetchDataManager() {} | 20 BackgroundFetchDataManager::~BackgroundFetchDataManager() { |
21 // TODO(harkness): Either require that the BackgroundFetchDataManager is | |
22 // always destructed after the BatchManager or do ref counting on the | |
23 // BatchData objects that are allocated. | |
24 for (auto pair : batch_map_) { | |
Peter Beverloo
2017/02/24 02:05:16
note: this'll go away because of std::unique_ptr<>
harkness
2017/02/24 11:47:10
Removed it.
| |
25 delete pair.second; | |
26 } | |
27 } | |
20 | 28 |
21 void BackgroundFetchDataManager::CreateRequest( | 29 BackgroundFetchDataManager::BatchData* |
22 const FetchRequest& fetch_request) { | 30 BackgroundFetchDataManager::CreateRequest( |
23 FetchIdentifier id(fetch_request.service_worker_registration_id(), | 31 const BatchRequest& batch_request, |
24 fetch_request.tag()); | 32 const std::vector<FetchRequest>& fetch_requests) { |
25 if (fetch_map_.find(id) != fetch_map_.end()) { | 33 BatchIdentifier id(batch_request.service_worker_registration_id(), |
26 DLOG(ERROR) << "Origin " << fetch_request.origin() | 34 batch_request.tag()); |
27 << " has already created a fetch request with tag " | 35 if (service_worker_tag_map_.find(id) != service_worker_tag_map_.end()) { |
28 << fetch_request.tag(); | 36 DLOG(ERROR) << "Origin " << batch_request.origin() |
37 << " has already created a batch request with tag " | |
38 << batch_request.tag(); | |
29 // TODO(harkness) Figure out how to return errors like this. | 39 // TODO(harkness) Figure out how to return errors like this. |
30 return; | 40 return nullptr; |
31 } | 41 } |
32 fetch_map_[id] = fetch_request; | 42 |
43 if (batch_map_.find(batch_request.guid()) != batch_map_.end()) { | |
44 DLOG(ERROR) << "Batch with UID " << batch_request.guid() | |
45 << " already exists."; | |
46 // TODO(harkness) Figure out how to return errors like this. | |
47 return nullptr; | |
48 } | |
49 | |
50 service_worker_tag_map_[id] = batch_request.guid(); | |
51 BatchData* batch_data = new BatchData(batch_request, fetch_requests); | |
Peter Beverloo
2017/02/24 02:05:15
new -> std::MakeUnique<>
harkness
2017/02/24 11:47:10
Done.
| |
52 batch_map_.insert(std::make_pair(batch_request.guid(), batch_data)); | |
53 return batch_data; | |
54 } | |
55 | |
56 BackgroundFetchDataManager::BatchData::BatchData( | |
57 const BatchRequest& batch_request, | |
58 const FetchRequests& fetch_requests) | |
59 : fetch_requests_(std::move(fetch_requests)) {} | |
Peter Beverloo
2017/02/24 02:05:15
How can we *move* a const& vector? Moving invalida
harkness
2017/02/24 11:47:10
I've removed the const-ness of the vector for now,
| |
60 | |
61 BackgroundFetchDataManager::BatchData::~BatchData() {} | |
62 | |
63 BackgroundFetchDataManager::BatchData::BatchData() = default; | |
64 | |
65 // TODO(harkness): Provide more detail about status and where the returned data | |
66 // is now available. | |
67 bool BackgroundFetchDataManager::BatchData::FetchRequestComplete( | |
68 const std::string& fetch_guid) { | |
69 DCHECK(fetch_request_index_.find(fetch_guid) != fetch_request_index_.end()); | |
70 fetch_requests_[fetch_request_index_[fetch_guid]].set_complete(true); | |
71 fetch_request_index_.erase(fetch_guid); | |
Peter Beverloo
2017/02/24 02:05:15
This does three lookups in fetch_request_index_. I
harkness
2017/02/24 11:47:11
Personally, I find the bracket syntax more readabl
| |
72 return next_fetch_request_ != fetch_requests_.size(); | |
73 } | |
74 | |
75 const FetchRequest& | |
76 BackgroundFetchDataManager::BatchData::GetNextFetchRequest() { | |
77 DCHECK(next_fetch_request_ != fetch_requests_.size()); | |
Peter Beverloo
2017/02/24 02:05:16
DCHECK_NE
harkness
2017/02/24 11:47:10
Done.
| |
78 | |
79 const FetchRequest& next_request = fetch_requests_[next_fetch_request_]; | |
80 DCHECK(!next_request.complete()); | |
81 fetch_request_index_[next_request.guid()] = next_fetch_request_++; | |
82 | |
83 return next_request; | |
84 } | |
85 | |
86 bool BackgroundFetchDataManager::BatchData::IsComplete() const { | |
87 return ((next_fetch_request_ == fetch_requests_.size()) && | |
88 fetch_request_index_.empty()); | |
89 } | |
90 | |
91 bool BackgroundFetchDataManager::BatchData::HasRequestsRemaining() const { | |
92 return next_fetch_request_ != fetch_requests_.size(); | |
33 } | 93 } |
34 | 94 |
35 } // namespace content | 95 } // namespace content |
OLD | NEW |