Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

embedding browser without UI

3 views
Skip to first unread message

Raghunath

unread,
Feb 8, 2006, 1:16:52 AM2/8/06
to
Hi,

I want to embed the browser engine (without UI) in an application.
Its only purpose would be to fetch the URL and return the page contents.
Can this be achieved?

Thanks,
Raghu

Niky Williams

unread,
Feb 8, 2006, 10:28:31 AM2/8/06
to Raghunath

Yup, when you embed your browser, just don't create a UI. Do a search
for embedding on http://www.Mozilla.org/developer and that should get
you started.

Niky Williams

Raghunath

unread,
Feb 9, 2006, 7:13:35 AM2/9/06
to
Hi,

I was trying to get a simple sample code snippet that would get me
started for embedding.
Can someone help me regarding this..

Thanks
Raghu

Niky Williams

unread,
Feb 9, 2006, 8:04:22 AM2/9/06
to Raghunath

Raghu,
Well, there is more to it than just a simple code snippet, you have 1 of
2 options here. You can implement the interfaces yourself or you can
use Adam Lock's ActiveX control. The ActiveX control is pretty straight
forward for just displaying a web page, but if think you will need
somemore robust stuff like having your code reacting to button clicks in
the HTML, then you need to implement the interfaces yourself. What OS
and environment will you be using? Either way, you will need to also
download and build the mozilla source on your system so you will have
the required files. Sounds more complicated than it is. If you are
using Windows, I can help you get started, let me know what you are
wanting to use.

Niky Williams

Raghunath

unread,
Feb 10, 2006, 7:52:26 AM2/10/06
to
Hi Niky,

Thanks for your help.

I am able to write a small program which creates an instance of
nsIWebBrowser.
As soon as I try to get an handle to a navigation object through
do_QueryInterface, I get an unresolved reference error.

My class has the following lines for "nsISupports"

NS_IMPL_ADDREF(<my class>)
NS_IMPL_RELEASE(<my class>)

How do I add an implementation for QueryInterface?

Thanks for your help.

Niky Williams

unread,
Feb 10, 2006, 9:26:10 AM2/10/06
to Raghunath


Raghu,
What objects do you have implemented in your class? It COULD be that
it's not sure which object to use query...not real sure on that error.
Here is what I've got for my interface map...I believe it's what tells
QueryInterface what object it needs to use when it's invoked.

NS_INTERFACE_MAP_BEGIN (<my class>)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS (nsISupports, nsIWebBrowserChrome)
NS_INTERFACE_MAP_ENTRY (nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY (nsIWebBrowserChrome)
NS_INTERFACE_MAP_ENTRY (nsIEmbeddingSiteWindow)
NS_INTERFACE_MAP_ENTRY (nsIEmbeddingSiteWindow2)
NS_INTERFACE_MAP_ENTRY (nsIWebProgressListener)
NS_INTERFACE_MAP_ENTRY (nsIDOMEventListener)
NS_INTERFACE_MAP_ENTRY (nsISupportsWeakReference)
NS_INTERFACE_MAP_END

Raghunath

unread,
Feb 14, 2006, 2:56:46 AM2/14/06
to
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 ********************************/

Niky Williams

unread,
Feb 14, 2006, 8:10:11 AM2/14/06
to Raghunath

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

Callum Prentice

unread,
Feb 14, 2006, 12:46:06 PM2/14/06
to
http://www.ubrowser.com/downloads.php

There might be something useful here - sounds like it's a bit like what you want to do.

Cal.

Raghunath

unread,
Feb 20, 2006, 2:15:20 AM2/20/06
to Niky.W...@ntsmarketing.com, cal...@lindenlab.com
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

Raghunath

unread,
Feb 20, 2006, 4:44:15 AM2/20/06
to Niky.W...@ntsmarketing.com, cal...@lindenlab.com
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

Raghunath

unread,
Feb 20, 2006, 4:44:44 AM2/20/06
to
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

unread,
Feb 20, 2006, 8:42:12 AM2/20/06
to Raghunath

I understand what you are asking now...I thought you just wanted to
"disable" the GUI...didn't realize you wanted skip the code completely.
Unless someone can correct me, I do not believe there is a way to do
what you are asking without implementing those classes. The
nsIWebBrowser object needs support from those other classes...I don't
believe it will work just on it's own. Also, just because you implement
the "chrome" doesn't mean you have to create status bars, menus, etc.
You can skip all that...I did in my code. I've got just a window, no
fancy GUI. Perhaps a more experienced developer can shed some light on
this subject?

Niky Williams

0 new messages