Hi,

Thanks for the reply...

As I have mentioned in my earlier mails, I want to use the browser engine only. I don't want to create any gui (windows, menu bars, status bars, etc).
I'm still not able to figure our whether I can do this.

Thanks,
Raghunath

Niky Williams wrote:
Raghunath wrote:
Hi Niky,

I have written a small sample code to navigate to URL. I have put it inline with this mail. When I call LoadURI on a nsIWebNavigation object, I get an NS_ERROR_UNEXPECTED error. Can you tell me what am I missing?

Thanks,
Raghu

/*********************** Header File Begin ****************************/
#ifndef __REmbedBrowser_h
#define __REmbedBrowser_h

#include <nsString.h>
#include <nsISupports.h>
#include <nsIWebBrowser.h>
#include <nsCOMPtr.h>
#include <nsIWebNavigation.h>
#include <nsISHistory.h>
#include <nsIInterfaceRequestor.h>

class REmbedBrowser : public nsISupports
{
public:
    REmbedBrowser();
    virtual ~REmbedBrowser();

    nsresult Init            (void);
    nsresult Deinit            (void);

    NS_DECL_ISUPPORTS
        nsresult LoadURI(const char *uri);

private:
    nsCOMPtr<nsIWebBrowser>  mWebBrowser; // [OWNER]
    nsCOMPtr<nsIWebNavigation>     mNavigation;
    nsCOMPtr<nsISHistory>          mSessionHistory;
};

#endif /* __REmbedBrowser_h */

/*********************** Header File End ******************************/
/*********************** Source File Begin ****************************/

//for NS_WEBBROWSER_CONTRACTID
#include <nsCWebBrowser.h>
//for nsIWebBrowser
#include <nsIWebBrowser.h>
//for do_CreateInstance
#include <nsIComponentManager.h>
// for do_GetInterface
#include <nsIInterfaceRequestor.h>
#include <nsString.h>
#include "nsNativeCharsetUtils.h"
#include <nsEmbedAPI.h>
#include "REmbedBrowser.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

REmbedBrowser::REmbedBrowser(void)
{
    mWebBrowser = nsnull;
    mNavigation = nsnull;
    mSessionHistory = nsnull;
}

REmbedBrowser::~REmbedBrowser()
{
}

NS_IMPL_ISUPPORTS0(REmbedBrowser)

nsresult REmbedBrowser::Init(void)
{
    nsresult rv = NS_InitEmbedding(nsnull, nsnull);
    if (NS_FAILED(rv))
        return rv;

    // create our nsIWebBrowser object
    mWebBrowser = do_CreateInstance(NS_WEBBROWSER_CONTRACTID, &rv);
    if (NS_FAILED(rv))
        return rv;
    if (!mWebBrowser)
        return NS_ERROR_FAILURE;

    // get a handle on the navigation object
    mNavigation = do_QueryInterface(mWebBrowser, &rv);
    if (NS_FAILED(rv))
        return rv;

    // Create our session history object and tell the
    // navigation object to use it.We need to do this
    // before we create the web browser window.
    mSessionHistory = do_CreateInstance(NS_SHISTORY_CONTRACTID,
                        &rv);
    if (NS_FAILED(rv))
        return rv;
    mNavigation->SetSessionHistory(mSessionHistory);

    return NS_OK;
}

nsresult REmbedBrowser::Deinit(void)
{
    NS_TermEmbedding();
    return NS_OK;
}

nsresult REmbedBrowser::LoadURI(const char *uri)
{
    nsString mURI;
    uri = "www.yahoo.com";
    NS_CopyNativeToUnicode(nsDependentCString(uri), mURI);
        //mNavigation->LoadURI(NS_ConvertASCIItoUCS2(uri).get(),
    nsresult rv = mNavigation->LoadURI(mURI.get(),
            nsIWebNavigation::LOAD_FLAGS_NONE,
            nsnull,
            nsnull,
            nsnull);
    printf("LoadURI rv = %08X\n", rv);

    uri = "www.google.com";
    NS_CopyNativeToUnicode(nsDependentCString(uri), mURI);
        //mNavigation->LoadURI(NS_ConvertASCIItoUCS2(uri).get(),
    mNavigation->LoadURI(mURI.get(),
            nsIWebNavigation::LOAD_FLAGS_NONE,
            nsnull,
            nsnull,
            nsnull);
    printf("LoadURI rv = %08X\n", rv);
        return NS_OK;
}

