1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 nsEditingSession_h__
7 #define nsEditingSession_h__
8 
9 #include "nsCOMPtr.h"               // for nsCOMPtr
10 #include "nsISupportsImpl.h"        // for NS_DECL_ISUPPORTS
11 #include "nsIWeakReferenceUtils.h"  // for nsWeakPtr
12 #include "nsWeakReference.h"        // for nsSupportsWeakReference, etc
13 #include "nscore.h"                 // for nsresult
14 
15 #ifndef __gen_nsIWebProgressListener_h__
16 #  include "nsIWebProgressListener.h"
17 #endif
18 
19 #ifndef __gen_nsIEditingSession_h__
20 #  include "nsIEditingSession.h"  // for NS_DECL_NSIEDITINGSESSION, etc
21 #endif
22 
23 #include "nsString.h"  // for nsCString
24 
25 class mozIDOMWindowProxy;
26 class nsBaseCommandController;
27 class nsIDOMWindow;
28 class nsISupports;
29 class nsITimer;
30 class nsIChannel;
31 class nsIControllers;
32 class nsIDocShell;
33 class nsIWebProgress;
34 class nsIPIDOMWindowOuter;
35 class nsIPIDOMWindowInner;
36 
37 namespace mozilla {
38 class ComposerCommandsUpdater;
39 class HTMLEditor;
40 }  // namespace mozilla
41 
42 class nsEditingSession final : public nsIEditingSession,
43                                public nsIWebProgressListener,
44                                public nsSupportsWeakReference {
45  public:
46   nsEditingSession();
47 
48   // nsISupports
49   NS_DECL_ISUPPORTS
50 
51   // nsIWebProgressListener
52   NS_DECL_NSIWEBPROGRESSLISTENER
53 
54   // nsIEditingSession
55   NS_DECL_NSIEDITINGSESSION
56 
57   /**
58    * Removes all the editor's controllers/listeners etc and makes the window
59    * uneditable.
60    */
61   nsresult DetachFromWindow(nsPIDOMWindowOuter* aWindow);
62 
63   /**
64    * Undos DetachFromWindow(), reattaches this editing session/editor
65    * to the window.
66    */
67   nsresult ReattachToWindow(nsPIDOMWindowOuter* aWindow);
68 
69  protected:
70   virtual ~nsEditingSession();
71 
72   typedef already_AddRefed<nsBaseCommandController> (*ControllerCreatorFn)();
73 
74   nsresult SetupEditorCommandController(
75       ControllerCreatorFn aControllerCreatorFn, mozIDOMWindowProxy* aWindow,
76       nsISupports* aContext, uint32_t* aControllerId);
77 
78   nsresult SetContextOnControllerById(nsIControllers* aControllers,
79                                       nsISupports* aContext, uint32_t aID);
80 
81   /**
82    *  Set the editor on the controller(s) for this window
83    */
84   nsresult SetEditorOnControllers(nsPIDOMWindowOuter& aWindow,
85                                   mozilla::HTMLEditor* aEditor);
86 
87   /**
88    *  Setup editor and related support objects
89    */
90   MOZ_CAN_RUN_SCRIPT nsresult SetupEditorOnWindow(nsPIDOMWindowOuter& aWindow);
91 
92   nsresult PrepareForEditing(nsPIDOMWindowOuter* aWindow);
93 
94   static void TimerCallback(nsITimer* aTimer, void* aClosure);
95   nsCOMPtr<nsITimer> mLoadBlankDocTimer;
96 
97   // progress load stuff
98   nsresult StartDocumentLoad(nsIWebProgress* aWebProgress,
99                              bool isToBeMadeEditable);
100   MOZ_CAN_RUN_SCRIPT_BOUNDARY
101   nsresult EndDocumentLoad(nsIWebProgress* aWebProgress, nsIChannel* aChannel,
102                            nsresult aStatus, bool isToBeMadeEditable);
103   nsresult StartPageLoad(nsIChannel* aChannel);
104   nsresult EndPageLoad(nsIWebProgress* aWebProgress, nsIChannel* aChannel,
105                        nsresult aStatus);
106 
107   bool IsProgressForTargetDocument(nsIWebProgress* aWebProgress);
108 
109   void RemoveEditorControllers(nsPIDOMWindowOuter* aWindow);
110   void RemoveWebProgressListener(nsPIDOMWindowOuter* aWindow);
111   void RestoreAnimationMode(nsPIDOMWindowOuter* aWindow);
112   void RemoveListenersAndControllers(nsPIDOMWindowOuter* aWindow,
113                                      mozilla::HTMLEditor* aHTMLEditor);
114 
115   /**
116    * Disable scripts and plugins in aDocShell.
117    */
118   nsresult DisableJSAndPlugins(nsPIDOMWindowInner* aWindow);
119 
120   /**
121    * Restore JS and plugins (enable/disable them) according to the state they
122    * were before the last call to disableJSAndPlugins.
123    */
124   nsresult RestoreJSAndPlugins(nsPIDOMWindowInner* aWindow);
125 
126  protected:
127   bool mDoneSetup;  // have we prepared for editing yet?
128 
129   // Used to prevent double creation of editor because nsIWebProgressListener
130   //  receives a STATE_STOP notification before the STATE_START
131   //  for our document, so we wait for the STATE_START, then STATE_STOP
132   //  before creating an editor
133   bool mCanCreateEditor;
134 
135   bool mInteractive;
136   bool mMakeWholeDocumentEditable;
137 
138   bool mDisabledJSAndPlugins;
139 
140   // True if scripts were enabled before the editor turned scripts
141   // off, otherwise false.
142   bool mScriptsEnabled;
143 
144   // True if plugins were enabled before the editor turned plugins
145   // off, otherwise false.
146   bool mPluginsEnabled;
147 
148   bool mProgressListenerRegistered;
149 
150   // The image animation mode before it was turned off.
151   uint16_t mImageAnimationMode;
152 
153   // THE REMAINING MEMBER VARIABLES WILL BECOME A SET WHEN WE EDIT
154   // MORE THAN ONE EDITOR PER EDITING SESSION
155   RefPtr<mozilla::ComposerCommandsUpdater> mComposerCommandsUpdater;
156 
157   // Save the editor type so we can create the editor after loading uri
158   nsCString mEditorType;
159   uint32_t mEditorFlags;
160   uint32_t mEditorStatus;
161   uint32_t mBaseCommandControllerId;
162   uint32_t mDocStateControllerId;
163   uint32_t mHTMLCommandControllerId;
164 
165   // Make sure the docshell we use is safe
166   nsWeakPtr mDocShell;
167 
168   // See if we can reuse an existing editor
169   nsWeakPtr mExistingEditor;
170 };
171 
172 #endif  // nsEditingSession_h__
173