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.
27 * Adam Lock <adamlock@netscape.com>
29 * ***** END LICENSE BLOCK ***** */
32 //#include "nsIGenericFactory.h"
33 #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 "nsIURIContentListener.h"
43 #include "nsCWebBrowser.h"
46 #include "GeckoContainer.h"
50 GeckoContainer::GeckoContainer(GeckoContainerUI
*pUI
, const char *aRole
) :
52 mNativeWindow(nsnull
),
54 mIsChromeContainer(PR_FALSE
),
55 mIsURIContentListener(PR_TRUE
)
57 NS_ASSERTION(mUI
, "No GeckoContainerUI! How do you expect this to work???");
62 GeckoContainer::~GeckoContainer()
67 nsresult
GeckoContainer::CreateBrowser(PRInt32 aX
, PRInt32 aY
,
68 PRInt32 aCX
, PRInt32 aCY
, nativeWindow aParent
,
69 nsIWebBrowser
**aBrowser
)
71 NS_ENSURE_ARG_POINTER(aBrowser
);
75 mWebBrowser
= do_CreateInstance(NS_WEBBROWSER_CONTRACTID
, &rv
);
76 if (!mWebBrowser
|| NS_FAILED(rv
))
77 return NS_ERROR_FAILURE
;
79 mWebBrowser
->SetContainerWindow(static_cast<nsIWebBrowserChrome
*>(this));
81 nsCOMPtr
<nsIDocShellTreeItem
> dsti
= do_QueryInterface(mWebBrowser
);
84 nsIDocShellTreeItem::typeChromeWrapper
:
85 nsIDocShellTreeItem::typeContentWrapper
);
87 nsCOMPtr
<nsIBaseWindow
> browserBaseWindow
= do_QueryInterface(mWebBrowser
);
89 mNativeWindow
= aParent
;
92 return NS_ERROR_FAILURE
;
94 browserBaseWindow
->InitWindow( mNativeWindow
,
97 browserBaseWindow
->Create();
99 nsCOMPtr
<nsIWebProgressListener
> listener(static_cast<nsIWebProgressListener
*>(this));
100 nsCOMPtr
<nsIWeakReference
> thisListener(do_GetWeakReference(listener
));
101 (void)mWebBrowser
->AddWebBrowserListener(thisListener
,
102 NS_GET_IID(nsIWebProgressListener
));
104 if (mIsURIContentListener
)
105 mWebBrowser
->SetParentURIContentListener(static_cast<nsIURIContentListener
*>(this));
108 // The window has been created. Now register for history notifications
109 // mWebBrowser->AddWebBrowserListener(thisListener, NS_GET_IID(nsISHistoryListener));
113 *aBrowser
= mWebBrowser
;
114 NS_ADDREF(*aBrowser
);
117 return NS_ERROR_FAILURE
;
121 void GeckoContainer::ContentFinishedLoading()
123 // if it was a chrome window and no one has already specified a size,
125 if (mWebBrowser
&& !mSizeSet
&&
126 (mChromeFlags
& nsIWebBrowserChrome::CHROME_OPENAS_CHROME
)) {
127 nsCOMPtr
<nsIDOMWindow
> contentWin
;
128 mWebBrowser
->GetContentDOMWindow(getter_AddRefs(contentWin
));
130 contentWin
->SizeToContent();
131 mUI
->ShowWindow(PR_TRUE
);
135 //*****************************************************************************
136 // GeckoContainer::nsISupports
137 //*****************************************************************************
139 NS_IMPL_ADDREF(GeckoContainer
)
140 NS_IMPL_RELEASE(GeckoContainer
)
142 NS_INTERFACE_MAP_BEGIN(GeckoContainer
)
143 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, nsIWebBrowserChrome
)
144 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor
)
145 NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChrome
)
146 NS_INTERFACE_MAP_ENTRY(nsIWebBrowserChromeFocus
)
147 NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow
)
148 NS_INTERFACE_MAP_ENTRY(nsIEmbeddingSiteWindow2
)
149 NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener
) // optional
150 NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference
)
151 NS_INTERFACE_MAP_ENTRY(nsIObserver
)
152 NS_INTERFACE_MAP_ENTRY(nsIURIContentListener
)
153 NS_INTERFACE_MAP_ENTRY(nsIContextMenuListener2
)
154 NS_INTERFACE_MAP_ENTRY(nsITooltipListener
)
155 NS_INTERFACE_MAP_ENTRY(nsIGeckoContainer
)
159 //*****************************************************************************
160 // GeckoContainer::nsIGeckoContainer
161 //*****************************************************************************
163 NS_IMETHODIMP
GeckoContainer::GetRole(nsACString
&aRole
)
169 NS_IMETHODIMP
GeckoContainer::GetContainerUI(GeckoContainerUI
**pUI
)
175 //*****************************************************************************
176 // GeckoContainer::nsIInterfaceRequestor
177 //*****************************************************************************
179 NS_IMETHODIMP
GeckoContainer::GetInterface(const nsIID
&aIID
, void** aInstancePtr
)
181 NS_ENSURE_ARG_POINTER(aInstancePtr
);
184 if (aIID
.Equals(NS_GET_IID(nsIDOMWindow
)))
188 return mWebBrowser
->GetContentDOMWindow((nsIDOMWindow
**) aInstancePtr
);
190 return NS_ERROR_NOT_INITIALIZED
;
192 return QueryInterface(aIID
, aInstancePtr
);
195 //*****************************************************************************
196 // GeckoContainer::nsIWebBrowserChrome
197 //*****************************************************************************
199 NS_IMETHODIMP
GeckoContainer::SetStatus(PRUint32 aType
, const PRUnichar
* aStatus
)
201 mUI
->UpdateStatusBarText(aStatus
);
205 NS_IMETHODIMP
GeckoContainer::GetWebBrowser(nsIWebBrowser
** aWebBrowser
)
207 NS_ENSURE_ARG_POINTER(aWebBrowser
);
208 *aWebBrowser
= mWebBrowser
;
209 NS_IF_ADDREF(*aWebBrowser
);
213 NS_IMETHODIMP
GeckoContainer::SetWebBrowser(nsIWebBrowser
* aWebBrowser
)
215 mWebBrowser
= aWebBrowser
;
219 NS_IMETHODIMP
GeckoContainer::GetChromeFlags(PRUint32
* aChromeMask
)
221 *aChromeMask
= mChromeFlags
;
225 NS_IMETHODIMP
GeckoContainer::SetChromeFlags(PRUint32 aChromeMask
)
227 mChromeFlags
= aChromeMask
;
231 NS_IMETHODIMP
GeckoContainer::DestroyBrowserWindow(void)
238 // IN: The desired browser client area dimensions.
239 NS_IMETHODIMP
GeckoContainer::SizeBrowserTo(PRInt32 aWidth
, PRInt32 aHeight
)
241 /* This isn't exactly correct: we're setting the whole window to
242 the size requested for the browser. At time of writing, though,
243 it's fine and useful for winEmbed's purposes. */
244 mUI
->SizeTo(aWidth
, aHeight
);
250 NS_IMETHODIMP
GeckoContainer::ShowAsModal(void)
252 /* if (mDependentParent)
253 mUI->EnableChromeWindow(mDependentParent, PR_FALSE);
255 mContinueModalLoop = PR_TRUE;
256 mUI->RunEventLoop(mContinueModalLoop);
258 if (mDependentParent)
259 mUI->EnableChromeWindow(mDependentParent, PR_TRUE);
264 NS_IMETHODIMP
GeckoContainer::IsWindowModal(PRBool
*_retval
)
267 return NS_ERROR_NOT_IMPLEMENTED
;
270 NS_IMETHODIMP
GeckoContainer::ExitModalEventLoop(nsresult aStatus
)
272 mContinueModalLoop
= PR_FALSE
;
276 //*****************************************************************************
277 // GeckoContainer::nsIWebBrowserChromeFocus
278 //*****************************************************************************
280 NS_IMETHODIMP
GeckoContainer::FocusNextElement()
282 return NS_ERROR_NOT_IMPLEMENTED
;
285 NS_IMETHODIMP
GeckoContainer::FocusPrevElement()
287 return NS_ERROR_NOT_IMPLEMENTED
;
290 //*****************************************************************************
291 // GeckoContainer::nsIWebProgressListener
292 //*****************************************************************************
294 NS_IMETHODIMP
GeckoContainer::OnProgressChange(nsIWebProgress
*progress
, nsIRequest
*request
,
295 PRInt32 curSelfProgress
, PRInt32 maxSelfProgress
,
296 PRInt32 curTotalProgress
, PRInt32 maxTotalProgress
)
298 mUI
->UpdateProgress(curTotalProgress
, maxTotalProgress
);
302 NS_IMETHODIMP
GeckoContainer::OnStateChange(nsIWebProgress
*progress
, nsIRequest
*request
,
303 PRUint32 progressStateFlags
, nsresult status
)
305 if ((progressStateFlags
& STATE_START
) && (progressStateFlags
& STATE_IS_DOCUMENT
))
307 mUI
->UpdateBusyState(PR_TRUE
);
310 if ((progressStateFlags
& STATE_STOP
) && (progressStateFlags
& STATE_IS_DOCUMENT
))
312 mUI
->UpdateBusyState(PR_FALSE
);
313 mUI
->UpdateProgress(0, 100);
314 mUI
->UpdateStatusBarText(nsnull
);
315 ContentFinishedLoading();
322 NS_IMETHODIMP
GeckoContainer::OnLocationChange(nsIWebProgress
* aWebProgress
,
323 nsIRequest
* aRequest
,
326 PRBool isSubFrameLoad
= PR_FALSE
; // Is this a subframe load
329 nsCOMPtr
<nsIDOMWindow
> domWindow
;
330 nsCOMPtr
<nsIDOMWindow
> topDomWindow
;
331 aWebProgress
->GetDOMWindow(getter_AddRefs(domWindow
));
334 // Get root domWindow
335 domWindow
->GetTop(getter_AddRefs(topDomWindow
));
337 if (domWindow
!= topDomWindow
)
338 isSubFrameLoad
= PR_TRUE
;
341 mUI
->UpdateCurrentURI();
346 GeckoContainer::OnStatusChange(nsIWebProgress
* aWebProgress
,
347 nsIRequest
* aRequest
,
349 const PRUnichar
* aMessage
)
351 mUI
->UpdateStatusBarText(aMessage
);
358 GeckoContainer::OnSecurityChange(nsIWebProgress
*aWebProgress
,
359 nsIRequest
*aRequest
,
366 //*****************************************************************************
367 // GeckoContainer::nsIEmbeddingSiteWindow
368 //*****************************************************************************
370 NS_IMETHODIMP
GeckoContainer::SetDimensions(PRUint32 aFlags
, PRInt32 x
, PRInt32 y
, PRInt32 cx
, PRInt32 cy
)
372 return NS_ERROR_NOT_IMPLEMENTED
;
375 NS_IMETHODIMP
GeckoContainer::GetDimensions(PRUint32 aFlags
, PRInt32
*x
, PRInt32
*y
, PRInt32
*cx
, PRInt32
*cy
)
377 if (aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_POSITION
)
382 if (aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_INNER
||
383 aFlags
& nsIEmbeddingSiteWindow::DIM_FLAGS_SIZE_OUTER
)
388 return NS_ERROR_NOT_IMPLEMENTED
;
391 /* void setFocus (); */
392 NS_IMETHODIMP
GeckoContainer::SetFocus()
398 /* attribute wstring title; */
399 NS_IMETHODIMP
GeckoContainer::GetTitle(PRUnichar
* *aTitle
)
401 NS_ENSURE_ARG_POINTER(aTitle
);
405 return NS_ERROR_NOT_IMPLEMENTED
;
407 NS_IMETHODIMP
GeckoContainer::SetTitle(const PRUnichar
* aTitle
)
409 return NS_ERROR_NOT_IMPLEMENTED
;
412 /* attribute boolean visibility; */
413 NS_IMETHODIMP
GeckoContainer::GetVisibility(PRBool
* aVisibility
)
415 NS_ENSURE_ARG_POINTER(aVisibility
);
416 *aVisibility
= PR_TRUE
;
419 NS_IMETHODIMP
GeckoContainer::SetVisibility(PRBool aVisibility
)
424 /* attribute nativeSiteWindow siteWindow */
425 NS_IMETHODIMP
GeckoContainer::GetSiteWindow(void * *aSiteWindow
)
427 NS_ENSURE_ARG_POINTER(aSiteWindow
);
429 *aSiteWindow
= mNativeWindow
;
433 //*****************************************************************************
434 // GeckoContainer::nsIEmbeddingSiteWindow2
435 //*****************************************************************************
437 NS_IMETHODIMP
GeckoContainer::Blur()
443 //*****************************************************************************
444 // GeckoContainer::nsIObserver
445 //*****************************************************************************
447 NS_IMETHODIMP
GeckoContainer::Observe(nsISupports
*aSubject
, const char *aTopic
, const PRUnichar
*someData
)
450 if (nsCRT::strcmp(aTopic
, "profile-change-teardown") == 0)
452 // A profile change means death for this window
458 //*****************************************************************************
459 // GeckoContainer::nsIURIContentListener
460 //*****************************************************************************
462 /* boolean onStartURIOpen (in nsIURI aURI); */
463 NS_IMETHODIMP
GeckoContainer::OnStartURIOpen(nsIURI
*aURI
, PRBool
*_retval
)
465 return NS_ERROR_NOT_IMPLEMENTED
;
468 /* boolean doContent (in string aContentType, in boolean aIsContentPreferred, in nsIRequest aRequest, out nsIStreamListener aContentHandler); */
469 NS_IMETHODIMP
GeckoContainer::DoContent(const char *aContentType
, PRBool aIsContentPreferred
, nsIRequest
*aRequest
, nsIStreamListener
**aContentHandler
, PRBool
*_retval
)
471 return NS_ERROR_NOT_IMPLEMENTED
;
474 /* boolean isPreferred (in string aContentType, out string aDesiredContentType); */
475 NS_IMETHODIMP
GeckoContainer::IsPreferred(const char *aContentType
, char **aDesiredContentType
, PRBool
*_retval
)
477 return NS_ERROR_NOT_IMPLEMENTED
;
480 /* boolean canHandleContent (in string aContentType, in boolean aIsContentPreferred, out string aDesiredContentType); */
481 NS_IMETHODIMP
GeckoContainer::CanHandleContent(const char *aContentType
, PRBool aIsContentPreferred
, char **aDesiredContentType
, PRBool
*_retval
)
483 return NS_ERROR_NOT_IMPLEMENTED
;
486 /* attribute nsISupports loadCookie; */
487 NS_IMETHODIMP
GeckoContainer::GetLoadCookie(nsISupports
* *aLoadCookie
)
489 return NS_ERROR_NOT_IMPLEMENTED
;
491 NS_IMETHODIMP
GeckoContainer::SetLoadCookie(nsISupports
* aLoadCookie
)
493 return NS_ERROR_NOT_IMPLEMENTED
;
496 /* attribute nsIURIContentListener parentContentListener; */
497 NS_IMETHODIMP
GeckoContainer::GetParentContentListener(nsIURIContentListener
* *aParentContentListener
)
499 return NS_ERROR_NOT_IMPLEMENTED
;
501 NS_IMETHODIMP
GeckoContainer::SetParentContentListener(nsIURIContentListener
* aParentContentListener
)
503 return NS_ERROR_NOT_IMPLEMENTED
;
506 //*****************************************************************************
507 // GeckoContainer::nsIContextMenuListener2
508 //*****************************************************************************
510 /* void onShowContextMenu (in unsigned long aContextFlags, in nsIContextMenuInfo aUtils); */
511 NS_IMETHODIMP
GeckoContainer::OnShowContextMenu(PRUint32 aContextFlags
, nsIContextMenuInfo
*aContextMenuInfo
)
513 mUI
->ShowContextMenu(aContextFlags
, aContextMenuInfo
);
518 //*****************************************************************************
519 // GeckoContainer::nsITooltipListener
520 //*****************************************************************************
522 /* void OnShowTooltip (in long aXCoords, in long aYCoords, in wstring aTipText); */
523 NS_IMETHODIMP
GeckoContainer::OnShowTooltip(PRInt32 aXCoords
, PRInt32 aYCoords
, const PRUnichar
*aTipText
)
525 mUI
->ShowTooltip(aXCoords
, aYCoords
, aTipText
);
529 /* void OnHideTooltip (); */
530 NS_IMETHODIMP
GeckoContainer::OnHideTooltip()