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 code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * the Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef mozilla_places_History_h_
41 : #define mozilla_places_History_h_
42 :
43 : #include "mozilla/IHistory.h"
44 : #include "mozilla/Mutex.h"
45 : #include "mozIAsyncHistory.h"
46 : #include "nsIDownloadHistory.h"
47 : #include "Database.h"
48 :
49 : #include "mozilla/dom/Link.h"
50 : #include "nsTHashtable.h"
51 : #include "nsString.h"
52 : #include "nsURIHashKey.h"
53 : #include "nsTObserverArray.h"
54 : #include "nsDeque.h"
55 : #include "nsIObserver.h"
56 : #include "mozIStorageConnection.h"
57 :
58 : namespace mozilla {
59 : namespace places {
60 :
61 : struct VisitData;
62 :
63 : #define NS_HISTORYSERVICE_CID \
64 : {0x0937a705, 0x91a6, 0x417a, {0x82, 0x92, 0xb2, 0x2e, 0xb1, 0x0d, 0xa8, 0x6c}}
65 :
66 : class History : public IHistory
67 : , public nsIDownloadHistory
68 : , public mozIAsyncHistory
69 : , public nsIObserver
70 : {
71 : public:
72 : NS_DECL_ISUPPORTS
73 : NS_DECL_IHISTORY
74 : NS_DECL_NSIDOWNLOADHISTORY
75 : NS_DECL_MOZIASYNCHISTORY
76 : NS_DECL_NSIOBSERVER
77 :
78 : History();
79 :
80 : /**
81 : * Notifies about the visited status of a given URI.
82 : *
83 : * @param aURI
84 : * The URI to notify about.
85 : */
86 : void NotifyVisited(nsIURI* aURI);
87 :
88 : /**
89 : * Obtains the statement to use to check if a URI is visited or not.
90 : */
91 : mozIStorageAsyncStatement* GetIsVisitedStatement();
92 :
93 : /**
94 : * Adds an entry in moz_places with the data in aVisitData.
95 : *
96 : * @param aVisitData
97 : * The visit data to use to populate a new row in moz_places.
98 : */
99 : nsresult InsertPlace(const VisitData& aVisitData);
100 :
101 : /**
102 : * Updates an entry in moz_places with the data in aVisitData.
103 : *
104 : * @param aVisitData
105 : * The visit data to use to update the existing row in moz_places.
106 : */
107 : nsresult UpdatePlace(const VisitData& aVisitData);
108 :
109 : /**
110 : * Loads information about the page into _place from moz_places.
111 : *
112 : * @param _place
113 : * The VisitData for the place we need to know information about.
114 : * @return true if the page was recorded in moz_places, false otherwise.
115 : */
116 : bool FetchPageInfo(VisitData& _place);
117 :
118 : /**
119 : * Get the number of bytes of memory this History object is using,
120 : * including sizeof(*this))
121 : */
122 : size_t SizeOfIncludingThis(nsMallocSizeOfFun aMallocSizeOf);
123 :
124 : /**
125 : * Obtains a pointer to this service.
126 : */
127 : static History* GetService();
128 :
129 : /**
130 : * Obtains a pointer that has had AddRef called on it. Used by the service
131 : * manager only.
132 : */
133 : static History* GetSingleton();
134 :
135 : template<int N>
136 : already_AddRefed<mozIStorageStatement>
137 3333 : GetStatement(const char (&aQuery)[N])
138 : {
139 3333 : mozIStorageConnection* dbConn = GetDBConn();
140 3333 : NS_ENSURE_TRUE(dbConn, nsnull);
141 3333 : return mDB->GetStatement(aQuery);
142 : }
143 :
144 604 : bool IsShuttingDown() const {
145 604 : return mShuttingDown;
146 : }
147 121 : Mutex& GetShutdownMutex() {
148 121 : return mShutdownMutex;
149 : }
150 :
151 : private:
152 : virtual ~History();
153 :
154 : /**
155 : * Obtains a read-write database connection.
156 : */
157 : mozIStorageConnection* GetDBConn();
158 :
159 : /**
160 : * The database handle. This is initialized lazily by the first call to
161 : * GetDBConn(), so never use it directly, or, if you really need, always
162 : * invoke GetDBConn() before.
163 : */
164 : nsRefPtr<mozilla::places::Database> mDB;
165 :
166 : /**
167 : * A read-only database connection used for checking if a URI is visited.
168 : *
169 : * @note this should only be accessed by GetIsVisistedStatement and Shutdown.
170 : */
171 : nsCOMPtr<mozIStorageConnection> mReadOnlyDBConn;
172 :
173 : /**
174 : * An asynchronous statement to query if a URI is visited or not.
175 : *
176 : * @note this should only be accessed by GetIsVisistedStatement and Shutdown.
177 : */
178 : nsCOMPtr<mozIStorageAsyncStatement> mIsVisitedStatement;
179 :
180 : /**
181 : * Remove any memory references to tasks and do not take on any more.
182 : */
183 : void Shutdown();
184 :
185 : static History* gService;
186 :
187 : // Ensures new tasks aren't started on destruction.
188 : bool mShuttingDown;
189 : // This mutex guards mShuttingDown. Code running in other threads that might
190 : // schedule tasks that use the database should grab it and check the value of
191 : // mShuttingDown. If we are already shutting down, the code must gracefully
192 : // avoid using the db. If we are not, the lock will prevent shutdown from
193 : // starting in an unexpected moment.
194 : Mutex mShutdownMutex;
195 :
196 : typedef nsTObserverArray<mozilla::dom::Link* > ObserverArray;
197 :
198 : class KeyClass : public nsURIHashKey
199 10 : {
200 : public:
201 10 : KeyClass(const nsIURI* aURI)
202 10 : : nsURIHashKey(aURI)
203 : {
204 10 : }
205 : KeyClass(const KeyClass& aOther)
206 : : nsURIHashKey(aOther)
207 : {
208 : NS_NOTREACHED("Do not call me!");
209 : }
210 : ObserverArray array;
211 : };
212 :
213 : /**
214 : * Helper function for nsTHashtable::SizeOfExcludingThis call in
215 : * SizeOfIncludingThis().
216 : */
217 : static size_t SizeOfEntryExcludingThis(KeyClass* aEntry,
218 : nsMallocSizeOfFun aMallocSizeOf,
219 : void*);
220 :
221 : nsTHashtable<KeyClass> mObservers;
222 : };
223 :
224 : } // namespace places
225 : } // namespace mozilla
226 :
227 : #endif // mozilla_places_History_h_
|