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