1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: Mozilla-sample-code 1.0
4 * Copyright (c) 2002 Netscape Communications Corporation and
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this Mozilla sample software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do so, subject to the
13 * following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
28 * ***** END LICENSE BLOCK ***** */
31 #include "nsIGenericFactory.h"
32 #include "nsIComponentManager.h"
34 #include "nsXPIDLString.h"
36 #include "nsIWebProgress.h"
37 #include "nsIDocShellTreeItem.h"
38 #include "nsIDOMWindow.h"
39 #include "nsIDOMWindowInternal.h"
40 #include "nsIInterfaceRequestor.h"
41 #include "nsIInterfaceRequestorUtils.h"
42 #include "nsIRequest.h"
43 #include "nsIChannel.h"
44 #include "nsCWebBrowser.h"
45 #include "nsWidgetsCID.h"
46 #include "nsIProfileChangeStatus.h"
53 #include "WebBrowserChrome.h"
56 WebBrowserChrome::WebBrowserChrome()
58 mNativeWindow
= nsnull
;
62 WebBrowserChrome::~WebBrowserChrome()
64 WebBrowserChromeUI::Destroyed(this);
67 nsresult
WebBrowserChrome::CreateBrowser(PRInt32 aX
, PRInt32 aY
,
68 PRInt32 aCX
, PRInt32 aCY
,
69 nsIWebBrowser
**aBrowser
)
71 NS_ENSURE_ARG_POINTER(aBrowser
);
74 mWebBrowser
= do_CreateInstance(NS_WEBBROWSER_CONTRACTID
);
77 return NS_ERROR_FAILURE
;
79 (void)mWebBrowser
->SetContainerWindow(static_cast<nsIWebBrowserChrome
*>(this));
81 nsCOMPtr
<nsIDocShellTreeItem
> dsti
= do_QueryInterface(mWebBrowser
);
82 dsti
->SetItemType(nsIDocShellTreeItem::typeContentWrapper
);
84 nsCOMPtr
<nsIBaseWindow
> browserBaseWindow
= do_QueryInterface(mWebBrowser
);
86 mNativeWindow
= WebBrowserChromeUI::CreateNativeWindow(static_cast<nsIWebBrowserChrome
*>(this));
89 return NS_ERROR_FAILURE
;
91 browserBaseWindow
->InitWindow( mNativeWindow
,
94 browserBaseWindow
->Create();
96 nsCOMPtr
<nsIWebProgressListener
> listener(static_cast<nsIWebProgressListener
*>(this));
97 nsCOMPtr
<nsIWeakReference
> thisListener(do_GetWeakReference(listener
));
98 (void)mWebBrowser
->AddWebBrowserListener(thisListener
,
99 NS_GET_IID(nsIWebProgressListener
));
101 // The window has been created. Now register for history notifications
102 mWebBrowser
->AddWebBrowserListener(thisListener
, NS_GET_IID(nsISHistoryListener
));
106 *aBrowser
= mWebBrowser
;
107 NS_ADDREF(*aBrowser
);
110 return NS_ERROR_FAILURE
;
113 //*****************************************************************************
114 // WebBrowserChrome::nsISupports
115 //*****************************************************************************
117 NS_IMPL_ADDREF(WebBrowserChrome
)
118 NS_IMPL_RELEASE(WebBrowserChrome
)
120 NS_INTERFACE_MAP_BEGIN(WebBrowserChrome
)
121 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIWebBrowserChrome
)
122 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor
)
123 NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome
)
124 NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow
)
125 NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener
) // optional
126 NS_INTERFACE_MAP_ENTRY(nsISHistoryListener
)
127 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
128 NS_INTERFACE_MAP_ENTRY(nsIObserver
)
129 NS_INTERFACE_MAP_ENTRY(nsIContextMenuListener
)
130 NS_INTERFACE_MAP_ENTRY(nsITooltipListener
)
133 //*****************************************************************************
134 // WebBrowserChrome::nsIInterfaceRequestor
135 //*****************************************************************************
137 NS_IMETHODIMP
WebBrowserChrome::GetInterface(const nsIID
&aIID
, void** aInstancePtr
)
139 NS_ENSURE_ARG_POINTER(aInstancePtr
);
142 if (aIID
.Equals(NS_GET_IID(nsIDOMWindow
)))
146 return mWebBrowser
->GetContentDOMWindow((nsIDOMWindow
**) aInstancePtr
);
148 return NS_ERROR_NOT_INITIALIZED
;
150 return QueryInterface(aIID
, aInstancePtr
);
153 //*****************************************************************************
154 // WebBrowserChrome::nsIWebBrowserChrome
155 //*****************************************************************************
157 NS_IMETHODIMP
WebBrowserChrome::SetStatus(PRUint32 aType
, const PRUnichar
* aStatus
)
159 WebBrowserChromeUI::UpdateStatusBarText(this, aStatus
);
163 NS_IMETHODIMP
WebBrowserChrome::GetWebBrowser(nsIWebBrowser
** aWebBrowser
)
165 NS_ENSURE_ARG_POINTER(aWebBrowser
);
166 *aWebBrowser
= mWebBrowser
;
167 NS_IF_ADDREF(*aWebBrowser
);
171 NS_IMETHODIMP
WebBrowserChrome::SetWebBrowser(nsIWebBrowser
* aWebBrowser
)
173 mWebBrowser
= aWebBrowser
;
177 NS_IMETHODIMP
WebBrowserChrome::GetChromeFlags(PRUint32
* aChromeMask
)
179 *aChromeMask
= mChromeFlags
;
183 NS_IMETHODIMP
WebBrowserChrome::SetChromeFlags(PRUint32 aChromeMask
)
185 mChromeFlags
= aChromeMask
;
189 NS_IMETHODIMP
WebBrowserChrome::DestroyBrowserWindow(void)
191 WebBrowserChromeUI::Destroy(this);
196 // IN: The desired browser client area dimensions.
197 NS_IMETHODIMP
WebBrowserChrome::SizeBrowserTo(PRInt32 aWidth
, PRInt32 aHeight
)
199 /* This isn't exactly correct: we're setting the whole window to
200 the size requested for the browser. At time of writing, though,
201 it's fine and useful for os2Embed's purposes. */
202 WebBrowserChromeUI::SizeTo(this, aWidth
, aHeight
);
208 NS_IMETHODIMP
WebBrowserChrome::ShowAsModal(void)
210 if (mDependentParent
)
211 EnableChromeWindow(mDependentParent
, PR_FALSE
);
213 mContinueModalLoop
= PR_TRUE
;
214 RunEventLoop(mContinueModalLoop
);
216 if (mDependentParent
)
217 EnableChromeWindow(mDependentParent
, PR_TRUE
);
222 NS_IMETHODIMP
WebBrowserChrome::IsWindowModal(PRBool
*_retval
)
225 return NS_ERROR_NOT_IMPLEMENTED
;
228 NS_IMETHODIMP
WebBrowserChrome::ExitModalEventLoop(nsresult aStatus
)
230 mContinueModalLoop
= PR_FALSE
;
234 //*****************************************************************************
235 // WebBrowserChrome::nsIWebBrowserChromeFocus
236 //*****************************************************************************
238 NS_IMETHODIMP
WebBrowserChrome::FocusNextElement()
240 return NS_ERROR_NOT_IMPLEMENTED
;
243 NS_IMETHODIMP
WebBrowserChrome::FocusPrevElement()
245 return NS_ERROR_NOT_IMPLEMENTED
;
248 //*****************************************************************************
249 // WebBrowserChrome::nsIWebProgressListener
250 //*****************************************************************************
252 NS_IMETHODIMP
WebBrowserChrome::OnProgressChange(nsIWebProgress
*progress
, nsIRequest
*request
,
253 PRInt32 curSelfProgress
, PRInt32 maxSelfProgress
,
254 PRInt32 curTotalProgress
, PRInt32 maxTotalProgress
)
256 WebBrowserChromeUI::UpdateProgress(this, curTotalProgress
, maxTotalProgress
);
260 NS_IMETHODIMP
WebBrowserChrome::OnStateChange(nsIWebProgress
*progress
, nsIRequest
*request
,
261 PRUint32 progressStateFlags
, nsresult status
)
263 if ((progressStateFlags
& STATE_START
) && (progressStateFlags
& STATE_IS_DOCUMENT
))
265 WebBrowserChromeUI::UpdateBusyState(this, PR_TRUE
);
268 if ((progressStateFlags
& STATE_STOP
) && (progressStateFlags
& STATE_IS_DOCUMENT
))
270 WebBrowserChromeUI::UpdateBusyState(this, PR_FALSE
);
271 WebBrowserChromeUI::UpdateProgress(this, 0, 100);
272 WebBrowserChromeUI::UpdateStatusBarText(this, nsnull
);
273 ContentFinishedLoading();
280 NS_IMETHODIMP
WebBrowserChrome::OnLocationChange(nsIWebProgress
* aWebProgress
,
281 nsIRequest
* aRequest
,
284 PRBool isSubFrameLoad
= PR_FALSE
; // Is this a subframe load
286 nsCOMPtr
<nsIDOMWindow
> domWindow
;
287 nsCOMPtr
<nsIDOMWindow
> topDomWindow
;
288 aWebProgress
->GetDOMWindow(getter_AddRefs(domWindow
));
289 if (domWindow
) { // Get root domWindow
290 domWindow
->GetTop(getter_AddRefs(topDomWindow
));
292 if (domWindow
!= topDomWindow
)
293 isSubFrameLoad
= PR_TRUE
;
296 WebBrowserChromeUI::UpdateCurrentURI(this);
301 WebBrowserChrome::OnStatusChange(nsIWebProgress
* aWebProgress
,
302 nsIRequest
* aRequest
,
304 const PRUnichar
* aMessage
)
306 WebBrowserChromeUI::UpdateStatusBarText(this, aMessage
);
313 WebBrowserChrome::OnSecurityChange(nsIWebProgress
*aWebProgress
,
314 nsIRequest
*aRequest
,
320 //*****************************************************************************
321 // WebBrowserChrome::nsISHistoryListener
322 //*****************************************************************************
325 WebBrowserChrome::OnHistoryNewEntry(nsIURI
* aNewURI
)
327 return SendHistoryStatusMessage(aNewURI
, "add");
331 WebBrowserChrome::OnHistoryGoBack(nsIURI
* aBackURI
, PRBool
* aContinue
)
333 // For now, let the operation continue
334 *aContinue
= PR_TRUE
;
335 return SendHistoryStatusMessage(aBackURI
, "back");
340 WebBrowserChrome::OnHistoryGoForward(nsIURI
* aForwardURI
, PRBool
* aContinue
)
342 // For now, let the operation continue
343 *aContinue
= PR_TRUE
;
344 return SendHistoryStatusMessage(aForwardURI
, "forward");
349 WebBrowserChrome::OnHistoryGotoIndex(PRInt32 aIndex
, nsIURI
* aGotoURI
, PRBool
* aContinue
)
351 // For now, let the operation continue
352 *aContinue
= PR_TRUE
;
353 return SendHistoryStatusMessage(aGotoURI
, "goto", aIndex
);
357 WebBrowserChrome::OnHistoryReload(nsIURI
* aURI
, PRUint32 aReloadFlags
, PRBool
* aContinue
)
359 // For now, let the operation continue
360 *aContinue
= PR_TRUE
;
361 return SendHistoryStatusMessage(aURI
, "reload", 0 /* no info to pass here */, aReloadFlags
);
365 WebBrowserChrome::OnHistoryPurge(PRInt32 aNumEntries
, PRBool
*aContinue
)
367 // For now let the operation continue
368 *aContinue
= PR_FALSE
;
369 return SendHistoryStatusMessage(nsnull
, "purge", aNumEntries
);
373 WebBrowserChrome::SendHistoryStatusMessage(nsIURI
* aURI
, char * operation
, PRInt32 info1
, PRUint32 aReloadFlags
)
375 nsCAutoString uriCStr
;
378 aURI
->GetSpec(uriCStr
);
383 if(!(nsCRT::strcmp(operation
, "back")))
385 // Going back. XXX Get string from a resource file
386 uriAStr
.AppendLiteral("Going back to url:");
387 AppendUTF8toUTF16(uriCStr
, uriAStr
);
389 else if (!(nsCRT::strcmp(operation
, "forward")))
391 // Going forward. XXX Get string from a resource file
392 uriAStr
.AppendLiteral("Going forward to url:");
393 AppendUTF8toUTF16(uriCStr
, uriAStr
);
395 else if (!(nsCRT::strcmp(operation
, "reload")))
397 // Reloading. XXX Get string from a resource file
398 if (aReloadFlags
& nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
&&
399 aReloadFlags
& nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
)
401 uriAStr
.Append(NS_LITERAL_STRING("Reloading url,(bypassing proxy and cache) :"));
403 else if (aReloadFlags
& nsIWebNavigation::LOAD_FLAGS_BYPASS_PROXY
)
405 uriAStr
.Append(NS_LITERAL_STRING("Reloading url, (bypassing proxy):"));
407 else if (aReloadFlags
& nsIWebNavigation::LOAD_FLAGS_BYPASS_CACHE
)
409 uriAStr
.Append(NS_LITERAL_STRING("Reloading url, (bypassing cache):"));
413 uriAStr
.Append(NS_LITERAL_STRING("Reloading url, (normal):"));
415 AppendUTF8toUTF16(uriCStr
, uriAStr
);
417 else if (!(nsCRT::strcmp(operation
, "add")))
419 // Adding new entry. XXX Get string from a resource file
420 AppendUTF8toUTF16(uriCStr
, uriAStr
);
421 uriAStr
.AppendLiteral(" added to session History");
423 else if (!(nsCRT::strcmp(operation
, "goto")))
425 // Goto. XXX Get string from a resource file
426 uriAStr
.AppendLiteral("Going to HistoryIndex:");
427 uriAStr
.AppendInt(info1
);
428 uriAStr
.AppendLiteral(" Url:");
429 AppendUTF8toUTF16(uriCStr
, uriAStr
);
431 else if (!(nsCRT::strcmp(operation
, "purge")))
433 // Purging old entries
434 uriAStr
.AppendInt(info1
);
435 uriAStr
.AppendLiteral(" purged from Session History");
438 WebBrowserChromeUI::UpdateStatusBarText(this, uriAStr
.get());
443 void WebBrowserChrome::ContentFinishedLoading()
445 // if it was a chrome window and no one has already specified a size,
447 if (mWebBrowser
&& !mSizeSet
&&
448 (mChromeFlags
& nsIWebBrowserChrome::CHROME_OPENAS_CHROME
)) {
449 nsCOMPtr
<nsIDOMWindow
> contentWin
;
450 mWebBrowser
->GetContentDOMWindow(getter_AddRefs(contentWin
));
452 contentWin
->SizeToContent();
453 WebBrowserChromeUI::ShowWindow(this, PR_TRUE
);
458 //*****************************************************************************
459 // WebBrowserChrome::nsIEmbeddingSiteWindow
460 //*****************************************************************************
462 NS_IMETHODIMP
WebBrowserChrome::SetDimensions(PRUint32 aFlags
, PRInt32 x
, PRInt32 y
, PRInt32 cx
, PRInt32 cy
)
464 return NS_ERROR_NOT_IMPLEMENTED
;
467 NS_IMETHODIMP
WebBrowserChrome::GetDimensions(PRUint32 aFlags
, PRInt32
*x
, PRInt32
*y
, PRInt32
*cx
, PRInt32
*cy
)
469 if (aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION
)
474 if (aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER
||
475 aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER
)
480 return NS_ERROR_NOT_IMPLEMENTED
;
483 /* void setFocus (); */
484 NS_IMETHODIMP
WebBrowserChrome::SetFocus()
486 WebBrowserChromeUI::SetFocus(this);
490 /* attribute wstring title; */
491 NS_IMETHODIMP
WebBrowserChrome::GetTitle(PRUnichar
* *aTitle
)
493 NS_ENSURE_ARG_POINTER(aTitle
);
497 return NS_ERROR_NOT_IMPLEMENTED
;
499 NS_IMETHODIMP
WebBrowserChrome::SetTitle(const PRUnichar
* aTitle
)
501 return NS_ERROR_NOT_IMPLEMENTED
;
504 /* attribute boolean visibility; */
505 NS_IMETHODIMP
WebBrowserChrome::GetVisibility(PRBool
* aVisibility
)
507 NS_ENSURE_ARG_POINTER(aVisibility
);
508 *aVisibility
= PR_TRUE
;
511 NS_IMETHODIMP
WebBrowserChrome::SetVisibility(PRBool aVisibility
)
516 /* attribute nativeSiteWindow siteWindow */
517 NS_IMETHODIMP
WebBrowserChrome::GetSiteWindow(void * *aSiteWindow
)
519 NS_ENSURE_ARG_POINTER(aSiteWindow
);
521 *aSiteWindow
= mNativeWindow
;
526 //*****************************************************************************
527 // WebBrowserChrome::nsIObserver
528 //*****************************************************************************
530 NS_IMETHODIMP
WebBrowserChrome::Observe(nsISupports
*aSubject
, const char *aTopic
, const PRUnichar
*someData
)
533 if (nsCRT::strcmp(aTopic
, "profile-change-teardown") == 0)
535 // A profile change means death for this window
536 WebBrowserChromeUI::Destroy(this);
541 //*****************************************************************************
542 // WebBrowserChrome::nsIContextMenuListener
543 //*****************************************************************************
545 /* void OnShowContextMenu (in unsigned long aContextFlags, in nsIDOMEvent aEvent, in nsIDOMNode aNode); */
546 NS_IMETHODIMP
WebBrowserChrome::OnShowContextMenu(PRUint32 aContextFlags
, nsIDOMEvent
*aEvent
, nsIDOMNode
*aNode
)
548 WebBrowserChromeUI::ShowContextMenu(this, aContextFlags
, aEvent
, aNode
);
552 //*****************************************************************************
553 // WebBrowserChrome::nsITooltipListener
554 //*****************************************************************************
556 /* void OnShowTooltip (in long aXCoords, in long aYCoords, in wstring aTipText); */
557 NS_IMETHODIMP
WebBrowserChrome::OnShowTooltip(PRInt32 aXCoords
, PRInt32 aYCoords
, const PRUnichar
*aTipText
)
559 WebBrowserChromeUI::ShowTooltip(this, aXCoords
, aYCoords
, aTipText
);
563 /* void OnHideTooltip (); */
564 NS_IMETHODIMP
WebBrowserChrome::OnHideTooltip()
566 WebBrowserChromeUI::HideTooltip(this);