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 "base/supports_user_data.h" |
| 8 #include "chrome/browser/favicon/favicon_download_helper_delegate.h" |
| 9 #include "chrome/browser/favicon/favicon_util.h" |
| 10 #include "chrome/common/icon_messages.h" |
| 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/browser/web_contents_delegate.h" |
| 14 |
| 15 using content::WebContents; |
| 16 |
| 17 namespace { |
| 18 |
| 19 const char* kFaviconDownloadHelperWebContentsUserDataKey = |
| 20 "web_contents_favicon_download"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 // static |
| 25 void FaviconDownloadHelper::CreateForWebContentsAndDelegate( |
| 26 content::WebContents* contents, |
| 27 FaviconDownloadHelperDelegate* delegate) { |
| 28 if (FromWebContents(contents)) |
| 29 return; |
| 30 |
| 31 contents->SetUserData(kFaviconDownloadHelperWebContentsUserDataKey, |
| 32 new base::UserDataAdapter<FaviconDownloadHelper>( |
| 33 new FaviconDownloadHelper(contents, delegate))); |
| 34 } |
| 35 |
| 36 // static |
| 37 FaviconDownloadHelper* FaviconDownloadHelper::FromWebContents( |
| 38 content::WebContents* contents) { |
| 39 return base::UserDataAdapter<FaviconDownloadHelper>::Get( |
| 40 contents, kFaviconDownloadHelperWebContentsUserDataKey); |
| 41 } |
| 42 |
| 43 |
| 44 FaviconDownloadHelper::FaviconDownloadHelper( |
| 45 WebContents* web_contents, |
| 46 FaviconDownloadHelperDelegate* delegate) |
| 47 : content::WebContentsObserver(web_contents), |
| 48 delegate_(delegate) { |
| 49 } |
| 50 |
| 51 FaviconDownloadHelper::~FaviconDownloadHelper() { |
| 52 } |
| 53 |
| 54 int FaviconDownloadHelper::DownloadFavicon(const GURL& url, int image_size) { |
| 55 content::RenderViewHost* host = web_contents()->GetRenderViewHost(); |
| 56 int id = FaviconUtil::DownloadFavicon(host, url, image_size); |
| 57 return id; |
| 58 } |
| 59 |
| 60 bool FaviconDownloadHelper::OnMessageReceived(const IPC::Message& message) { |
| 61 bool message_handled = false; // Allow other handlers to receive these. |
| 62 IPC_BEGIN_MESSAGE_MAP(FaviconDownloadHelper, message) |
| 63 IPC_MESSAGE_HANDLER(IconHostMsg_DidDownloadFavicon, OnDidDownloadFavicon) |
| 64 IPC_MESSAGE_HANDLER(IconHostMsg_UpdateFaviconURL, OnUpdateFaviconURL) |
| 65 IPC_MESSAGE_UNHANDLED(message_handled = false) |
| 66 IPC_END_MESSAGE_MAP() |
| 67 return message_handled; |
| 68 } |
| 69 |
| 70 void FaviconDownloadHelper::OnDidDownloadFavicon( |
| 71 int id, |
| 72 const GURL& image_url, |
| 73 bool errored, |
| 74 int requested_size, |
| 75 const std::vector<SkBitmap>& bitmaps) { |
| 76 if (delegate_) { |
| 77 delegate_->OnDidDownloadFavicon( |
| 78 id, image_url, errored, requested_size, bitmaps); |
| 79 } |
| 80 } |
| 81 |
| 82 void FaviconDownloadHelper::OnUpdateFaviconURL( |
| 83 int32 page_id, |
| 84 const std::vector<FaviconURL>& candidates) { |
| 85 if (delegate_) { |
| 86 delegate_->OnUpdateFaviconURL(page_id, candidates); |
| 87 } |
| 88 } |
OLD | NEW |