OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/favicon/content/content_favicon_driver.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/favicon/content/favicon_url_util.h" |
| 9 #include "components/favicon/core/favicon_url.h" |
| 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/favicon_status.h" |
| 12 #include "content/public/browser/navigation_controller.h" |
| 13 #include "content/public/browser/navigation_details.h" |
| 14 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/common/favicon_url.h" |
| 16 #include "ui/gfx/image/image.h" |
| 17 |
| 18 DEFINE_WEB_CONTENTS_USER_DATA_KEY(favicon::ContentFaviconDriver); |
| 19 |
| 20 namespace favicon { |
| 21 |
| 22 // static |
| 23 void ContentFaviconDriver::CreateForWebContents( |
| 24 content::WebContents* web_contents, |
| 25 FaviconService* favicon_service, |
| 26 history::HistoryService* history_service, |
| 27 bookmarks::BookmarkModel* bookmark_model) { |
| 28 if (FromWebContents(web_contents)) |
| 29 return; |
| 30 |
| 31 web_contents->SetUserData( |
| 32 UserDataKey(), new ContentFaviconDriver(web_contents, favicon_service, |
| 33 history_service, bookmark_model)); |
| 34 } |
| 35 |
| 36 gfx::Image ContentFaviconDriver::GetFavicon() const { |
| 37 // Like GetTitle(), we also want to use the favicon for the last committed |
| 38 // entry rather than a pending navigation entry. |
| 39 const content::NavigationController& controller = |
| 40 web_contents()->GetController(); |
| 41 content::NavigationEntry* entry = controller.GetTransientEntry(); |
| 42 if (entry) |
| 43 return entry->GetFavicon().image; |
| 44 |
| 45 entry = controller.GetLastCommittedEntry(); |
| 46 if (entry) |
| 47 return entry->GetFavicon().image; |
| 48 return gfx::Image(); |
| 49 } |
| 50 |
| 51 bool ContentFaviconDriver::FaviconIsValid() const { |
| 52 const content::NavigationController& controller = |
| 53 web_contents()->GetController(); |
| 54 content::NavigationEntry* entry = controller.GetTransientEntry(); |
| 55 if (entry) |
| 56 return entry->GetFavicon().valid; |
| 57 |
| 58 entry = controller.GetLastCommittedEntry(); |
| 59 if (entry) |
| 60 return entry->GetFavicon().valid; |
| 61 |
| 62 return false; |
| 63 } |
| 64 |
| 65 int ContentFaviconDriver::StartDownload(const GURL& url, int max_image_size) { |
| 66 if (WasUnableToDownloadFavicon(url)) { |
| 67 DVLOG(1) << "Skip Failed FavIcon: " << url; |
| 68 return 0; |
| 69 } |
| 70 |
| 71 bool bypass_cache = (bypass_cache_page_url_ == GetActiveURL()); |
| 72 bypass_cache_page_url_ = GURL(); |
| 73 |
| 74 return web_contents()->DownloadImage( |
| 75 url, true, max_image_size, bypass_cache, |
| 76 base::Bind(&FaviconDriverImpl::DidDownloadFavicon, |
| 77 base::Unretained(this))); |
| 78 } |
| 79 |
| 80 bool ContentFaviconDriver::IsOffTheRecord() { |
| 81 DCHECK(web_contents()); |
| 82 return web_contents()->GetBrowserContext()->IsOffTheRecord(); |
| 83 } |
| 84 |
| 85 GURL ContentFaviconDriver::GetActiveURL() { |
| 86 content::NavigationEntry* entry = |
| 87 web_contents()->GetController().GetActiveEntry(); |
| 88 return entry ? entry->GetURL() : GURL(); |
| 89 } |
| 90 |
| 91 base::string16 ContentFaviconDriver::GetActiveTitle() { |
| 92 content::NavigationEntry* entry = |
| 93 web_contents()->GetController().GetActiveEntry(); |
| 94 return entry ? entry->GetTitle() : base::string16(); |
| 95 } |
| 96 |
| 97 bool ContentFaviconDriver::GetActiveFaviconValidity() { |
| 98 return GetFaviconStatus().valid; |
| 99 } |
| 100 |
| 101 void ContentFaviconDriver::SetActiveFaviconValidity(bool valid) { |
| 102 GetFaviconStatus().valid = valid; |
| 103 } |
| 104 |
| 105 GURL ContentFaviconDriver::GetActiveFaviconURL() { |
| 106 return GetFaviconStatus().url; |
| 107 } |
| 108 |
| 109 void ContentFaviconDriver::SetActiveFaviconURL(const GURL& url) { |
| 110 GetFaviconStatus().url = url; |
| 111 } |
| 112 |
| 113 gfx::Image ContentFaviconDriver::GetActiveFaviconImage() { |
| 114 return GetFaviconStatus().image; |
| 115 } |
| 116 |
| 117 void ContentFaviconDriver::SetActiveFaviconImage(const gfx::Image& image) { |
| 118 GetFaviconStatus().image = image; |
| 119 } |
| 120 |
| 121 content::FaviconStatus& ContentFaviconDriver::GetFaviconStatus() { |
| 122 DCHECK(web_contents()->GetController().GetActiveEntry()); |
| 123 return web_contents()->GetController().GetActiveEntry()->GetFavicon(); |
| 124 } |
| 125 |
| 126 ContentFaviconDriver::ContentFaviconDriver( |
| 127 content::WebContents* web_contents, |
| 128 FaviconService* favicon_service, |
| 129 history::HistoryService* history_service, |
| 130 bookmarks::BookmarkModel* bookmark_model) |
| 131 : content::WebContentsObserver(web_contents), |
| 132 FaviconDriverImpl(favicon_service, history_service, bookmark_model) { |
| 133 } |
| 134 |
| 135 ContentFaviconDriver::~ContentFaviconDriver() { |
| 136 } |
| 137 |
| 138 void ContentFaviconDriver::NotifyFaviconUpdated(bool icon_url_changed) { |
| 139 FaviconDriverImpl::NotifyFaviconUpdated(icon_url_changed); |
| 140 web_contents()->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
| 141 } |
| 142 |
| 143 void ContentFaviconDriver::DidUpdateFaviconURL( |
| 144 const std::vector<content::FaviconURL>& candidates) { |
| 145 DCHECK(!candidates.empty()); |
| 146 favicon_urls_ = candidates; |
| 147 OnUpdateFaviconURL(FaviconURLsFromContentFaviconURLs(candidates)); |
| 148 } |
| 149 |
| 150 void ContentFaviconDriver::DidStartNavigationToPendingEntry( |
| 151 const GURL& url, |
| 152 content::NavigationController::ReloadType reload_type) { |
| 153 if (reload_type == content::NavigationController::NO_RELOAD || |
| 154 IsOffTheRecord()) |
| 155 return; |
| 156 |
| 157 bypass_cache_page_url_ = url; |
| 158 SetFaviconOutOfDateForPage( |
| 159 url, reload_type == content::NavigationController::RELOAD_IGNORING_CACHE); |
| 160 } |
| 161 |
| 162 void ContentFaviconDriver::DidNavigateMainFrame( |
| 163 const content::LoadCommittedDetails& details, |
| 164 const content::FrameNavigateParams& params) { |
| 165 favicon_urls_.clear(); |
| 166 |
| 167 // Wait till the user navigates to a new URL to start checking the cache |
| 168 // again. The cache may be ignored for non-reload navigations (e.g. |
| 169 // history.replace() in-page navigation). This is allowed to increase the |
| 170 // likelihood that "reloading a page ignoring the cache" redownloads the |
| 171 // favicon. In particular, a page may do an in-page navigation before |
| 172 // FaviconHandler has the time to determine that the favicon needs to be |
| 173 // redownloaded. |
| 174 GURL url = details.entry->GetURL(); |
| 175 if (url != bypass_cache_page_url_) |
| 176 bypass_cache_page_url_ = GURL(); |
| 177 |
| 178 // Get the favicon, either from history or request it from the net. |
| 179 FetchFavicon(url); |
| 180 } |
| 181 |
| 182 } // namespace favicon |
OLD | NEW |