[Sync] Componentize UIModelWorker.
[chromium-blink-merge.git] / components / favicon / ios / web_favicon_driver.cc
blobb691ecfb91de4403dc691c93552751d0218c7e9e
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"
7 #include "base/bind.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);
19 namespace favicon {
21 // static
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))
28 return;
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;
50 return 0;
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() {
60 DCHECK(web_state());
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 bool WebFaviconDriver::GetActiveFaviconValidity() {
71 return GetFaviconStatus().valid;
74 void WebFaviconDriver::SetActiveFaviconValidity(bool validity) {
75 GetFaviconStatus().valid = validity;
78 GURL WebFaviconDriver::GetActiveFaviconURL() {
79 return GetFaviconStatus().url;
82 void WebFaviconDriver::SetActiveFaviconURL(const GURL& url) {
83 GetFaviconStatus().url = url;
86 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) {
87 GetFaviconStatus().image = image;
90 web::FaviconStatus& WebFaviconDriver::GetFaviconStatus() {
91 DCHECK(web_state()->GetNavigationManager()->GetVisibleItem());
92 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon();
95 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state,
96 FaviconService* favicon_service,
97 history::HistoryService* history_service,
98 bookmarks::BookmarkModel* bookmark_model)
99 : web::WebStateObserver(web_state),
100 FaviconDriverImpl(favicon_service, history_service, bookmark_model) {
103 WebFaviconDriver::~WebFaviconDriver() {
106 void WebFaviconDriver::FaviconUrlUpdated(
107 const std::vector<web::FaviconURL>& candidates) {
108 DCHECK(!candidates.empty());
109 OnUpdateFaviconURL(FaviconURLsFromWebFaviconURLs(candidates));
112 } // namespace favicon