Bug 634435: Add a property to expose the current pushState-state. r=jlebar a=beltzner
[mozilla-central.git] / docshell / base / nsDocShell.h
blob81cd09fb7a9db72bc61fb5663dadcda926e62ba6
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 // If aCloneChildren is true then the children of the entry with id
387 // |aCloneID| will be cloned into |aReplaceEntry|.
388 static nsresult CloneAndReplace(nsISHEntry *aSrcEntry,
389 nsDocShell *aSrcShell,
390 PRUint32 aCloneID,
391 nsISHEntry *aReplaceEntry,
392 PRBool aCloneChildren,
393 nsISHEntry **aDestEntry);
395 // Child-walking callback for CloneAndReplace
396 static nsresult CloneAndReplaceChild(nsISHEntry *aEntry,
397 nsDocShell *aShell,
398 PRInt32 aChildIndex, void *aData);
400 nsresult GetRootSessionHistory(nsISHistory ** aReturn);
401 nsresult GetHttpChannel(nsIChannel * aChannel, nsIHttpChannel ** aReturn);
402 PRBool ShouldDiscardLayoutState(nsIHttpChannel * aChannel);
404 // Determine whether this docshell corresponds to the given history entry,
405 // via having a pointer to it in mOSHE or mLSHE.
406 PRBool HasHistoryEntry(nsISHEntry *aEntry) const
408 return aEntry && (aEntry == mOSHE || aEntry == mLSHE);
411 // Update any pointers (mOSHE or mLSHE) to aOldEntry to point to aNewEntry
412 void SwapHistoryEntries(nsISHEntry *aOldEntry, nsISHEntry *aNewEntry);
414 // Call this method to swap in a new history entry to m[OL]SHE, rather than
415 // setting it directly. This completes the navigation in all docshells
416 // in the case of a subframe navigation.
417 void SetHistoryEntry(nsCOMPtr<nsISHEntry> *aPtr, nsISHEntry *aEntry);
419 // Child-walking callback for SetHistoryEntry
420 static nsresult SetChildHistoryEntry(nsISHEntry *aEntry,
421 nsDocShell *aShell,
422 PRInt32 aEntryIndex, void *aData);
424 // Callback prototype for WalkHistoryEntries.
425 // aEntry is the child history entry, aShell is its corresponding docshell,
426 // aChildIndex is the child's index in its parent entry, and aData is
427 // the opaque pointer passed to WalkHistoryEntries.
428 typedef nsresult (*WalkHistoryEntriesFunc)(nsISHEntry *aEntry,
429 nsDocShell *aShell,
430 PRInt32 aChildIndex,
431 void *aData);
433 // For each child of aRootEntry, find the corresponding docshell which is
434 // a child of aRootShell, and call aCallback. The opaque pointer aData
435 // is passed to the callback.
436 static nsresult WalkHistoryEntries(nsISHEntry *aRootEntry,
437 nsDocShell *aRootShell,
438 WalkHistoryEntriesFunc aCallback,
439 void *aData);
441 // overridden from nsDocLoader, this provides more information than the
442 // normal OnStateChange with flags STATE_REDIRECTING
443 virtual void OnRedirectStateChange(nsIChannel* aOldChannel,
444 nsIChannel* aNewChannel,
445 PRUint32 aRedirectFlags,
446 PRUint32 aStateFlags);
449 * Helper function that determines if channel is an HTTP POST.
451 * @param aChannel
452 * The channel to test
454 * @return True iff channel is an HTTP post.
456 bool ChannelIsPost(nsIChannel* aChannel);
459 * Helper function that finds the last URI and its transition flags for a
460 * channel.
462 * This method first checks the channel's property bag to see if previous
463 * info has been saved. If not, it gives back the referrer of the channel.
465 * @param aChannel
466 * The channel we are transitioning to
467 * @param aURI
468 * Output parameter with the previous URI, not addref'd
469 * @param aChannelRedirectFlags
470 * If a redirect, output parameter with the previous redirect flags
471 * from nsIChannelEventSink
473 void ExtractLastVisit(nsIChannel* aChannel,
474 nsIURI** aURI,
475 PRUint32* aChannelRedirectFlags);
478 * Helper function that caches a URI and a transition for saving later.
480 * @param aChannel
481 * Channel that will have these properties saved
482 * @param aURI
483 * The URI to save for later
484 * @param aChannelRedirectFlags
485 * The nsIChannelEventSink redirect flags to save for later
487 void SaveLastVisit(nsIChannel* aChannel,
488 nsIURI* aURI,
489 PRUint32 aChannelRedirectFlags);
492 * Helper function for adding a URI visit using IHistory. If IHistory is
493 * not available, the method tries nsIGlobalHistory2.
495 * The IHistory API maintains chains of visits, tracking both HTTP referrers
496 * and redirects for a user session. VisitURI requires the current URI and
497 * the previous URI in the chain.
499 * Visits can be saved either during a redirect or when the request has
500 * reached its final destination. The previous URI in the visit may be
501 * from another redirect or it may be the referrer.
503 * @pre aURI is not null.
505 * @param aURI
506 * The URI that was just visited
507 * @param aReferrerURI
508 * The referrer URI of this request
509 * @param aPreviousURI
510 * The previous URI of this visit (may be the same as aReferrerURI)
511 * @param aChannelRedirectFlags
512 * For redirects, the redirect flags from nsIChannelEventSink
513 * (0 otherwise)
515 void AddURIVisit(nsIURI* aURI,
516 nsIURI* aReferrerURI,
517 nsIURI* aPreviousURI,
518 PRUint32 aChannelRedirectFlags);
520 // Helper Routines
521 nsresult ConfirmRepost(PRBool * aRepost);
522 NS_IMETHOD GetPromptAndStringBundle(nsIPrompt ** aPrompt,
523 nsIStringBundle ** aStringBundle);
524 NS_IMETHOD GetChildOffset(nsIDOMNode * aChild, nsIDOMNode * aParent,
525 PRInt32 * aOffset);
526 nsIScrollableFrame* GetRootScrollFrame();
527 NS_IMETHOD EnsureScriptEnvironment();
528 NS_IMETHOD EnsureEditorData();
529 nsresult EnsureTransferableHookData();
530 NS_IMETHOD EnsureFind();
531 nsresult RefreshURIFromQueue();
532 NS_IMETHOD DisplayLoadError(nsresult aError, nsIURI *aURI,
533 const PRUnichar *aURL,
534 nsIChannel* aFailedChannel = nsnull);
535 NS_IMETHOD LoadErrorPage(nsIURI *aURI, const PRUnichar *aURL,
536 const char *aErrorPage,
537 const PRUnichar *aErrorType,
538 const PRUnichar *aDescription,
539 const char *aCSSClass,
540 nsIChannel* aFailedChannel);
541 PRBool IsNavigationAllowed(PRBool aDisplayPrintErrorDialog = PR_TRUE);
542 PRBool IsPrintingOrPP(PRBool aDisplayErrorDialog = PR_TRUE);
544 nsresult SetBaseUrlForWyciwyg(nsIContentViewer * aContentViewer);
546 static inline PRUint32
547 PRTimeToSeconds(PRTime t_usec)
549 PRTime usec_per_sec;
550 PRUint32 t_sec;
551 LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
552 LL_DIV(t_usec, t_usec, usec_per_sec);
553 LL_L2I(t_sec, t_usec);
554 return t_sec;
557 PRBool IsFrame();
560 // Helper method that is called when a new document (including any
561 // sub-documents - ie. frames) has been completely loaded.
563 virtual nsresult EndPageLoad(nsIWebProgress * aProgress,
564 nsIChannel * aChannel,
565 nsresult aResult);
567 // Sets the current document's current state object to the given SHEntry's
568 // state object. The current state object is eventually given to the page
569 // in the PopState event.
570 nsresult SetDocCurrentStateObj(nsISHEntry *shEntry);
572 nsresult CheckLoadingPermissions();
574 // Security checks to prevent frameset spoofing. See comments at
575 // implementation sites.
576 static PRBool CanAccessItem(nsIDocShellTreeItem* aTargetItem,
577 nsIDocShellTreeItem* aAccessingItem,
578 PRBool aConsiderOpener = PR_TRUE);
579 static PRBool ValidateOrigin(nsIDocShellTreeItem* aOriginTreeItem,
580 nsIDocShellTreeItem* aTargetTreeItem);
582 // Returns PR_TRUE if would have called FireOnLocationChange,
583 // but did not because aFireOnLocationChange was false on entry.
584 // In this case it is the caller's responsibility to ensure
585 // FireOnLocationChange is called.
586 // In all other cases PR_FALSE is returned.
587 PRBool SetCurrentURI(nsIURI *aURI, nsIRequest *aRequest,
588 PRBool aFireOnLocationChange);
590 // The following methods deal with saving and restoring content viewers
591 // in session history.
593 // mContentViewer points to the current content viewer associated with
594 // this docshell. When loading a new document, the content viewer is
595 // either destroyed or stored into a session history entry. To make sure
596 // that destruction happens in a controlled fashion, a given content viewer
597 // is always owned in exactly one of these ways:
598 // 1) The content viewer is active and owned by a docshell's
599 // mContentViewer.
600 // 2) The content viewer is still being displayed while we begin loading
601 // a new document. The content viewer is owned by the _new_
602 // content viewer's mPreviousViewer, and has a pointer to the
603 // nsISHEntry where it will eventually be stored. The content viewer
604 // has been close()d by the docshell, which detaches the document from
605 // the window object.
606 // 3) The content viewer is cached in session history. The nsISHEntry
607 // has the only owning reference to the content viewer. The viewer
608 // has released its nsISHEntry pointer to prevent circular ownership.
610 // When restoring a content viewer from session history, open() is called
611 // to reattach the document to the window object. The content viewer is
612 // then placed into mContentViewer and removed from the history entry.
613 // (mContentViewer is put into session history as described above, if
614 // applicable).
616 // Determines whether we can safely cache the current mContentViewer in
617 // session history. This checks a number of factors such as cache policy,
618 // pending requests, and unload handlers.
619 // |aLoadType| should be the load type that will replace the current
620 // presentation. |aNewRequest| should be the request for the document to
621 // be loaded in place of the current document, or null if such a request
622 // has not been created yet. |aNewDocument| should be the document that will
623 // replace the current document.
624 PRBool CanSavePresentation(PRUint32 aLoadType,
625 nsIRequest *aNewRequest,
626 nsIDocument *aNewDocument);
628 // Captures the state of the supporting elements of the presentation
629 // (the "window" object, docshell tree, meta-refresh loads, and security
630 // state) and stores them on |mOSHE|.
631 nsresult CaptureState();
633 // Begin the toplevel restore process for |aSHEntry|.
634 // This simulates a channel open, and defers the real work until
635 // RestoreFromHistory is called from a PLEvent.
636 nsresult RestorePresentation(nsISHEntry *aSHEntry, PRBool *aRestoring);
638 // Call BeginRestore(nsnull, PR_FALSE) for each child of this shell.
639 nsresult BeginRestoreChildren();
641 // Method to get our current position and size without flushing
642 void DoGetPositionAndSize(PRInt32 * x, PRInt32 * y, PRInt32 * cx,
643 PRInt32 * cy);
645 // Check whether aURI should inherit our security context
646 static nsresult URIInheritsSecurityContext(nsIURI* aURI, PRBool* aResult);
648 // Check whether aURI is a URI_IS_LOCAL_FILE or not
649 static PRBool URIIsLocalFile(nsIURI *aURI);
651 // Check whether aURI is about:blank
652 static PRBool IsAboutBlank(nsIURI* aURI);
654 // Call this when a URI load is handed to us (via OnLinkClick or
655 // InternalLoad). This makes sure that we're not inside unload, or that if
656 // we are it's still OK to load this URI.
657 PRBool IsOKToLoadURI(nsIURI* aURI);
659 void ReattachEditorToWindow(nsISHEntry *aSHEntry);
661 nsresult GetSessionStorageForURI(nsIURI* aURI,
662 const nsSubstring& aDocumentURI,
663 PRBool create,
664 nsIDOMStorage** aStorage);
666 // helpers for executing commands
667 nsresult GetControllerForCommand(const char *inCommand,
668 nsIController** outController);
669 nsresult IsCommandEnabled(const char * inCommand, PRBool* outEnabled);
670 nsresult DoCommand(const char * inCommand);
671 nsresult EnsureCommandHandler();
673 nsIChannel* GetCurrentDocChannel();
674 protected:
675 // Override the parent setter from nsDocLoader
676 virtual nsresult SetDocLoaderParent(nsDocLoader * aLoader);
678 void ClearFrameHistory(nsISHEntry* aEntry);
680 // Event type dispatched by RestorePresentation
681 class RestorePresentationEvent : public nsRunnable {
682 public:
683 NS_DECL_NSIRUNNABLE
684 RestorePresentationEvent(nsDocShell *ds) : mDocShell(ds) {}
685 void Revoke() { mDocShell = nsnull; }
686 private:
687 nsRefPtr<nsDocShell> mDocShell;
690 // hash of session storages, keyed by domain
691 nsInterfaceHashtable<nsCStringHashKey, nsIDOMStorage> mStorages;
693 // Dimensions of the docshell
694 nsIntRect mBounds;
695 nsString mName;
696 nsString mTitle;
699 * Content-Type Hint of the most-recently initiated load. Used for
700 * session history entries.
702 nsCString mContentTypeHint;
703 nsIntPoint mDefaultScrollbarPref; // persistent across doc loads
705 nsCOMPtr<nsISupportsArray> mRefreshURIList;
706 nsCOMPtr<nsISupportsArray> mSavedRefreshURIList;
707 nsRefPtr<nsDSURIContentListener> mContentListener;
708 nsCOMPtr<nsIContentViewer> mContentViewer;
709 nsCOMPtr<nsIDocumentCharsetInfo> mDocumentCharsetInfo;
710 nsCOMPtr<nsIWidget> mParentWidget;
711 nsCOMPtr<nsIPrefBranch> mPrefs;
713 // mCurrentURI should be marked immutable on set if possible.
714 nsCOMPtr<nsIURI> mCurrentURI;
715 nsCOMPtr<nsIURI> mReferrerURI;
716 nsCOMPtr<nsIScriptGlobalObject> mScriptGlobal;
717 nsCOMPtr<nsISHistory> mSessionHistory;
718 nsCOMPtr<nsIGlobalHistory2> mGlobalHistory;
719 nsCOMPtr<nsIWebBrowserFind> mFind;
720 nsCOMPtr<nsICommandManager> mCommandManager;
721 // Reference to the SHEntry for this docshell until the page is destroyed.
722 // Somebody give me better name
723 nsCOMPtr<nsISHEntry> mOSHE;
724 // Reference to the SHEntry for this docshell until the page is loaded
725 // Somebody give me better name
726 nsCOMPtr<nsISHEntry> mLSHE;
728 // Holds a weak pointer to a RestorePresentationEvent object if any that
729 // holds a weak pointer back to us. We use this pointer to possibly revoke
730 // the event whenever necessary.
731 nsRevocableEventPtr<RestorePresentationEvent> mRestorePresentationEvent;
733 // Editor data, if this document is designMode or contentEditable.
734 nsAutoPtr<nsDocShellEditorData> mEditorData;
736 // Transferable hooks/callbacks
737 nsCOMPtr<nsIClipboardDragDropHookList> mTransferableHookData;
739 // Secure browser UI object
740 nsCOMPtr<nsISecureBrowserUI> mSecurityUI;
742 // The URI we're currently loading. This is only relevant during the
743 // firing of a pagehide/unload. The caller of FirePageHideNotification()
744 // is responsible for setting it and unsetting it. It may be null if the
745 // pagehide/unload is happening for some reason other than just loading a
746 // new URI.
747 nsCOMPtr<nsIURI> mLoadingURI;
749 // Set in LoadErrorPage from the method argument and used later
750 // in CreateContentViewer. We have to delay an shistory entry creation
751 // for which these objects are needed.
752 nsCOMPtr<nsIURI> mFailedURI;
753 nsCOMPtr<nsIChannel> mFailedChannel;
754 PRUint32 mFailedLoadType;
756 // WEAK REFERENCES BELOW HERE.
757 // Note these are intentionally not addrefd. Doing so will create a cycle.
758 // For that reasons don't use nsCOMPtr.
760 nsIDocShellTreeOwner * mTreeOwner; // Weak Reference
761 nsPIDOMEventTarget * mChromeEventHandler; //Weak Reference
763 eCharsetReloadState mCharsetReloadState;
765 // Offset in the parent's child list.
766 // -1 if the docshell is added dynamically to the parent shell.
767 PRUint32 mChildOffset;
768 PRUint32 mBusyFlags;
769 PRUint32 mAppType;
770 PRUint32 mLoadType;
772 PRInt32 mMarginWidth;
773 PRInt32 mMarginHeight;
775 // This can either be a content docshell or a chrome docshell. After
776 // Create() is called, the type is not expected to change.
777 PRInt32 mItemType;
779 // Index into the SHTransaction list, indicating the previous and current
780 // transaction at the time that this DocShell begins to load
781 PRInt32 mPreviousTransIndex;
782 PRInt32 mLoadedTransIndex;
784 PRPackedBool mAllowSubframes;
785 PRPackedBool mAllowPlugins;
786 PRPackedBool mAllowJavascript;
787 PRPackedBool mAllowMetaRedirects;
788 PRPackedBool mAllowImages;
789 PRPackedBool mAllowDNSPrefetch;
790 PRPackedBool mCreatingDocument; // (should be) debugging only
791 PRPackedBool mUseErrorPages;
792 PRPackedBool mObserveErrorPages;
793 PRPackedBool mAllowAuth;
794 PRPackedBool mAllowKeywordFixup;
795 PRPackedBool mIsOffScreenBrowser;
796 PRPackedBool mIsActive;
797 PRPackedBool mIsAppTab;
798 PRPackedBool mUseGlobalHistory;
800 // This boolean is set to true right before we fire pagehide and generally
801 // unset when we embed a new content viewer. While it's true no navigation
802 // is allowed in this docshell.
803 PRPackedBool mFiredUnloadEvent;
805 // this flag is for bug #21358. a docshell may load many urls
806 // which don't result in new documents being created (i.e. a new
807 // content viewer) we want to make sure we don't call a on load
808 // event more than once for a given content viewer.
809 PRPackedBool mEODForCurrentDocument;
810 PRPackedBool mURIResultedInDocument;
812 PRPackedBool mIsBeingDestroyed;
814 PRPackedBool mIsExecutingOnLoadHandler;
816 // Indicates that a DocShell in this "docshell tree" is printing
817 PRPackedBool mIsPrintingOrPP;
819 // Indicates to CreateContentViewer() that it is safe to cache the old
820 // presentation of the page, and to SetupNewViewer() that the old viewer
821 // should be passed a SHEntry to save itself into.
822 PRPackedBool mSavingOldViewer;
824 // @see nsIDocShellHistory::createdDynamically
825 PRPackedBool mDynamicallyCreated;
827 // If this is true, we won't fire a popstate event.
828 PRPackedBool mSuppressPopstate;
829 #ifdef DEBUG
830 PRPackedBool mInEnsureScriptEnv;
831 #endif
832 PRUint64 mHistoryID;
834 static nsIURIFixup *sURIFixup;
836 #ifdef DEBUG
837 private:
838 // We're counting the number of |nsDocShells| to help find leaks
839 static unsigned long gNumberOfDocShells;
840 #endif /* DEBUG */
842 public:
843 class InterfaceRequestorProxy : public nsIInterfaceRequestor {
844 public:
845 InterfaceRequestorProxy(nsIInterfaceRequestor* p);
846 virtual ~InterfaceRequestorProxy();
847 NS_DECL_ISUPPORTS
848 NS_DECL_NSIINTERFACEREQUESTOR
850 protected:
851 InterfaceRequestorProxy() {}
852 nsWeakPtr mWeakPtr;
856 #endif /* nsDocShell_h__ */