1 // Copyright 2015 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.
5 #include "components/favicon/core/favicon_driver_impl.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/strings/string_util.h"
11 #include "components/bookmarks/browser/bookmark_model.h"
12 #include "components/favicon/core/favicon_driver_observer.h"
13 #include "components/favicon/core/favicon_handler.h"
14 #include "components/favicon/core/favicon_service.h"
15 #include "components/history/core/browser/history_service.h"
16 #include "ui/base/ui_base_switches.h"
21 // Returns whether icon NTP is enabled by experiment.
22 // TODO(huangs): Remove all 3 copies of this routine once Icon NTP launches.
23 bool IsIconNTPEnabled() {
24 // Note: It's important to query the field trial state first, to ensure that
25 // UMA reports the correct group.
26 const std::string group_name
= base::FieldTrialList::FindFullName("IconNTP");
27 using base::CommandLine
;
28 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDisableIconNtp
))
30 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableIconNtp
))
33 return base::StartsWith(group_name
, "Enabled", base::CompareCase::SENSITIVE
);
36 #if defined(OS_ANDROID) || defined(OS_IOS)
37 const bool kEnableTouchIcon
= true;
39 const bool kEnableTouchIcon
= false;
44 FaviconDriverImpl::FaviconDriverImpl(FaviconService
* favicon_service
,
45 history::HistoryService
* history_service
,
46 bookmarks::BookmarkModel
* bookmark_model
)
47 : favicon_service_(favicon_service
),
48 history_service_(history_service
),
49 bookmark_model_(bookmark_model
) {
50 favicon_handler_
.reset(new FaviconHandler(
51 favicon_service_
, this, FaviconHandler::FAVICON
, kEnableTouchIcon
));
52 if (kEnableTouchIcon
) {
53 touch_icon_handler_
.reset(new FaviconHandler(favicon_service_
, this,
54 FaviconHandler::TOUCH
, true));
56 if (IsIconNTPEnabled()) {
57 large_icon_handler_
.reset(new FaviconHandler(favicon_service_
, this,
58 FaviconHandler::LARGE
, true));
62 FaviconDriverImpl::~FaviconDriverImpl() {
65 void FaviconDriverImpl::FetchFavicon(const GURL
& url
) {
66 favicon_handler_
->FetchFavicon(url
);
67 if (touch_icon_handler_
.get())
68 touch_icon_handler_
->FetchFavicon(url
);
69 if (large_icon_handler_
.get())
70 large_icon_handler_
->FetchFavicon(url
);
73 void FaviconDriverImpl::DidDownloadFavicon(
76 const GURL
& image_url
,
77 const std::vector
<SkBitmap
>& bitmaps
,
78 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
79 if (bitmaps
.empty() && http_status_code
== 404) {
80 DVLOG(1) << "Failed to Download Favicon:" << image_url
;
82 favicon_service_
->UnableToDownloadFavicon(image_url
);
85 favicon_handler_
->OnDidDownloadFavicon(id
, image_url
, bitmaps
,
86 original_bitmap_sizes
);
87 if (touch_icon_handler_
.get()) {
88 touch_icon_handler_
->OnDidDownloadFavicon(id
, image_url
, bitmaps
,
89 original_bitmap_sizes
);
91 if (large_icon_handler_
.get()) {
92 large_icon_handler_
->OnDidDownloadFavicon(id
, image_url
, bitmaps
,
93 original_bitmap_sizes
);
97 bool FaviconDriverImpl::IsBookmarked(const GURL
& url
) {
98 return bookmark_model_
&& bookmark_model_
->IsBookmarked(url
);
101 void FaviconDriverImpl::OnFaviconAvailable(const gfx::Image
& image
,
102 const GURL
& icon_url
,
103 bool is_active_favicon
) {
104 if (is_active_favicon
) {
105 bool icon_url_changed
= GetActiveFaviconURL() != icon_url
;
106 // No matter what happens, we need to mark the favicon as being set.
107 SetActiveFaviconValidity(true);
108 SetActiveFaviconURL(icon_url
);
113 SetActiveFaviconImage(image
);
114 NotifyFaviconUpdated(icon_url_changed
);
116 if (!image
.IsEmpty())
117 NotifyFaviconAvailable(image
);
120 bool FaviconDriverImpl::HasPendingTasksForTest() {
121 if (favicon_handler_
->HasPendingTasksForTest())
123 if (touch_icon_handler_
&& touch_icon_handler_
->HasPendingTasksForTest())
125 if (large_icon_handler_
&& large_icon_handler_
->HasPendingTasksForTest())
130 bool FaviconDriverImpl::WasUnableToDownloadFavicon(const GURL
& url
) {
131 return favicon_service_
&& favicon_service_
->WasUnableToDownloadFavicon(url
);
134 void FaviconDriverImpl::SetFaviconOutOfDateForPage(const GURL
& url
,
136 if (favicon_service_
) {
137 favicon_service_
->SetFaviconOutOfDateForPage(url
);
139 favicon_service_
->ClearUnableToDownloadFavicons();
143 void FaviconDriverImpl::OnUpdateFaviconURL(
144 const std::vector
<FaviconURL
>& candidates
) {
145 DCHECK(!candidates
.empty());
146 favicon_handler_
->OnUpdateFaviconURL(candidates
);
147 if (touch_icon_handler_
.get())
148 touch_icon_handler_
->OnUpdateFaviconURL(candidates
);
149 if (large_icon_handler_
.get())
150 large_icon_handler_
->OnUpdateFaviconURL(candidates
);
153 } // namespace favicon