toolkit/components/places/AsyncFaviconHelpers.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/AsyncFaviconHelpers.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,371 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef AsyncFaviconHelpers_h_
    1.11 +#define AsyncFaviconHelpers_h_
    1.12 +
    1.13 +#include "nsIFaviconService.h"
    1.14 +#include "nsIChannelEventSink.h"
    1.15 +#include "nsIInterfaceRequestor.h"
    1.16 +#include "nsIStreamListener.h"
    1.17 +#include "nsThreadUtils.h"
    1.18 +
    1.19 +#include "Database.h"
    1.20 +#include "mozilla/storage.h"
    1.21 +
    1.22 +#define ICON_STATUS_UNKNOWN 0
    1.23 +#define ICON_STATUS_CHANGED 1 << 0
    1.24 +#define ICON_STATUS_SAVED 1 << 1
    1.25 +#define ICON_STATUS_ASSOCIATED 1 << 2
    1.26 +#define ICON_STATUS_CACHED 1 << 3
    1.27 +
    1.28 +#define TO_CHARBUFFER(_buffer) \
    1.29 +  reinterpret_cast<char*>(const_cast<uint8_t*>(_buffer))
    1.30 +#define TO_INTBUFFER(_string) \
    1.31 +  reinterpret_cast<uint8_t*>(const_cast<char*>(_string.get()))
    1.32 +
    1.33 +/**
    1.34 + * The maximum time we will keep a favicon around.  We always ask the cache, if
    1.35 + * we can, but default to this value if we do not get a time back, or the time
    1.36 + * is more in the future than this.
    1.37 + * Currently set to one week from now.
    1.38 + */
    1.39 +#define MAX_FAVICON_EXPIRATION ((PRTime)7 * 24 * 60 * 60 * PR_USEC_PER_SEC)
    1.40 +
    1.41 +namespace mozilla {
    1.42 +namespace places {
    1.43 +
    1.44 +/**
    1.45 + * Indicates when a icon should be fetched from network.
    1.46 + */
    1.47 +enum AsyncFaviconFetchMode {
    1.48 +  FETCH_NEVER = 0
    1.49 +, FETCH_IF_MISSING
    1.50 +, FETCH_ALWAYS
    1.51 +};
    1.52 +
    1.53 +/**
    1.54 + * Data cache for a icon entry.
    1.55 + */
    1.56 +struct IconData
    1.57 +{
    1.58 +  IconData()
    1.59 +  : id(0)
    1.60 +  , expiration(0)
    1.61 +  , fetchMode(FETCH_NEVER)
    1.62 +  , status(ICON_STATUS_UNKNOWN)
    1.63 +  {
    1.64 +    guid.SetIsVoid(true);
    1.65 +  }
    1.66 +
    1.67 +  int64_t id;
    1.68 +  nsCString spec;
    1.69 +  nsCString data;
    1.70 +  nsCString mimeType;
    1.71 +  PRTime expiration;
    1.72 +  enum AsyncFaviconFetchMode fetchMode;
    1.73 +  uint16_t status; // This is a bitset, see ICON_STATUS_* defines above.
    1.74 +  nsCString guid;
    1.75 +};
    1.76 +
    1.77 +/**
    1.78 + * Data cache for a page entry.
    1.79 + */
    1.80 +struct PageData
    1.81 +{
    1.82 +  PageData()
    1.83 +  : id(0)
    1.84 +  , canAddToHistory(true)
    1.85 +  , iconId(0)
    1.86 +  {
    1.87 +    guid.SetIsVoid(true);
    1.88 +  }
    1.89 +
    1.90 +  int64_t id;
    1.91 +  nsCString spec;
    1.92 +  nsCString bookmarkedSpec;
    1.93 +  nsString revHost;
    1.94 +  bool canAddToHistory; // False for disabled history and unsupported schemas.
    1.95 +  int64_t iconId;
    1.96 +  nsCString guid;
    1.97 +};
    1.98 +
    1.99 +/**
   1.100 + * Base class for events declared in this file.  This class's main purpose is
   1.101 + * to declare a destructor which releases mCallback on the main thread.
   1.102 + */
   1.103 +class AsyncFaviconHelperBase : public nsRunnable
   1.104 +{
   1.105 +protected:
   1.106 +  AsyncFaviconHelperBase(nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.107 +
   1.108 +  virtual ~AsyncFaviconHelperBase();
   1.109 +
   1.110 +  nsRefPtr<Database> mDB;
   1.111 +  // Strong reference since we are responsible for its existence.
   1.112 +  nsCOMPtr<nsIFaviconDataCallback> mCallback;
   1.113 +};
   1.114 +
   1.115 +/**
   1.116 + * Async fetches icon from database or network, associates it with the required
   1.117 + * page and finally notifies the change.
   1.118 + */
   1.119 +class AsyncFetchAndSetIconForPage : public AsyncFaviconHelperBase
   1.120 +{
   1.121 +public:
   1.122 +  NS_DECL_NSIRUNNABLE
   1.123 +
   1.124 +  /**
   1.125 +   * Creates the event and dispatches it to the async thread.
   1.126 +   *
   1.127 +   * @param aFaviconURI
   1.128 +   *        URI of the icon to be fetched and associated.
   1.129 +   * @param aPageURI
   1.130 +   *        URI of the page to which associate the icon.
   1.131 +   * @param aFetchMode
   1.132 +   *        Specifies whether a icon should be fetched from network if not found
   1.133 +   *        in the database.
   1.134 +   * @param aCallback
   1.135 +   *        Function to be called when the fetch-and-associate process finishes.
   1.136 +   */
   1.137 +  static nsresult start(nsIURI* aFaviconURI,
   1.138 +                        nsIURI* aPageURI,
   1.139 +                        enum AsyncFaviconFetchMode aFetchMode,
   1.140 +                        uint32_t aFaviconLoadType,
   1.141 +                        nsIFaviconDataCallback* aCallback);
   1.142 +
   1.143 +  /**
   1.144 +   * Constructor.
   1.145 +   *
   1.146 +   * @param aIcon
   1.147 +   *        Icon to be fetched and associated.
   1.148 +   * @param aPage
   1.149 +   *        Page to which associate the icon.
   1.150 +   * @param aCallback
   1.151 +   *        Function to be called when the fetch-and-associate process finishes.
   1.152 +   */
   1.153 +  AsyncFetchAndSetIconForPage(IconData& aIcon,
   1.154 +                              PageData& aPage,
   1.155 +                              uint32_t aFaviconLoadType,
   1.156 +                              nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.157 +
   1.158 +  virtual ~AsyncFetchAndSetIconForPage();
   1.159 +
   1.160 +protected:
   1.161 +  IconData mIcon;
   1.162 +  PageData mPage;
   1.163 +  const bool mFaviconLoadPrivate;
   1.164 +};
   1.165 +
   1.166 +/**
   1.167 + * If needed will asynchronously fetch the icon from the network.  It will
   1.168 + * finally dispatch an event to the async thread to associate the icon with
   1.169 + * the required page.
   1.170 + */
   1.171 +class AsyncFetchAndSetIconFromNetwork : public AsyncFaviconHelperBase
   1.172 +                                      , public nsIStreamListener
   1.173 +                                      , public nsIInterfaceRequestor
   1.174 +                                      , public nsIChannelEventSink
   1.175 +{
   1.176 +public:
   1.177 +  NS_DECL_NSISTREAMLISTENER
   1.178 +  NS_DECL_NSIINTERFACEREQUESTOR
   1.179 +  NS_DECL_NSICHANNELEVENTSINK
   1.180 +  NS_DECL_NSIREQUESTOBSERVER
   1.181 +  NS_DECL_NSIRUNNABLE
   1.182 +  NS_DECL_ISUPPORTS_INHERITED
   1.183 +
   1.184 +  /**
   1.185 +   * Constructor.
   1.186 +   *
   1.187 +   * @param aIcon
   1.188 +   *        Icon to be fetched and associated.
   1.189 +   * @param aPage
   1.190 +   *        Page to which associate the icon.
   1.191 +   * @param aCallback
   1.192 +   *        Function to be called when the fetch-and-associate process finishes.
   1.193 +   */
   1.194 +  AsyncFetchAndSetIconFromNetwork(IconData& aIcon,
   1.195 +                                  PageData& aPage,
   1.196 +                                  bool aFaviconLoadPrivate,
   1.197 +                                  nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.198 +
   1.199 +  virtual ~AsyncFetchAndSetIconFromNetwork();
   1.200 +
   1.201 +protected:
   1.202 +  IconData mIcon;
   1.203 +  PageData mPage;
   1.204 +  const bool mFaviconLoadPrivate;
   1.205 +};
   1.206 +
   1.207 +/**
   1.208 + * Associates the icon to the required page, finally dispatches an event to the
   1.209 + * main thread to notify the change to observers.
   1.210 + */
   1.211 +class AsyncAssociateIconToPage : public AsyncFaviconHelperBase
   1.212 +{
   1.213 +public:
   1.214 +  NS_DECL_NSIRUNNABLE
   1.215 +
   1.216 +  /**
   1.217 +   * Constructor.
   1.218 +   *
   1.219 +   * @param aIcon
   1.220 +   *        Icon to be associated.
   1.221 +   * @param aPage
   1.222 +   *        Page to which associate the icon.
   1.223 +   * @param aCallback
   1.224 +   *        Function to be called when the associate process finishes.
   1.225 +   */
   1.226 +  AsyncAssociateIconToPage(IconData& aIcon,
   1.227 +                           PageData& aPage,
   1.228 +                           nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.229 +
   1.230 +  virtual ~AsyncAssociateIconToPage();
   1.231 +
   1.232 +protected:
   1.233 +  IconData mIcon;
   1.234 +  PageData mPage;
   1.235 +};
   1.236 +
   1.237 +/**
   1.238 + * Asynchronously tries to get the URL of a page's favicon, then notifies the
   1.239 + * given observer.
   1.240 + */
   1.241 +class AsyncGetFaviconURLForPage : public AsyncFaviconHelperBase
   1.242 +{
   1.243 +public:
   1.244 +  NS_DECL_NSIRUNNABLE
   1.245 +
   1.246 +  /**
   1.247 +   * Creates the event and dispatches it to the I/O thread.
   1.248 +   *
   1.249 +   * @param aPageURI
   1.250 +   *        URL of the page whose favicon's URL we're fetching
   1.251 +   * @param aCallback
   1.252 +   *        function to be called once finished
   1.253 +   */
   1.254 +  static nsresult start(nsIURI* aPageURI,
   1.255 +                        nsIFaviconDataCallback* aCallback);
   1.256 +
   1.257 +  /**
   1.258 +   * Constructor.
   1.259 +   *
   1.260 +   * @param aPageSpec
   1.261 +   *        URL of the page whose favicon's URL we're fetching
   1.262 +   * @param aCallback
   1.263 +   *        function to be called once finished
   1.264 +   */
   1.265 +  AsyncGetFaviconURLForPage(const nsACString& aPageSpec,
   1.266 +                            nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.267 +
   1.268 +  virtual ~AsyncGetFaviconURLForPage();
   1.269 +
   1.270 +private:
   1.271 +  nsCString mPageSpec;
   1.272 +};
   1.273 +
   1.274 +
   1.275 +/**
   1.276 + * Asynchronously tries to get the URL and data of a page's favicon, then
   1.277 + * notifies the given observer.
   1.278 + */
   1.279 +class AsyncGetFaviconDataForPage : public AsyncFaviconHelperBase
   1.280 +{
   1.281 +public:
   1.282 +  NS_DECL_NSIRUNNABLE
   1.283 +
   1.284 +  /**
   1.285 +   * Creates the event and dispatches it to the I/O thread.
   1.286 +   *
   1.287 +   * @param aPageURI
   1.288 +   *        URL of the page whose favicon URL and data we're fetching
   1.289 +   * @param aCallback
   1.290 +   *        function to be called once finished
   1.291 +   */
   1.292 +  static nsresult start(nsIURI* aPageURI,
   1.293 +                        nsIFaviconDataCallback* aCallback);
   1.294 +
   1.295 +  /**
   1.296 +   * Constructor.
   1.297 +   *
   1.298 +   * @param aPageSpec
   1.299 +   *        URL of the page whose favicon URL and data we're fetching
   1.300 +   * @param aCallback
   1.301 +   *        function to be called once finished
   1.302 +   */
   1.303 +  AsyncGetFaviconDataForPage(const nsACString& aPageSpec,
   1.304 +                             nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.305 +
   1.306 +  virtual ~AsyncGetFaviconDataForPage();
   1.307 +
   1.308 +private:
   1.309 +  nsCString mPageSpec;
   1.310 +};
   1.311 +
   1.312 +class AsyncReplaceFaviconData : public AsyncFaviconHelperBase
   1.313 +{
   1.314 +public:
   1.315 +  NS_DECL_NSIRUNNABLE
   1.316 +
   1.317 +  static nsresult start(IconData *aIcon);
   1.318 +
   1.319 +  AsyncReplaceFaviconData(IconData &aIcon,
   1.320 +                          nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.321 +
   1.322 +  virtual ~AsyncReplaceFaviconData();
   1.323 +
   1.324 +protected:
   1.325 +  IconData mIcon;
   1.326 +};
   1.327 +
   1.328 +class RemoveIconDataCacheEntry : public AsyncFaviconHelperBase
   1.329 +{
   1.330 +public:
   1.331 +  NS_DECL_NSIRUNNABLE
   1.332 +
   1.333 +  RemoveIconDataCacheEntry(IconData &aIcon,
   1.334 +                           nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.335 +  virtual ~RemoveIconDataCacheEntry();
   1.336 +
   1.337 +protected:
   1.338 +  IconData mIcon;
   1.339 +};
   1.340 +
   1.341 +/**
   1.342 + * Notifies the icon change to favicon observers.
   1.343 + */
   1.344 +class NotifyIconObservers : public AsyncFaviconHelperBase
   1.345 +{
   1.346 +public:
   1.347 +  NS_DECL_NSIRUNNABLE
   1.348 +
   1.349 +  /**
   1.350 +   * Constructor.
   1.351 +   *
   1.352 +   * @param aIcon
   1.353 +   *        Icon information. Can be empty if no icon is associated to the page.
   1.354 +   * @param aPage
   1.355 +   *        Page to which the icon information applies.
   1.356 +   * @param aCallback
   1.357 +   *        Function to be notified in all cases.
   1.358 +   */
   1.359 +  NotifyIconObservers(IconData& aIcon,
   1.360 +                      PageData& aPage,
   1.361 +                      nsCOMPtr<nsIFaviconDataCallback>& aCallback);
   1.362 +  virtual ~NotifyIconObservers();
   1.363 +
   1.364 +protected:
   1.365 +  IconData mIcon;
   1.366 +  PageData mPage;
   1.367 +
   1.368 +  void SendGlobalNotifications(nsIURI* aIconURI);
   1.369 +};
   1.370 +
   1.371 +} // namespace places
   1.372 +} // namespace mozilla
   1.373 +
   1.374 +#endif // AsyncFaviconHelpers_h_

mercurial