[JAEGER] Merge from tracemonkey.
[mozilla-central.git] / toolkit / components / places / src / History.cpp
blob6d264e9a5a0b2cff2bbe9470d60606e52334ecaa
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
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/
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.
16 * The Original Code is Places code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Foundation.
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
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.
38 * ***** END LICENSE BLOCK ***** */
40 #include "History.h"
41 #include "nsNavHistory.h"
42 #include "Helpers.h"
44 #include "mozilla/storage.h"
45 #include "mozilla/dom/Link.h"
46 #include "nsDocShellCID.h"
47 #include "nsIEventStateManager.h"
48 #include "mozilla/Services.h"
50 using namespace mozilla::dom;
52 namespace mozilla {
53 namespace places {
55 ////////////////////////////////////////////////////////////////////////////////
56 //// Global Defines
58 #define URI_VISITED "visited"
59 #define URI_NOT_VISITED "not visited"
60 #define URI_VISITED_RESOLUTION_TOPIC "visited-status-resolution"
62 ////////////////////////////////////////////////////////////////////////////////
63 //// Anonymous Helpers
65 namespace {
67 class VisitedQuery : public mozIStorageStatementCallback
69 public:
70 NS_DECL_ISUPPORTS
72 static nsresult Start(nsIURI* aURI)
74 NS_ASSERTION(aURI, "Don't pass a null URI!");
76 nsNavHistory* navHist = nsNavHistory::GetHistoryService();
77 NS_ENSURE_TRUE(navHist, NS_ERROR_FAILURE);
78 mozIStorageStatement* stmt = navHist->GetStatementById(DB_IS_PAGE_VISITED);
79 NS_ENSURE_STATE(stmt);
81 // Bind by index for performance.
82 nsresult rv = URIBinder::Bind(stmt, 0, aURI);
83 NS_ENSURE_SUCCESS(rv, rv);
85 nsRefPtr<VisitedQuery> callback = new VisitedQuery(aURI);
86 NS_ENSURE_TRUE(callback, NS_ERROR_OUT_OF_MEMORY);
88 nsCOMPtr<mozIStoragePendingStatement> handle;
89 return stmt->ExecuteAsync(callback, getter_AddRefs(handle));
92 NS_IMETHOD HandleResult(mozIStorageResultSet* aResults)
94 // If this method is called, we've gotten results, which means we have a
95 // visit.
96 mIsVisited = true;
97 return NS_OK;
100 NS_IMETHOD HandleError(mozIStorageError* aError)
102 // mIsVisited is already set to false, and that's the assumption we will
103 // make if an error occurred.
104 return NS_OK;
107 NS_IMETHOD HandleCompletion(PRUint16 aReason)
109 if (mIsVisited) {
110 History::GetService()->NotifyVisited(mURI);
113 // Notify any observers about that we have resolved the visited state of
114 // this URI.
115 nsCOMPtr<nsIObserverService> observerService =
116 mozilla::services::GetObserverService();
117 if (observerService) {
118 nsAutoString status;
119 if (mIsVisited) {
120 status.AssignLiteral(URI_VISITED);
122 else {
123 status.AssignLiteral(URI_NOT_VISITED);
125 (void)observerService->NotifyObservers(mURI,
126 URI_VISITED_RESOLUTION_TOPIC,
127 status.get());
130 return NS_OK;
132 private:
133 VisitedQuery(nsIURI* aURI)
134 : mURI(aURI)
135 , mIsVisited(false)
139 nsCOMPtr<nsIURI> mURI;
140 bool mIsVisited;
142 NS_IMPL_ISUPPORTS1(
143 VisitedQuery,
144 mozIStorageStatementCallback
147 } // anonymous namespace
149 ////////////////////////////////////////////////////////////////////////////////
150 //// History
152 History* History::gService = NULL;
154 History::History()
156 NS_ASSERTION(!gService, "Ruh-roh! This service has already been created!");
157 gService = this;
160 History::~History()
162 gService = NULL;
163 #ifdef DEBUG
164 if (mObservers.IsInitialized()) {
165 NS_ASSERTION(mObservers.Count() == 0,
166 "Not all Links were removed before we disappear!");
168 #endif
171 void
172 History::NotifyVisited(nsIURI* aURI)
174 NS_ASSERTION(aURI, "Ruh-roh! A NULL URI was passed to us!");
176 // If the hash table has not been initialized, then we have nothing to notify
177 // about.
178 if (!mObservers.IsInitialized()) {
179 return;
182 // Additionally, if we have no observers for this URI, we have nothing to
183 // notify about.
184 KeyClass* key = mObservers.GetEntry(aURI);
185 if (!key) {
186 return;
189 // Walk through the array, and update each Link node.
190 const ObserverArray& observers = key->array;
191 ObserverArray::index_type len = observers.Length();
192 for (ObserverArray::index_type i = 0; i < len; i++) {
193 Link* link = observers[i];
194 link->SetLinkState(eLinkState_Visited);
195 NS_ASSERTION(len == observers.Length(),
196 "Calling SetLinkState added or removed an observer!");
199 // All the registered nodes can now be removed for this URI.
200 mObservers.RemoveEntry(aURI);
203 /* static */
204 History*
205 History::GetService()
207 if (gService) {
208 return gService;
211 nsCOMPtr<IHistory> service(do_GetService(NS_IHISTORY_CONTRACTID));
212 NS_ABORT_IF_FALSE(service, "Cannot obtain IHistory service!");
213 NS_ASSERTION(gService, "Our constructor was not run?!");
215 return gService;
218 /* static */
219 History*
220 History::GetSingleton()
222 if (!gService) {
223 gService = new History();
224 NS_ENSURE_TRUE(gService, nsnull);
227 NS_ADDREF(gService);
228 return gService;
231 ////////////////////////////////////////////////////////////////////////////////
232 //// IHistory
234 NS_IMETHODIMP
235 History::RegisterVisitedCallback(nsIURI* aURI,
236 Link* aLink)
238 NS_ASSERTION(aURI, "Must pass a non-null URI!");
239 NS_ASSERTION(aLink, "Must pass a non-null Link object!");
241 // First, ensure that our hash table is setup.
242 if (!mObservers.IsInitialized()) {
243 NS_ENSURE_TRUE(mObservers.Init(), NS_ERROR_OUT_OF_MEMORY);
246 // Obtain our array of observers for this URI.
247 #ifdef DEBUG
248 bool keyAlreadyExists = !!mObservers.GetEntry(aURI);
249 #endif
250 KeyClass* key = mObservers.PutEntry(aURI);
251 NS_ENSURE_TRUE(key, NS_ERROR_OUT_OF_MEMORY);
252 ObserverArray& observers = key->array;
254 if (observers.IsEmpty()) {
255 NS_ASSERTION(!keyAlreadyExists,
256 "An empty key was kept around in our hashtable!");
258 // We are the first Link node to ask about this URI, or there are no pending
259 // Links wanting to know about this URI. Therefore, we should query the
260 // database now.
261 nsresult rv = VisitedQuery::Start(aURI);
262 if (NS_FAILED(rv)) {
263 // Remove our array from the hashtable so we don't keep it around.
264 mObservers.RemoveEntry(aURI);
265 return rv;
269 // Sanity check that Links are not registered more than once for a given URI.
270 // This will not catch a case where it is registered for two different URIs.
271 NS_ASSERTION(!observers.Contains(aLink),
272 "Already tracking this Link object!");
274 // Start tracking our Link.
275 if (!observers.AppendElement(aLink)) {
276 // Curses - unregister and return failure.
277 (void)UnregisterVisitedCallback(aURI, aLink);
278 return NS_ERROR_OUT_OF_MEMORY;
281 return NS_OK;
284 NS_IMETHODIMP
285 History::UnregisterVisitedCallback(nsIURI* aURI,
286 Link* aLink)
288 NS_ASSERTION(aURI, "Must pass a non-null URI!");
289 NS_ASSERTION(aLink, "Must pass a non-null Link object!");
291 // Get the array, and remove the item from it.
292 KeyClass* key = mObservers.GetEntry(aURI);
293 if (!key) {
294 NS_ERROR("Trying to unregister for a URI that wasn't registered!");
295 return NS_ERROR_UNEXPECTED;
297 ObserverArray& observers = key->array;
298 if (!observers.RemoveElement(aLink)) {
299 NS_ERROR("Trying to unregister a node that wasn't registered!");
300 return NS_ERROR_UNEXPECTED;
303 // If the array is now empty, we should remove it from the hashtable.
304 if (observers.IsEmpty()) {
305 mObservers.RemoveEntry(aURI);
308 return NS_OK;
311 ////////////////////////////////////////////////////////////////////////////////
312 //// nsISupports
314 NS_IMPL_ISUPPORTS1(
315 History,
316 IHistory
319 } // namespace places
320 } // namespace mozilla