| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/fav_icon_helper.h" | 5 #include "chrome/browser/fav_icon_helper.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/ref_counted_memory.h" | 12 #include "base/ref_counted_memory.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/renderer_host/render_view_host.h" | 15 #include "chrome/browser/renderer_host/render_view_host.h" |
| 16 #include "chrome/browser/tab_contents/navigation_controller.h" | 16 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 17 #include "chrome/browser/tab_contents/navigation_entry.h" | 17 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 18 #include "chrome/browser/tab_contents/tab_contents_delegate.h" | 18 #include "chrome/browser/tab_contents/tab_contents_delegate.h" |
| 19 #include "chrome/browser/tab_contents/tab_contents.h" | 19 #include "chrome/browser/tab_contents/tab_contents.h" |
| 20 #include "chrome/common/render_messages.h" |
| 20 #include "gfx/codec/png_codec.h" | 21 #include "gfx/codec/png_codec.h" |
| 21 #include "gfx/favicon_size.h" | 22 #include "gfx/favicon_size.h" |
| 22 #include "skia/ext/image_operations.h" | 23 #include "skia/ext/image_operations.h" |
| 23 | 24 |
| 24 FavIconHelper::FavIconHelper(TabContents* tab_contents) | 25 FavIconHelper::FavIconHelper(TabContents* tab_contents) |
| 25 : tab_contents_(tab_contents), | 26 : tab_contents_(tab_contents), |
| 26 got_fav_icon_url_(false), | 27 got_fav_icon_url_(false), |
| 27 got_fav_icon_from_history_(false), | 28 got_fav_icon_from_history_(false), |
| 28 fav_icon_expired_(false) { | 29 fav_icon_expired_(false) { |
| 29 } | 30 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 // No matter what happens, we need to mark the favicon as being set. | 105 // No matter what happens, we need to mark the favicon as being set. |
| 105 entry->favicon().set_is_valid(true); | 106 entry->favicon().set_is_valid(true); |
| 106 | 107 |
| 107 if (image.empty()) | 108 if (image.empty()) |
| 108 return; | 109 return; |
| 109 | 110 |
| 110 entry->favicon().set_bitmap(image); | 111 entry->favicon().set_bitmap(image); |
| 111 tab_contents_->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB); | 112 tab_contents_->NotifyNavigationStateChanged(TabContents::INVALIDATE_TAB); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void FavIconHelper::UpdateFavIconURL(RenderViewHost* render_view_host, | 115 void FavIconHelper::OnUpdateFavIconURL(int32 page_id, const GURL& icon_url) { |
| 115 int32 page_id, | |
| 116 const GURL& icon_url) { | |
| 117 // TODO(davemoore) Should clear on empty url. Currently we ignore it. | 116 // TODO(davemoore) Should clear on empty url. Currently we ignore it. |
| 118 // This appears to be what FF does as well. | 117 // This appears to be what FF does as well. |
| 119 if (icon_url.is_empty()) | 118 if (icon_url.is_empty()) |
| 120 return; | 119 return; |
| 121 | 120 |
| 122 NavigationEntry* entry = GetEntry(); | 121 NavigationEntry* entry = GetEntry(); |
| 123 if (!entry) | 122 if (!entry) |
| 124 return; | 123 return; |
| 125 | 124 |
| 126 got_fav_icon_url_ = true; | 125 got_fav_icon_url_ = true; |
| 127 | 126 |
| 128 if (!GetFaviconService()) | 127 if (!GetFaviconService()) |
| 129 return; | 128 return; |
| 130 | 129 |
| 131 if (!fav_icon_expired_ && entry->favicon().is_valid() && | 130 if (!fav_icon_expired_ && entry->favicon().is_valid() && |
| 132 entry->favicon().url() == icon_url) { | 131 entry->favicon().url() == icon_url) { |
| 133 // We already have the icon, no need to proceed. | 132 // We already have the icon, no need to proceed. |
| 134 return; | 133 return; |
| 135 } | 134 } |
| 136 | 135 |
| 137 entry->favicon().set_url(icon_url); | 136 entry->favicon().set_url(icon_url); |
| 138 | 137 |
| 139 if (got_fav_icon_from_history_) | 138 if (got_fav_icon_from_history_) |
| 140 DownloadFavIconOrAskHistory(entry); | 139 DownloadFavIconOrAskHistory(entry); |
| 141 } | 140 } |
| 142 | 141 |
| 143 void FavIconHelper::DidDownloadFavIcon(RenderViewHost* render_view_host, | 142 bool FavIconHelper::OnMessageReceived(const IPC::Message& message) { |
| 144 int id, | 143 bool handled = true; |
| 145 const GURL& image_url, | 144 IPC_BEGIN_MESSAGE_MAP(FavIconHelper, message) |
| 146 bool errored, | 145 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFavIconURL, OnUpdateFavIconURL) |
| 147 const SkBitmap& image) { | 146 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDownloadFavIcon, OnDidDownloadFavIcon) |
| 147 IPC_MESSAGE_UNHANDLED(handled = false) |
| 148 IPC_END_MESSAGE_MAP() |
| 149 return handled; |
| 150 } |
| 151 |
| 152 void FavIconHelper::OnDidDownloadFavIcon(int id, |
| 153 const GURL& image_url, |
| 154 bool errored, |
| 155 const SkBitmap& image) { |
| 148 DownloadRequests::iterator i = download_requests_.find(id); | 156 DownloadRequests::iterator i = download_requests_.find(id); |
| 149 if (i == download_requests_.end()) { | 157 if (i == download_requests_.end()) { |
| 150 // Currently TabContents notifies us of ANY downloads so that it is | 158 // Currently TabContents notifies us of ANY downloads so that it is |
| 151 // possible to get here. | 159 // possible to get here. |
| 152 return; | 160 return; |
| 153 } | 161 } |
| 154 | 162 |
| 155 if (i->second.callback) { | 163 if (i->second.callback) { |
| 156 i->second.callback->Run(id, errored, image); | 164 i->second.callback->Run(id, errored, image); |
| 157 } else if (!errored) { | 165 } else if (!errored) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 } | 307 } |
| 300 | 308 |
| 301 bool FavIconHelper::ShouldSaveFavicon(const GURL& url) { | 309 bool FavIconHelper::ShouldSaveFavicon(const GURL& url) { |
| 302 if (!profile()->IsOffTheRecord()) | 310 if (!profile()->IsOffTheRecord()) |
| 303 return true; | 311 return true; |
| 304 | 312 |
| 305 // Otherwise store the favicon if the page is bookmarked. | 313 // Otherwise store the favicon if the page is bookmarked. |
| 306 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); | 314 BookmarkModel* bookmark_model = profile()->GetBookmarkModel(); |
| 307 return bookmark_model && bookmark_model->IsBookmarked(url); | 315 return bookmark_model && bookmark_model->IsBookmarked(url); |
| 308 } | 316 } |
| OLD | NEW |