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