| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "components/favicon/ios/web_favicon_driver.h" | 5 #include "components/favicon/ios/web_favicon_driver.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "components/favicon/core/favicon_url.h" | 8 #include "components/favicon/core/favicon_url.h" |
| 9 #include "components/favicon/ios/favicon_url_util.h" | 9 #include "components/favicon/ios/favicon_url_util.h" |
| 10 #include "ios/web/public/browser_state.h" | 10 #include "ios/web/public/browser_state.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 history::HistoryService* history_service, | 25 history::HistoryService* history_service, |
| 26 bookmarks::BookmarkModel* bookmark_model) { | 26 bookmarks::BookmarkModel* bookmark_model) { |
| 27 if (FromWebState(web_state)) | 27 if (FromWebState(web_state)) |
| 28 return; | 28 return; |
| 29 | 29 |
| 30 web_state->SetUserData(UserDataKey(), | 30 web_state->SetUserData(UserDataKey(), |
| 31 new WebFaviconDriver(web_state, favicon_service, | 31 new WebFaviconDriver(web_state, favicon_service, |
| 32 history_service, bookmark_model)); | 32 history_service, bookmark_model)); |
| 33 } | 33 } |
| 34 | 34 |
| 35 void WebFaviconDriver::FetchFavicon(const GURL& url) { |
| 36 fetch_favicon_url_ = url; |
| 37 FaviconDriverImpl::FetchFavicon(url); |
| 38 } |
| 39 |
| 35 gfx::Image WebFaviconDriver::GetFavicon() const { | 40 gfx::Image WebFaviconDriver::GetFavicon() const { |
| 36 web::NavigationItem* item = | 41 web::NavigationItem* item = |
| 37 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 42 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
| 38 return item ? item->GetFavicon().image : gfx::Image(); | 43 return item ? item->GetFavicon().image : gfx::Image(); |
| 39 } | 44 } |
| 40 | 45 |
| 41 bool WebFaviconDriver::FaviconIsValid() const { | 46 bool WebFaviconDriver::FaviconIsValid() const { |
| 42 web::NavigationItem* item = | 47 web::NavigationItem* item = |
| 43 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 48 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
| 44 return item ? item->GetFavicon().valid : false; | 49 return item ? item->GetFavicon().valid : false; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 61 return web_state()->GetBrowserState()->IsOffTheRecord(); | 66 return web_state()->GetBrowserState()->IsOffTheRecord(); |
| 62 } | 67 } |
| 63 | 68 |
| 64 GURL WebFaviconDriver::GetActiveURL() { | 69 GURL WebFaviconDriver::GetActiveURL() { |
| 65 web::NavigationItem* item = | 70 web::NavigationItem* item = |
| 66 web_state()->GetNavigationManager()->GetVisibleItem(); | 71 web_state()->GetNavigationManager()->GetVisibleItem(); |
| 67 return item ? item->GetURL() : GURL(); | 72 return item ? item->GetURL() : GURL(); |
| 68 } | 73 } |
| 69 | 74 |
| 70 bool WebFaviconDriver::GetActiveFaviconValidity() { | 75 bool WebFaviconDriver::GetActiveFaviconValidity() { |
| 71 return GetFaviconStatus().valid; | 76 return !ActiveURLChangedSinceFetchFavicon() && GetFaviconStatus().valid; |
| 72 } | 77 } |
| 73 | 78 |
| 74 void WebFaviconDriver::SetActiveFaviconValidity(bool validity) { | 79 void WebFaviconDriver::SetActiveFaviconValidity(bool validity) { |
| 75 GetFaviconStatus().valid = validity; | 80 GetFaviconStatus().valid = validity; |
| 76 } | 81 } |
| 77 | 82 |
| 78 GURL WebFaviconDriver::GetActiveFaviconURL() { | 83 GURL WebFaviconDriver::GetActiveFaviconURL() { |
| 79 return GetFaviconStatus().url; | 84 return ActiveURLChangedSinceFetchFavicon() ? GURL() : GetFaviconStatus().url; |
| 80 } | 85 } |
| 81 | 86 |
| 82 void WebFaviconDriver::SetActiveFaviconURL(const GURL& url) { | 87 void WebFaviconDriver::SetActiveFaviconURL(const GURL& url) { |
| 83 GetFaviconStatus().url = url; | 88 GetFaviconStatus().url = url; |
| 84 } | 89 } |
| 85 | 90 |
| 86 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) { | 91 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) { |
| 87 GetFaviconStatus().image = image; | 92 GetFaviconStatus().image = image; |
| 88 } | 93 } |
| 89 | 94 |
| 95 bool WebFaviconDriver::ActiveURLChangedSinceFetchFavicon() { |
| 96 // On iOS the active URL can change in between calls to FetchFavicon(). For |
| 97 // instance, FetchFavicon() is not synchronously called when the active URL |
| 98 // changes as a result of CRWSessionController::goToEntry(). |
| 99 // TODO(stuartmorgan): Remove this once iOS always triggers favicon fetches |
| 100 // synchronously after active URL changes. |
| 101 return GetActiveURL() != fetch_favicon_url_; |
| 102 } |
| 103 |
| 90 web::FaviconStatus& WebFaviconDriver::GetFaviconStatus() { | 104 web::FaviconStatus& WebFaviconDriver::GetFaviconStatus() { |
| 91 DCHECK(web_state()->GetNavigationManager()->GetVisibleItem()); | 105 DCHECK(!ActiveURLChangedSinceFetchFavicon()); |
| 92 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon(); | 106 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon(); |
| 93 } | 107 } |
| 94 | 108 |
| 95 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, | 109 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, |
| 96 FaviconService* favicon_service, | 110 FaviconService* favicon_service, |
| 97 history::HistoryService* history_service, | 111 history::HistoryService* history_service, |
| 98 bookmarks::BookmarkModel* bookmark_model) | 112 bookmarks::BookmarkModel* bookmark_model) |
| 99 : web::WebStateObserver(web_state), | 113 : web::WebStateObserver(web_state), |
| 100 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { | 114 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { |
| 101 } | 115 } |
| 102 | 116 |
| 103 WebFaviconDriver::~WebFaviconDriver() { | 117 WebFaviconDriver::~WebFaviconDriver() { |
| 104 } | 118 } |
| 105 | 119 |
| 106 void WebFaviconDriver::FaviconUrlUpdated( | 120 void WebFaviconDriver::FaviconUrlUpdated( |
| 107 const std::vector<web::FaviconURL>& candidates) { | 121 const std::vector<web::FaviconURL>& candidates) { |
| 108 DCHECK(!candidates.empty()); | 122 DCHECK(!candidates.empty()); |
| 109 OnUpdateFaviconURL(FaviconURLsFromWebFaviconURLs(candidates)); | 123 OnUpdateFaviconURL(GetActiveURL(), FaviconURLsFromWebFaviconURLs(candidates)); |
| 110 } | 124 } |
| 111 | 125 |
| 112 } // namespace favicon | 126 } // namespace favicon |
| OLD | NEW |