This source file includes following definitions.
- weak_ptr_factory_
- bitmap
- DidUpdateFaviconURL
- HasPendingDownloads
- DidDownloadFavicon
- AddFavicon
- GetFavicon
- HasPendingDownloads
#include "chrome/browser/ui/ash/launcher/launcher_favicon_loader.h"
#include "ash/shelf/shelf_constants.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "url/gurl.h"
namespace internal {
const int kMaxBitmapSize = 256;
class FaviconBitmapHandler : public content::WebContentsObserver {
public:
FaviconBitmapHandler(content::WebContents* web_contents,
LauncherFaviconLoader::Delegate* delegate)
: content::WebContentsObserver(web_contents),
delegate_(delegate),
web_contents_(web_contents),
weak_ptr_factory_(this) {
}
virtual ~FaviconBitmapHandler() {}
const SkBitmap& bitmap() const { return bitmap_; }
bool HasPendingDownloads() const;
virtual void DidUpdateFaviconURL(
int32 page_id,
const std::vector<content::FaviconURL>& candidates) OVERRIDE;
private:
void DidDownloadFavicon(
int id,
int http_status_code,
const GURL& image_url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& original_bitmap_sizes);
void AddFavicon(const GURL& image_url, const SkBitmap& new_bitmap);
LauncherFaviconLoader::Delegate* delegate_;
content::WebContents* web_contents_;
typedef std::set<GURL> UrlSet;
UrlSet pending_requests_;
UrlSet processed_requests_;
SkBitmap bitmap_;
GURL bitmap_url_;
base::WeakPtrFactory<FaviconBitmapHandler> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(FaviconBitmapHandler);
};
void FaviconBitmapHandler::DidUpdateFaviconURL(
int32 page_id,
const std::vector<content::FaviconURL>& candidates) {
UrlSet new_pending, new_processed;
std::set<GURL> urls;
std::vector<content::FaviconURL>::const_iterator iter;
for (iter = candidates.begin(); iter != candidates.end(); ++iter) {
if (iter->icon_type != content::FaviconURL::FAVICON)
continue;
const GURL& url = iter->icon_url;
if (url.is_valid())
urls.insert(url);
if (pending_requests_.find(url) != pending_requests_.end())
new_pending.insert(url);
if (processed_requests_.find(url) != processed_requests_.end())
new_processed.insert(url);
}
pending_requests_ = new_pending;
processed_requests_ = new_processed;
if (urls.find(bitmap_url_) == urls.end()) {
bitmap_url_ = GURL();
bitmap_.reset();
}
for (std::set<GURL>::iterator iter = urls.begin();
iter != urls.end(); ++iter) {
if (processed_requests_.find(*iter) != processed_requests_.end())
continue;
if (pending_requests_.find(*iter) != pending_requests_.end())
continue;
pending_requests_.insert(*iter);
web_contents_->DownloadImage(
*iter,
true,
0,
base::Bind(&FaviconBitmapHandler::DidDownloadFavicon,
weak_ptr_factory_.GetWeakPtr()));
}
}
bool FaviconBitmapHandler::HasPendingDownloads() const {
return !pending_requests_.empty();
}
void FaviconBitmapHandler::DidDownloadFavicon(
int id,
int http_status_code,
const GURL& image_url,
const std::vector<SkBitmap>& bitmaps,
const std::vector<gfx::Size>& original_bitmap_sizes) {
UrlSet::iterator iter = pending_requests_.find(image_url);
if (iter == pending_requests_.end()) {
return;
}
pending_requests_.erase(iter);
if (!bitmaps.empty())
AddFavicon(image_url, bitmaps[0]);
}
void FaviconBitmapHandler::AddFavicon(const GURL& image_url,
const SkBitmap& new_bitmap) {
processed_requests_.insert(image_url);
if (new_bitmap.height() > kMaxBitmapSize ||
new_bitmap.width() > kMaxBitmapSize)
return;
if (new_bitmap.height() < ash::kShelfPreferredSize)
return;
if (!bitmap_.isNull()) {
if (new_bitmap.height() > bitmap_.height())
return;
}
bitmap_url_ = image_url;
bitmap_ = new_bitmap;
delegate_->FaviconUpdated();
}
}
LauncherFaviconLoader::LauncherFaviconLoader(Delegate* delegate,
content::WebContents* web_contents)
: web_contents_(web_contents) {
favicon_handler_.reset(
new internal::FaviconBitmapHandler(web_contents, delegate));
}
LauncherFaviconLoader::~LauncherFaviconLoader() {
}
SkBitmap LauncherFaviconLoader::GetFavicon() const {
return favicon_handler_->bitmap();
}
bool LauncherFaviconLoader::HasPendingDownloads() const {
return favicon_handler_->HasPendingDownloads();
}