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/ios/web_favicon_driver.h"
8 #include "components/favicon/core/favicon_url.h"
9 #include "components/favicon/ios/favicon_url_util.h"
10 #include "ios/web/public/browser_state.h"
11 #include "ios/web/public/favicon_status.h"
12 #include "ios/web/public/navigation_item.h"
13 #include "ios/web/public/navigation_manager.h"
14 #include "ios/web/public/web_state/web_state.h"
15 #include "ui/gfx/image/image.h"
17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver
);
22 void WebFaviconDriver::CreateForWebState(
23 web::WebState
* web_state
,
24 FaviconService
* favicon_service
,
25 history::HistoryService
* history_service
,
26 bookmarks::BookmarkModel
* bookmark_model
) {
27 if (FromWebState(web_state
))
30 web_state
->SetUserData(UserDataKey(),
31 new WebFaviconDriver(web_state
, favicon_service
,
32 history_service
, bookmark_model
));
35 gfx::Image
WebFaviconDriver::GetFavicon() const {
36 web::NavigationItem
* item
=
37 web_state()->GetNavigationManager()->GetLastCommittedItem();
38 return item
? item
->GetFavicon().image
: gfx::Image();
41 bool WebFaviconDriver::FaviconIsValid() const {
42 web::NavigationItem
* item
=
43 web_state()->GetNavigationManager()->GetLastCommittedItem();
44 return item
? item
->GetFavicon().valid
: false;
47 int WebFaviconDriver::StartDownload(const GURL
& url
, int max_image_size
) {
48 if (WasUnableToDownloadFavicon(url
)) {
49 DVLOG(1) << "Skip Failed FavIcon: " << url
;
53 return web_state()->DownloadImage(
54 url
, true, max_image_size
, false,
55 base::Bind(&FaviconDriverImpl::DidDownloadFavicon
,
56 base::Unretained(this)));
59 bool WebFaviconDriver::IsOffTheRecord() {
61 return web_state()->GetBrowserState()->IsOffTheRecord();
64 GURL
WebFaviconDriver::GetActiveURL() {
65 web::NavigationItem
* item
=
66 web_state()->GetNavigationManager()->GetVisibleItem();
67 return item
? item
->GetURL() : GURL();
70 base::string16
WebFaviconDriver::GetActiveTitle() {
71 web::NavigationItem
* item
=
72 web_state()->GetNavigationManager()->GetVisibleItem();
73 return item
? item
->GetTitle() : base::string16();
76 bool WebFaviconDriver::GetActiveFaviconValidity() {
77 return GetFaviconStatus().valid
;
80 void WebFaviconDriver::SetActiveFaviconValidity(bool validity
) {
81 GetFaviconStatus().valid
= validity
;
84 GURL
WebFaviconDriver::GetActiveFaviconURL() {
85 return GetFaviconStatus().url
;
88 void WebFaviconDriver::SetActiveFaviconURL(const GURL
& url
) {
89 GetFaviconStatus().url
= url
;
92 gfx::Image
WebFaviconDriver::GetActiveFaviconImage() {
93 return GetFaviconStatus().image
;
96 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image
& image
) {
97 GetFaviconStatus().image
= image
;
100 web::FaviconStatus
& WebFaviconDriver::GetFaviconStatus() {
101 DCHECK(web_state()->GetNavigationManager()->GetVisibleItem());
102 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon();
105 WebFaviconDriver::WebFaviconDriver(web::WebState
* web_state
,
106 FaviconService
* favicon_service
,
107 history::HistoryService
* history_service
,
108 bookmarks::BookmarkModel
* bookmark_model
)
109 : web::WebStateObserver(web_state
),
110 FaviconDriverImpl(favicon_service
, history_service
, bookmark_model
) {
113 WebFaviconDriver::~WebFaviconDriver() {
116 void WebFaviconDriver::FaviconUrlUpdated(
117 const std::vector
<web::FaviconURL
>& candidates
) {
118 DCHECK(!candidates
.empty());
119 OnUpdateFaviconURL(FaviconURLsFromWebFaviconURLs(candidates
));
122 } // namespace favicon