This fix adds basic authentication capability to webclient.

public interface Prompt

 * The custom app must implement this interface in order to supply the
 * underlying browser with basic authentication behavior.  The custom
 * app must tell webclient about its Prompt implementation by calling
 * Navigation.setPrompt().  This must be done FOR EACH BrowserControl
 * instance!

public void setPrompt(Prompt yourPrompt);

 * Gives this Navigation instance the ability to call back the custom
 * app when a site with basic authentication is encountered.  The custom
 * app can choose to put up appropriate modal UI.

Please note that due to bug
http://bugzilla.mozilla.org/show_bug.cgi?id=61669 you must disable the
cache to have this work.  Put these lines in your prefs file:

user_pref("browser.cache.disk_cache_size", 0);
user_pref("browser.cache.enabled", false);

The following files are in this fix:

M classes_spec/org/mozilla/webclient/Navigation.java
M classes_spec/org/mozilla/webclient/test/EMWindow.java
A classes_spec/org/mozilla/webclient/test/PasswordDialog.java
M classes_spec/org/mozilla/webclient/wrapper_native/NavigationImpl.java
M src_moz/CBrowserContainer.cpp
M src_moz/CBrowserContainer.h
M src_moz/NavigationImpl.cpp
M src_moz/wcIBrowserContainer.h
M src_moz/motif/NativeLoaderStub.cpp
M src_share/jni_util.cpp
M src_share/jni_util.h
M src_share/jni_util_export.cpp
M src_share/jni_util_export.h
A classes_spec/org/mozilla/webclient/Prompt.java


git-svn-id: svn://10.0.0.236/branches/JAVADEV_RTM_20001102@83172 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
edburns%acm.org
2000-12-01 01:33:52 +00:00
parent 3901e08768
commit a014aefe38
14 changed files with 681 additions and 18 deletions

View File

