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