| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/ui/ash/launcher/launcher_favicon_loader.h" | |
| 6 | |
| 7 #include "ash/shelf/shelf_constants.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/browser/web_contents_observer.h" | |
| 15 #include "third_party/skia/include/core/SkBitmap.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace internal { | |
| 19 | |
| 20 const int kMaxBitmapSize = 256; | |
| 21 | |
| 22 //////////////////////////////////////////////////////////////////////////////// | |
| 23 // FaviconRawBitmapHandler fetchs all bitmaps with the 'icon' (or 'shortcut | |
| 24 // icon') link tag, storing the one that best matches ash::kShelfSize. These | |
| 25 // icon bitmaps are not resized and are not cached beyond the lifetime of the | |
| 26 // class. Bitmaps larger than kMaxBitmapSize are ignored. | |
| 27 | |
| 28 class FaviconRawBitmapHandler : public content::WebContentsObserver { | |
| 29 public: | |
| 30 FaviconRawBitmapHandler(content::WebContents* web_contents, | |
| 31 LauncherFaviconLoader::Delegate* delegate) | |
| 32 : content::WebContentsObserver(web_contents), | |
| 33 delegate_(delegate), | |
| 34 weak_ptr_factory_(this) {} | |
| 35 | |
| 36 ~FaviconRawBitmapHandler() override {} | |
| 37 | |
| 38 const SkBitmap& bitmap() const { return bitmap_; } | |
| 39 | |
| 40 bool HasPendingDownloads() const; | |
| 41 | |
| 42 // content::WebContentObserver implementation. | |
| 43 void DidUpdateFaviconURL( | |
| 44 const std::vector<content::FaviconURL>& candidates) override; | |
| 45 | |
| 46 private: | |
| 47 void DidDownloadFavicon( | |
| 48 int id, | |
| 49 int http_status_code, | |
| 50 const GURL& image_url, | |
| 51 const std::vector<SkBitmap>& bitmaps, | |
| 52 const std::vector<gfx::Size>& original_bitmap_sizes); | |
| 53 | |
| 54 void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap); | |
| 55 | |
| 56 LauncherFaviconLoader::Delegate* delegate_; | |
| 57 | |
| 58 typedef std::set<GURL> UrlSet; | |
| 59 // Map of pending download urls. | |
| 60 UrlSet pending_requests_; | |
| 61 // Map of processed urls. | |
| 62 UrlSet processed_requests_; | |
| 63 // Current bitmap and source url. | |
| 64 SkBitmap bitmap_; | |
| 65 GURL bitmap_url_; | |
| 66 | |
| 67 base::WeakPtrFactory<FaviconRawBitmapHandler> weak_ptr_factory_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(FaviconRawBitmapHandler); | |
| 70 }; | |
| 71 | |
| 72 void FaviconRawBitmapHandler::DidUpdateFaviconURL( | |
| 73 const std::vector<content::FaviconURL>& candidates) { | |
| 74 // This function receives a complete list of faviocn urls for the page. | |
| 75 // It may get called multiple times with the same list, and will also get | |
| 76 // called any time an item is added or removed. As such, we track processed | |
| 77 // and pending urls, but only until they are removed from the list. | |
| 78 UrlSet new_pending, new_processed; | |
| 79 // Create a map of valid favicon urls. | |
| 80 std::set<GURL> urls; | |
| 81 std::vector<content::FaviconURL>::const_iterator iter; | |
| 82 for (iter = candidates.begin(); iter != candidates.end(); ++iter) { | |
| 83 if (iter->icon_type != content::FaviconURL::FAVICON) | |
| 84 continue; | |
| 85 const GURL& url = iter->icon_url; | |
| 86 if (url.is_valid()) | |
| 87 urls.insert(url); | |
| 88 // Preserve matching pending requests amd processed requests. | |
| 89 if (pending_requests_.find(url) != pending_requests_.end()) | |
| 90 new_pending.insert(url); | |
| 91 if (processed_requests_.find(url) != processed_requests_.end()) | |
| 92 new_processed.insert(url); | |
| 93 } | |
| 94 pending_requests_ = new_pending; | |
| 95 processed_requests_ = new_processed; | |
| 96 // Reset bitmap_ if no longer valid (i.e. not in the list of urls). | |
| 97 if (urls.find(bitmap_url_) == urls.end()) { | |
| 98 bitmap_url_ = GURL(); | |
| 99 bitmap_.reset(); | |
| 100 } | |
| 101 // Request any new urls. | |
| 102 for (std::set<GURL>::iterator iter = urls.begin(); | |
| 103 iter != urls.end(); ++iter) { | |
| 104 if (processed_requests_.find(*iter) != processed_requests_.end()) | |
| 105 continue; // Skip already processed downloads. | |
| 106 if (pending_requests_.find(*iter) != pending_requests_.end()) | |
| 107 continue; // Skip already pending downloads. | |
| 108 pending_requests_.insert(*iter); | |
| 109 web_contents()->DownloadImage( | |
| 110 *iter, | |
| 111 true, // is a favicon | |
| 112 0, // no maximum size | |
| 113 false, // normal cache policy | |
| 114 base::Bind(&FaviconRawBitmapHandler::DidDownloadFavicon, | |
| 115 weak_ptr_factory_.GetWeakPtr())); | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool FaviconRawBitmapHandler::HasPendingDownloads() const { | |
| 120 return !pending_requests_.empty(); | |
| 121 } | |
| 122 | |
| 123 void FaviconRawBitmapHandler::DidDownloadFavicon( | |
| 124 int id, | |
| 125 int http_status_code, | |
| 126 const GURL& image_url, | |
| 127 const std::vector<SkBitmap>& bitmaps, | |
| 128 const std::vector<gfx::Size>& original_bitmap_sizes) { | |
| 129 UrlSet::iterator iter = pending_requests_.find(image_url); | |
| 130 if (iter == pending_requests_.end()) { | |
| 131 // Updates are received for all downloads; ignore unrequested urls. | |
| 132 return; | |
| 133 } | |
| 134 pending_requests_.erase(iter); | |
| 135 | |
| 136 // Favicon bitmaps are ordered by decreasing width. | |
| 137 if (!bitmaps.empty()) | |
| 138 AddFavicon(image_url, bitmaps[0]); | |
| 139 } | |
| 140 | |
| 141 void FaviconRawBitmapHandler::AddFavicon(const GURL& image_url, | |
| 142 const SkBitmap& new_bitmap) { | |
| 143 processed_requests_.insert(image_url); | |
| 144 if (new_bitmap.height() > kMaxBitmapSize || | |
| 145 new_bitmap.width() > kMaxBitmapSize) | |
| 146 return; | |
| 147 if (new_bitmap.height() < ash::kShelfSize) | |
| 148 return; | |
| 149 if (!bitmap_.isNull()) { | |
| 150 // We want the smallest icon that is large enough. | |
| 151 if (new_bitmap.height() > bitmap_.height()) | |
| 152 return; | |
| 153 } | |
| 154 bitmap_url_ = image_url; | |
| 155 bitmap_ = new_bitmap; | |
| 156 delegate_->FaviconUpdated(); | |
| 157 } | |
| 158 | |
| 159 } // namespace internal | |
| 160 | |
| 161 //////////////////////////////////////////////////////////////////////////////// | |
| 162 | |
| 163 LauncherFaviconLoader::LauncherFaviconLoader(Delegate* delegate, | |
| 164 content::WebContents* web_contents) | |
| 165 : web_contents_(web_contents) { | |
| 166 favicon_handler_.reset( | |
| 167 new internal::FaviconRawBitmapHandler(web_contents, delegate)); | |
| 168 } | |
| 169 | |
| 170 LauncherFaviconLoader::~LauncherFaviconLoader() { | |
| 171 } | |
| 172 | |
| 173 SkBitmap LauncherFaviconLoader::GetFavicon() const { | |
| 174 return favicon_handler_->bitmap(); | |
| 175 } | |
| 176 | |
| 177 bool LauncherFaviconLoader::HasPendingDownloads() const { | |
| 178 return favicon_handler_->HasPendingDownloads(); | |
| 179 } | |
| OLD | NEW |