/*********************** Source File End ******************************/
/*********************** Helper File Begin ****************************/
#include <stdio.h>

#include "REmbedBrowser.h"

int r_embed_run(void)
{
    nsresult rv = NS_OK;
    REmbedBrowser *browser = new REmbedBrowser();
    rv = browser->Init();
    printf("Init rv = %08X\n", rv);

    rv = browser->LoadURI("www.yahoo.com");
    if(rv == NS_OK)
    {
        printf("URL loaded successfully\n");
    }
    else
    {
        printf("URL load failed\n");
        return rv;
    }
    browser->Deinit();
    return NS_OK;
}
/*********************** Helper File End ******************************/
/*********************** Test File Begin ******************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "rmozembed.h"

int main(void)
{
    int res;

    res = r_embed_run();
    printf("res = %08X\n", res);
}
/*********************** Test File End ********************************/

You need to have a window created before you call the navigate method. To create a window, you have to implement these base classes at minimum:

nsIWindowCreator
nsIWebBrowserChrome
nsIEmbeddingSiteWindow
nsIWebProgressListener

Check this link out, it will explain what the above interfaces do.
http://www.mozilla.org/projects/embedding/embedoverview/EmbeddingBasics13.html

How I've got mine setup is I actually have 2 classes. One just uses the nsIWindowCreator as the base class...it handles all the creation of the windows..JS popups, security popups, etc. Then my 2nd class uses the other interfaces (nsIWebBrowserChrome, nsIEmbeddingSiteWindow, nsIWebProgressListener, and a few others) as the base classes.

Here is a brief overview of how I initialize everything...
In my main class (nsIWindowCreator as the base) I have an Initialization function that gets passed the directory to the bin and the profile name I want to use. In that function I call NS_InitEmbedding, setup a profile (take a look at NS_NewProfileDirServiceProvider ()), and setup my window watcher. There is only one function you have to implement in the nsIWindowCreator interface...that is the nsIWindowCreator::CreateChromeWindow () method. When you setup a window watcher like I did above, any calls to create new web windows..like a JS popup..will call this function. You have to implement it otherwise your JS alerts/popups won't work. Once that is setup, you can call the CreateChromeWindow method yourself from your code to start your first window. Within that function You have to create an HWND (assuming you are on Win32 platform) and create an instance of your other class (the one that has nsIWebBrowserChrome, nsIWebProgressListener and nsIEmbeddingSiteWindow). Now you have to tell it to use that HWND to display all that HTML. This is where you create your WebBrowser instance like you have in your code already, then you call the nsIWebBrowser::SetContainerWindow () method from the web browser object you just created and pass it the 2nd class that you just created...as the browser needs a "home". Query interface on the Web Browser for a nsIBaseWindow and call the InitWindow () function on it. This is where you pass it the HWND and the size you want it to be...then call the Create () function right after that. Don't forget to call the SetVisibility () method on the base window here too. From the web browser object, you can query interface for a nsIWebBrowserFocus object. Once you have it, call the Activate () method...otherwise you won't be able to type in any of the edit boxes. That's pretty much the basics on getting a browser setup...I didn't include all the details, otherwise you'd be reading a lot longer email, I think it's getting too long already. I hope this helps, if you will look in \mozilla\embedding\tests, there is the winEmbed and mfcembed examples there. I used the winEmbed example more to get started as the code is not as bloated as the mfcembed. These will be your best source for getting started. If you aren't up to implementing all the above, there is always the ActiveX object from Adam Lock. Let me know if you have any more questions and I'll try to answer them.

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

Reply via email to