1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/Helpers.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,269 @@ 1.4 +/* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_places_Helpers_h_ 1.10 +#define mozilla_places_Helpers_h_ 1.11 + 1.12 +/** 1.13 + * This file contains helper classes used by various bits of Places code. 1.14 + */ 1.15 + 1.16 +#include "mozilla/storage.h" 1.17 +#include "nsIURI.h" 1.18 +#include "nsThreadUtils.h" 1.19 +#include "nsProxyRelease.h" 1.20 +#include "mozilla/Telemetry.h" 1.21 + 1.22 +namespace mozilla { 1.23 +namespace places { 1.24 + 1.25 +//////////////////////////////////////////////////////////////////////////////// 1.26 +//// Asynchronous Statement Callback Helper 1.27 + 1.28 +class AsyncStatementCallback : public mozIStorageStatementCallback 1.29 +{ 1.30 +public: 1.31 + NS_DECL_ISUPPORTS 1.32 + NS_DECL_MOZISTORAGESTATEMENTCALLBACK 1.33 + AsyncStatementCallback() {} 1.34 + 1.35 +protected: 1.36 + virtual ~AsyncStatementCallback() {} 1.37 +}; 1.38 + 1.39 +/** 1.40 + * Macros to use in place of NS_DECL_MOZISTORAGESTATEMENTCALLBACK to declare the 1.41 + * methods this class assumes silent or notreached. 1.42 + */ 1.43 +#define NS_DECL_ASYNCSTATEMENTCALLBACK \ 1.44 + NS_IMETHOD HandleResult(mozIStorageResultSet *); \ 1.45 + NS_IMETHOD HandleCompletion(uint16_t); 1.46 + 1.47 +/** 1.48 + * Utils to bind a specified URI (or URL) to a statement or binding params, at 1.49 + * the specified index or name. 1.50 + * @note URIs are always bound as UTF8. 1.51 + */ 1.52 +class URIBinder // static 1.53 +{ 1.54 +public: 1.55 + // Bind URI to statement by index. 1.56 + static nsresult Bind(mozIStorageStatement* statement, 1.57 + int32_t index, 1.58 + nsIURI* aURI); 1.59 + // Statement URLCString to statement by index. 1.60 + static nsresult Bind(mozIStorageStatement* statement, 1.61 + int32_t index, 1.62 + const nsACString& aURLString); 1.63 + // Bind URI to statement by name. 1.64 + static nsresult Bind(mozIStorageStatement* statement, 1.65 + const nsACString& aName, 1.66 + nsIURI* aURI); 1.67 + // Bind URLCString to statement by name. 1.68 + static nsresult Bind(mozIStorageStatement* statement, 1.69 + const nsACString& aName, 1.70 + const nsACString& aURLString); 1.71 + // Bind URI to params by index. 1.72 + static nsresult Bind(mozIStorageBindingParams* aParams, 1.73 + int32_t index, 1.74 + nsIURI* aURI); 1.75 + // Bind URLCString to params by index. 1.76 + static nsresult Bind(mozIStorageBindingParams* aParams, 1.77 + int32_t index, 1.78 + const nsACString& aURLString); 1.79 + // Bind URI to params by name. 1.80 + static nsresult Bind(mozIStorageBindingParams* aParams, 1.81 + const nsACString& aName, 1.82 + nsIURI* aURI); 1.83 + // Bind URLCString to params by name. 1.84 + static nsresult Bind(mozIStorageBindingParams* aParams, 1.85 + const nsACString& aName, 1.86 + const nsACString& aURLString); 1.87 +}; 1.88 + 1.89 +/** 1.90 + * This extracts the hostname from the URI and reverses it in the 1.91 + * form that we use (always ending with a "."). So 1.92 + * "http://microsoft.com/" becomes "moc.tfosorcim." 1.93 + * 1.94 + * The idea behind this is that we can create an index over the items in 1.95 + * the reversed host name column, and then query for as much or as little 1.96 + * of the host name as we feel like. 1.97 + * 1.98 + * For example, the query "host >= 'gro.allizom.' AND host < 'gro.allizom/' 1.99 + * Matches all host names ending in '.mozilla.org', including 1.100 + * 'developer.mozilla.org' and just 'mozilla.org' (since we define all 1.101 + * reversed host names to end in a period, even 'mozilla.org' matches). 1.102 + * The important thing is that this operation uses the index. Any substring 1.103 + * calls in a select statement (even if it's for the beginning of a string) 1.104 + * will bypass any indices and will be slow). 1.105 + * 1.106 + * @param aURI 1.107 + * URI that contains spec to reverse 1.108 + * @param aRevHost 1.109 + * Out parameter 1.110 + */ 1.111 +nsresult GetReversedHostname(nsIURI* aURI, nsString& aRevHost); 1.112 + 1.113 +/** 1.114 + * Similar method to GetReversedHostName but for strings 1.115 + */ 1.116 +void GetReversedHostname(const nsString& aForward, nsString& aRevHost); 1.117 + 1.118 +/** 1.119 + * Reverses a string. 1.120 + * 1.121 + * @param aInput 1.122 + * The string to be reversed 1.123 + * @param aReversed 1.124 + * Output parameter will contain the reversed string 1.125 + */ 1.126 +void ReverseString(const nsString& aInput, nsString& aReversed); 1.127 + 1.128 +/** 1.129 + * Generates an 12 character guid to be used by bookmark and history entries. 1.130 + * 1.131 + * @note This guid uses the characters a-z, A-Z, 0-9, '-', and '_'. 1.132 + */ 1.133 +nsresult GenerateGUID(nsCString& _guid); 1.134 + 1.135 +/** 1.136 + * Determines if the string is a valid guid or not. 1.137 + * 1.138 + * @param aGUID 1.139 + * The guid to test. 1.140 + * @return true if it is a valid guid, false otherwise. 1.141 + */ 1.142 +bool IsValidGUID(const nsACString& aGUID); 1.143 + 1.144 +/** 1.145 + * Truncates the title if it's longer than TITLE_LENGTH_MAX. 1.146 + * 1.147 + * @param aTitle 1.148 + * The title to truncate (if necessary) 1.149 + * @param aTrimmed 1.150 + * Output parameter to return the trimmed string 1.151 + */ 1.152 +void TruncateTitle(const nsACString& aTitle, nsACString& aTrimmed); 1.153 + 1.154 +/** 1.155 + * Used to finalize a statementCache on a specified thread. 1.156 + */ 1.157 +template<typename StatementType> 1.158 +class FinalizeStatementCacheProxy : public nsRunnable 1.159 +{ 1.160 +public: 1.161 + /** 1.162 + * Constructor. 1.163 + * 1.164 + * @param aStatementCache 1.165 + * The statementCache that should be finalized. 1.166 + * @param aOwner 1.167 + * The object that owns the statement cache. This runnable will hold 1.168 + * a strong reference to it so aStatementCache will not disappear from 1.169 + * under us. 1.170 + */ 1.171 + FinalizeStatementCacheProxy( 1.172 + mozilla::storage::StatementCache<StatementType>& aStatementCache, 1.173 + nsISupports* aOwner 1.174 + ) 1.175 + : mStatementCache(aStatementCache) 1.176 + , mOwner(aOwner) 1.177 + , mCallingThread(do_GetCurrentThread()) 1.178 + { 1.179 + } 1.180 + 1.181 + NS_IMETHOD Run() 1.182 + { 1.183 + mStatementCache.FinalizeStatements(); 1.184 + // Release the owner back on the calling thread. 1.185 + (void)NS_ProxyRelease(mCallingThread, mOwner); 1.186 + return NS_OK; 1.187 + } 1.188 + 1.189 +protected: 1.190 + mozilla::storage::StatementCache<StatementType>& mStatementCache; 1.191 + nsCOMPtr<nsISupports> mOwner; 1.192 + nsCOMPtr<nsIThread> mCallingThread; 1.193 +}; 1.194 + 1.195 +/** 1.196 + * Forces a WAL checkpoint. This will cause all transactions stored in the 1.197 + * journal file to be committed to the main database. 1.198 + * 1.199 + * @note The checkpoint will force a fsync/flush. 1.200 + */ 1.201 +void ForceWALCheckpoint(); 1.202 + 1.203 +/** 1.204 + * Determines if a visit should be marked as hidden given its transition type 1.205 + * and whether or not it was a redirect. 1.206 + * 1.207 + * @param aIsRedirect 1.208 + * True if this visit was a redirect, false otherwise. 1.209 + * @param aTransitionType 1.210 + * The transition type of the visit. 1.211 + * @return true if this visit should be hidden. 1.212 + */ 1.213 +bool GetHiddenState(bool aIsRedirect, 1.214 + uint32_t aTransitionType); 1.215 + 1.216 +/** 1.217 + * Notifies a specified topic via the observer service. 1.218 + */ 1.219 +class PlacesEvent : public nsRunnable 1.220 +{ 1.221 +public: 1.222 + NS_DECL_THREADSAFE_ISUPPORTS 1.223 + NS_DECL_NSIRUNNABLE 1.224 + 1.225 + PlacesEvent(const char* aTopic); 1.226 +protected: 1.227 + void Notify(); 1.228 + 1.229 + const char* const mTopic; 1.230 +}; 1.231 + 1.232 +/** 1.233 + * Used to notify a topic to system observers on async execute completion. 1.234 + */ 1.235 +class AsyncStatementCallbackNotifier : public AsyncStatementCallback 1.236 +{ 1.237 +public: 1.238 + AsyncStatementCallbackNotifier(const char* aTopic) 1.239 + : mTopic(aTopic) 1.240 + { 1.241 + } 1.242 + 1.243 + NS_IMETHOD HandleCompletion(uint16_t aReason); 1.244 + 1.245 +private: 1.246 + const char* mTopic; 1.247 +}; 1.248 + 1.249 +/** 1.250 + * Used to notify a topic to system observers on async execute completion. 1.251 + */ 1.252 +class AsyncStatementTelemetryTimer : public AsyncStatementCallback 1.253 +{ 1.254 +public: 1.255 + AsyncStatementTelemetryTimer(Telemetry::ID aHistogramId, 1.256 + TimeStamp aStart = TimeStamp::Now()) 1.257 + : mHistogramId(aHistogramId) 1.258 + , mStart(aStart) 1.259 + { 1.260 + } 1.261 + 1.262 + NS_IMETHOD HandleCompletion(uint16_t aReason); 1.263 + 1.264 +private: 1.265 + const Telemetry::ID mHistogramId; 1.266 + const TimeStamp mStart; 1.267 +}; 1.268 + 1.269 +} // namespace places 1.270 +} // namespace mozilla 1.271 + 1.272 +#endif // mozilla_places_Helpers_h_