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.

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

mercurial