EmbedHistoryListener.h 3.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef EmbedHistoryListener_H_
#define EmbedHistoryListener_H_

#include "mozilla/IHistory.h"
#include "nsDataHashtable.h"
#include "nsTPriorityQueue.h"
13
#include "nsIRunnable.h"
14
#include "nsIEmbedAppService.h"
15
#include "nsIObserver.h"
16 17
#include "nsIURI.h"
#include "nsITimer.h"
18 19

#define NS_EMBEDLITEHISTORY_CID \
Tatiana Meshkova's avatar
Tatiana Meshkova committed
20 21 22 23
{ 0xec7cf1e2, \
  0x6e88, \
  0x11e2, \
  { 0xa7, 0x9a, 0xfb, 0x19, 0xfe, 0x29, 0x97 }}
24

25 26 27 28 29 30
// Max size of History::mRecentlyVisitedURIs
#define RECENTLY_VISITED_URI_SIZE 8

// Max size of History::mEmbedURIs
#define EMBED_URI_SIZE 128

31 32 33
class EmbedHistoryListener : public mozilla::IHistory
                           , public nsIRunnable
                           , public nsIObserver
34
                           , public nsITimerCallback
35 36
{
public:
37 38 39 40
  NS_DECL_ISUPPORTS
  NS_DECL_IHISTORY
  NS_DECL_NSIRUNNABLE
  NS_DECL_NSIOBSERVER
41
  NS_DECL_NSITIMERCALLBACK
42

43
  nsresult Init() { return NS_OK; }
44

45 46 47 48 49
  /**
   * Obtains a pointer that has had AddRef called on it.  Used by the service
   * manager only.
   */
  static EmbedHistoryListener* GetSingleton();
50

51
  EmbedHistoryListener();
52 53

private:
54
  virtual ~EmbedHistoryListener() {}
55
  nsIEmbedAppService* GetService();
56

57
  static EmbedHistoryListener* sHistory;
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  // Will mimic the value of the places.history.enabled preference.
  bool mHistoryEnabled;

  void LoadPrefs();
  bool ShouldRecordHistory();
  nsresult CanAddURI(nsIURI* aURI, bool* canAdd);

  /**
   * We need to manage data used to determine a:visited status.
   */
  nsDataHashtable<nsStringHashKey, nsTArray<mozilla::dom::Link *> *> mListeners;
  nsTPriorityQueue<nsString> mPendingLinkURIs;

  /**
   * Redirection (temporary and permanent) flags are sent with the redirected
   * URI, not the original URI. Since we want to ignore the original URI, we
   * need to cache the pending visit and make sure it doesn't redirect.
   */
  nsRefPtr<nsITimer> mTimer;
  typedef nsAutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE> PendingVisitArray;
  PendingVisitArray mPendingVisitURIs;

  bool RemovePendingVisitURI(nsIURI* aURI);
  void SaveVisitURI(nsIURI* aURI);

  /**
   * mRecentlyVisitedURIs remembers URIs which are recently added to the DB,
   * to avoid saving these locations repeatedly in a short period.
   */
  typedef nsAutoTArray<nsCOMPtr<nsIURI>, RECENTLY_VISITED_URI_SIZE> RecentlyVisitedArray;
  RecentlyVisitedArray mRecentlyVisitedURIs;
  RecentlyVisitedArray::index_type mRecentlyVisitedURIsNextIndex;

  void AppendToRecentlyVisitedURIs(nsIURI* aURI);
  bool IsRecentlyVisitedURI(nsIURI* aURI);

  /**
   * mEmbedURIs remembers URIs which are explicitly not added to the DB,
   * to avoid wasting time on these locations.
   */
  typedef nsAutoTArray<nsCOMPtr<nsIURI>, EMBED_URI_SIZE> EmbedArray;
  EmbedArray::index_type mEmbedURIsNextIndex;
  EmbedArray mEmbedURIs;

  void AppendToEmbedURIs(nsIURI* aURI);
  bool IsEmbedURI(nsIURI* aURI);

106
  nsCOMPtr<nsIEmbedAppService> mService;
107 108 109 110 111 112 113
};

#define NS_EMBED_HISTORY_CONTRACTID "@mozilla.org/embed-history-component;1"
#define NS_EMBED_HISTORY_SERVICE_CLASSNAME "Embed History Listener Component"
#define NS_EMBED_HISTORY_SERVICE_CID NS_EMBEDLITEHISTORY_CID

#endif /*EmbedHistoryListener_H_*/