Bug 630001, part2 - fix nsAccUtils::TextLength to not use nsIFrame::GetRenderedText...
[mozilla-central.git] / docshell / base / nsDocShell.h
blob5e8ac81b82fa21368bf890d3886a9903578a08ee
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=4 sw=4 et tw=80:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
15 * License.
17 * The Original Code is the Mozilla browser.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 1999
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Travis Bogard <travis@netscape.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef nsDocShell_h__
42 #define nsDocShell_h__
44 #include "nsIDOMNode.h"
45 #include "nsIDOMNodeList.h"
46 #include "nsIContentViewer.h"
47 #include "nsIPrefBranch.h"
48 #include "nsInterfaceHashtable.h"
49 #include "nsIScriptContext.h"
50 #include "nsITimer.h"
52 #include "nsIDocShell.h"
53 #include "nsIDocShellTreeItem.h"
54 #include "nsIDocShellTreeNode.h"
55 #include "nsIBaseWindow.h"
56 #include "nsIScrollable.h"
57 #include "nsITextScroll.h"
58 #include "nsIDocShellTreeOwner.h"
59 #include "nsIContentViewerContainer.h"
60 #include "nsIDeviceContext.h"
62 #include "nsDocLoader.h"
63 #include "nsIURILoader.h"
64 #include "nsIEditorDocShell.h"
66 #include "nsWeakReference.h"
68 // Local Includes
69 #include "nsDSURIContentListener.h"
70 #include "nsDocShellEditorData.h"
72 // Helper Classes
73 #include "nsCOMPtr.h"
74 #include "nsPoint.h" // mCurrent/mDefaultScrollbarPreferences
75 #include "nsString.h"
76 #include "nsAutoPtr.h"
77 #include "nsThreadUtils.h"
79 // Threshold value in ms for META refresh based redirects
80 #define REFRESH_REDIRECT_TIMER 15000
82 // Interfaces Needed
83 #include "nsIDocumentCharsetInfo.h"
84 #include "nsIDocCharset.h"
85 #include "nsIGlobalHistory2.h"
86 #include "nsIInterfaceRequestor.h"
87 #include "nsIInterfaceRequestorUtils.h"
88 #include "nsIPrompt.h"
89 #include "nsIRefreshURI.h"
90 #include "nsIScriptGlobalObject.h"
91 #include "nsIScriptGlobalObjectOwner.h"
92 #include "nsISHistory.h"
93 #include "nsILayoutHistoryState.h"
94 #include "nsIStringBundle.h"
95 #include "nsISupportsArray.h"
96 #include "nsIWebNavigation.h"
97 #include "nsIWebPageDescriptor.h"
98 #include "nsIWebProgressListener.h"
99 #include "nsISHContainer.h"
100 #include "nsIDocShellLoadInfo.h"
101 #include "nsIDocShellHistory.h"
102 #include "nsIURIFixup.h"
103 #include "nsIWebBrowserFind.h"
104 #include "nsIHttpChannel.h"
105 #include "nsDocShellTransferableHooks.h"
106 #include "nsIAuthPromptProvider.h"
107 #include "nsISecureBrowserUI.h"
108 #include "nsIObserver.h"
109 #include "nsDocShellLoadTypes.h"
110 #include "nsPIDOMEventTarget.h"
111 #include "nsILoadContext.h"
112 #include "nsIWidget.h"
113 #include "nsIWebShellServices.h"
114 #include "nsILinkHandler.h"
115 #include "nsIClipboardCommands.h"
116 #include "nsICommandManager.h"
117 #include "nsCRT.h"
119 class nsDocShell;
120 class nsIController;
121 class OnLinkClickEvent;
122 class nsIScrollableFrame;
124 /* load commands were moved to nsIDocShell.h */
125 /* load types were moved to nsDocShellLoadTypes.h */
127 /* internally used ViewMode types */
128 enum ViewMode {
129 viewNormal = 0x0,
130 viewSource = 0x1
133 //*****************************************************************************
134 //*** nsRefreshTimer
135 //*****************************************************************************
137 class nsRefreshTimer : public nsITimerCallback
139 public:
140 nsRefreshTimer();
142 NS_DECL_ISUPPORTS
143 NS_DECL_NSITIMERCALLBACK
145 PRInt32 GetDelay() { return mDelay ;}
147 nsRefPtr<nsDocShell> mDocShell;
148 nsCOMPtr<nsIURI> mURI;
149 PRInt32 mDelay;
150 PRPackedBool mRepeat;
151 PRPackedBool mMetaRefresh;
153 protected:
154 virtual ~nsRefreshTimer();
157 #define NS_ERROR_DOCSHELL_REQUEST_REJECTED NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_GENERAL,1001)
159 typedef enum {
160 eCharsetReloadInit,
161 eCharsetReloadRequested,
162 eCharsetReloadStopOrigional
163 } eCharsetReloadState;
165 //*****************************************************************************
166 //*** nsDocShell
167 //*****************************************************************************
169 class nsDocShell : public nsDocLoader,
170 public nsIDocShell,
171 public nsIDocShellTreeItem,
172 public nsIDocShellHistory,
173 public nsIWebNavigation,
174 public nsIBaseWindow,
175 public nsIScrollable,
176 public nsITextScroll,
177 public nsIDocCharset,
178 public nsIContentViewerContainer,
179 public nsIScriptGlobalObjectOwner,
180 public nsIRefreshURI,
181 public nsIWebProgressListener,
182 public nsIEditorDocShell,
183 public nsIWebPageDescriptor,
184 public nsIAuthPromptProvider,
185 public nsIObserver,
186 public nsILoadContext,
187 public nsIWebShellServices,
188 public nsILinkHandler,
189 public nsIClipboardCommands,
190 public nsIDocShell_MOZILLA_2_0_BRANCH
192 friend class nsDSURIContentListener;
194 public:
195 // Object Management
196 nsDocShell();
198 NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
200 virtual nsresult Init();
202 NS_DECL_ISUPPORTS_INHERITED
204 NS_DECL_NSIDOCSHELL
205 NS_DECL_NSIDOCSHELL_MOZILLA_2_0_BRANCH
206 NS_DECL_NSIDOCSHELLTREEITEM
207 NS_DECL_NSIDOCSHELLTREENODE
208 NS_DECL_NSIDOCSHELLHISTORY
209 NS_DECL_NSIWEBNAVIGATION
210 NS_DECL_NSIBASEWINDOW
211 NS_DECL_NSISCROLLABLE
212 NS_DECL_NSITEXTSCROLL
213 NS_DECL_NSIDOCCHARSET
214 NS_DECL_NSIINTERFACEREQUESTOR
215 NS_DECL_NSIWEBPROGRESSLISTENER
216 NS_DECL_NSIREFRESHURI
217 NS_DECL_NSICONTENTVIEWERCONTAINER
218 NS_DECL_NSIEDITORDOCSHELL
219 NS_DECL_NSIWEBPAGEDESCRIPTOR
220 NS_DECL_NSIAUTHPROMPTPROVIDER
221 NS_DECL_NSIOBSERVER
222 NS_DECL_NSILOADCONTEXT
223 NS_DECL_NSICLIPBOARDCOMMANDS
224 NS_DECL_NSIWEBSHELLSERVICES
226 NS_IMETHOD Stop() {
227 // Need this here because otherwise nsIWebNavigation::Stop
228 // overrides the docloader's Stop()
229 return nsDocLoader::Stop();
232 // Need to implement (and forward) nsISecurityEventSink, because
233 // nsIWebProgressListener has methods with identical names...
234 NS_FORWARD_NSISECURITYEVENTSINK(nsDocLoader::)
236 // nsILinkHandler
237 NS_IMETHOD OnLinkClick(nsIContent* aContent,
238 nsIURI* aURI,
239 const PRUnichar* aTargetSpec,
240 nsIInputStream* aPostDataStream = 0,
241 nsIInputStream* aHeadersDataStream = 0);
242 NS_IMETHOD OnLinkClickSync(nsIContent* aContent,
243 nsIURI* aURI,
244 const PRUnichar* aTargetSpec,
245 nsIInputStream* aPostDataStream = 0,
246 nsIInputStream* aHeadersDataStream = 0,
247 nsIDocShell** aDocShell = 0,
248 nsIRequest** aRequest = 0);
249 NS_IMETHOD OnOverLink(nsIContent* aContent,
250 nsIURI* aURI,
251 const PRUnichar* aTargetSpec);
252 NS_IMETHOD OnLeaveLink();
254 nsDocShellInfoLoadType ConvertLoadTypeToDocShellLoadInfo(PRUint32 aLoadType);
255 PRUint32 ConvertDocShellLoadInfoToLoadType(nsDocShellInfoLoadType aDocShellLoadType);
257 // nsIScriptGlobalObjectOwner methods
258 virtual nsIScriptGlobalObject* GetScriptGlobalObject();
260 // Restores a cached presentation from history (mLSHE).
261 // This method swaps out the content viewer and simulates loads for
262 // subframes. It then simulates the completion of the toplevel load.
263 nsresult RestoreFromHistory();
265 // Perform a URI load from a refresh timer. This is just like the
266 // ForceRefreshURI method on nsIRefreshURI, but makes sure to take
267 // the timer involved out of mRefreshURIList if it's there.
268 // aTimer must not be null.
269 nsresult ForceRefreshURIFromTimer(nsIURI * aURI, PRInt32 aDelay,
270 PRBool aMetaRefresh, nsITimer* aTimer);
272 friend class OnLinkClickEvent;
274 // We need dummy OnLocationChange in some cases to update the UI.
275 void FireDummyOnLocationChange()
277 FireOnLocationChange(this, nsnull, mCurrentURI);
280 nsresult HistoryTransactionRemoved(PRInt32 aIndex);
281 protected:
282 // Object Management
283 virtual ~nsDocShell();
284 virtual void DestroyChildren();
286 // Content Viewer Management
287 NS_IMETHOD EnsureContentViewer();
288 // aPrincipal can be passed in if the caller wants. If null is
289 // passed in, the about:blank principal will end up being used.
290 nsresult CreateAboutBlankContentViewer(nsIPrincipal* aPrincipal,
291 nsIURI* aBaseURI,
292 PRBool aTryToSaveOldPresentation = PR_TRUE);
293 NS_IMETHOD CreateContentViewer(const char * aContentType,
294 nsIRequest * request, nsIStreamListener ** aContentHandler);
295 NS_IMETHOD NewContentViewerObj(const char * aContentType,
296 nsIRequest * request, nsILoadGroup * aLoadGroup,
297 nsIStreamListener ** aContentHandler, nsIContentViewer ** aViewer);
298 NS_IMETHOD SetupNewViewer(nsIContentViewer * aNewViewer);
300 void SetupReferrerFromChannel(nsIChannel * aChannel);
302 NS_IMETHOD GetEldestPresContext(nsPresContext** aPresContext);
304 // Get the principal that we'll set on the channel if we're inheriting. If
305 // aConsiderCurrentDocument is true, we try to use the current document if
306 // at all possible. If that fails, we fall back on the parent document.
307 // If that fails too, we force creation of a content viewer and use the
308 // resulting principal. If aConsiderCurrentDocument is false, we just look
309 // at the parent.
310 nsIPrincipal* GetInheritedPrincipal(PRBool aConsiderCurrentDocument);
312 // True if when loading aURI into this docshell, the channel should look
313 // for an appropriate application cache.
314 PRBool ShouldCheckAppCache(nsIURI * aURI);
316 // Actually open a channel and perform a URI load. Note: whatever owner is
317 // passed to this function will be set on the channel. Callers who wish to
318 // not have an owner on the channel should just pass null.
319 virtual nsresult DoURILoad(nsIURI * aURI,
320 nsIURI * aReferrer,
321 PRBool aSendReferrer,
322 nsISupports * aOwner,
323 const char * aTypeHint,
324 nsIInputStream * aPostData,
325 nsIInputStream * aHeadersData,
326 PRBool firstParty,
327 nsIDocShell ** aDocShell,
328 nsIRequest ** aRequest,
329 PRBool aIsNewWindowTarget,
330 PRBool aBypassClassifier,
331 PRBool aForceAllowCookies);
332 NS_IMETHOD AddHeadersToChannel(nsIInputStream * aHeadersData,
333 nsIChannel * aChannel);
334 virtual nsresult DoChannelLoad(nsIChannel * aChannel,
335 nsIURILoader * aURILoader,
336 PRBool aBypassClassifier);
338 nsresult ScrollIfAnchor(nsIURI * aURI, PRBool * aWasAnchor,
339 PRUint32 aLoadType, PRBool * aDoHashchange);
341 // Tries to stringify a given variant by converting it to JSON. This only
342 // works if the variant is backed by a JSVal.
343 nsresult StringifyJSValVariant(nsIVariant *aData, nsAString &aResult);
345 // Returns PR_TRUE if would have called FireOnLocationChange,
346 // but did not because aFireOnLocationChange was false on entry.
347 // In this case it is the caller's responsibility to ensure
348 // FireOnLocationChange is called.
349 // In all other cases PR_FALSE is returned.
350 PRBool OnLoadingSite(nsIChannel * aChannel,
351 PRBool aFireOnLocationChange,
352 PRBool aAddToGlobalHistory = PR_TRUE);
354 // Returns PR_TRUE if would have called FireOnLocationChange,
355 // but did not because aFireOnLocationChange was false on entry.
356 // In this case it is the caller's responsibility to ensure
357 // FireOnLocationChange is called.
358 // In all other cases PR_FALSE is returned.
359 // Either aChannel or aOwner must be null. If aChannel is
360 // present, the owner should be gotten from it.
361 PRBool OnNewURI(nsIURI * aURI, nsIChannel * aChannel, nsISupports* aOwner,
362 PRUint32 aLoadType,
363 PRBool aFireOnLocationChange,
364 PRBool aAddToGlobalHistory = PR_TRUE);
366 virtual void SetReferrerURI(nsIURI * aURI);
368 // Session History
369 virtual PRBool ShouldAddToSessionHistory(nsIURI * aURI);
370 // Either aChannel or aOwner must be null. If aChannel is
371 // present, the owner should be gotten from it.
372 virtual nsresult AddToSessionHistory(nsIURI * aURI, nsIChannel * aChannel,
373 nsISupports* aOwner,
374 nsISHEntry ** aNewEntry);
375 nsresult DoAddChildSHEntry(nsISHEntry* aNewEntry, PRInt32 aChildOffset);
377 NS_IMETHOD LoadHistoryEntry(nsISHEntry * aEntry, PRUint32 aLoadType);
378 NS_IMETHOD PersistLayoutHistoryState();
380 // Clone a session history tree for subframe navigation.
381 // The tree rooted at |aSrcEntry| will be cloned into |aDestEntry|, except
382 // for the entry with id |aCloneID|, which will be replaced with
383 // |aReplaceEntry|. |aSrcShell| is a (possibly null) docshell which
384 // corresponds to |aSrcEntry| via its mLSHE or mOHE pointers, and will
385 // have that pointer updated to point to the cloned history entry.
386 static nsresult CloneAndReplace(nsISHEntry *aSrcEntry,
387 nsDocShell *aSrcShell,
388 PRUint32 aCloneID,
389 nsISHEntry *aReplaceEntry,
390 nsISHEntry **aDestEntry);
392 // Child-walking callback for CloneAndReplace
393 static nsresult CloneAndReplaceChild(nsISHEntry *aEntry,
394 nsDocShell *aShell,
395 PRInt32 aChildIndex, void *aData);
397 nsresult GetRootSessionHistory(nsISHistory ** aReturn);
398 nsresult GetHttpChannel(nsIChannel * aChannel, nsIHttpChannel ** aReturn);
399 PRBool ShouldDiscardLayoutState(nsIHttpChannel * aChannel);
401 // Determine whether this docshell corresponds to the given history entry,
402 // via having a pointer to it in mOSHE or mLSHE.
403 PRBool HasHistoryEntry(nsISHEntry *aEntry) const
405 return aEntry && (aEntry == mOSHE || aEntry == mLSHE);
408 // Update any pointers (mOSHE or mLSHE) to aOldEntry to point to aNewEntry
409 void SwapHistoryEntries(nsISHEntry *aOldEntry, nsISHEntry *aNewEntry);
411 // Call this method to swap in a new history entry to m[OL]SHE, rather than
412 // setting it directly. This completes the navigation in all docshells
413 // in the case of a subframe navigation.
414 void SetHistoryEntry(nsCOMPtr<nsISHEntry> *aPtr, nsISHEntry *aEntry);
416 // Child-walking callback for SetHistoryEntry
417 static nsresult SetChildHistoryEntry(nsISHEntry *aEntry,
418 nsDocShell *aShell,
419 PRInt32 aEntryIndex, void *aData);
421 // Callback prototype for WalkHistoryEntries.
422 // aEntry is the child history entry, aShell is its corresponding docshell,
423 // aChildIndex is the child's index in its parent entry, and aData is
424 // the opaque pointer passed to WalkHistoryEntries.
425 typedef nsresult (*WalkHistoryEntriesFunc)(nsISHEntry *aEntry,
426 nsDocShell *aShell,
427 PRInt32 aChildIndex,
428 void *aData);
430 // For each child of aRootEntry, find the corresponding docshell which is
431 // a child of aRootShell, and call aCallback. The opaque pointer aData
432 // is passed to the callback.
433 static nsresult WalkHistoryEntries(nsISHEntry *aRootEntry,
434 nsDocShell *aRootShell,
435 WalkHistoryEntriesFunc aCallback,
436 void *aData);
438 // overridden from nsDocLoader, this provides more information than the
439 // normal OnStateChange with flags STATE_REDIRECTING
440 virtual void OnRedirectStateChange(nsIChannel* aOldChannel,
441 nsIChannel* aNewChannel,
442 PRUint32 aRedirectFlags,
443 PRUint32 aStateFlags);
446 * Helper function that determines if channel is an HTTP POST.
448 * @param aChannel
449 * The channel to test
451 * @return True iff channel is an HTTP post.
453 bool ChannelIsPost(nsIChannel* aChannel);
456 * Helper function that finds the last URI and its transition flags for a
457 * channel.
459 * This method first checks the channel's property bag to see if previous
460 * info has been saved. If not, it gives back the referrer of the channel.
462 * @param aChannel
463 * The channel we are transitioning to
464 * @param aURI
465 * Output parameter with the previous URI, not addref'd
466 * @param aChannelRedirectFlags
467 * If a redirect, output parameter with the previous redirect flags
468 * from nsIChannelEventSink
470 void ExtractLastVisit(nsIChannel* aChannel,
471 nsIURI** aURI,
472 PRUint32* aChannelRedirectFlags);
475 * Helper function that caches a URI and a transition for saving later.
477 * @param aChannel
478 * Channel that will have these properties saved
479 * @param aURI
480 * The URI to save for later
481 * @param aChannelRedirectFlags
482 * The nsIChannelEventSink redirect flags to save for later
484 void SaveLastVisit(nsIChannel* aChannel,
485 nsIURI* aURI,
486 PRUint32 aChannelRedirectFlags);
489 * Helper function for adding a URI visit using IHistory. If IHistory is
490 * not available, the method tries nsIGlobalHistory2.
492 * The IHistory API maintains chains of visits, tracking both HTTP referrers
493 * and redirects for a user session. VisitURI requires the current URI and
494 * the previous URI in the chain.
496 * Visits can be saved either during a redirect or when the request has
497 * reached its final destination. The previous URI in the visit may be
498 * from another redirect or it may be the referrer.
500 * @pre aURI is not null.
502 * @param aURI
503 * The URI that was just visited
504 * @param aReferrerURI
505 * The referrer URI of this request
506 * @param aPreviousURI
507 * The previous URI of this visit (may be the same as aReferrerURI)
508 * @param aChannelRedirectFlags
509 * For redirects, the redirect flags from nsIChannelEventSink
510 * (0 otherwise)
512 void AddURIVisit(nsIURI* aURI,
513 nsIURI* aReferrerURI,
514 nsIURI* aPreviousURI,
515 PRUint32 aChannelRedirectFlags);
517 // Helper Routines
518 nsresult ConfirmRepost(PRBool * aRepost);
519 NS_IMETHOD GetPromptAndStringBundle(nsIPrompt ** aPrompt,
520 nsIStringBundle ** aStringBundle);
521 NS_IMETHOD GetChildOffset(nsIDOMNode * aChild, nsIDOMNode * aParent,
522 PRInt32 * aOffset);
523 nsIScrollableFrame* GetRootScrollFrame();
524 NS_IMETHOD EnsureScriptEnvironment();
525 NS_IMETHOD EnsureEditorData();
526 nsresult EnsureTransferableHookData();
527 NS_IMETHOD EnsureFind();
528 nsresult RefreshURIFromQueue();
529 NS_IMETHOD DisplayLoadError(nsresult aError, nsIURI *aURI,
530 const PRUnichar *aURL,
531 nsIChannel* aFailedChannel = nsnull);
532 NS_IMETHOD LoadErrorPage(nsIURI *aURI, const PRUnichar *aURL,
533 const char *aErrorPage,
534 const PRUnichar *aErrorType,
535 const PRUnichar *aDescription,
536 const char *aCSSClass,
537 nsIChannel* aFailedChannel);
538 PRBool IsNavigationAllowed(PRBool aDisplayPrintErrorDialog = PR_TRUE);
539 PRBool IsPrintingOrPP(PRBool aDisplayErrorDialog = PR_TRUE);
541 nsresult SetBaseUrlForWyciwyg(nsIContentViewer * aContentViewer);
543 static inline PRUint32
544 PRTimeToSeconds(PRTime t_usec)
546 PRTime usec_per_sec;
547 PRUint32 t_sec;
548 LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
549 LL_DIV(t_usec, t_usec, usec_per_sec);
550 LL_L2I(t_sec, t_usec);
551 return t_sec;
554 PRBool IsFrame();
557 // Helper method that is called when a new document (including any
558 // sub-documents - ie. frames) has been completely loaded.
560 virtual nsresult EndPageLoad(nsIWebProgress * aProgress,
561 nsIChannel * aChannel,
562 nsresult aResult);
564 // Sets the current document's pending state object to the given SHEntry's
565 // state object. The pending state object is eventually given to the page
566 // in the PopState event.
567 nsresult SetDocPendingStateObj(nsISHEntry *shEntry);
569 nsresult CheckLoadingPermissions();
571 // Security checks to prevent frameset spoofing. See comments at
572 // implementation sites.
573 static PRBool CanAccessItem(nsIDocShellTreeItem* aTargetItem,
574 nsIDocShellTreeItem* aAccessingItem,
575 PRBool aConsiderOpener = PR_TRUE);
576 static PRBool ValidateOrigin(nsIDocShellTreeItem* aOriginTreeItem,
577 nsIDocShellTreeItem* aTargetTreeItem);
579 // Returns PR_TRUE if would have called FireOnLocationChange,
580 // but did not because aFireOnLocationChange was false on entry.
581 // In this case it is the caller's responsibility to ensure
582 // FireOnLocationChange is called.
583 // In all other cases PR_FALSE is returned.
584 PRBool SetCurrentURI(nsIURI *aURI, nsIRequest *aRequest,
585 PRBool aFireOnLocationChange);
587 // The following methods deal with saving and restoring content viewers
588 // in session history.
590 // mContentViewer points to the current content viewer associated with
591 // this docshell. When loading a new document, the content viewer is
592 // either destroyed or stored into a session history entry. To make sure
593 // that destruction happens in a controlled fashion, a given content viewer
594 // is always owned in exactly one of these ways:
595 // 1) The content viewer is active and owned by a docshell's
596 // mContentViewer.
597 // 2) The content viewer is still being displayed while we begin loading
598 // a new document. The content viewer is owned by the _new_
599 // content viewer's mPreviousViewer, and has a pointer to the
600 // nsISHEntry where it will eventually be stored. The content viewer
601 // has been close()d by the docshell, which detaches the document from
602 // the window object.
603 // 3) The content viewer is cached in session history. The nsISHEntry
604 // has the only owning reference to the content viewer. The viewer
605 // has released its nsISHEntry pointer to prevent circular ownership.
607 // When restoring a content viewer from session history, open() is called
608 // to reattach the document to the window object. The content viewer is
609 // then placed into mContentViewer and removed from the history entry.
610 // (mContentViewer is put into session history as described above, if
611 // applicable).
613 // Determines whether we can safely cache the current mContentViewer in
614 // session history. This checks a number of factors such as cache policy,
615 // pending requests, and unload handlers.
616 // |aLoadType| should be the load type that will replace the current
617 // presentation. |aNewRequest| should be the request for the document to
618 // be loaded in place of the current document, or null if such a request
619 // has not been created yet. |aNewDocument| should be the document that will
620 // replace the current document.
621 PRBool CanSavePresentation(PRUint32 aLoadType,
622 nsIRequest *aNewRequest,
623 nsIDocument *aNewDocument);
625 // Captures the state of the supporting elements of the presentation
626 // (the "window" object, docshell tree, meta-refresh loads, and security
627 // state) and stores them on |mOSHE|.
628 nsresult CaptureState();
630 // Begin the toplevel restore process for |aSHEntry|.
631 // This simulates a channel open, and defers the real work until
632 // RestoreFromHistory is called from a PLEvent.
633 nsresult RestorePresentation(nsISHEntry *aSHEntry, PRBool *aRestoring);
635 // Call BeginRestore(nsnull, PR_FALSE) for each child of this shell.
636 nsresult BeginRestoreChildren();
638 // Method to get our current position and size without flushing
639 void DoGetPositionAndSize(PRInt32 * x, PRInt32 * y, PRInt32 * cx,
640 PRInt32 * cy);
642 // Check whether aURI should inherit our security context
643 static nsresult URIInheritsSecurityContext(nsIURI* aURI, PRBool* aResult);
645 // Check whether aURI is a URI_IS_LOCAL_FILE or not
646 static PRBool URIIsLocalFile(nsIURI *aURI);
648 // Check whether aURI is about:blank
649 static PRBool IsAboutBlank(nsIURI* aURI);
651 // Call this when a URI load is handed to us (via OnLinkClick or
652 // InternalLoad). This makes sure that we're not inside unload, or that if
653 // we are it's still OK to load this URI.
654 PRBool IsOKToLoadURI(nsIURI* aURI);
656 void ReattachEditorToWindow(nsISHEntry *aSHEntry);
658 nsresult GetSessionStorageForURI(nsIURI* aURI,
659 const nsSubstring& aDocumentURI,
660 PRBool create,
661 nsIDOMStorage** aStorage);
663 // helpers for executing commands
664 nsresult GetControllerForCommand(const char *inCommand,
665 nsIController** outController);
666 nsresult IsCommandEnabled(const char * inCommand, PRBool* outEnabled);
667 nsresult DoCommand(const char * inCommand);
668 nsresult EnsureCommandHandler();
670 nsIChannel* GetCurrentDocChannel();
671 protected:
672 // Override the parent setter from nsDocLoader
673 virtual nsresult SetDocLoaderParent(nsDocLoader * aLoader);
675 void ClearFrameHistory(nsISHEntry* aEntry);
677 // Event type dispatched by RestorePresentation
678 class RestorePresentationEvent : public nsRunnable {
679 public:
680 NS_DECL_NSIRUNNABLE
681 RestorePresentationEvent(nsDocShell *ds) : mDocShell(ds) {}
682 void Revoke() { mDocShell = nsnull; }
683 private:
684 nsRefPtr<nsDocShell> mDocShell;
687 // hash of session storages, keyed by domain
688 nsInterfaceHashtable<nsCStringHashKey, nsIDOMStorage> mStorages;
690 // Dimensions of the docshell
691 nsIntRect mBounds;
692 nsString mName;
693 nsString mTitle;
696 * Content-Type Hint of the most-recently initiated load. Used for
697 * session history entries.
699 nsCString mContentTypeHint;
700 nsIntPoint mDefaultScrollbarPref; // persistent across doc loads
702 nsCOMPtr<nsISupportsArray> mRefreshURIList;
703 nsCOMPtr<nsISupportsArray> mSavedRefreshURIList;
704 nsRefPtr<nsDSURIContentListener> mContentListener;
705 nsCOMPtr<nsIContentViewer> mContentViewer;
706 nsCOMPtr<nsIDocumentCharsetInfo> mDocumentCharsetInfo;
707 nsCOMPtr<nsIWidget> mParentWidget;
708 nsCOMPtr<nsIPrefBranch> mPrefs;
710 // mCurrentURI should be marked immutable on set if possible.
711 nsCOMPtr<nsIURI> mCurrentURI;
712 nsCOMPtr<nsIURI> mReferrerURI;
713 nsCOMPtr<nsIScriptGlobalObject> mScriptGlobal;
714 nsCOMPtr<nsISHistory> mSessionHistory;
715 nsCOMPtr<nsIGlobalHistory2> mGlobalHistory;
716 nsCOMPtr<nsIWebBrowserFind> mFind;
717 nsCOMPtr<nsICommandManager> mCommandManager;
718 // Reference to the SHEntry for this docshell until the page is destroyed.
719 // Somebody give me better name
720 nsCOMPtr<nsISHEntry> mOSHE;
721 // Reference to the SHEntry for this docshell until the page is loaded
722 // Somebody give me better name
723 nsCOMPtr<nsISHEntry> mLSHE;
725 // Holds a weak pointer to a RestorePresentationEvent object if any that
726 // holds a weak pointer back to us. We use this pointer to possibly revoke
727 // the event whenever necessary.
728 nsRevocableEventPtr<RestorePresentationEvent> mRestorePresentationEvent;
730 // Editor data, if this document is designMode or contentEditable.
731 nsAutoPtr<nsDocShellEditorData> mEditorData;
733 // Transferable hooks/callbacks
734 nsCOMPtr<nsIClipboardDragDropHookList> mTransferableHookData;
736 // Secure browser UI object
737 nsCOMPtr<nsISecureBrowserUI> mSecurityUI;
739 // The URI we're currently loading. This is only relevant during the
740 // firing of a pagehide/unload. The caller of FirePageHideNotification()
741 // is responsible for setting it and unsetting it. It may be null if the
742 // pagehide/unload is happening for some reason other than just loading a
743 // new URI.
744 nsCOMPtr<nsIURI> mLoadingURI;
746 // Set in LoadErrorPage from the method argument and used later
747 // in CreateContentViewer. We have to delay an shistory entry creation
748 // for which these objects are needed.
749 nsCOMPtr<nsIURI> mFailedURI;
750 nsCOMPtr<nsIChannel> mFailedChannel;
751 PRUint32 mFailedLoadType;
753 // WEAK REFERENCES BELOW HERE.
754 // Note these are intentionally not addrefd. Doing so will create a cycle.
755 // For that reasons don't use nsCOMPtr.
757 nsIDocShellTreeOwner * mTreeOwner; // Weak Reference
758 nsPIDOMEventTarget * mChromeEventHandler; //Weak Reference
760 eCharsetReloadState mCharsetReloadState;
762 // Offset in the parent's child list.
763 // -1 if the docshell is added dynamically to the parent shell.
764 PRUint32 mChildOffset;
765 PRUint32 mBusyFlags;
766 PRUint32 mAppType;
767 PRUint32 mLoadType;
769 PRInt32 mMarginWidth;
770 PRInt32 mMarginHeight;
772 // This can either be a content docshell or a chrome docshell. After
773 // Create() is called, the type is not expected to change.
774 PRInt32 mItemType;
776 // Index into the SHTransaction list, indicating the previous and current
777 // transaction at the time that this DocShell begins to load
778 PRInt32 mPreviousTransIndex;
779 PRInt32 mLoadedTransIndex;
781 PRPackedBool mAllowSubframes;
782 PRPackedBool mAllowPlugins;
783 PRPackedBool mAllowJavascript;
784 PRPackedBool mAllowMetaRedirects;
785 PRPackedBool mAllowImages;
786 PRPackedBool mAllowDNSPrefetch;
787 PRPackedBool mCreatingDocument; // (should be) debugging only
788 PRPackedBool mUseErrorPages;
789 PRPackedBool mObserveErrorPages;
790 PRPackedBool mAllowAuth;
791 PRPackedBool mAllowKeywordFixup;
792 PRPackedBool mIsOffScreenBrowser;
793 PRPackedBool mIsActive;
794 PRPackedBool mIsAppTab;
795 PRPackedBool mUseGlobalHistory;
797 // This boolean is set to true right before we fire pagehide and generally
798 // unset when we embed a new content viewer. While it's true no navigation
799 // is allowed in this docshell.
800 PRPackedBool mFiredUnloadEvent;
802 // this flag is for bug #21358. a docshell may load many urls
803 // which don't result in new documents being created (i.e. a new
804 // content viewer) we want to make sure we don't call a on load
805 // event more than once for a given content viewer.
806 PRPackedBool mEODForCurrentDocument;
807 PRPackedBool mURIResultedInDocument;
809 PRPackedBool mIsBeingDestroyed;
811 PRPackedBool mIsExecutingOnLoadHandler;
813 // Indicates that a DocShell in this "docshell tree" is printing
814 PRPackedBool mIsPrintingOrPP;
816 // Indicates to CreateContentViewer() that it is safe to cache the old
817 // presentation of the page, and to SetupNewViewer() that the old viewer
818 // should be passed a SHEntry to save itself into.
819 PRPackedBool mSavingOldViewer;
821 // @see nsIDocShellHistory::createdDynamically
822 PRPackedBool mDynamicallyCreated;
823 #ifdef DEBUG
824 PRPackedBool mInEnsureScriptEnv;
825 #endif
826 PRUint64 mHistoryID;
828 static nsIURIFixup *sURIFixup;
830 #ifdef DEBUG
831 private:
832 // We're counting the number of |nsDocShells| to help find leaks
833 static unsigned long gNumberOfDocShells;
834 #endif /* DEBUG */
836 public:
837 class InterfaceRequestorProxy : public nsIInterfaceRequestor {
838 public:
839 InterfaceRequestorProxy(nsIInterfaceRequestor* p);
840 virtual ~InterfaceRequestorProxy();
841 NS_DECL_ISUPPORTS
842 NS_DECL_NSIINTERFACEREQUESTOR
844 protected:
845 InterfaceRequestorProxy() {}
846 nsWeakPtr mWeakPtr;
850 #endif /* nsDocShell_h__ */