| 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if ((*iter)->request_id() == request_id) { | 76 if ((*iter)->request_id() == request_id) { |
| 77 requests_.erase(iter); | 77 requests_.erase(iter); |
| 78 // Deliberately leave the associated fetcher running to populate cache. | 78 // Deliberately leave the associated fetcher running to populate cache. |
| 79 return; | 79 return; |
| 80 } | 80 } |
| 81 } | 81 } |
| 82 } | 82 } |
| 83 | 83 |
| 84 BitmapFetcherService::RequestId BitmapFetcherService::RequestImage( | 84 BitmapFetcherService::RequestId BitmapFetcherService::RequestImage( |
| 85 const GURL& url, | 85 const GURL& url, |
| 86 Observer* observer) { | 86 Observer* observer, |
| 87 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 87 // Create a new request, assigning next available request ID. | 88 // Create a new request, assigning next available request ID. |
| 88 ++current_request_id_; | 89 ++current_request_id_; |
| 89 if (current_request_id_ == REQUEST_ID_INVALID) | 90 if (current_request_id_ == REQUEST_ID_INVALID) |
| 90 ++current_request_id_; | 91 ++current_request_id_; |
| 91 int request_id = current_request_id_; | 92 int request_id = current_request_id_; |
| 92 std::unique_ptr<BitmapFetcherRequest> request( | 93 std::unique_ptr<BitmapFetcherRequest> request( |
| 93 new BitmapFetcherRequest(request_id, observer)); | 94 new BitmapFetcherRequest(request_id, observer)); |
| 94 | 95 |
| 95 // Reject invalid URLs. | 96 // Reject invalid URLs. |
| 96 if (!url.is_valid()) | 97 if (!url.is_valid()) |
| 97 return REQUEST_ID_INVALID; | 98 return REQUEST_ID_INVALID; |
| 98 | 99 |
| 99 // Check for existing images first. | 100 // Check for existing images first. |
| 100 auto iter = cache_.Get(url); | 101 auto iter = cache_.Get(url); |
| 101 if (iter != cache_.end()) { | 102 if (iter != cache_.end()) { |
| 102 BitmapFetcherService::CacheEntry* entry = iter->second.get(); | 103 BitmapFetcherService::CacheEntry* entry = iter->second.get(); |
| 103 request->NotifyImageChanged(entry->bitmap.get()); | 104 request->NotifyImageChanged(entry->bitmap.get()); |
| 104 | 105 |
| 105 // There is no request ID associated with this - data is already delivered. | 106 // There is no request ID associated with this - data is already delivered. |
| 106 return REQUEST_ID_INVALID; | 107 return REQUEST_ID_INVALID; |
| 107 } | 108 } |
| 108 | 109 |
| 109 // Limit number of simultaneous in-flight requests. | 110 // Limit number of simultaneous in-flight requests. |
| 110 if (requests_.size() > kMaxRequests) | 111 if (requests_.size() > kMaxRequests) |
| 111 return REQUEST_ID_INVALID; | 112 return REQUEST_ID_INVALID; |
| 112 | 113 |
| 113 // Make sure there's a fetcher for this URL and attach to request. | 114 // Make sure there's a fetcher for this URL and attach to request. |
| 114 const chrome::BitmapFetcher* fetcher = EnsureFetcherForUrl(url); | 115 const chrome::BitmapFetcher* fetcher = EnsureFetcherForUrl( |
| 116 url, traffic_annotation); |
| 115 request->set_fetcher(fetcher); | 117 request->set_fetcher(fetcher); |
| 116 | 118 |
| 117 requests_.push_back(request.release()); | 119 requests_.push_back(request.release()); |
| 118 return requests_.back()->request_id(); | 120 return requests_.back()->request_id(); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void BitmapFetcherService::Prefetch(const GURL& url) { | 123 void BitmapFetcherService::Prefetch( |
| 124 const GURL& url, |
| 125 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 122 if (url.is_valid()) | 126 if (url.is_valid()) |
| 123 EnsureFetcherForUrl(url); | 127 EnsureFetcherForUrl(url, traffic_annotation); |
| 124 } | 128 } |
| 125 | 129 |
| 126 std::unique_ptr<chrome::BitmapFetcher> BitmapFetcherService::CreateFetcher( | 130 std::unique_ptr<chrome::BitmapFetcher> BitmapFetcherService::CreateFetcher( |
| 127 const GURL& url) { | 131 const GURL& url, |
| 132 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 128 std::unique_ptr<chrome::BitmapFetcher> new_fetcher( | 133 std::unique_ptr<chrome::BitmapFetcher> new_fetcher( |
| 129 new chrome::BitmapFetcher(url, this)); | 134 new chrome::BitmapFetcher(url, this, traffic_annotation)); |
| 130 | 135 |
| 131 new_fetcher->Init( | 136 new_fetcher->Init( |
| 132 content::BrowserContext::GetDefaultStoragePartition(context_)-> | 137 content::BrowserContext::GetDefaultStoragePartition(context_)-> |
| 133 GetURLRequestContext(), | 138 GetURLRequestContext(), |
| 134 std::string(), | 139 std::string(), |
| 135 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 140 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 136 net::LOAD_NORMAL); | 141 net::LOAD_NORMAL); |
| 137 new_fetcher->Start(); | 142 new_fetcher->Start(); |
| 138 return new_fetcher; | 143 return new_fetcher; |
| 139 } | 144 } |
| 140 | 145 |
| 141 const chrome::BitmapFetcher* BitmapFetcherService::EnsureFetcherForUrl( | 146 const chrome::BitmapFetcher* BitmapFetcherService::EnsureFetcherForUrl( |
| 142 const GURL& url) { | 147 const GURL& url, |
| 148 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 143 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | 149 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); |
| 144 if (fetcher) | 150 if (fetcher) |
| 145 return fetcher; | 151 return fetcher; |
| 146 | 152 |
| 147 std::unique_ptr<chrome::BitmapFetcher> new_fetcher = CreateFetcher(url); | 153 std::unique_ptr<chrome::BitmapFetcher> new_fetcher = CreateFetcher( |
| 154 url, traffic_annotation); |
| 148 active_fetchers_.push_back(std::move(new_fetcher)); | 155 active_fetchers_.push_back(std::move(new_fetcher)); |
| 149 return active_fetchers_.back().get(); | 156 return active_fetchers_.back().get(); |
| 150 } | 157 } |
| 151 | 158 |
| 152 const chrome::BitmapFetcher* BitmapFetcherService::FindFetcherForUrl( | 159 const chrome::BitmapFetcher* BitmapFetcherService::FindFetcherForUrl( |
| 153 const GURL& url) { | 160 const GURL& url) { |
| 154 for (auto it = active_fetchers_.begin(); it != active_fetchers_.end(); ++it) { | 161 for (auto it = active_fetchers_.begin(); it != active_fetchers_.end(); ++it) { |
| 155 if (url == (*it)->url()) | 162 if (url == (*it)->url()) |
| 156 return it->get(); | 163 return it->get(); |
| 157 } | 164 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 186 } | 193 } |
| 187 | 194 |
| 188 if (bitmap && !bitmap->isNull()) { | 195 if (bitmap && !bitmap->isNull()) { |
| 189 std::unique_ptr<CacheEntry> entry(new CacheEntry); | 196 std::unique_ptr<CacheEntry> entry(new CacheEntry); |
| 190 entry->bitmap.reset(new SkBitmap(*bitmap)); | 197 entry->bitmap.reset(new SkBitmap(*bitmap)); |
| 191 cache_.Put(fetcher->url(), std::move(entry)); | 198 cache_.Put(fetcher->url(), std::move(entry)); |
| 192 } | 199 } |
| 193 | 200 |
| 194 RemoveFetcher(fetcher); | 201 RemoveFetcher(fetcher); |
| 195 } | 202 } |
| OLD | NEW |