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" |
11 #include "ios/web/public/favicon_status.h" | 11 #include "ios/web/public/favicon_status.h" |
12 #include "ios/web/public/navigation_item.h" | 12 #include "ios/web/public/navigation_item.h" |
13 #include "ios/web/public/navigation_manager.h" | 13 #include "ios/web/public/navigation_manager.h" |
14 #include "ios/web/public/web_state/web_state.h" | 14 #include "ios/web/public/web_state/web_state.h" |
15 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
16 | 16 |
17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); | 17 DEFINE_WEB_STATE_USER_DATA_KEY(favicon::WebFaviconDriver); |
18 | 18 |
19 namespace favicon { | 19 namespace favicon { |
20 namespace { | |
21 | |
22 std::string UrlWithoutFragment(const GURL& gurl) { | |
sky
2015/09/11 15:27:15
Return a GURL? Especially given the name you went
pkotwicz
2015/09/11 17:39:58
Done.
| |
23 GURL::Replacements replacements; | |
24 replacements.ClearRef(); | |
25 return gurl.ReplaceComponents(replacements).spec(); | |
26 } | |
27 | |
28 } // namespace | |
20 | 29 |
21 // static | 30 // static |
22 void WebFaviconDriver::CreateForWebState( | 31 void WebFaviconDriver::CreateForWebState( |
23 web::WebState* web_state, | 32 web::WebState* web_state, |
24 FaviconService* favicon_service, | 33 FaviconService* favicon_service, |
25 history::HistoryService* history_service, | 34 history::HistoryService* history_service, |
26 bookmarks::BookmarkModel* bookmark_model) { | 35 bookmarks::BookmarkModel* bookmark_model) { |
27 if (FromWebState(web_state)) | 36 if (FromWebState(web_state)) |
28 return; | 37 return; |
29 | 38 |
30 web_state->SetUserData(UserDataKey(), | 39 web_state->SetUserData(UserDataKey(), |
31 new WebFaviconDriver(web_state, favicon_service, | 40 new WebFaviconDriver(web_state, favicon_service, |
32 history_service, bookmark_model)); | 41 history_service, bookmark_model)); |
33 } | 42 } |
34 | 43 |
44 void WebFaviconDriver::FetchFavicon(const GURL& url) { | |
45 fetch_favicon_url_ = url; | |
46 FaviconDriverImpl::FetchFavicon(url); | |
47 } | |
48 | |
35 gfx::Image WebFaviconDriver::GetFavicon() const { | 49 gfx::Image WebFaviconDriver::GetFavicon() const { |
36 web::NavigationItem* item = | 50 web::NavigationItem* item = |
37 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 51 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
38 return item ? item->GetFavicon().image : gfx::Image(); | 52 return item ? item->GetFavicon().image : gfx::Image(); |
39 } | 53 } |
40 | 54 |
41 bool WebFaviconDriver::FaviconIsValid() const { | 55 bool WebFaviconDriver::FaviconIsValid() const { |
42 web::NavigationItem* item = | 56 web::NavigationItem* item = |
43 web_state()->GetNavigationManager()->GetLastCommittedItem(); | 57 web_state()->GetNavigationManager()->GetLastCommittedItem(); |
44 return item ? item->GetFavicon().valid : false; | 58 return item ? item->GetFavicon().valid : false; |
(...skipping 16 matching lines...) Expand all Loading... | |
61 return web_state()->GetBrowserState()->IsOffTheRecord(); | 75 return web_state()->GetBrowserState()->IsOffTheRecord(); |
62 } | 76 } |
63 | 77 |
64 GURL WebFaviconDriver::GetActiveURL() { | 78 GURL WebFaviconDriver::GetActiveURL() { |
65 web::NavigationItem* item = | 79 web::NavigationItem* item = |
66 web_state()->GetNavigationManager()->GetVisibleItem(); | 80 web_state()->GetNavigationManager()->GetVisibleItem(); |
67 return item ? item->GetURL() : GURL(); | 81 return item ? item->GetURL() : GURL(); |
68 } | 82 } |
69 | 83 |
70 bool WebFaviconDriver::GetActiveFaviconValidity() { | 84 bool WebFaviconDriver::GetActiveFaviconValidity() { |
71 return GetFaviconStatus().valid; | 85 return !ActiveURLChangedSinceFetchFavicon() && GetFaviconStatus().valid; |
72 } | 86 } |
73 | 87 |
74 void WebFaviconDriver::SetActiveFaviconValidity(bool validity) { | 88 void WebFaviconDriver::SetActiveFaviconValidity(bool validity) { |
75 GetFaviconStatus().valid = validity; | 89 GetFaviconStatus().valid = validity; |
76 } | 90 } |
77 | 91 |
78 GURL WebFaviconDriver::GetActiveFaviconURL() { | 92 GURL WebFaviconDriver::GetActiveFaviconURL() { |
79 return GetFaviconStatus().url; | 93 return ActiveURLChangedSinceFetchFavicon() ? GURL() : GetFaviconStatus().url; |
80 } | 94 } |
81 | 95 |
82 void WebFaviconDriver::SetActiveFaviconURL(const GURL& url) { | 96 void WebFaviconDriver::SetActiveFaviconURL(const GURL& url) { |
83 GetFaviconStatus().url = url; | 97 GetFaviconStatus().url = url; |
84 } | 98 } |
85 | 99 |
86 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) { | 100 void WebFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) { |
87 GetFaviconStatus().image = image; | 101 GetFaviconStatus().image = image; |
88 } | 102 } |
89 | 103 |
104 bool WebFaviconDriver::ShouldSendFaviconAvailableNotifications() { | |
105 // FaviconHandler sends notifications with the assumption that the active URL | |
106 // has not changed. Suppress FaviconHandler notifications if the active URL | |
107 // has changed. | |
108 return !ActiveURLChangedSinceFetchFavicon(); | |
109 } | |
110 | |
111 bool WebFaviconDriver::ActiveURLChangedSinceFetchFavicon() { | |
112 // On iOS the active URL can change in between calls to FetchFavicon(). For | |
113 // instance, FetchFavicon() is not synchronously called when the active URL | |
114 // changes as a result of CRWSessionController::goToEntry(). | |
115 // TODO(stuartmorgan): Remove this once iOS always triggers favicon fetches | |
116 // synchronously after active URL changes. | |
117 return UrlWithoutFragment(GetActiveURL()) != | |
118 UrlWithoutFragment(fetch_favicon_url_); | |
119 } | |
120 | |
90 web::FaviconStatus& WebFaviconDriver::GetFaviconStatus() { | 121 web::FaviconStatus& WebFaviconDriver::GetFaviconStatus() { |
91 DCHECK(web_state()->GetNavigationManager()->GetVisibleItem()); | 122 DCHECK(!ActiveURLChangedSinceFetchFavicon()); |
92 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon(); | 123 return web_state()->GetNavigationManager()->GetVisibleItem()->GetFavicon(); |
93 } | 124 } |
94 | 125 |
95 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, | 126 WebFaviconDriver::WebFaviconDriver(web::WebState* web_state, |
96 FaviconService* favicon_service, | 127 FaviconService* favicon_service, |
97 history::HistoryService* history_service, | 128 history::HistoryService* history_service, |
98 bookmarks::BookmarkModel* bookmark_model) | 129 bookmarks::BookmarkModel* bookmark_model) |
99 : web::WebStateObserver(web_state), | 130 : web::WebStateObserver(web_state), |
100 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { | 131 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { |
101 } | 132 } |
102 | 133 |
103 WebFaviconDriver::~WebFaviconDriver() { | 134 WebFaviconDriver::~WebFaviconDriver() { |
104 } | 135 } |
105 | 136 |
106 void WebFaviconDriver::FaviconUrlUpdated( | 137 void WebFaviconDriver::FaviconUrlUpdated( |
107 const std::vector<web::FaviconURL>& candidates) { | 138 const std::vector<web::FaviconURL>& candidates) { |
139 if (fetch_favicon_url_.is_empty() || ActiveURLChangedSinceFetchFavicon()) | |
140 return; | |
141 | |
108 DCHECK(!candidates.empty()); | 142 DCHECK(!candidates.empty()); |
109 OnUpdateFaviconURL(FaviconURLsFromWebFaviconURLs(candidates)); | 143 OnUpdateFaviconURL(FaviconURLsFromWebFaviconURLs(candidates)); |
110 } | 144 } |
111 | 145 |
112 } // namespace favicon | 146 } // namespace favicon |
OLD | NEW |