OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" | 5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 } | 64 } |
65 | 65 |
66 BitmapFetcherService::BitmapFetcherService(content::BrowserContext* context) | 66 BitmapFetcherService::BitmapFetcherService(content::BrowserContext* context) |
67 : cache_(kMaxCacheEntries), current_request_id_(1), context_(context) { | 67 : cache_(kMaxCacheEntries), current_request_id_(1), context_(context) { |
68 } | 68 } |
69 | 69 |
70 BitmapFetcherService::~BitmapFetcherService() { | 70 BitmapFetcherService::~BitmapFetcherService() { |
71 } | 71 } |
72 | 72 |
73 void BitmapFetcherService::CancelRequest(int request_id) { | 73 void BitmapFetcherService::CancelRequest(int request_id) { |
74 ScopedVector<BitmapFetcherRequest>::iterator iter; | 74 for (auto iter = requests_.begin(); iter != requests_.end(); ++iter) { |
75 for (iter = requests_.begin(); iter != requests_.end(); ++iter) { | |
76 if ((*iter)->request_id() == request_id) { | 75 if ((*iter)->request_id() == request_id) { |
77 requests_.erase(iter); | 76 requests_.erase(iter); |
78 // Deliberately leave the associated fetcher running to populate cache. | 77 // Deliberately leave the associated fetcher running to populate cache. |
79 return; | 78 return; |
80 } | 79 } |
81 } | 80 } |
82 } | 81 } |
83 | 82 |
84 BitmapFetcherService::RequestId BitmapFetcherService::RequestImage( | 83 BitmapFetcherService::RequestId BitmapFetcherService::RequestImage( |
85 const GURL& url, | 84 const GURL& url, |
86 Observer* observer, | 85 Observer* observer, |
87 const net::NetworkTrafficAnnotationTag& traffic_annotation) { | 86 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
88 // Create a new request, assigning next available request ID. | 87 // Create a new request, assigning next available request ID. |
89 ++current_request_id_; | 88 ++current_request_id_; |
90 if (current_request_id_ == REQUEST_ID_INVALID) | 89 if (current_request_id_ == REQUEST_ID_INVALID) |
91 ++current_request_id_; | 90 ++current_request_id_; |
92 int request_id = current_request_id_; | 91 int request_id = current_request_id_; |
93 std::unique_ptr<BitmapFetcherRequest> request( | 92 auto request = base::MakeUnique<BitmapFetcherRequest>(request_id, observer); |
94 new BitmapFetcherRequest(request_id, observer)); | |
95 | 93 |
96 // Reject invalid URLs. | 94 // Reject invalid URLs. |
97 if (!url.is_valid()) | 95 if (!url.is_valid()) |
98 return REQUEST_ID_INVALID; | 96 return REQUEST_ID_INVALID; |
99 | 97 |
100 // Check for existing images first. | 98 // Check for existing images first. |
101 auto iter = cache_.Get(url); | 99 auto iter = cache_.Get(url); |
102 if (iter != cache_.end()) { | 100 if (iter != cache_.end()) { |
103 BitmapFetcherService::CacheEntry* entry = iter->second.get(); | 101 BitmapFetcherService::CacheEntry* entry = iter->second.get(); |
104 request->NotifyImageChanged(entry->bitmap.get()); | 102 request->NotifyImageChanged(entry->bitmap.get()); |
105 | 103 |
106 // There is no request ID associated with this - data is already delivered. | 104 // There is no request ID associated with this - data is already delivered. |
107 return REQUEST_ID_INVALID; | 105 return REQUEST_ID_INVALID; |
108 } | 106 } |
109 | 107 |
110 // Limit number of simultaneous in-flight requests. | 108 // Limit number of simultaneous in-flight requests. |
111 if (requests_.size() > kMaxRequests) | 109 if (requests_.size() > kMaxRequests) |
112 return REQUEST_ID_INVALID; | 110 return REQUEST_ID_INVALID; |
113 | 111 |
114 // Make sure there's a fetcher for this URL and attach to request. | 112 // Make sure there's a fetcher for this URL and attach to request. |
115 const chrome::BitmapFetcher* fetcher = | 113 const chrome::BitmapFetcher* fetcher = |
116 EnsureFetcherForUrl(url, traffic_annotation); | 114 EnsureFetcherForUrl(url, traffic_annotation); |
117 request->set_fetcher(fetcher); | 115 request->set_fetcher(fetcher); |
118 | 116 |
119 requests_.push_back(request.release()); | 117 requests_.push_back(std::move(request)); |
120 return requests_.back()->request_id(); | 118 return requests_.back()->request_id(); |
121 } | 119 } |
122 | 120 |
123 void BitmapFetcherService::Prefetch( | 121 void BitmapFetcherService::Prefetch( |
124 const GURL& url, | 122 const GURL& url, |
125 const net::NetworkTrafficAnnotationTag& traffic_annotation) { | 123 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
126 if (url.is_valid()) | 124 if (url.is_valid()) |
127 EnsureFetcherForUrl(url, traffic_annotation); | 125 EnsureFetcherForUrl(url, traffic_annotation); |
128 } | 126 } |
129 | 127 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 DCHECK(it != active_fetchers_.end()); | 173 DCHECK(it != active_fetchers_.end()); |
176 active_fetchers_.erase(it); | 174 active_fetchers_.erase(it); |
177 } | 175 } |
178 | 176 |
179 void BitmapFetcherService::OnFetchComplete(const GURL& url, | 177 void BitmapFetcherService::OnFetchComplete(const GURL& url, |
180 const SkBitmap* bitmap) { | 178 const SkBitmap* bitmap) { |
181 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | 179 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); |
182 DCHECK(fetcher); | 180 DCHECK(fetcher); |
183 | 181 |
184 // Notify all attached requests of completion. | 182 // Notify all attached requests of completion. |
185 ScopedVector<BitmapFetcherRequest>::iterator iter = requests_.begin(); | 183 auto iter = requests_.begin(); |
186 while (iter != requests_.end()) { | 184 while (iter != requests_.end()) { |
187 if ((*iter)->get_fetcher() == fetcher) { | 185 if ((*iter)->get_fetcher() == fetcher) { |
188 (*iter)->NotifyImageChanged(bitmap); | 186 (*iter)->NotifyImageChanged(bitmap); |
189 iter = requests_.erase(iter); | 187 iter = requests_.erase(iter); |
190 } else { | 188 } else { |
191 ++iter; | 189 ++iter; |
192 } | 190 } |
193 } | 191 } |
194 | 192 |
195 if (bitmap && !bitmap->isNull()) { | 193 if (bitmap && !bitmap->isNull()) { |
196 std::unique_ptr<CacheEntry> entry(new CacheEntry); | 194 std::unique_ptr<CacheEntry> entry(new CacheEntry); |
197 entry->bitmap.reset(new SkBitmap(*bitmap)); | 195 entry->bitmap.reset(new SkBitmap(*bitmap)); |
198 cache_.Put(fetcher->url(), std::move(entry)); | 196 cache_.Put(fetcher->url(), std::move(entry)); |
199 } | 197 } |
200 | 198 |
201 RemoveFetcher(fetcher); | 199 RemoveFetcher(fetcher); |
202 } | 200 } |
OLD | NEW |