LCOV - code coverage report
Current view: directory - toolkit/components/places - AsyncFaviconHelpers.h (source / functions) Found Hit Coverage
Test: app.info Lines: 10 10 100.0 %
Date: 2012-06-02 Functions: 7 7 100.0 %

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

Generated by: LCOV version 1.7