| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_FAVICON_TAB_HELPER_H__ | |
| 6 #define CHROME_BROWSER_FAVICON_TAB_HELPER_H__ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/callback.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "chrome/browser/favicon_service.h" | |
| 15 #include "content/browser/cancelable_request.h" | |
| 16 #include "content/browser/tab_contents/tab_contents_observer.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 | |
| 19 class NavigationEntry; | |
| 20 class RefCountedMemory; | |
| 21 class SkBitmap; | |
| 22 class TabContents; | |
| 23 | |
| 24 // FaviconTabHelper is used to fetch the favicon for TabContents. | |
| 25 // | |
| 26 // FetchFavicon requests the favicon from the favicon service which in turn | |
| 27 // requests the favicon from the history database. At this point | |
| 28 // we only know the URL of the page, and not necessarily the url of the | |
| 29 // favicon. To ensure we handle reloading stale favicons as well as | |
| 30 // reloading a favicon on page reload we always request the favicon from | |
| 31 // history regardless of whether the NavigationEntry has a favicon. | |
| 32 // | |
| 33 // After the navigation two types of events are delivered (which is | |
| 34 // first depends upon who is faster): notification from the history | |
| 35 // db on our request for the favicon (OnFaviconDataForInitialURL), | |
| 36 // or a message from the renderer giving us the URL of the favicon for | |
| 37 // the page (SetFaviconURL). | |
| 38 // . If the history db has a valid up to date favicon for the page, we update | |
| 39 // the NavigationEntry and use the favicon. | |
| 40 // . When we receive the favicon url if it matches that of the NavigationEntry | |
| 41 // and the NavigationEntry's favicon is set, we do nothing (everything is | |
| 42 // ok). | |
| 43 // . On the other hand if the database does not know the favicon for url, or | |
| 44 // the favicon is out date, or the URL from the renderer does not match that | |
| 45 // NavigationEntry we proceed to DownloadFaviconOrAskHistory. Before we | |
| 46 // invoke DownloadFaviconOrAskHistory we wait until we've received both | |
| 47 // the favicon url and the callback from history. We wait to ensure we | |
| 48 // truly know both the favicon url and the state of the database. | |
| 49 // | |
| 50 // DownloadFaviconOrAskHistory does the following: | |
| 51 // . If we have a valid favicon, but it is expired we ask the renderer to | |
| 52 // download the favicon. | |
| 53 // . Otherwise we ask the history database to update the mapping from | |
| 54 // page url to favicon url and call us back with the favicon. Remember, it is | |
| 55 // possible for the db to already have the favicon, just not the mapping | |
| 56 // between page to favicon url. The callback for this is OnFaviconData. | |
| 57 // | |
| 58 // OnFaviconData either updates the favicon of the NavigationEntry (if the | |
| 59 // db knew about the favicon), or requests the renderer to download the | |
| 60 // favicon. | |
| 61 // | |
| 62 // When the renderer downloads the favicon SetFaviconImageData is invoked, | |
| 63 // at which point we update the favicon of the NavigationEntry and notify | |
| 64 // the database to save the favicon. | |
| 65 | |
| 66 class FaviconTabHelper : public TabContentsObserver { | |
| 67 public: | |
| 68 explicit FaviconTabHelper(TabContents* tab_contents); | |
| 69 virtual ~FaviconTabHelper(); | |
| 70 | |
| 71 // Returns the favicon for this tab, or an isNull() bitmap if the tab does not | |
| 72 // have a favicon. The default implementation uses the current navigation | |
| 73 // entry. | |
| 74 SkBitmap GetFavicon() const; | |
| 75 | |
| 76 // Returns true if we are not using the default favicon. | |
| 77 bool FaviconIsValid() const; | |
| 78 | |
| 79 // Returns whether the favicon should be displayed. If this returns false, no | |
| 80 // space is provided for the favicon, and the favicon is never displayed. | |
| 81 virtual bool ShouldDisplayFavicon(); | |
| 82 | |
| 83 // Saves the favicon for the current page. | |
| 84 void SaveFavicon(); | |
| 85 | |
| 86 // Initiates loading an image from given |image_url|. Returns a download id | |
| 87 // for caller to track the request. When download completes, |callback| is | |
| 88 // called with the three params: the download_id, a boolean flag to indicate | |
| 89 // whether the download succeeds and a SkBitmap as the downloaded image. | |
| 90 // Note that |image_size| is a hint for images with multiple sizes. The | |
| 91 // downloaded image is not resized to the given image_size. If 0 is passed, | |
| 92 // the first frame of the image is returned. | |
| 93 typedef Callback3<int, bool, const SkBitmap&>::Type ImageDownloadCallback; | |
| 94 int DownloadImage(const GURL& image_url, int image_size, | |
| 95 ImageDownloadCallback* callback); | |
| 96 | |
| 97 // Message Handler. Must be public, because also called from | |
| 98 // PrerenderContents. | |
| 99 void OnUpdateFaviconURL(int32 page_id, const GURL& icon_url); | |
| 100 | |
| 101 private: | |
| 102 struct DownloadRequest { | |
| 103 DownloadRequest() : callback(NULL) {} | |
| 104 DownloadRequest(const GURL& url, | |
| 105 const GURL& image_url, | |
| 106 ImageDownloadCallback* callback) | |
| 107 : url(url), | |
| 108 image_url(image_url), | |
| 109 callback(callback) {} | |
| 110 | |
| 111 GURL url; | |
| 112 GURL image_url; | |
| 113 ImageDownloadCallback* callback; | |
| 114 }; | |
| 115 | |
| 116 // TabContentsObserver overrides. | |
| 117 virtual void NavigateToPendingEntry( | |
| 118 const GURL& url, | |
| 119 NavigationController::ReloadType reload_type) OVERRIDE; | |
| 120 virtual void DidNavigateMainFramePostCommit( | |
| 121 const NavigationController::LoadCommittedDetails& details, | |
| 122 const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE; | |
| 123 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 124 | |
| 125 void OnDidDownloadFavicon(int id, | |
| 126 const GURL& image_url, | |
| 127 bool errored, | |
| 128 const SkBitmap& image); | |
| 129 | |
| 130 // Initiates loading the favicon for the specified url. | |
| 131 void FetchFavicon(const GURL& url); | |
| 132 | |
| 133 // Return the NavigationEntry for the active entry, or NULL if the active | |
| 134 // entries URL does not match that of the URL last passed to FetchFavicon. | |
| 135 NavigationEntry* GetEntry(); | |
| 136 | |
| 137 FaviconService* GetFaviconService(); | |
| 138 | |
| 139 // See description above class for details. | |
| 140 void OnFaviconDataForInitialURL(FaviconService::Handle handle, | |
| 141 history::FaviconData favicon); | |
| 142 | |
| 143 // If the favicon has expired, asks the renderer to download the favicon. | |
| 144 // Otherwise asks history to update the mapping between page url and icon | |
| 145 // url with a callback to OnFaviconData when done. | |
| 146 void DownloadFaviconOrAskHistory(NavigationEntry* entry); | |
| 147 | |
| 148 // See description above class for details. | |
| 149 void OnFaviconData(FaviconService::Handle handle, | |
| 150 history::FaviconData favicon); | |
| 151 | |
| 152 // Schedules a download for the specified entry. This adds the request to | |
| 153 // download_requests_. | |
| 154 int ScheduleDownload(const GURL& url, const GURL& image_url, int image_size, | |
| 155 ImageDownloadCallback* callback); | |
| 156 | |
| 157 // Sets the image data for the favicon. This is invoked asynchronously after | |
| 158 // we request the TabContents to download the favicon. | |
| 159 void SetFavicon(const GURL& url, const GURL& icon_url, const SkBitmap& image); | |
| 160 | |
| 161 // Converts the image data to an SkBitmap and sets it on the NavigationEntry. | |
| 162 // If the TabContents has a delegate, it is notified of the new favicon | |
| 163 // (INVALIDATE_FAVICON). | |
| 164 void UpdateFavicon(NavigationEntry* entry, | |
| 165 scoped_refptr<RefCountedMemory> data); | |
| 166 void UpdateFavicon(NavigationEntry* entry, const SkBitmap& image); | |
| 167 | |
| 168 // Scales the image such that either the width and/or height is 16 pixels | |
| 169 // wide. Does nothing if the image is empty. | |
| 170 SkBitmap ConvertToFaviconSize(const SkBitmap& image); | |
| 171 | |
| 172 // Returns true if the favicon should be saved. | |
| 173 bool ShouldSaveFavicon(const GURL& url); | |
| 174 | |
| 175 // Used for history requests. | |
| 176 CancelableRequestConsumer cancelable_consumer_; | |
| 177 | |
| 178 // URL of the page we're requesting the favicon for. | |
| 179 GURL url_; | |
| 180 | |
| 181 // Whether we got the url for the page back from the renderer. | |
| 182 // See "Favicon Details" in tab_contents.cc for more details. | |
| 183 bool got_favicon_url_; | |
| 184 | |
| 185 // Whether we got the initial response for the favicon back from the renderer. | |
| 186 // See "Favicon Details" in tab_contents.cc for more details. | |
| 187 bool got_favicon_from_history_; | |
| 188 | |
| 189 // Whether the favicon is out of date. If true, it means history knows about | |
| 190 // the favicon, but we need to download the favicon because the icon has | |
| 191 // expired. | |
| 192 // See "Favicon Details" in tab_contents.cc for more details. | |
| 193 bool favicon_expired_; | |
| 194 | |
| 195 // Requests to the renderer to download favicons. | |
| 196 typedef std::map<int, DownloadRequest> DownloadRequests; | |
| 197 DownloadRequests download_requests_; | |
| 198 | |
| 199 DISALLOW_COPY_AND_ASSIGN(FaviconTabHelper); | |
| 200 }; | |
| 201 | |
| 202 #endif // CHROME_BROWSER_FAVICON_TAB_HELPER_H__ | |
| OLD | NEW |