@@ -38,6 +38,8 @@
#include "dom_util.h"
#include "nsActions.h"
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
#if defined(XP_UNIX) || defined(XP_MAC) || defined(XP_BEOS)
@@ -47,12 +49,136 @@ static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
#define WC_ITOA(intVal, buf, radix) itoa(intVal, buf, radix)
#endif
static jobject gAuthProperties = nsnull;
class wsPromptEvent : public nsActionEvent {
public:
wsPromptEvent (WebShellInitContext *yourInitContext,
jobject yourPromptGlobalRef,
const PRUnichar *dialogTitle,
const PRUnichar *text,
const PRUnichar *passwordRealm,
PRUint32 savePassword,
PRUnichar **outUser,
PRUnichar **outPwd,
PRBool *_retval);
void * handleEvent (void);
protected:
WebShellInitContext *mInitContext;
jobject mPromptGlobalRef;
nsAutoString mDialogTitle;
nsAutoString mText;
nsAutoString mPasswordRealm;
PRUint32 mSavePassword;
PRUnichar **mOutUser;
PRUnichar **mOutPwd;
PRBool *mRetVal;
};
wsPromptEvent::wsPromptEvent(WebShellInitContext *yourInitContext,
jobject yourPromptGlobalRef,
const PRUnichar *dialogTitle,
const PRUnichar *text,
const PRUnichar *passwordRealm,
PRUint32 savePassword,
PRUnichar **outUser,
PRUnichar **outPwd,
PRBool *_retval) :
mInitContext(yourInitContext), mPromptGlobalRef(yourPromptGlobalRef),
mDialogTitle(dialogTitle), mText(text),
mPasswordRealm(passwordRealm), mOutUser(outUser),
mOutPwd(outPwd), mRetVal(_retval)
{
}
void *wsPromptEvent::handleEvent()
{
JNIEnv *env = (JNIEnv *) JNU_GetEnv(gVm, JNI_VERSION);
jstring title = nsnull;
jstring text = nsnull;
jstring passwordRealm = nsnull;
jboolean result = JNI_FALSE;
jstring user = nsnull;
jstring password = nsnull;
const jchar *userJchar = nsnull;
const jchar *passwordJchar = nsnull;
jclass promptClass = nsnull;
jmethodID mid = nsnull;
nsAutoString autoUser;
nsAutoString autoPassword;
if (!gAuthProperties) {
return (void *) NS_ERROR_FAILURE;
}
// step one, convert to strings
if (mDialogTitle.GetUnicode()) {
title = ::util_NewString(env,
(const jchar *) mDialogTitle.GetUnicode(),
mDialogTitle.Length());
}
if (mText.GetUnicode()) {
text = ::util_NewString(env, (const jchar *) mText.GetUnicode(),
mText.Length());
}
if (mPasswordRealm.GetUnicode()) {
passwordRealm = ::util_NewString(env,
(const jchar *)
mPasswordRealm.GetUnicode(),
mPasswordRealm.Length());
}
#ifdef BAL_INTERFACE
#else
// step two, call the java method.
if (!(promptClass = env->GetObjectClass(mPromptGlobalRef))) {
return (void *) NS_ERROR_FAILURE;
}
if (!(mid = env->GetMethodID(promptClass, "promptUsernameAndPassword",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ILjava/util/Properties;)Z"))) {
return (void *) NS_ERROR_FAILURE;
}
result = env->CallBooleanMethod(mPromptGlobalRef, mid, title, text,
passwordRealm, (jint) mSavePassword,
gAuthProperties);
#endif
// pull userName and password entries out of the properties table
user = (jstring) ::util_GetFromPropertiesObject(env, gAuthProperties,
USER_NAME_KEY, (jobject)
&(mInitContext->shareContext));
userJchar = ::util_GetStringChars(env, user);
autoUser = (PRUnichar *) userJchar;
*mOutUser = autoUser.ToNewUnicode();
::util_ReleaseStringChars(env, user, userJchar);
password = (jstring) ::util_GetFromPropertiesObject(env, gAuthProperties,
PASSWORD_KEY, (jobject)
&(mInitContext->shareContext));
passwordJchar = ::util_GetStringChars(env, password);
autoPassword = (PRUnichar *) passwordJchar;
*mOutPwd = autoPassword.ToNewUnicode();
::util_ReleaseStringChars(env, password, passwordJchar);
*mRetVal = (result == JNI_TRUE) ? PR_TRUE : PR_FALSE;
::util_DeleteString(env, title);
::util_DeleteString(env, text);
::util_DeleteString(env, passwordRealm);
return (void *) NS_OK;
}
CBrowserContainer::CBrowserContainer(nsIWebBrowser *pOwner, JNIEnv *env,
WebShellInitContext *yourInitContext) :
m_pOwner(pOwner), mJNIEnv(env), mInitContext(yourInitContext),
mDocTarget(nsnull), mMouseTarget(nsnull), mDomEventTarget(nsnull),
inverseDepth(-1), properties(nsnull), currentDOMEvent(nsnull)
mDocTarget(nsnull), mMouseTarget(nsnull), mPrompt(nsnull),
mDomEventTarget(nsnull), inverseDepth(-1),
properties(nsnull), currentDOMEvent(nsnull)
{
NS_INIT_REFCNT();
// initialize the string constants (including properties keys)
@@ -170,7 +296,50 @@ NS_IMETHODIMP CBrowserContainer::PromptUsernameAndPassword(const PRUnichar *dial
PRUnichar **pwd,
PRBool *_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
nsresult rv = NS_ERROR_FAILURE;
// if the user hasn't given us a prompt, oh well
if (!mPrompt) {
return NS_OK;
}
JNIEnv *env = (JNIEnv *) JNU_GetEnv(gVm, JNI_VERSION);
// PENDING(edburns): uniformly apply checks for this throughout the
// code
PR_ASSERT(mInitContext);
PR_ASSERT(mInitContext->initComplete);
// try to initialize the properties object for basic auth
if (!gAuthProperties) {
gAuthProperties =
::util_CreatePropertiesObject(env, (jobject)
&(mInitContext->shareContext));
if (!gAuthProperties) {
printf("Error: can't create properties object for authentitication");
return NS_OK;
}
}
else {
::util_ClearPropertiesObject(env, gAuthProperties, (jobject)
&(mInitContext->shareContext));
}
wsPromptEvent *actionEvent = nsnull;
void *voidResult = nsnull;
if (!(actionEvent = new wsPromptEvent(mInitContext, mPrompt,
dialogTitle, text,
passwordRealm, savePassword,
user, pwd, _retval))) {
::util_ThrowExceptionToJava(env, "Exception: PromptUserNameAndPassword: can't create wsPromptEvent");
return rv;
}
// the out params to this method are set in wsPromptEvent::handleEvent()
voidResult = ::util_PostSynchronousEvent(mInitContext,
(PLEvent *) *actionEvent);
return (nsresult) voidResult;
}
/* boolean promptPassword (in wstring text, in wstring title, out wstring pwd); */
@@ -1019,6 +1188,24 @@ NS_IMETHODIMP CBrowserContainer::AddDocumentLoadListener(jobject target)
return rv;
}
NS_IMETHODIMP CBrowserContainer::SetPrompt(jobject target)
{
nsresult rv = NS_OK;
JNIEnv *env = (JNIEnv *) JNU_GetEnv(gVm, JNI_VERSION);
if (mPrompt) {
::util_DeleteGlobalRef(env, mPrompt);
mPrompt = nsnull;
}
if (nsnull == (mPrompt = ::util_NewGlobalRef(env, target))) {
::util_ThrowExceptionToJava(env, "Exception: Navigation.nativeSetPrompt(): can't create NewGlobalRef\n\tfor argument");
rv = NS_ERROR_NULL_POINTER;
}
return rv;
}
NS_IMETHODIMP CBrowserContainer::RemoveMouseListener()
{
nsresult rv = NS_OK;

View File

@@ -86,6 +86,7 @@ protected:
WebShellInitContext *mInitContext;
jobject mDocTarget;
jobject mMouseTarget;
jobject mPrompt;
nsCOMPtr<nsIDOMEventTarget> mDomEventTarget;
//

View File

@@ -191,3 +191,34 @@ JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NavigationImpl
::util_PostEvent(initContext, event);
}
}
JNIEXPORT void JNICALL
Java_org_mozilla_webclient_wrapper_1native_NavigationImpl_nativeSetPrompt
(JNIEnv *env, jobject obj, jint webShellPtr, jobject userPrompt)
{
JNIEnv * pEnv = env;
jobject jobj = obj;
WebShellInitContext* initContext = (WebShellInitContext *) webShellPtr;
if (initContext == nsnull) {
::util_ThrowExceptionToJava(env, "Exception: null webShellPtr passed to nativeSetPrompt");
return;
}
if (userPrompt == nsnull) {
::util_ThrowExceptionToJava(env, "Exception: null properties passed to nativeSetPrompt");
return;
}
if (!initContext->initComplete) {
return;
}
// IMPORTANT: do the DeleteGlobalRef when we set a new prompt!
PR_ASSERT(initContext->browserContainer);
initContext->browserContainer->SetPrompt(userPrompt);
}

