Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h" | |
| 6 | |
| 7 #include "base/memory/weak_ptr.h" | |
| 8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "net/base/load_flags.h" | |
| 11 #include "third_party/skia/include/core/SkBitmap.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const int kMaxRequests = 25; // Maximum number of in-flight requests allowed. | |
| 16 const int kMaxCacheEntries = 5; // Maximum number of cache entries. | |
| 17 | |
| 18 } // namespace. | |
| 19 | |
| 20 class BitmapFetcherRequest { | |
| 21 public: | |
| 22 BitmapFetcherRequest(RequestId request_id, BitmapFetcherObserver* observer); | |
| 23 ~BitmapFetcherRequest(); | |
| 24 | |
| 25 void NotifyImageChanged(const SkBitmap& bitmap); | |
| 26 RequestId request_id() const { return request_id_; } | |
| 27 | |
| 28 // Weak ptr |fetcher| is used to identify associated fetchers. | |
| 29 void set_fetcher(const chrome::BitmapFetcher* fetcher) { fetcher_ = fetcher; } | |
| 30 const chrome::BitmapFetcher* get_fetcher() const { return fetcher_; } | |
| 31 | |
| 32 private: | |
| 33 RequestId request_id_; | |
|
sky
2014/06/17 00:13:40
const?
groby-ooo-7-16
2014/06/17 18:06:23
Done.
| |
| 34 scoped_ptr<BitmapFetcherObserver> observer_; | |
| 35 const chrome::BitmapFetcher* fetcher_; | |
| 36 }; | |
|
sky
2014/06/17 00:13:39
DISALLOW...
groby-ooo-7-16
2014/06/17 18:06:23
Done.
| |
| 37 | |
| 38 BitmapFetcherRequest::BitmapFetcherRequest(RequestId request_id, | |
| 39 BitmapFetcherObserver* observer) | |
| 40 : request_id_(request_id), observer_(observer) { | |
| 41 } | |
| 42 | |
| 43 BitmapFetcherRequest::~BitmapFetcherRequest() { | |
| 44 observer_->OnRequestFinished(request_id_); | |
| 45 } | |
| 46 | |
| 47 void BitmapFetcherRequest::NotifyImageChanged(const SkBitmap& bitmap) { | |
| 48 observer_->OnImageChanged(bitmap); | |
| 49 } | |
| 50 | |
| 51 BitmapFetcherService::CacheEntry::CacheEntry() { | |
| 52 } | |
| 53 | |
| 54 BitmapFetcherService::CacheEntry::~CacheEntry() { | |
| 55 } | |
| 56 | |
| 57 BitmapFetcherService::BitmapFetcherService(content::BrowserContext* context) | |
| 58 : cache_(kMaxCacheEntries), current_request_id_(1), context_(context) { | |
| 59 Profile::FromBrowserContext(context); | |
|
sky
2014/06/17 00:13:40
Why do you need this?
groby-ooo-7-16
2014/06/17 18:06:23
I don't - missed to delete this.
| |
| 60 } | |
| 61 | |
| 62 BitmapFetcherService::~BitmapFetcherService() { | |
| 63 } | |
| 64 | |
| 65 void BitmapFetcherService::CancelRequest(int request_id) { | |
| 66 ScopedVector<BitmapFetcherRequest>::iterator iter; | |
| 67 for (iter = requests_.begin(); iter != requests_.end(); ++iter) { | |
| 68 if ((*iter)->request_id() == request_id) { | |
| 69 requests_.erase(iter); | |
|
sky
2014/06/17 03:00:02
Shouldn't you potentially delete the BitmapFetcher
groby-ooo-7-16
2014/06/17 18:06:22
That's actually intentional - we will likely need
sky
2014/06/17 19:19:47
Isn't this is a general service though that could
groby-ooo-7-16
2014/06/17 21:24:30
Yes, but
1) There are currently no consumers requ
| |
| 70 return; | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 const chrome::BitmapFetcher* BitmapFetcherService::FindFetcherForUrl( | |
| 76 const GURL& url) { | |
| 77 ScopedVector<chrome::BitmapFetcher>::iterator iter = active_fetchers_.begin(); | |
|
sky
2014/06/17 03:00:02
nit: move into for loop.
groby-ooo-7-16
2014/06/17 18:06:23
Done. Even though it's ugly :)
| |
| 78 for (; iter != active_fetchers_.end(); ++iter) { | |
| 79 if (url == (*iter)->url()) | |
| 80 return *iter; | |
| 81 } | |
| 82 return NULL; | |
| 83 } | |
| 84 | |
| 85 void BitmapFetcherService::RemoveFetcher(const chrome::BitmapFetcher* fetcher) { | |
| 86 ScopedVector<chrome::BitmapFetcher>::iterator iter = active_fetchers_.begin(); | |
| 87 for (; iter != active_fetchers_.end(); ++iter) { | |
| 88 if (fetcher == (*iter)) | |
| 89 break; | |
|
sky
2014/06/17 00:13:40
Any reason you don't erase here then return? Simil
groby-ooo-7-16
2014/06/17 18:06:22
Done.
| |
| 90 } | |
| 91 DCHECK(iter != active_fetchers_.end()); | |
| 92 active_fetchers_.erase(iter); | |
| 93 } | |
| 94 | |
| 95 chrome::BitmapFetcher* BitmapFetcherService::CreateFetcher(const GURL& url) { | |
| 96 chrome::BitmapFetcher* new_fetcher = new chrome::BitmapFetcher(url, this); | |
| 97 | |
| 98 new_fetcher->Start( | |
| 99 context_->GetRequestContext(), | |
| 100 std::string(), | |
| 101 net::URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE, | |
| 102 net::LOAD_NORMAL); | |
| 103 return new_fetcher; | |
| 104 } | |
| 105 | |
| 106 const chrome::BitmapFetcher* BitmapFetcherService::EnsureFetcherForUrl( | |
| 107 const GURL& url) { | |
| 108 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | |
| 109 if (fetcher) | |
| 110 return fetcher; | |
| 111 | |
| 112 chrome::BitmapFetcher* new_fetcher = CreateFetcher(url); | |
| 113 active_fetchers_.push_back(new_fetcher); | |
| 114 return new_fetcher; | |
| 115 } | |
| 116 | |
| 117 void BitmapFetcherService::Prefetch(const GURL& url) { | |
| 118 (void)EnsureFetcherForUrl(url); | |
|
sky
2014/06/17 03:00:02
Remove (void)
groby-ooo-7-16
2014/06/17 18:06:23
Done.
| |
| 119 } | |
| 120 | |
| 121 RequestId BitmapFetcherService::RequestImage(const GURL& url, | |
|
sky
2014/06/17 00:13:40
make order match header.
groby-ooo-7-16
2014/06/17 18:06:23
Done.
| |
| 122 BitmapFetcherObserver* observer) { | |
|
sky
2014/06/17 00:13:40
indentation is off (maybe for all lines that you'v
groby-ooo-7-16
2014/06/17 18:06:22
Done.
| |
| 123 // Create a new request, assigning next available request ID. | |
| 124 ++current_request_id_; | |
| 125 if (current_request_id_ == REQUEST_ID_INVALID) | |
| 126 ++current_request_id_; | |
| 127 int request_id = current_request_id_; | |
| 128 scoped_ptr<BitmapFetcherRequest> request( | |
| 129 new BitmapFetcherRequest(request_id, observer)); | |
| 130 | |
| 131 // Check for existing images first. | |
| 132 base::OwningMRUCache<GURL, CacheEntry*>::iterator iter = cache_.Get(url); | |
| 133 if (iter != cache_.end()) { | |
| 134 BitmapFetcherService::CacheEntry* entry = iter->second; | |
| 135 request->NotifyImageChanged(*(entry->bitmap.get())); | |
| 136 | |
| 137 // There is no request ID associated with this - data is already delivered. | |
| 138 return REQUEST_ID_INVALID; | |
| 139 } | |
| 140 | |
| 141 // Limit number of simultaneous in-flight requests. | |
| 142 if (requests_.size() > kMaxRequests) | |
| 143 return REQUEST_ID_INVALID; | |
| 144 | |
| 145 // Make sure there's a fetcher for this URL and attach to request. | |
| 146 const chrome::BitmapFetcher* fetcher = EnsureFetcherForUrl(url); | |
| 147 request->set_fetcher(fetcher); | |
| 148 | |
| 149 requests_.push_back(request.release()); | |
| 150 return requests_.back()->request_id(); | |
| 151 } | |
| 152 | |
| 153 void BitmapFetcherService::OnFetchComplete(const GURL url, | |
| 154 const SkBitmap* bitmap) { | |
| 155 DCHECK(bitmap); // can never be NULL, guaranteed by BitmapFetcher. | |
| 156 | |
| 157 const chrome::BitmapFetcher* fetcher = FindFetcherForUrl(url); | |
| 158 DCHECK(fetcher); | |
| 159 | |
| 160 // Notify all attached requests of completion. | |
| 161 ScopedVector<BitmapFetcherRequest>::iterator iter; | |
|
sky
2014/06/17 03:00:02
Use for loop and move iter into it.
groby-ooo-7-16
2014/06/17 18:06:22
Can't use for(), since iter is only incremented if
| |
| 162 iter = requests_.begin(); | |
| 163 while (iter != requests_.end()) { | |
| 164 if ((*iter)->get_fetcher() == fetcher) { | |
| 165 (*iter)->NotifyImageChanged(*bitmap); | |
| 166 iter = requests_.erase(iter); | |
| 167 } else { | |
| 168 ++iter; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 if (!bitmap->isNull()) { | |
| 173 CacheEntry* entry = new CacheEntry; | |
| 174 entry->bitmap.reset(new SkBitmap(*bitmap)); | |
| 175 cache_.Put(fetcher->url(), entry); | |
| 176 } | |
| 177 | |
| 178 RemoveFetcher(fetcher); | |
| 179 } | |
| OLD | NEW |