1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_dom_SessionStoreListener_h
8 #define mozilla_dom_SessionStoreListener_h
9 
10 #include "SessionStoreData.h"
11 #include "nsIDOMEventListener.h"
12 #include "nsIObserver.h"
13 #include "nsIPrivacyTransitionObserver.h"
14 #include "nsIWebProgressListener.h"
15 #include "nsWeakReference.h"
16 
17 class nsITimer;
18 
19 namespace mozilla {
20 namespace dom {
21 
22 class ContentSessionStore {
23  public:
24   explicit ContentSessionStore(nsIDocShell* aDocShell);
25   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(ContentSessionStore)
26   NS_DECL_CYCLE_COLLECTION_NATIVE_CLASS(ContentSessionStore)
27 
28   void OnPrivateModeChanged(bool aEnabled);
IsDocCapChanged()29   bool IsDocCapChanged() { return mDocCapChanged; }
30   nsCString GetDocShellCaps();
IsPrivateChanged()31   bool IsPrivateChanged() { return mPrivateChanged; }
32   bool GetPrivateModeEnabled();
33 
34   void SetSHistoryChanged();
35   // request "collect sessionHistory" which is happened in the parent process
36   void SetSHistoryFromParentChanged();
GetAndClearSHistoryChanged()37   bool GetAndClearSHistoryChanged() {
38     bool ret = mSHistoryChanged;
39     mSHistoryChanged = false;
40     mSHistoryChangedFromParent = false;
41     return ret;
42   }
43 
44   void OnDocumentStart();
45   void OnDocumentEnd();
UpdateNeeded()46   bool UpdateNeeded() {
47     return mPrivateChanged || mDocCapChanged || mSHistoryChanged ||
48            mSHistoryChangedFromParent;
49   }
50 
51  private:
52   virtual ~ContentSessionStore() = default;
53   nsCString CollectDocShellCapabilities();
54 
55   nsCOMPtr<nsIDocShell> mDocShell;
56   bool mPrivateChanged;
57   bool mIsPrivate;
58   bool mDocCapChanged;
59   nsCString mDocCaps;
60   // mSHistoryChanged means there are history changes which are found
61   // in the child process. The flag is set when
62   //    1. webProgress changes to STATE_START
63   //    2. webProgress changes to STATE_STOP
64   //    3. receiving "DOMTitleChanged" event
65   bool mSHistoryChanged;
66   // mSHistoryChangedFromParent means there are history changes which
67   // are found by session history listener in the parent process.
68   bool mSHistoryChangedFromParent;
69 };
70 
71 class TabListener : public nsIDOMEventListener,
72                     public nsIObserver,
73                     public nsIPrivacyTransitionObserver,
74                     public nsIWebProgressListener,
75                     public nsSupportsWeakReference {
76  public:
77   explicit TabListener(nsIDocShell* aDocShell, Element* aElement);
78   EventTarget* GetEventTarget();
79   nsresult Init();
GetSessionStore()80   ContentSessionStore* GetSessionStore() { return mSessionStore; }
81   // the function is called only when TabListener is in parent process
82   bool ForceFlushFromParent();
83   void RemoveListeners();
SetEpoch(uint32_t aEpoch)84   void SetEpoch(uint32_t aEpoch) { mEpoch = aEpoch; }
GetEpoch()85   uint32_t GetEpoch() { return mEpoch; }
UpdateSHistoryChanges()86   void UpdateSHistoryChanges() { AddTimerForUpdate(); }
87   void SetOwnerContent(Element* aElement);
88 
89   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
90   NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(TabListener, nsIDOMEventListener)
91 
92   NS_DECL_NSIDOMEVENTLISTENER
93   NS_DECL_NSIOBSERVER
94   NS_DECL_NSIPRIVACYTRANSITIONOBSERVER
95   NS_DECL_NSIWEBPROGRESSLISTENER
96 
97  private:
98   static void TimerCallback(nsITimer* aTimer, void* aClosure);
99   void AddTimerForUpdate();
100   void StopTimerForUpdate();
101   void AddEventListeners();
102   void RemoveEventListeners();
103   bool UpdateSessionStore(bool aIsFlush = false);
104   virtual ~TabListener();
105 
106   nsCOMPtr<nsIDocShell> mDocShell;
107   RefPtr<ContentSessionStore> mSessionStore;
108   RefPtr<mozilla::dom::Element> mOwnerContent;
109   bool mProgressListenerRegistered;
110   bool mEventListenerRegistered;
111   bool mPrefObserverRegistered;
112   // Timer used to update data
113   nsCOMPtr<nsITimer> mUpdatedTimer;
114   bool mTimeoutDisabled;
115   int32_t mUpdateInterval;
116   uint32_t mEpoch;
117 };
118 
119 }  // namespace dom
120 }  // namespace mozilla
121 
122 #endif  // mozilla_dom_SessionStoreListener_h
123