toolkit/components/places/Helpers.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef mozilla_places_Helpers_h_
     7 #define mozilla_places_Helpers_h_
     9 /**
    10  * This file contains helper classes used by various bits of Places code.
    11  */
    13 #include "mozilla/storage.h"
    14 #include "nsIURI.h"
    15 #include "nsThreadUtils.h"
    16 #include "nsProxyRelease.h"
    17 #include "mozilla/Telemetry.h"
    19 namespace mozilla {
    20 namespace places {
    22 ////////////////////////////////////////////////////////////////////////////////
    23 //// Asynchronous Statement Callback Helper
    25 class AsyncStatementCallback : public mozIStorageStatementCallback
    26 {
    27 public:
    28   NS_DECL_ISUPPORTS
    29   NS_DECL_MOZISTORAGESTATEMENTCALLBACK
    30   AsyncStatementCallback() {}
    32 protected:
    33   virtual ~AsyncStatementCallback() {}
    34 };
    36 /**
    37  * Macros to use in place of NS_DECL_MOZISTORAGESTATEMENTCALLBACK to declare the
    38  * methods this class assumes silent or notreached.
    39  */
    40 #define NS_DECL_ASYNCSTATEMENTCALLBACK \
    41   NS_IMETHOD HandleResult(mozIStorageResultSet *); \
    42   NS_IMETHOD HandleCompletion(uint16_t);
    44 /**
    45  * Utils to bind a specified URI (or URL) to a statement or binding params, at
    46  * the specified index or name.
    47  * @note URIs are always bound as UTF8.
    48  */
    49 class URIBinder // static
    50 {
    51 public:
    52   // Bind URI to statement by index.
    53   static nsresult Bind(mozIStorageStatement* statement,
    54                        int32_t index,
    55                        nsIURI* aURI);
    56   // Statement URLCString to statement by index.
    57   static nsresult Bind(mozIStorageStatement* statement,
    58                        int32_t index,
    59                        const nsACString& aURLString);
    60   // Bind URI to statement by name.
    61   static nsresult Bind(mozIStorageStatement* statement,
    62                        const nsACString& aName,
    63                        nsIURI* aURI);
    64   // Bind URLCString to statement by name.
    65   static nsresult Bind(mozIStorageStatement* statement,
    66                        const nsACString& aName,
    67                        const nsACString& aURLString);
    68   // Bind URI to params by index.
    69   static nsresult Bind(mozIStorageBindingParams* aParams,
    70                        int32_t index,
    71                        nsIURI* aURI);
    72   // Bind URLCString to params by index.
    73   static nsresult Bind(mozIStorageBindingParams* aParams,
    74                        int32_t index,
    75                        const nsACString& aURLString);
    76   // Bind URI to params by name.
    77   static nsresult Bind(mozIStorageBindingParams* aParams,
    78                        const nsACString& aName,
    79                        nsIURI* aURI);
    80   // Bind URLCString to params by name.
    81   static nsresult Bind(mozIStorageBindingParams* aParams,
    82                        const nsACString& aName,
    83                        const nsACString& aURLString);
    84 };
    86 /**
    87  * This extracts the hostname from the URI and reverses it in the
    88  * form that we use (always ending with a "."). So
    89  * "http://microsoft.com/" becomes "moc.tfosorcim."
    90  * 
    91  * The idea behind this is that we can create an index over the items in
    92  * the reversed host name column, and then query for as much or as little
    93  * of the host name as we feel like.
    94  * 
    95  * For example, the query "host >= 'gro.allizom.' AND host < 'gro.allizom/'
    96  * Matches all host names ending in '.mozilla.org', including
    97  * 'developer.mozilla.org' and just 'mozilla.org' (since we define all
    98  * reversed host names to end in a period, even 'mozilla.org' matches).
    99  * The important thing is that this operation uses the index. Any substring
   100  * calls in a select statement (even if it's for the beginning of a string)
   101  * will bypass any indices and will be slow).
   102  *
   103  * @param aURI
   104  *        URI that contains spec to reverse
   105  * @param aRevHost
   106  *        Out parameter
   107  */
   108 nsresult GetReversedHostname(nsIURI* aURI, nsString& aRevHost);
   110 /**
   111  * Similar method to GetReversedHostName but for strings
   112  */
   113 void GetReversedHostname(const nsString& aForward, nsString& aRevHost);
   115 /**
   116  * Reverses a string.
   117  *
   118  * @param aInput
   119  *        The string to be reversed
   120  * @param aReversed
   121  *        Output parameter will contain the reversed string
   122  */
   123 void ReverseString(const nsString& aInput, nsString& aReversed);
   125 /**
   126  * Generates an 12 character guid to be used by bookmark and history entries.
   127  *
   128  * @note This guid uses the characters a-z, A-Z, 0-9, '-', and '_'.
   129  */
   130 nsresult GenerateGUID(nsCString& _guid);
   132 /**
   133  * Determines if the string is a valid guid or not.
   134  *
   135  * @param aGUID
   136  *        The guid to test.
   137  * @return true if it is a valid guid, false otherwise.
   138  */
   139 bool IsValidGUID(const nsACString& aGUID);
   141 /**
   142  * Truncates the title if it's longer than TITLE_LENGTH_MAX.
   143  *
   144  * @param aTitle
   145  *        The title to truncate (if necessary)
   146  * @param aTrimmed
   147  *        Output parameter to return the trimmed string
   148  */
   149 void TruncateTitle(const nsACString& aTitle, nsACString& aTrimmed);
   151 /**
   152  * Used to finalize a statementCache on a specified thread.
   153  */
   154 template<typename StatementType>
   155 class FinalizeStatementCacheProxy : public nsRunnable
   156 {
   157 public:
   158   /**
   159    * Constructor.
   160    *
   161    * @param aStatementCache
   162    *        The statementCache that should be finalized.
   163    * @param aOwner
   164    *        The object that owns the statement cache.  This runnable will hold
   165    *        a strong reference to it so aStatementCache will not disappear from
   166    *        under us.
   167    */
   168   FinalizeStatementCacheProxy(
   169     mozilla::storage::StatementCache<StatementType>& aStatementCache,
   170     nsISupports* aOwner
   171   )
   172   : mStatementCache(aStatementCache)
   173   , mOwner(aOwner)
   174   , mCallingThread(do_GetCurrentThread())
   175   {
   176   }
   178   NS_IMETHOD Run()
   179   {
   180     mStatementCache.FinalizeStatements();
   181     // Release the owner back on the calling thread.
   182     (void)NS_ProxyRelease(mCallingThread, mOwner);
   183     return NS_OK;
   184   }
   186 protected:
   187   mozilla::storage::StatementCache<StatementType>& mStatementCache;
   188   nsCOMPtr<nsISupports> mOwner;
   189   nsCOMPtr<nsIThread> mCallingThread;
   190 };
   192 /**
   193  * Forces a WAL checkpoint. This will cause all transactions stored in the
   194  * journal file to be committed to the main database.
   195  * 
   196  * @note The checkpoint will force a fsync/flush.
   197  */
   198 void ForceWALCheckpoint();
   200 /**
   201  * Determines if a visit should be marked as hidden given its transition type
   202  * and whether or not it was a redirect.
   203  *
   204  * @param aIsRedirect
   205  *        True if this visit was a redirect, false otherwise.
   206  * @param aTransitionType
   207  *        The transition type of the visit.
   208  * @return true if this visit should be hidden.
   209  */
   210 bool GetHiddenState(bool aIsRedirect,
   211                     uint32_t aTransitionType);
   213 /**
   214  * Notifies a specified topic via the observer service.
   215  */
   216 class PlacesEvent : public nsRunnable
   217 {
   218 public:
   219   NS_DECL_THREADSAFE_ISUPPORTS
   220   NS_DECL_NSIRUNNABLE
   222   PlacesEvent(const char* aTopic);
   223 protected:
   224   void Notify();
   226   const char* const mTopic;
   227 };
   229 /**
   230  * Used to notify a topic to system observers on async execute completion.
   231  */
   232 class AsyncStatementCallbackNotifier : public AsyncStatementCallback
   233 {
   234 public:
   235   AsyncStatementCallbackNotifier(const char* aTopic)
   236     : mTopic(aTopic)
   237   {
   238   }
   240   NS_IMETHOD HandleCompletion(uint16_t aReason);
   242 private:
   243   const char* mTopic;
   244 };
   246 /**
   247  * Used to notify a topic to system observers on async execute completion.
   248  */
   249 class AsyncStatementTelemetryTimer : public AsyncStatementCallback
   250 {
   251 public:
   252   AsyncStatementTelemetryTimer(Telemetry::ID aHistogramId,
   253                                TimeStamp aStart = TimeStamp::Now())
   254     : mHistogramId(aHistogramId)
   255     , mStart(aStart)
   256   {
   257   }
   259   NS_IMETHOD HandleCompletion(uint16_t aReason);
   261 private:
   262   const Telemetry::ID mHistogramId;
   263   const TimeStamp mStart;
   264 };
   266 } // namespace places
   267 } // namespace mozilla
   269 #endif // mozilla_places_Helpers_h_

mercurial