Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef AsyncFaviconHelpers_h_ |
michael@0 | 8 | #define AsyncFaviconHelpers_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "nsIFaviconService.h" |
michael@0 | 11 | #include "nsIChannelEventSink.h" |
michael@0 | 12 | #include "nsIInterfaceRequestor.h" |
michael@0 | 13 | #include "nsIStreamListener.h" |
michael@0 | 14 | #include "nsThreadUtils.h" |
michael@0 | 15 | |
michael@0 | 16 | #include "Database.h" |
michael@0 | 17 | #include "mozilla/storage.h" |
michael@0 | 18 | |
michael@0 | 19 | #define ICON_STATUS_UNKNOWN 0 |
michael@0 | 20 | #define ICON_STATUS_CHANGED 1 << 0 |
michael@0 | 21 | #define ICON_STATUS_SAVED 1 << 1 |
michael@0 | 22 | #define ICON_STATUS_ASSOCIATED 1 << 2 |
michael@0 | 23 | #define ICON_STATUS_CACHED 1 << 3 |
michael@0 | 24 | |
michael@0 | 25 | #define TO_CHARBUFFER(_buffer) \ |
michael@0 | 26 | reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer)) |
michael@0 | 27 | #define TO_INTBUFFER(_string) \ |
michael@0 | 28 | reinterpret_cast<uint8_t*>(const_cast<char*>(_string.get())) |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * The maximum time we will keep a favicon around. We always ask the cache, if |
michael@0 | 32 | * we can, but default to this value if we do not get a time back, or the time |
michael@0 | 33 | * is more in the future than this. |
michael@0 | 34 | * Currently set to one week from now. |
michael@0 | 35 | */ |
michael@0 | 36 | #define MAX_FAVICON_EXPIRATION ((PRTime)7 * 24 * 60 * 60 * PR_USEC_PER_SEC) |
michael@0 | 37 | |
michael@0 | 38 | namespace mozilla { |
michael@0 | 39 | namespace places { |
michael@0 | 40 | |
michael@0 | 41 | /** |
michael@0 | 42 | * Indicates when a icon should be fetched from network. |
michael@0 | 43 | */ |
michael@0 | 44 | enum AsyncFaviconFetchMode { |
michael@0 | 45 | FETCH_NEVER = 0 |
michael@0 | 46 | , FETCH_IF_MISSING |
michael@0 | 47 | , FETCH_ALWAYS |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Data cache for a icon entry. |
michael@0 | 52 | */ |
michael@0 | 53 | struct IconData |
michael@0 | 54 | { |
michael@0 | 55 | IconData() |
michael@0 | 56 | : id(0) |
michael@0 | 57 | , expiration(0) |
michael@0 | 58 | , fetchMode(FETCH_NEVER) |
michael@0 | 59 | , status(ICON_STATUS_UNKNOWN) |
michael@0 | 60 | { |
michael@0 | 61 | guid.SetIsVoid(true); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | int64_t id; |
michael@0 | 65 | nsCString spec; |
michael@0 | 66 | nsCString data; |
michael@0 | 67 | nsCString mimeType; |
michael@0 | 68 | PRTime expiration; |
michael@0 | 69 | enum AsyncFaviconFetchMode fetchMode; |
michael@0 | 70 | uint16_t status; // This is a bitset, see ICON_STATUS_* defines above. |
michael@0 | 71 | nsCString guid; |
michael@0 | 72 | }; |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Data cache for a page entry. |
michael@0 | 76 | */ |
michael@0 | 77 | struct PageData |
michael@0 | 78 | { |
michael@0 | 79 | PageData() |
michael@0 | 80 | : id(0) |
michael@0 | 81 | , canAddToHistory(true) |
michael@0 | 82 | , iconId(0) |
michael@0 | 83 | { |
michael@0 | 84 | guid.SetIsVoid(true); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | int64_t id; |
michael@0 | 88 | nsCString spec; |
michael@0 | 89 | nsCString bookmarkedSpec; |
michael@0 | 90 | nsString revHost; |
michael@0 | 91 | bool canAddToHistory; // False for disabled history and unsupported schemas. |
michael@0 | 92 | int64_t iconId; |
michael@0 | 93 | nsCString guid; |
michael@0 | 94 | }; |
michael@0 | 95 | |
michael@0 | 96 | /** |
michael@0 | 97 | * Base class for events declared in this file. This class's main purpose is |
michael@0 | 98 | * to declare a destructor which releases mCallback on the main thread. |
michael@0 | 99 | */ |
michael@0 | 100 | class AsyncFaviconHelperBase : public nsRunnable |
michael@0 | 101 | { |
michael@0 | 102 | protected: |
michael@0 | 103 | AsyncFaviconHelperBase(nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 104 | |
michael@0 | 105 | virtual ~AsyncFaviconHelperBase(); |
michael@0 | 106 | |
michael@0 | 107 | nsRefPtr<Database> mDB; |
michael@0 | 108 | // Strong reference since we are responsible for its existence. |
michael@0 | 109 | nsCOMPtr<nsIFaviconDataCallback> mCallback; |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Async fetches icon from database or network, associates it with the required |
michael@0 | 114 | * page and finally notifies the change. |
michael@0 | 115 | */ |
michael@0 | 116 | class AsyncFetchAndSetIconForPage : public AsyncFaviconHelperBase |
michael@0 | 117 | { |
michael@0 | 118 | public: |
michael@0 | 119 | NS_DECL_NSIRUNNABLE |
michael@0 | 120 | |
michael@0 | 121 | /** |
michael@0 | 122 | * Creates the event and dispatches it to the async thread. |
michael@0 | 123 | * |
michael@0 | 124 | * @param aFaviconURI |
michael@0 | 125 | * URI of the icon to be fetched and associated. |
michael@0 | 126 | * @param aPageURI |
michael@0 | 127 | * URI of the page to which associate the icon. |
michael@0 | 128 | * @param aFetchMode |
michael@0 | 129 | * Specifies whether a icon should be fetched from network if not found |
michael@0 | 130 | * in the database. |
michael@0 | 131 | * @param aCallback |
michael@0 | 132 | * Function to be called when the fetch-and-associate process finishes. |
michael@0 | 133 | */ |
michael@0 | 134 | static nsresult start(nsIURI* aFaviconURI, |
michael@0 | 135 | nsIURI* aPageURI, |
michael@0 | 136 | enum AsyncFaviconFetchMode aFetchMode, |
michael@0 | 137 | uint32_t aFaviconLoadType, |
michael@0 | 138 | nsIFaviconDataCallback* aCallback); |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * Constructor. |
michael@0 | 142 | * |
michael@0 | 143 | * @param aIcon |
michael@0 | 144 | * Icon to be fetched and associated. |
michael@0 | 145 | * @param aPage |
michael@0 | 146 | * Page to which associate the icon. |
michael@0 | 147 | * @param aCallback |
michael@0 | 148 | * Function to be called when the fetch-and-associate process finishes. |
michael@0 | 149 | */ |
michael@0 | 150 | AsyncFetchAndSetIconForPage(IconData& aIcon, |
michael@0 | 151 | PageData& aPage, |
michael@0 | 152 | uint32_t aFaviconLoadType, |
michael@0 | 153 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 154 | |
michael@0 | 155 | virtual ~AsyncFetchAndSetIconForPage(); |
michael@0 | 156 | |
michael@0 | 157 | protected: |
michael@0 | 158 | IconData mIcon; |
michael@0 | 159 | PageData mPage; |
michael@0 | 160 | const bool mFaviconLoadPrivate; |
michael@0 | 161 | }; |
michael@0 | 162 | |
michael@0 | 163 | /** |
michael@0 | 164 | * If needed will asynchronously fetch the icon from the network. It will |
michael@0 | 165 | * finally dispatch an event to the async thread to associate the icon with |
michael@0 | 166 | * the required page. |
michael@0 | 167 | */ |
michael@0 | 168 | class AsyncFetchAndSetIconFromNetwork : public AsyncFaviconHelperBase |
michael@0 | 169 | , public nsIStreamListener |
michael@0 | 170 | , public nsIInterfaceRequestor |
michael@0 | 171 | , public nsIChannelEventSink |
michael@0 | 172 | { |
michael@0 | 173 | public: |
michael@0 | 174 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 175 | NS_DECL_NSIINTERFACEREQUESTOR |
michael@0 | 176 | NS_DECL_NSICHANNELEVENTSINK |
michael@0 | 177 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 178 | NS_DECL_NSIRUNNABLE |
michael@0 | 179 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 180 | |
michael@0 | 181 | /** |
michael@0 | 182 | * Constructor. |
michael@0 | 183 | * |
michael@0 | 184 | * @param aIcon |
michael@0 | 185 | * Icon to be fetched and associated. |
michael@0 | 186 | * @param aPage |
michael@0 | 187 | * Page to which associate the icon. |
michael@0 | 188 | * @param aCallback |
michael@0 | 189 | * Function to be called when the fetch-and-associate process finishes. |
michael@0 | 190 | */ |
michael@0 | 191 | AsyncFetchAndSetIconFromNetwork(IconData& aIcon, |
michael@0 | 192 | PageData& aPage, |
michael@0 | 193 | bool aFaviconLoadPrivate, |
michael@0 | 194 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 195 | |
michael@0 | 196 | virtual ~AsyncFetchAndSetIconFromNetwork(); |
michael@0 | 197 | |
michael@0 | 198 | protected: |
michael@0 | 199 | IconData mIcon; |
michael@0 | 200 | PageData mPage; |
michael@0 | 201 | const bool mFaviconLoadPrivate; |
michael@0 | 202 | }; |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * Associates the icon to the required page, finally dispatches an event to the |
michael@0 | 206 | * main thread to notify the change to observers. |
michael@0 | 207 | */ |
michael@0 | 208 | class AsyncAssociateIconToPage : public AsyncFaviconHelperBase |
michael@0 | 209 | { |
michael@0 | 210 | public: |
michael@0 | 211 | NS_DECL_NSIRUNNABLE |
michael@0 | 212 | |
michael@0 | 213 | /** |
michael@0 | 214 | * Constructor. |
michael@0 | 215 | * |
michael@0 | 216 | * @param aIcon |
michael@0 | 217 | * Icon to be associated. |
michael@0 | 218 | * @param aPage |
michael@0 | 219 | * Page to which associate the icon. |
michael@0 | 220 | * @param aCallback |
michael@0 | 221 | * Function to be called when the associate process finishes. |
michael@0 | 222 | */ |
michael@0 | 223 | AsyncAssociateIconToPage(IconData& aIcon, |
michael@0 | 224 | PageData& aPage, |
michael@0 | 225 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 226 | |
michael@0 | 227 | virtual ~AsyncAssociateIconToPage(); |
michael@0 | 228 | |
michael@0 | 229 | protected: |
michael@0 | 230 | IconData mIcon; |
michael@0 | 231 | PageData mPage; |
michael@0 | 232 | }; |
michael@0 | 233 | |
michael@0 | 234 | /** |
michael@0 | 235 | * Asynchronously tries to get the URL of a page's favicon, then notifies the |
michael@0 | 236 | * given observer. |
michael@0 | 237 | */ |
michael@0 | 238 | class AsyncGetFaviconURLForPage : public AsyncFaviconHelperBase |
michael@0 | 239 | { |
michael@0 | 240 | public: |
michael@0 | 241 | NS_DECL_NSIRUNNABLE |
michael@0 | 242 | |
michael@0 | 243 | /** |
michael@0 | 244 | * Creates the event and dispatches it to the I/O thread. |
michael@0 | 245 | * |
michael@0 | 246 | * @param aPageURI |
michael@0 | 247 | * URL of the page whose favicon's URL we're fetching |
michael@0 | 248 | * @param aCallback |
michael@0 | 249 | * function to be called once finished |
michael@0 | 250 | */ |
michael@0 | 251 | static nsresult start(nsIURI* aPageURI, |
michael@0 | 252 | nsIFaviconDataCallback* aCallback); |
michael@0 | 253 | |
michael@0 | 254 | /** |
michael@0 | 255 | * Constructor. |
michael@0 | 256 | * |
michael@0 | 257 | * @param aPageSpec |
michael@0 | 258 | * URL of the page whose favicon's URL we're fetching |
michael@0 | 259 | * @param aCallback |
michael@0 | 260 | * function to be called once finished |
michael@0 | 261 | */ |
michael@0 | 262 | AsyncGetFaviconURLForPage(const nsACString& aPageSpec, |
michael@0 | 263 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 264 | |
michael@0 | 265 | virtual ~AsyncGetFaviconURLForPage(); |
michael@0 | 266 | |
michael@0 | 267 | private: |
michael@0 | 268 | nsCString mPageSpec; |
michael@0 | 269 | }; |
michael@0 | 270 | |
michael@0 | 271 | |
michael@0 | 272 | /** |
michael@0 | 273 | * Asynchronously tries to get the URL and data of a page's favicon, then |
michael@0 | 274 | * notifies the given observer. |
michael@0 | 275 | */ |
michael@0 | 276 | class AsyncGetFaviconDataForPage : public AsyncFaviconHelperBase |
michael@0 | 277 | { |
michael@0 | 278 | public: |
michael@0 | 279 | NS_DECL_NSIRUNNABLE |
michael@0 | 280 | |
michael@0 | 281 | /** |
michael@0 | 282 | * Creates the event and dispatches it to the I/O thread. |
michael@0 | 283 | * |
michael@0 | 284 | * @param aPageURI |
michael@0 | 285 | * URL of the page whose favicon URL and data we're fetching |
michael@0 | 286 | * @param aCallback |
michael@0 | 287 | * function to be called once finished |
michael@0 | 288 | */ |
michael@0 | 289 | static nsresult start(nsIURI* aPageURI, |
michael@0 | 290 | nsIFaviconDataCallback* aCallback); |
michael@0 | 291 | |
michael@0 | 292 | /** |
michael@0 | 293 | * Constructor. |
michael@0 | 294 | * |
michael@0 | 295 | * @param aPageSpec |
michael@0 | 296 | * URL of the page whose favicon URL and data we're fetching |
michael@0 | 297 | * @param aCallback |
michael@0 | 298 | * function to be called once finished |
michael@0 | 299 | */ |
michael@0 | 300 | AsyncGetFaviconDataForPage(const nsACString& aPageSpec, |
michael@0 | 301 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 302 | |
michael@0 | 303 | virtual ~AsyncGetFaviconDataForPage(); |
michael@0 | 304 | |
michael@0 | 305 | private: |
michael@0 | 306 | nsCString mPageSpec; |
michael@0 | 307 | }; |
michael@0 | 308 | |
michael@0 | 309 | class AsyncReplaceFaviconData : public AsyncFaviconHelperBase |
michael@0 | 310 | { |
michael@0 | 311 | public: |
michael@0 | 312 | NS_DECL_NSIRUNNABLE |
michael@0 | 313 | |
michael@0 | 314 | static nsresult start(IconData *aIcon); |
michael@0 | 315 | |
michael@0 | 316 | AsyncReplaceFaviconData(IconData &aIcon, |
michael@0 | 317 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 318 | |
michael@0 | 319 | virtual ~AsyncReplaceFaviconData(); |
michael@0 | 320 | |
michael@0 | 321 | protected: |
michael@0 | 322 | IconData mIcon; |
michael@0 | 323 | }; |
michael@0 | 324 | |
michael@0 | 325 | class RemoveIconDataCacheEntry : public AsyncFaviconHelperBase |
michael@0 | 326 | { |
michael@0 | 327 | public: |
michael@0 | 328 | NS_DECL_NSIRUNNABLE |
michael@0 | 329 | |
michael@0 | 330 | RemoveIconDataCacheEntry(IconData &aIcon, |
michael@0 | 331 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 332 | virtual ~RemoveIconDataCacheEntry(); |
michael@0 | 333 | |
michael@0 | 334 | protected: |
michael@0 | 335 | IconData mIcon; |
michael@0 | 336 | }; |
michael@0 | 337 | |
michael@0 | 338 | /** |
michael@0 | 339 | * Notifies the icon change to favicon observers. |
michael@0 | 340 | */ |
michael@0 | 341 | class NotifyIconObservers : public AsyncFaviconHelperBase |
michael@0 | 342 | { |
michael@0 | 343 | public: |
michael@0 | 344 | NS_DECL_NSIRUNNABLE |
michael@0 | 345 | |
michael@0 | 346 | /** |
michael@0 | 347 | * Constructor. |
michael@0 | 348 | * |
michael@0 | 349 | * @param aIcon |
michael@0 | 350 | * Icon information. Can be empty if no icon is associated to the page. |
michael@0 | 351 | * @param aPage |
michael@0 | 352 | * Page to which the icon information applies. |
michael@0 | 353 | * @param aCallback |
michael@0 | 354 | * Function to be notified in all cases. |
michael@0 | 355 | */ |
michael@0 | 356 | NotifyIconObservers(IconData& aIcon, |
michael@0 | 357 | PageData& aPage, |
michael@0 | 358 | nsCOMPtr<nsIFaviconDataCallback>& aCallback); |
michael@0 | 359 | virtual ~NotifyIconObservers(); |
michael@0 | 360 | |
michael@0 | 361 | protected: |
michael@0 | 362 | IconData mIcon; |
michael@0 | 363 | PageData mPage; |
michael@0 | 364 | |
michael@0 | 365 | void SendGlobalNotifications(nsIURI* aIconURI); |
michael@0 | 366 | }; |
michael@0 | 367 | |
michael@0 | 368 | } // namespace places |
michael@0 | 369 | } // namespace mozilla |
michael@0 | 370 | |
michael@0 | 371 | #endif // AsyncFaviconHelpers_h_ |