Chromium Code Reviews| 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 "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 | 111 |
| 112 requests_.push_back(request.release()); | 112 requests_.push_back(request.release()); |
| 113 return requests_.back()->request_id(); | 113 return requests_.back()->request_id(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 void BitmapFetcherService::Prefetch(const GURL& url) { | 116 void BitmapFetcherService::Prefetch(const GURL& url) { |
| 117 if (url.is_valid()) | 117 if (url.is_valid()) |
| 118 EnsureFetcherForUrl(url); | 118 EnsureFetcherForUrl(url); |
| 119 } | 119 } |
| 120 | 120 |
| 121 chrome::BitmapFetcher* BitmapFetcherService::CreateFetcher(const GURL& url) { | 121 scoped_ptr<chrome::BitmapFetcher> BitmapFetcherService::CreateFetcher( |
| 122 chrome::BitmapFetcher* new_fetcher = new chrome::BitmapFetcher(url, this); | 122 const GURL& url) { |
| 123 scoped_ptr<chrome::BitmapFetcher> new_fetcher( | |
| 124 new chrome::BitmapFetcher(url, this)); | |
| 123 | 125 |
| 124 new_fetcher->Init( | 126 new_fetcher->Init( |
| 125 context_->GetRequestContext(), | 127 context_->GetRequestContext(), |
| 126 std::string(), | 128 std::string(), |
| 127 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | 129 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, |
| 128 net::LOAD_NORMAL); | 130 net::LOAD_NORMAL); |
| 129 new_fetcher->Start(); | 131 new_fetcher->Start(); |
| 130 return new_fetcher; | 132 return new_fetcher; |
| 131 } | 133 } |
| 132 | 134 |
| 133 const chrome::BitmapFetcher* BitmapFetcherService::EnsureFetcherForUrl( | 135 const chrome::BitmapFetcher* BitmapFetcherService::EnsureFetcherForUrl( |
| 134 const GURL& url) { | 136 const GURL& url) { |
| 135 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | 137 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); |
| 136 if (fetcher) | 138 if (fetcher) |
| 137 return fetcher; | 139 return fetcher; |
| 138 | 140 |
| 139 chrome::BitmapFetcher* new_fetcher = CreateFetcher(url); | 141 scoped_ptr<chrome::BitmapFetcher> new_fetcher = CreateFetcher(url); |
| 140 active_fetchers_.push_back(new_fetcher); | 142 active_fetchers_.push_back(new_fetcher.Pass()); |
| 141 return new_fetcher; | 143 return active_fetchers_.back().get(); |
| 142 } | 144 } |
| 143 | 145 |
| 144 const chrome::BitmapFetcher* BitmapFetcherService::FindFetcherForUrl( | 146 const chrome::BitmapFetcher* BitmapFetcherService::FindFetcherForUrl( |
| 145 const GURL& url) { | 147 const GURL& url) { |
| 146 for (BitmapFetchers::iterator iter = active_fetchers_.begin(); | 148 for (auto it = active_fetchers_.begin(); it != active_fetchers_.end(); ++it) { |
| 147 iter != active_fetchers_.end(); | 149 if (url == (*it)->url()) |
| 148 ++iter) { | 150 return it->get(); |
| 149 if (url == (*iter)->url()) | |
| 150 return *iter; | |
| 151 } | 151 } |
| 152 return NULL; | 152 return nullptr; |
| 153 } | 153 } |
| 154 | 154 |
| 155 void BitmapFetcherService::RemoveFetcher(const chrome::BitmapFetcher* fetcher) { | 155 void BitmapFetcherService::RemoveFetcher(const chrome::BitmapFetcher* fetcher) { |
| 156 for (BitmapFetchers::iterator iter = active_fetchers_.begin(); | 156 auto it = |
| 157 iter != active_fetchers_.end(); | 157 std::find(active_fetchers_.begin(), active_fetchers_.end(), fetcher); |
|
danakj
2015/11/11 01:29:08
find() won't work with unique_ptr cuz it doesn't h
| |
| 158 ++iter) { | 158 // RemoveFetcher should always result in removal. |
| 159 if (fetcher == (*iter)) { | 159 DCHECK(it != active_fetchers_.end()); |
| 160 active_fetchers_.erase(iter); | 160 active_fetchers_.erase(it); |
| 161 return; | |
| 162 } | |
| 163 } | |
| 164 NOTREACHED(); // RemoveFetcher should always result in removal. | |
| 165 } | 161 } |
| 166 | 162 |
| 167 void BitmapFetcherService::OnFetchComplete(const GURL& url, | 163 void BitmapFetcherService::OnFetchComplete(const GURL& url, |
| 168 const SkBitmap* bitmap) { | 164 const SkBitmap* bitmap) { |
| 169 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | 165 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); |
| 170 DCHECK(fetcher); | 166 DCHECK(fetcher); |
| 171 | 167 |
| 172 // Notify all attached requests of completion. | 168 // Notify all attached requests of completion. |
| 173 ScopedVector<BitmapFetcherRequest>::iterator iter = requests_.begin(); | 169 ScopedVector<BitmapFetcherRequest>::iterator iter = requests_.begin(); |
| 174 while (iter != requests_.end()) { | 170 while (iter != requests_.end()) { |
| 175 if ((*iter)->get_fetcher() == fetcher) { | 171 if ((*iter)->get_fetcher() == fetcher) { |
| 176 (*iter)->NotifyImageChanged(bitmap); | 172 (*iter)->NotifyImageChanged(bitmap); |
| 177 iter = requests_.erase(iter); | 173 iter = requests_.erase(iter); |
| 178 } else { | 174 } else { |
| 179 ++iter; | 175 ++iter; |
| 180 } | 176 } |
| 181 } | 177 } |
| 182 | 178 |
| 183 if (bitmap && !bitmap->isNull()) { | 179 if (bitmap && !bitmap->isNull()) { |
| 184 CacheEntry* entry = new CacheEntry; | 180 CacheEntry* entry = new CacheEntry; |
| 185 entry->bitmap.reset(new SkBitmap(*bitmap)); | 181 entry->bitmap.reset(new SkBitmap(*bitmap)); |
| 186 cache_.Put(fetcher->url(), entry); | 182 cache_.Put(fetcher->url(), entry); |
| 187 } | 183 } |
| 188 | 184 |
| 189 RemoveFetcher(fetcher); | 185 RemoveFetcher(fetcher); |
| 190 } | 186 } |
| OLD | NEW |