| OLD | NEW |
| (Empty) |
| 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/favicon/favicon_download_helper.h" | |
| 6 | |
| 7 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" | |
| 8 #include "chrome/common/icon_messages.h" | |
| 9 #include "content/public/browser/render_view_host.h" | |
| 10 #include "content/public/browser/web_contents.h" | |
| 11 #include "content/public/browser/web_contents_delegate.h" | |
| 12 | |
| 13 using content::WebContents; | |
| 14 | |
| 15 namespace { | |
| 16 static int StartDownload(content::RenderViewHost* rvh, | |
| 17 const GURL& url, | |
| 18 int image_size) { | |
| 19 static int id = 0; | |
| 20 rvh->Send(new IconMsg_DownloadFavicon(rvh->GetRoutingID(), ++id, url, | |
| 21 image_size)); | |
| 22 return id; | |
| 23 } | |
| 24 } // namespace. | |
| 25 | |
| 26 FaviconDownloadHelper::FaviconDownloadHelper( | |
| 27 WebContents* web_contents, | |
| 28 FaviconDownloadHelperDelegate* delegate) | |
| 29 : content::WebContentsObserver(web_contents), | |
| 30 delegate_(delegate) { | |
| 31 DCHECK(delegate_); | |
| 32 } | |
| 33 | |
| 34 FaviconDownloadHelper::~FaviconDownloadHelper() { | |
| 35 } | |
| 36 | |
| 37 int FaviconDownloadHelper::DownloadFavicon(const GURL& url, int image_size) { | |
| 38 content::RenderViewHost* host = web_contents()->GetRenderViewHost(); | |
| 39 int id = StartDownload(host, url, image_size); | |
| 40 DownloadIdList::iterator i = | |
| 41 std::find(download_ids_.begin(), download_ids_.end(), id); | |
| 42 DCHECK(i == download_ids_.end()); | |
| 43 download_ids_.insert(id); | |
| 44 return id; | |
| 45 } | |
| 46 | |
| 47 bool FaviconDownloadHelper::OnMessageReceived(const IPC::Message& message) { | |
| 48 bool message_handled = false; // Allow other handlers to receive these. | |
| 49 IPC_BEGIN_MESSAGE_MAP(FaviconDownloadHelper, message) | |
| 50 IPC_MESSAGE_HANDLER(IconHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) | |
| 51 IPC_MESSAGE_HANDLER(IconHostMsg_UpdateFaviconURL, OnUpdateFaviconURL) | |
| 52 IPC_MESSAGE_UNHANDLED(message_handled = false) | |
| 53 IPC_END_MESSAGE_MAP() | |
| 54 return message_handled; | |
| 55 } | |
| 56 | |
| 57 void FaviconDownloadHelper::OnDidDownloadFavicon( | |
| 58 int id, | |
| 59 const GURL& image_url, | |
| 60 bool errored, | |
| 61 int requested_size, | |
| 62 const std::vector<SkBitmap>& bitmaps) { | |
| 63 DownloadIdList::iterator i = | |
| 64 std::find(download_ids_.begin(), download_ids_.end(), id); | |
| 65 if (i == download_ids_.end()) { | |
| 66 // Currently WebContents notifies us of ANY downloads so that it is | |
| 67 // possible to get here. | |
| 68 return; | |
| 69 } | |
| 70 delegate_->OnDidDownloadFavicon( | |
| 71 id, image_url, errored, requested_size, bitmaps); | |
| 72 download_ids_.erase(i); | |
| 73 } | |
| 74 | |
| 75 void FaviconDownloadHelper::OnUpdateFaviconURL( | |
| 76 int32 page_id, | |
| 77 const std::vector<FaviconURL>& candidates) { | |
| 78 delegate_->OnUpdateFaviconURL(page_id, candidates); | |
| 79 } | |
| OLD | NEW |