Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "android_webview/browser/icon_helper.h" | 5 #include "android_webview/browser/icon_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/hash.h" | |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/common/favicon_url.h" | 13 #include "content/public/common/favicon_url.h" |
| 13 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
| 14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 using content::WebContents; | 18 using content::WebContents; |
| 18 | 19 |
| 19 namespace android_webview { | 20 namespace android_webview { |
| 20 | 21 |
| 21 IconHelper::IconHelper(WebContents* web_contents) | 22 IconHelper::IconHelper(WebContents* web_contents) |
| 22 : WebContentsObserver(web_contents), | 23 : WebContentsObserver(web_contents), |
| 23 listener_(NULL) { | 24 listener_(NULL), |
| 25 missing_favicon_urls_() { | |
| 24 } | 26 } |
| 25 | 27 |
| 26 IconHelper::~IconHelper() { | 28 IconHelper::~IconHelper() { |
| 27 } | 29 } |
| 28 | 30 |
| 29 void IconHelper::SetListener(Listener* listener) { | 31 void IconHelper::SetListener(Listener* listener) { |
| 30 listener_ = listener; | 32 listener_ = listener; |
| 31 } | 33 } |
| 32 | 34 |
| 33 void IconHelper::DownloadFaviconCallback( | 35 void IconHelper::DownloadFaviconCallback( |
| 34 int id, | 36 int id, |
| 35 int http_status_code, | 37 int http_status_code, |
| 36 const GURL& image_url, | 38 const GURL& image_url, |
| 37 const std::vector<SkBitmap>& bitmaps, | 39 const std::vector<SkBitmap>& bitmaps, |
| 38 const std::vector<gfx::Size>& original_bitmap_sizes) { | 40 const std::vector<gfx::Size>& original_bitmap_sizes) { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 42 if (http_status_code == 404) { | |
| 43 MarkUnableToDownloadFavicon(image_url); | |
| 44 return; | |
| 45 } | |
| 46 | |
| 40 if (bitmaps.size() == 0) { | 47 if (bitmaps.size() == 0) { |
| 41 return; | 48 return; |
| 42 } | 49 } |
| 43 | 50 |
| 44 // We can protentially have multiple frames of the icon | 51 // We can protentially have multiple frames of the icon |
| 45 // in different sizes. We need more fine grain API spec | 52 // in different sizes. We need more fine grain API spec |
| 46 // to let clients pick out the frame they want. | 53 // to let clients pick out the frame they want. |
| 47 | 54 |
| 48 // TODO(acleung): Pick the best icon to return based on size. | 55 // TODO(acleung): Pick the best icon to return based on size. |
| 49 if (listener_) | 56 if (listener_) |
| 50 listener_->OnReceivedIcon(image_url, bitmaps[0]); | 57 listener_->OnReceivedIcon(image_url, bitmaps[0]); |
| 51 } | 58 } |
| 52 | 59 |
| 53 void IconHelper::DidUpdateFaviconURL(int32 page_id, | 60 void IconHelper::DidUpdateFaviconURL(int32 page_id, |
| 54 const std::vector<content::FaviconURL>& candidates) { | 61 const std::vector<content::FaviconURL>& candidates) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 56 for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin(); | 63 for (std::vector<content::FaviconURL>::const_iterator i = candidates.begin(); |
| 57 i != candidates.end(); ++i) { | 64 i != candidates.end(); ++i) { |
| 58 if (!i->icon_url.is_valid()) | 65 if (!i->icon_url.is_valid()) |
| 59 continue; | 66 continue; |
| 60 | 67 |
| 61 switch(i->icon_type) { | 68 switch(i->icon_type) { |
| 62 case content::FaviconURL::FAVICON: | 69 case content::FaviconURL::FAVICON: |
| 63 if (listener_ && !listener_->ShouldDownloadFavicon(i->icon_url)) break; | 70 if ((listener_ && !listener_->ShouldDownloadFavicon(i->icon_url)) || |
| 71 WasUnableToDownloadFavicon(i->icon_url)) { | |
| 72 break; | |
| 73 } | |
| 64 web_contents()->DownloadImage(i->icon_url, | 74 web_contents()->DownloadImage(i->icon_url, |
| 65 true, // Is a favicon | 75 true, // Is a favicon |
| 66 0, // No maximum size | 76 0, // No maximum size |
| 67 base::Bind( | 77 base::Bind( |
| 68 &IconHelper::DownloadFaviconCallback, base::Unretained(this))); | 78 &IconHelper::DownloadFaviconCallback, base::Unretained(this))); |
| 69 break; | 79 break; |
| 70 case content::FaviconURL::TOUCH_ICON: | 80 case content::FaviconURL::TOUCH_ICON: |
| 71 if (listener_) | 81 if (listener_) |
| 72 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false); | 82 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), false); |
| 73 break; | 83 break; |
| 74 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON: | 84 case content::FaviconURL::TOUCH_PRECOMPOSED_ICON: |
| 75 if (listener_) | 85 if (listener_) |
| 76 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true); | 86 listener_->OnReceivedTouchIconUrl(i->icon_url.spec(), true); |
| 77 break; | 87 break; |
| 78 case content::FaviconURL::INVALID_ICON: | 88 case content::FaviconURL::INVALID_ICON: |
| 79 // Silently ignore it. Only trigger a callback on valid icons. | 89 // Silently ignore it. Only trigger a callback on valid icons. |
| 80 break; | 90 break; |
| 81 default: | 91 default: |
| 82 NOTREACHED(); | 92 NOTREACHED(); |
| 83 break; | 93 break; |
| 84 } | 94 } |
| 85 } | 95 } |
| 86 } | 96 } |
| 87 | 97 |
| 98 void IconHelper::DidStartNavigationToPendingEntry( | |
| 99 const GURL& url, | |
| 100 content::NavigationController::ReloadType reload_type) { | |
| 101 if (reload_type == content::NavigationController::RELOAD_IGNORING_CACHE) | |
| 102 ClearUnableToDownloadFavicons(); | |
|
mkosiba (inactive)
2014/04/24 15:42:44
just curious - why don't we want to clear when we
cimamoglu (inactive)
2014/04/28 10:41:04
The original downstream bug description specifical
| |
| 103 } | |
| 104 | |
| 105 void IconHelper::MarkUnableToDownloadFavicon(const GURL& icon_url) { | |
| 106 MissingFaviconURLHash url_hash = base::Hash(icon_url.spec()); | |
| 107 missing_favicon_urls_.insert(url_hash); | |
| 108 } | |
| 109 | |
| 110 bool IconHelper::WasUnableToDownloadFavicon(const GURL& icon_url) const { | |
| 111 MissingFaviconURLHash url_hash = base::Hash(icon_url.spec()); | |
| 112 return missing_favicon_urls_.find(url_hash) != missing_favicon_urls_.end(); | |
| 113 } | |
| 114 | |
| 115 void IconHelper::ClearUnableToDownloadFavicons() { | |
| 116 missing_favicon_urls_.clear(); | |
| 117 } | |
| 118 | |
| 119 | |
|
benm (inactive)
2014/04/24 19:03:21
nit: double new line
cimamoglu (inactive)
2014/04/28 10:41:04
Done.
| |
| 88 } // namespace android_webview | 120 } // namespace android_webview |
| OLD | NEW |