1 /* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef NS_ANDROIDHISTORY_H
7 #define NS_ANDROIDHISTORY_H
8 
9 #include "IHistory.h"
10 #include "nsDataHashtable.h"
11 #include "nsTPriorityQueue.h"
12 #include "nsIRunnable.h"
13 #include "nsIURI.h"
14 #include "nsITimer.h"
15 
16 
17 #define NS_ANDROIDHISTORY_CID \
18     {0xCCAA4880, 0x44DD, 0x40A7, {0xA1, 0x3F, 0x61, 0x56, 0xFC, 0x88, 0x2C, 0x0B}}
19 
20 // Max size of History::mRecentlyVisitedURIs
21 #define RECENTLY_VISITED_URI_SIZE 8
22 
23 // Max size of History::mEmbedURIs
24 #define EMBED_URI_SIZE 128
25 
26 class nsAndroidHistory final : public mozilla::IHistory,
27                                public nsIRunnable,
28                                public nsITimerCallback
29 {
30 public:
31   NS_DECL_ISUPPORTS
32   NS_DECL_IHISTORY
33   NS_DECL_NSIRUNNABLE
34   NS_DECL_NSITIMERCALLBACK
35 
36   /**
37    * Obtains a pointer that has had AddRef called on it.  Used by the service
38    * manager only.
39    */
40   static nsAndroidHistory* GetSingleton();
41 
42   nsAndroidHistory();
43 
44 private:
~nsAndroidHistory()45   ~nsAndroidHistory() {}
46 
47   static nsAndroidHistory* sHistory;
48 
49   // Will mimic the value of the places.history.enabled preference.
50   bool mHistoryEnabled;
51 
52   void LoadPrefs();
53   bool ShouldRecordHistory();
54   nsresult CanAddURI(nsIURI* aURI, bool* canAdd);
55 
56   /**
57    * We need to manage data used to determine a:visited status.
58    */
59   nsDataHashtable<nsStringHashKey, nsTArray<mozilla::dom::Link *> *> mListeners;
60   nsTPriorityQueue<nsString> mPendingLinkURIs;
61 
62   /**
63    * Redirection (temporary and permanent) flags are sent with the redirected
64    * URI, not the original URI. Since we want to ignore the original URI, we
65    * need to cache the pending visit and make sure it doesn't redirect.
66    */
67   RefPtr<nsITimer> mTimer;
68   typedef AutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE> PendingVisitArray;
69   PendingVisitArray mPendingVisitURIs;
70 
71   bool RemovePendingVisitURI(nsIURI* aURI);
72   void SaveVisitURI(nsIURI* aURI);
73 
74   /**
75    * mRecentlyVisitedURIs remembers URIs which are recently added to the DB,
76    * to avoid saving these locations repeatedly in a short period.
77    */
78   typedef AutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE> RecentlyVisitedArray;
79   RecentlyVisitedArray mRecentlyVisitedURIs;
80   RecentlyVisitedArray::index_type mRecentlyVisitedURIsNextIndex;
81 
82   void AppendToRecentlyVisitedURIs(nsIURI* aURI);
83   bool IsRecentlyVisitedURI(nsIURI* aURI);
84 
85   /**
86    * mEmbedURIs remembers URIs which are explicitly not added to the DB,
87    * to avoid wasting time on these locations.
88    */
89   typedef AutoTArray<nsCOMPtr<nsIURI>, EMBED_URI_SIZE> EmbedArray;
90   EmbedArray::index_type mEmbedURIsNextIndex;
91   EmbedArray mEmbedURIs;
92 
93   void AppendToEmbedURIs(nsIURI* aURI);
94   bool IsEmbedURI(nsIURI* aURI);
95 };
96 
97 #endif
98