Okay, hopefully I can remember everything I did to get this working.

1. I have my WebBrowserChrome class inheriting from nsIURIContentListener (and thus implementing my version of it).  I may have modeled this after wxMozilla, so you may want to look at their source.

2. In <my URIContentListener>::OnStartURIOpen() I am looking at the URI to see if it is one I want to filter. If so, return PR_TRUE (otherwise PR_FALSE to indicate your content listener is not handling it).  I'd rather not post the code for this, so email me privately if you'd like to see it.

3. When creating an instance of the embedded browser, register your URIContentListener.  Here's a code snippet (with some proprietary code and types mixed in, so hopefully it's not too confusing):

/*
Create the physical, embedded window for the HTML control.  Does not affect
refcount for Gecko rendering engine being loaded into memory.  Does not affect
the Gecko embedding component held on to by the HTML control.  Simply creates the
client area that the embedded object draws into
*/
TH_SINGLE_UI static void CreateHTMLControlWindow(OHHandle h, ObjID o, Bool32 show /*=TRUE*/)
{
LVWebBrowserChrome *chrome;


chrome = (LVWebBrowserChrome*)HTMLControlPtr(h,o)->webBrowser;
if (chrome) {
nsCOMPtr<nsIWebBrowser> webBrowser;
chrome->GetWebBrowser(getter_AddRefs(webBrowser));

if (webBrowser) {
nsCOMPtr<nsIBaseWindow> baseWindow(do_QueryInterface(webBrowser));


if (baseWindow) {

Wind w;
Point pt;

w = HeapWindow(h);


if (w) {
Wind parent;

int32 height = xxx;
int32 width = xxx;
#if MSWin
HWND hwnd = HostHWND(parent);
baseWindow->InitWindow(nsNativeWidget(hwnd),nsnull, pt.h,pt.v,width,height);
#else if Mac
WindowPtr mw = HostMacWindow(parent);
baseWindow->InitWindow(nsNativeWidget(mw),nsnull, pt.h,pt.v,width,height);
#endif // MSWin
PRBool vis = (show == TRUE ? PR_TRUE : PR_FALSE);
baseWindow->Create();
baseWindow->SetVisibility(vis);
chrome->SetVisibility(vis);
nsWeakPtr weakling(getter_AddRefs(NS_GetWeakReference(NS_STATIC_CAST(nsIWebProgressListener*, 
chrome))));


// need to do these here as we need a docshell, which relies on the window
webBrowser->AddWebBrowserListener(weakling, NS_GET_IID(nsIWebProgressListener));
webBrowser->SetParentURIContentListener(NS_STATIC_CAST(nsIURIContentListener*,chrome));
}
}
}
}
}

4. You need to set yourself as the preferred listener.  The FAQ you linked suggests this can be accomplished by having your URIContentListener implement IsPreferred() and return PR_TRUE.  I will need to try this - perhaps I did something wrong.  I recall seeing a post somewhere that implied this was not sufficient (in the 1.75 codebase at least, which is what I'm working with), and instead in nsDSURIContentListener.cpp (in the mozilla source), I changed things up a little.  This was a while back, and looking at it now, I still am not clear how this works at all. It seems like my version of URIContentListener needs to return TRUE when asked if preferred.  At any rate, here's my version of nsDSURIContentListener::IsPreferred()

NS_IMETHODIMP
nsDSURIContentListener::IsPreferred(const char* aContentType,
                                    char ** aDesiredContentType,
                                    PRBool* aCanHandle)
{
    NS_ENSURE_ARG_POINTER(aCanHandle);
    NS_ENSURE_ARG_POINTER(aDesiredContentType);

    // the docshell has no idea if it is the preferred content provider or not.
    // It needs to ask it's parent if it is the preferred content handler or not...

    nsCOMPtr<nsIURIContentListener> parentListener;
    GetParentContentListener(getter_AddRefs(parentListener));
    if (parentListener) {
nsresult result;
result = parentListener->IsPreferred(aContentType,
                                                   aDesiredContentType,
                                                   aCanHandle);
// CHANGE FROM OFFICIAL MOZILLA SOURCE --> if we should pay attention to parent
if (result != NS_ERROR_NOT_IMPLEMENTED)
return result;
    }
    // we used to return false here if we didn't have a parent properly
    // registered at the top of the docshell hierarchy to dictate what
    // content types this docshell should be a preferred handler for.  But
    // this really makes it hard for developers using iframe or browser tags
    // because then they need to make sure they implement
    // nsIURIContentListener otherwise all link clicks would get sent to
    // another window because we said we weren't the preferred handler type.
    // I'm going to change the default now...if we can handle the content,
    // and someone didn't EXPLICITLY set a nsIURIContentListener at the top
    // of our docshell chain, then we'll now always attempt to process the
    // content ourselves...
    return CanHandleContent(aContentType,
                            PR_TRUE,
                            aDesiredContentType,
                            aCanHandle);
}

Hope that helps.

J



On Feb 8, 2006, at 8:56 AM, Tom Gethings wrote:

Well basically i would like an example of how to implement the nsIURIContentListener interface, and how to register it with my browser object, im sorry if this is a big ask but i dont really know what i have to do.  Looking through the MFCEmbed example is quite daunting and the winEmbed example does not provide any implementations of this.  I am not very experienced with C++ so im trying to learn as i go along which doesnt really help.

_______________________________________________
dev-embedding mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-embedding

Reply via email to