Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / android_webview / browser / icon_helper.cc
blob7ef757f39680d506b2a47fcbb91ccf7efe46f398
1 // Copyright (c) 2013 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.
5 #include "android_webview/browser/icon_helper.h"
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/hash.h"
10 #include "base/logging.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/favicon_url.h"
14 #include "third_party/skia/include/core/SkBitmap.h"
15 #include "ui/gfx/geometry/size.h"
17 using content::BrowserThread;
18 using content::WebContents;
20 namespace android_webview {
22 IconHelper::IconHelper(WebContents* web_contents)
23 : WebContentsObserver(web_contents),
24 listener_(NULL),
25 missing_favicon_urls_() {
28 IconHelper::~IconHelper() {
31 void IconHelper::SetListener(Listener* listener) {
32 listener_ = listener;
35 void IconHelper::DownloadFaviconCallback(
36 int id,
37 int http_status_code,
38 const GURL& image_url,
39 const std::vector<SkBitmap>& bitmaps,
40 const std::vector<gfx::Size>& original_bitmap_sizes) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 if (http_status_code == 404) {
43 MarkUnableToDownloadFavicon(image_url);
44 return;
47 if (bitmaps.size() == 0) {
48 return;
51 // We can protentially have multiple frames of the icon
52 // in different sizes. We need more fine grain API spec
53 // to let clients pick out the frame they want.
55 // TODO(acleung): Pick the best icon to return based on size.
56 if (listener_)
57 listener_->OnReceivedIcon(image_url, bitmaps[0]);
60 void IconHelper::DidUpdateFaviconURL(
61 const std::vector<content::FaviconURL>& candidates) {
62 DCHECK_CURRENTLY_ON(BrowserThread::UI);
63 for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin();
64 i != candidates.end(); ++i) {
65 if (!i->icon_url.is_valid())
66 continue;
68 switch(i->icon_type) {
69 case content::FaviconURL::FAVICON:
70 if ((listener_ && !listener_->ShouldDownloadFavicon(i->icon_url)) ||
71 WasUnableToDownloadFavicon(i->icon_url)) {
72 break;
74 web_contents()->DownloadImage(i->icon_url,
75 true, // Is a favicon
76 0, // No maximum size
77 false, // Normal cache policy
78 base::Bind(
79 &IconHelper::DownloadFaviconCallback, base::Unretained(this)));
80 break;
81 case content::FaviconURL::TOUCH_ICON:
82 if (listener_)
83 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false);
84 break;
85 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON:
86 if (listener_)
87 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true);
88 break;
89 case content::FaviconURL::INVALID_ICON:
90 // Silently ignore it. Only trigger a callback on valid icons.
91 break;
92 default:
93 NOTREACHED();
94 break;
99 void IconHelper::DidStartNavigationToPendingEntry(
100 const GURL& url,
101 content::NavigationController::ReloadType reload_type) {
102 if (reload_type == content::NavigationController::RELOAD_IGNORING_CACHE)
103 ClearUnableToDownloadFavicons();
106 void IconHelper::MarkUnableToDownloadFavicon(const GURL& icon_url) {
107 MissingFaviconURLHash url_hash = base::Hash(icon_url.spec());
108 missing_favicon_urls_.insert(url_hash);
111 bool IconHelper::WasUnableToDownloadFavicon(const GURL& icon_url) const {
112 MissingFaviconURLHash url_hash = base::Hash(icon_url.spec());
113 return missing_favicon_urls_.find(url_hash) != missing_favicon_urls_.end();
116 void IconHelper::ClearUnableToDownloadFavicons() {
117 missing_favicon_urls_.clear();
120 } // namespace android_webview