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
16 * The Original Code is Places code.
18 * The Initial Developer of the Original Code is
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
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 ***** */
41 #include "nsNavHistory.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
;
55 ////////////////////////////////////////////////////////////////////////////////
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
67 class VisitedQuery
: public mozIStorageStatementCallback
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
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.
107 NS_IMETHOD
HandleCompletion(PRUint16 aReason
)
110 History::GetService()->NotifyVisited(mURI
);
113 // Notify any observers about that we have resolved the visited state of
115 nsCOMPtr
<nsIObserverService
> observerService
=
116 mozilla::services::GetObserverService();
117 if (observerService
) {
120 status
.AssignLiteral(URI_VISITED
);
123 status
.AssignLiteral(URI_NOT_VISITED
);
125 (void)observerService
->NotifyObservers(mURI
,
126 URI_VISITED_RESOLUTION_TOPIC
,
133 VisitedQuery(nsIURI
* aURI
)
139 nsCOMPtr
<nsIURI
> mURI
;
144 mozIStorageStatementCallback
147 } // anonymous namespace
149 ////////////////////////////////////////////////////////////////////////////////
152 History
* History::gService
= NULL
;
156 NS_ASSERTION(!gService
, "Ruh-roh! This service has already been created!");
164 if (mObservers
.IsInitialized()) {
165 NS_ASSERTION(mObservers
.Count() == 0,
166 "Not all Links were removed before we disappear!");
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
178 if (!mObservers
.IsInitialized()) {
182 // Additionally, if we have no observers for this URI, we have nothing to
184 KeyClass
* key
= mObservers
.GetEntry(aURI
);
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
);
205 History::GetService()
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?!");
220 History::GetSingleton()
223 gService
= new History();
224 NS_ENSURE_TRUE(gService
, nsnull
);
231 ////////////////////////////////////////////////////////////////////////////////
235 History::RegisterVisitedCallback(nsIURI
* aURI
,
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.
248 bool keyAlreadyExists
= !!mObservers
.GetEntry(aURI
);
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
261 nsresult rv
= VisitedQuery::Start(aURI
);
263 // Remove our array from the hashtable so we don't keep it around.
264 mObservers
.RemoveEntry(aURI
);
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
;
285 History::UnregisterVisitedCallback(nsIURI
* aURI
,
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
);
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
);
311 ////////////////////////////////////////////////////////////////////////////////
319 } // namespace places
320 } // namespace mozilla