View File

@@ -143,6 +143,7 @@ void (* nativeLoadURL) (JNIEnv *, jobject, jint, jstring);
void (* nativeLoadFromStream) (JNIEnv *, jobject, jint, jobject, jstring, jstring, jint, jobject);
void (* nativeRefresh) (JNIEnv *, jobject, jint, jlong);
void (* nativeStop) (JNIEnv *, jobject, jint);
void (* nativeSetPrompt) (JNIEnv *, jobject, jint, jobject);
// from RDFEnumeration.h
void (* nativeFinalize) (JNIEnv *, jobject, jint);
jboolean (* nativeHasMoreElements) (JNIEnv *, jobject, jint, jint);
@@ -275,6 +276,10 @@ void locateBrowserControlStubFunctions(void * dll) {
if (!nativeStop) {
printf("got dlsym error %s\n", dlerror());
}
nativeSetPrompt = (void (*) (JNIEnv *, jobject, jint, jobject)) dlsym(dll, "Java_org_mozilla_webclient_wrapper_1native_NavigationImpl_nativeSetPrompt");
if (!nativeSetPrompt) {
printf("got dlsym error %s\n", dlerror());
}
nativeAddRef = (void (*) (JNIEnv *, jobject, jint)) dlsym(dll, "Java_org_mozilla_webclient_wrapper_1native_ISupportsPeer_nativeAddRef");
if (!nativeAddRef) {
@@ -791,6 +796,12 @@ JNIEXPORT void JNICALL Java_org_mozilla_webclient_wrapper_1native_NavigationImpl
(* nativeStop) (env, obj, webShellPtr);
}
JNIEXPORT void JNICALL
Java_org_mozilla_webclient_wrapper_1native_NavigationImpl_nativeSetPrompt
(JNIEnv *env, jobject obj, jint webShellPtr, jobject userPrompt)
{
(* nativeSetPrompt) (env, obj, webShellPtr, userPrompt);
}
// RDFEnumeration

View File

@@ -46,6 +46,7 @@ public:
NS_IMETHOD AddMouseListener(jobject target) = 0;
NS_IMETHOD AddDocumentLoadListener(jobject target) = 0;
NS_IMETHOD SetPrompt(jobject target) = 0;
NS_IMETHOD RemoveAllListeners() = 0;
NS_IMETHOD RemoveMouseListener() = 0;
NS_IMETHOD RemoveDocumentLoadListener() = 0;
@@ -55,6 +56,7 @@ public:
#define NS_DECL_WCIBROWSERCONTAINER \
NS_IMETHOD AddMouseListener(jobject target); \
NS_IMETHOD AddDocumentLoadListener(jobject target); \
NS_IMETHOD SetPrompt(jobject target); \
NS_IMETHOD RemoveAllListeners(); \
NS_IMETHOD RemoveMouseListener(); \
NS_IMETHOD RemoveDocumentLoadListener();