| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/favicon/favicon_service.h" | 5 #include "chrome/browser/favicon/favicon_service.h" |
| 6 | 6 |
| 7 #include "chrome/browser/history/history.h" | 7 #include "chrome/browser/history/history.h" |
| 8 #include "chrome/browser/history/history_backend.h" | 8 #include "chrome/browser/history/history_backend.h" |
| 9 #include "chrome/browser/history/history_service_factory.h" | 9 #include "chrome/browser/history/history_service_factory.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" | 11 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
| 12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 13 | 13 |
| 14 FaviconService::FaviconService(Profile* profile) : profile_(profile) { | 14 FaviconService::FaviconService(Profile* profile) : profile_(profile) { |
| 15 } | 15 } |
| 16 | 16 |
| 17 FaviconService::Handle FaviconService::GetFavicon( | 17 FaviconService::Handle FaviconService::GetFavicon( |
| 18 const GURL& icon_url, | 18 const GURL& icon_url, |
| 19 history::IconType icon_type, | 19 history::IconType icon_type, |
| 20 CancelableRequestConsumerBase* consumer, | 20 CancelableRequestConsumerBase* consumer, |
| 21 const FaviconDataCallback& callback) { | 21 const FaviconDataCallback& callback) { |
| 22 GetFaviconRequest* request = new GetFaviconRequest(callback); | 22 GetFaviconRequest* request = new GetFaviconRequest(callback); |
| 23 AddRequest(request, consumer); | 23 AddRequest(request, consumer); |
| 24 HistoryService* hs = | 24 HistoryService* hs = |
| 25 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 25 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 26 // There should only ever be a single favicon per history::IconType so |
| 27 // the requested size does not matter. |
| 26 if (hs) | 28 if (hs) |
| 27 hs->GetFavicon(request, icon_url, icon_type); | 29 hs->GetFaviconClosestToSize(request, icon_url, icon_type, gfx::Size()); |
| 28 else | 30 else |
| 29 ForwardEmptyResultAsync(request); | 31 ForwardEmptyResultAsync(request); |
| 30 return request->handle(); | 32 return request->handle(); |
| 31 } | 33 } |
| 32 | 34 |
| 33 FaviconService::Handle FaviconService::UpdateFaviconMappingAndFetch( | 35 FaviconService::Handle FaviconService::UpdateFaviconMappingAndFetch( |
| 34 const GURL& page_url, | 36 const GURL& page_url, |
| 35 const GURL& icon_url, | 37 const GURL& icon_url, |
| 36 history::IconType icon_type, | 38 history::IconType icon_type, |
| 37 CancelableRequestConsumerBase* consumer, | 39 CancelableRequestConsumerBase* consumer, |
| 38 const FaviconDataCallback& callback) { | 40 const FaviconDataCallback& callback) { |
| 39 GetFaviconRequest* request = new GetFaviconRequest(callback); | 41 GetFaviconRequest* request = new GetFaviconRequest(callback); |
| 40 AddRequest(request, consumer); | 42 AddRequest(request, consumer); |
| 41 HistoryService* hs = | 43 HistoryService* hs = |
| 42 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 44 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 43 if (hs) | 45 if (hs) { |
| 44 hs->UpdateFaviconMappingAndFetch(request, page_url, icon_url, icon_type); | 46 std::vector<GURL> icon_urls; |
| 45 else | 47 icon_urls.push_back(icon_url); |
| 48 hs->UpdateFaviconMappingsAndFetch(request, page_url, icon_urls, icon_type); |
| 49 } else { |
| 46 ForwardEmptyResultAsync(request); | 50 ForwardEmptyResultAsync(request); |
| 51 } |
| 47 return request->handle(); | 52 return request->handle(); |
| 48 } | 53 } |
| 49 | 54 |
| 50 FaviconService::Handle FaviconService::GetFaviconForURL( | 55 FaviconService::Handle FaviconService::GetFaviconForURL( |
| 51 const GURL& page_url, | 56 const GURL& page_url, |
| 52 int icon_types, | 57 int icon_types, |
| 53 CancelableRequestConsumerBase* consumer, | 58 CancelableRequestConsumerBase* consumer, |
| 54 const FaviconDataCallback& callback) { | 59 const FaviconDataCallback& callback) { |
| 55 GetFaviconRequest* request = new GetFaviconRequest(callback); | 60 GetFaviconRequest* request = new GetFaviconRequest(callback); |
| 56 AddRequest(request, consumer); | 61 AddRequest(request, consumer); |
| 57 FaviconService::Handle handle = request->handle(); | 62 FaviconService::Handle handle = request->handle(); |
| 58 if (page_url.SchemeIs(chrome::kChromeUIScheme) || | 63 if (page_url.SchemeIs(chrome::kChromeUIScheme) || |
| 59 page_url.SchemeIs(chrome::kExtensionScheme)) { | 64 page_url.SchemeIs(chrome::kExtensionScheme)) { |
| 60 ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL( | 65 ChromeWebUIControllerFactory::GetInstance()->GetFaviconForURL( |
| 61 profile_, request, page_url); | 66 profile_, request, page_url); |
| 62 } else { | 67 } else { |
| 63 HistoryService* hs = HistoryServiceFactory::GetForProfile( | 68 HistoryService* hs = HistoryServiceFactory::GetForProfile( |
| 64 profile_, Profile::EXPLICIT_ACCESS); | 69 profile_, Profile::EXPLICIT_ACCESS); |
| 65 if (hs) | 70 if (hs) { |
| 66 hs->GetFaviconForURL(request, page_url, icon_types); | 71 // There should only ever be a single favicon per history::IconType so |
| 67 else | 72 // the requested size does not matter. |
| 73 hs->GetFaviconForURLClosestToSize(request, page_url, icon_types, |
| 74 gfx::Size()); |
| 75 } else { |
| 68 ForwardEmptyResultAsync(request); | 76 ForwardEmptyResultAsync(request); |
| 77 } |
| 69 } | 78 } |
| 70 return handle; | 79 return handle; |
| 71 } | 80 } |
| 72 | 81 |
| 73 // Requests the favicon for |favicon_id|. The |consumer| is notified when the | |
| 74 // bits have been fetched. | |
| 75 FaviconService::Handle FaviconService::GetFaviconForID( | |
| 76 history::FaviconID favicon_id, | |
| 77 CancelableRequestConsumerBase* consumer, | |
| 78 const FaviconDataCallback& callback) { | |
| 79 GetFaviconRequest* request = new GetFaviconRequest(callback); | |
| 80 AddRequest(request, consumer); | |
| 81 FaviconService::Handle handle = request->handle(); | |
| 82 HistoryService* hs = | |
| 83 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | |
| 84 if (hs) | |
| 85 hs->GetFaviconForID(request, favicon_id); | |
| 86 else | |
| 87 ForwardEmptyResultAsync(request); | |
| 88 | |
| 89 return handle; | |
| 90 } | |
| 91 | |
| 92 | |
| 93 void FaviconService::SetFaviconOutOfDateForPage(const GURL& page_url) { | 82 void FaviconService::SetFaviconOutOfDateForPage(const GURL& page_url) { |
| 94 HistoryService* hs = | 83 HistoryService* hs = |
| 95 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 84 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 96 if (hs) | 85 if (hs) |
| 97 hs->SetFaviconOutOfDateForPage(page_url); | 86 hs->SetFaviconsOutOfDateForPage(page_url); |
| 98 } | 87 } |
| 99 | 88 |
| 100 void FaviconService::CloneFavicon(const GURL& old_page_url, | 89 void FaviconService::CloneFavicon(const GURL& old_page_url, |
| 101 const GURL& new_page_url) { | 90 const GURL& new_page_url) { |
| 102 HistoryService* hs = | 91 HistoryService* hs = |
| 103 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 92 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 104 if (hs) | 93 if (hs) |
| 105 hs->CloneFavicon(old_page_url, new_page_url); | 94 hs->CloneFavicon(old_page_url, new_page_url); |
| 106 } | 95 } |
| 107 | 96 |
| 108 void FaviconService::SetImportedFavicons( | 97 void FaviconService::SetImportedFavicons( |
| 109 const std::vector<history::ImportedFaviconUsage>& favicon_usage) { | 98 const std::vector<history::ImportedFaviconUsage>& favicon_usage) { |
| 110 HistoryService* hs = | 99 HistoryService* hs = |
| 111 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 100 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 112 if (hs) | 101 if (hs) |
| 113 hs->SetImportedFavicons(favicon_usage); | 102 hs->SetImportedFavicons(favicon_usage); |
| 114 } | 103 } |
| 115 | 104 |
| 116 void FaviconService::SetFavicon(const GURL& page_url, | 105 void FaviconService::SetFavicon(const GURL& page_url, |
| 117 const GURL& icon_url, | 106 const GURL& icon_url, |
| 118 const std::vector<unsigned char>& image_data, | 107 const std::vector<unsigned char>& image_data, |
| 119 history::IconType icon_type) { | 108 history::IconType icon_type) { |
| 120 HistoryService* hs = | 109 HistoryService* hs = |
| 121 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 110 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 122 if (hs) | 111 if (hs) { |
| 123 hs->SetFavicon(page_url, icon_url, image_data, icon_type); | 112 history::FaviconDataElement element; |
| 113 element.bitmap_data = scoped_refptr<base::RefCountedMemory>( |
| 114 new base::RefCountedBytes(image_data)); |
| 115 // As there should be only a single favicon per history::IconType the size |
| 116 // we use for inserting the favicon does not matter as long as the same |
| 117 // size is used for inserting everywhere. |
| 118 element.pixel_size = gfx::Size(); |
| 119 element.icon_url = icon_url; |
| 120 std::vector<history::FaviconDataElement> elements; |
| 121 elements.push_back(element); |
| 122 history::IconURLSizesMap icon_url_sizes; |
| 123 icon_url_sizes[icon_url].InsertSize(gfx::Size()); |
| 124 hs->SetFavicons(page_url, icon_type, elements, icon_url_sizes); |
| 125 } |
| 124 } | 126 } |
| 125 | 127 |
| 126 FaviconService::~FaviconService() { | 128 FaviconService::~FaviconService() { |
| 127 } | 129 } |
| 128 | 130 |
| 129 void FaviconService::ForwardEmptyResultAsync(GetFaviconRequest* request) { | 131 void FaviconService::ForwardEmptyResultAsync(GetFaviconRequest* request) { |
| 130 request->ForwardResultAsync(request->handle(), history::FaviconData()); | 132 request->ForwardResultAsync(request->handle(), history::FaviconData()); |
| 131 } | 133 } |
| OLD | NEW |