From 2329e0078186ac1fbfa4e78751a1907e895b1590 Mon Sep 17 00:00:00 2001 From: "edburns%acm.org" Date: Wed, 7 Mar 2007 21:02:54 +0000 Subject: [PATCH] To address the issue of listening for XMLHttpRequest traffic, I've taken a cue from Doron Rosenberg in #developers and looked at the Eclipse ATF project's XHRObserver.java. This was my first look at java code that uses the java xpcom bridge, and I'm very impressed. Once I get webclient 2.0 done, I'll definately rewrite as much as possible of the mozilla implementation using the java xpcom bridge. For now, I'm going to continue to crank with my "on-demand hand coded JNI C++" approach. I think I can get results pretty quickly with this. For example, just yesterday I learned that the regular nsIWebProgressListener doesn't get notifications on Ajax requests, and now I see a way to do it (thanks to Doron). Here is the work in progress. A webclient/src_moz/AjaxListener.cpp A webclient/src_moz/AjaxListener.h M logging.properties - set "ALL" for MCP level M dist/mcp-test/src/test/java/cardemo/CarDemoTest.java - Cause an Ajax transaction to happen M webclient/classes_spec/org/mozilla/mcp/MCP.java - log messages for outgoing HTTP requests M webclient/src_moz/EmbedProgress.cpp M webclient/src_moz/EmbedProgress.h - Leverage new AjaxListener class M webclient/src_moz/Makefile.in - add xmlextras, to include nsIXMLHttpRequest. git-svn-id: svn://10.0.0.236/trunk@221474 18797224-902f-48f8-a5cc-f745e15eee43 --- .../src/test/java/cardemo/CarDemoTest.java | 13 +- mozilla/java/logging.properties | 5 +- .../classes_spec/org/mozilla/mcp/MCP.java | 28 +++- .../java/webclient/src_moz/AjaxListener.cpp | 127 ++++++++++++++++++ mozilla/java/webclient/src_moz/AjaxListener.h | 51 +++++++ .../java/webclient/src_moz/EmbedProgress.cpp | 43 +++++- .../java/webclient/src_moz/EmbedProgress.h | 6 + mozilla/java/webclient/src_moz/Makefile.in | 2 + 8 files changed, 264 insertions(+), 11 deletions(-) create mode 100755 mozilla/java/webclient/src_moz/AjaxListener.cpp create mode 100755 mozilla/java/webclient/src_moz/AjaxListener.h diff --git a/mozilla/java/dist/mcp-test/src/test/java/cardemo/CarDemoTest.java b/mozilla/java/dist/mcp-test/src/test/java/cardemo/CarDemoTest.java index baf0080613d..5a052bbab2b 100755 --- a/mozilla/java/dist/mcp-test/src/test/java/cardemo/CarDemoTest.java +++ b/mozilla/java/dist/mcp-test/src/test/java/cardemo/CarDemoTest.java @@ -38,12 +38,17 @@ public class CarDemoTest extends WebclientTestCase { public void testCardemo() throws Exception { mcp.getRealizedVisibleBrowserWindow(); - mcp.blockingLoad("http://webdev1.sun.com/jsf-ajax-cardemo/faces/chooseLocale.jsp"); - mcp.blockingClickElement("j_id_id73:Germany"); - mcp.blockingClickElement("j_id_id18:j_id_id43"); + // Load the main page of the app + mcp.blockingLoad("http://webdev1.sun.com/jsf-ajax-cardemo/faces/chooseLocale.jsp"); + // Choose the "German" language button + mcp.blockingClickElement("j_id_id73:Germany"); + // Choose the roadster + mcp.blockingClickElement("j_id_id18:j_id_id43"); + // Choose the "Tempomat" checkbox + mcp.clickElement("j_id_id21:j_id_id67j_id_1"); - Thread.currentThread().sleep(30000); + Thread.currentThread().sleep(10000); } } diff --git a/mozilla/java/logging.properties b/mozilla/java/logging.properties index 4e922cbe93b..9289c5a18af 100644 --- a/mozilla/java/logging.properties +++ b/mozilla/java/logging.properties @@ -1,12 +1,9 @@ handlers= java.util.logging.FileHandler -.level=INFO - - java.util.logging.FileHandler.pattern = %h/moz-java-log.xml java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.append = true java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter org.mozilla.webclient.level=INFO - +org.mozilla.mcp.level=ALL diff --git a/mozilla/java/webclient/classes_spec/org/mozilla/mcp/MCP.java b/mozilla/java/webclient/classes_spec/org/mozilla/mcp/MCP.java index 4a26f0fe12d..5131067daea 100755 --- a/mozilla/java/webclient/classes_spec/org/mozilla/mcp/MCP.java +++ b/mozilla/java/webclient/classes_spec/org/mozilla/mcp/MCP.java @@ -292,8 +292,9 @@ public class MCP { } public void eventDispatched(WebclientEvent webclientEvent) { - Map eventData = - (Map) webclientEvent.getEventData(); + boolean logInfo = LOGGER.isLoggable(Level.INFO); + Map eventData = + (Map) webclientEvent.getEventData(); long type = webclientEvent.getType(); switch ((int)type) { @@ -302,6 +303,29 @@ public class MCP { owner.notifyAll(); } break; + case ((int) DocumentLoadEvent.START_URL_LOAD_EVENT_MASK): + String method = (String) eventData.get("method"); + method = (null != method) ? method : "no method given"; + String uri = (String) eventData.get("URI"); + uri = (null != uri) ? uri : "no URI given"; + Map requestHeaders = (Map) + eventData.get("headers"); + StringBuffer headerValues = null; + if (null != requestHeaders) { + headerValues = new StringBuffer(); + for (String cur : requestHeaders.keySet()) { + headerValues.append(cur + ": " + + requestHeaders.get(cur) + "\n"); + } + } + headerValues = (null != headerValues) ? headerValues : + new StringBuffer("no headers given: "); + if (logInfo) { + LOGGER.info("HTTP REQUEST:\n" + + method + " " + uri + "\n" + + headerValues); + } + break; default: break; } diff --git a/mozilla/java/webclient/src_moz/AjaxListener.cpp b/mozilla/java/webclient/src_moz/AjaxListener.cpp new file mode 100755 index 00000000000..45d8200c75e --- /dev/null +++ b/mozilla/java/webclient/src_moz/AjaxListener.cpp @@ -0,0 +1,127 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Christopher Blizzard. + * Portions created by Christopher Blizzard are Copyright (C) + * Christopher Blizzard. All Rights Reserved. + * + * Contributor(s): + * Ed Burns + */ + +#include "AjaxListener.h" +#include "EmbedProgress.h" + +#include "nsServiceManagerUtils.h" +#include "nsIObserverService.h" +#include "nsIHttpChannel.h" +#include "nsIRequest.h" +#include "nsIXMLHTTPRequest.h" +#include "nsIInterfaceRequestor.h" + +#include "jni_util.h" + +NS_IMPL_ISUPPORTS1(AjaxListener, nsIObserver) + +AjaxListener::AjaxListener(EmbedProgress *owner, + JNIEnv *env, + jobject eventRegistration) : + mOwner(owner), + mJNIEnv(env), + mEventRegistration(eventRegistration) +{ +} + +AjaxListener::~AjaxListener() +{ +} + +// +// Methods from nsIObserver +// + +NS_IMETHODIMP +AjaxListener::Observe(nsISupports *aSubject, + const char *aTopic, const PRUnichar *aData) +{ + nsresult rv = NS_OK; + if (0 != strcmp("http-on-modify-request", aTopic)) { + return rv; + } + + nsCOMPtr channel = do_QueryInterface(aSubject, &rv); + nsCOMPtr request = nsnull; + nsCOMPtr iface = nsnull; + nsCOMPtr ajax = nsnull; + nsLoadFlags loadFlags = 0; + + if (NS_SUCCEEDED(rv) && channel) { + request = do_QueryInterface(aSubject, &rv); + if (NS_SUCCEEDED(rv) && request) { + rv = request->GetLoadFlags(&loadFlags); + // If this is an XMLHttpRequest,loadFlags would + // have LOAD_BACKGROUND + if (NS_SUCCEEDED(rv) && + (loadFlags & nsIRequest::LOAD_BACKGROUND)) { + rv = channel->GetNotificationCallbacks(getter_AddRefs(iface)); + if (NS_SUCCEEDED(rv) && iface) { + ajax = do_QueryInterface(iface, &rv); + if (NS_SUCCEEDED(rv) && ajax) { + loadFlags = 0; + } + else { + // It's ok if we don't have an xmlhttprequest + rv = NS_OK; + } + } + else { + // It's ok if we don't have an iface. + rv = NS_OK; + } + } + } + } + + return rv; +} + +// +// Webclient private methods +// + +NS_IMETHODIMP +AjaxListener::StartObserving(void) +{ + nsresult rv = NS_ERROR_NOT_IMPLEMENTED; + + nsCOMPtr obsService = do_GetService("@mozilla.org/observer-service;1", &rv); + if (obsService && NS_SUCCEEDED(rv)) { + rv = obsService->AddObserver(this, "http-on-modify-request", PR_FALSE); + } + + return rv; +} + +NS_IMETHODIMP +AjaxListener::StopObserving() +{ + nsresult rv = NS_ERROR_NOT_IMPLEMENTED; + + nsCOMPtr obsService = do_GetService("@mozilla.org/observer-service;1", &rv); + if (obsService && NS_SUCCEEDED(rv)) { + rv = obsService->RemoveObserver(this, "http-on-modify-request"); + } + + return rv; +} + diff --git a/mozilla/java/webclient/src_moz/AjaxListener.h b/mozilla/java/webclient/src_moz/AjaxListener.h new file mode 100755 index 00000000000..eebff88e05d --- /dev/null +++ b/mozilla/java/webclient/src_moz/AjaxListener.h @@ -0,0 +1,51 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is Christopher Blizzard. + * Portions created by Christopher Blizzard are Copyright (C) + * Christopher Blizzard. All Rights Reserved. + * + * Contributor(s): + * Ed Burns + */ + +#ifndef __AjaxListener_h__ +#define __AjaxListener_h__ + + +#include "nsIObserver.h" + +#include "jni.h" + +class EmbedProgress; + +class AjaxListener : public nsIObserver +{ +public: + NS_DECL_ISUPPORTS + NS_DECL_NSIOBSERVER + + AjaxListener(EmbedProgress *owner, + JNIEnv *env, jobject eventRegistration); + virtual ~AjaxListener(); + + NS_IMETHOD StartObserving(void); + NS_IMETHOD StopObserving(void); + +private: + EmbedProgress* mOwner; + JNIEnv *mJNIEnv; + jobject mEventRegistration; + }; + +#endif diff --git a/mozilla/java/webclient/src_moz/EmbedProgress.cpp b/mozilla/java/webclient/src_moz/EmbedProgress.cpp index 77e78c3c28d..e028288ac5e 100644 --- a/mozilla/java/webclient/src_moz/EmbedProgress.cpp +++ b/mozilla/java/webclient/src_moz/EmbedProgress.cpp @@ -37,10 +37,13 @@ #include "NativeBrowserControl.h" #include "HttpHeaderVisitorImpl.h" +#include "AjaxListener.h" #include "ns_globals.h" // for prLogModuleInfo -EmbedProgress::EmbedProgress(void) : mCapturePageInfo(JNI_FALSE) +EmbedProgress::EmbedProgress(void) : + mCapturePageInfo(JNI_FALSE), + mAjaxListener(nsnull) { mOwner = nsnull; mEventRegistration = nsnull; @@ -53,6 +56,7 @@ EmbedProgress::~EmbedProgress() ::util_DeleteGlobalRef(env, mEventRegistration); mEventRegistration = nsnull; } + RemoveAjaxListener(); } @@ -94,6 +98,17 @@ nsresult EmbedProgress::SetCapturePageInfo(jboolean newState) { mCapturePageInfo = newState; + nsresult rv = NS_OK; + AjaxListener *observer = nsnull; + rv = GetAjaxListener(&observer); + if (observer && NS_SUCCEEDED(rv)) { + if (mCapturePageInfo) { + observer->StartObserving(); + } + else { + observer->StopObserving(); + } + } return NS_OK; } @@ -464,6 +479,32 @@ EmbedProgress::OnSecurityChange(nsIWebProgress *aWebProgress, return NS_OK; } +NS_IMETHODIMP +EmbedProgress::GetAjaxListener(AjaxListener* *result) +{ + if (nsnull == result) { + return NS_ERROR_NULL_POINTER; + } + + nsresult rv = NS_ERROR_FAILURE; + + if (nsnull == mAjaxListener) { + JNIEnv *env = (JNIEnv *) JNU_GetEnv(gVm, JNI_VERSION); + mAjaxListener = new AjaxListener(this, env, mEventRegistration); + } + *result = mAjaxListener; + rv = NS_OK; + + return rv; +} + +NS_IMETHODIMP +EmbedProgress::RemoveAjaxListener(void) +{ + nsresult rv = NS_ERROR_NOT_IMPLEMENTED; + return rv; +} + /* static */ void EmbedProgress::RequestToURIString(nsIRequest *aRequest, char **aString) diff --git a/mozilla/java/webclient/src_moz/EmbedProgress.h b/mozilla/java/webclient/src_moz/EmbedProgress.h index 1e576b14546..d10b93e649a 100644 --- a/mozilla/java/webclient/src_moz/EmbedProgress.h +++ b/mozilla/java/webclient/src_moz/EmbedProgress.h @@ -28,6 +28,7 @@ #include "jni_util.h" class NativeBrowserControl; +class AjaxListener; class EmbedProgress : public nsIWebProgressListener, public nsSupportsWeakReference @@ -48,9 +49,14 @@ class EmbedProgress : public nsIWebProgressListener, private: + NS_IMETHOD GetAjaxListener(AjaxListener** result); + NS_IMETHOD RemoveAjaxListener(void); + static void RequestToURIString (nsIRequest *aRequest, char **aString); NativeBrowserControl *mOwner; + + AjaxListener *mAjaxListener; jobject mEventRegistration; diff --git a/mozilla/java/webclient/src_moz/Makefile.in b/mozilla/java/webclient/src_moz/Makefile.in index 894b4f53866..d81c34543b3 100644 --- a/mozilla/java/webclient/src_moz/Makefile.in +++ b/mozilla/java/webclient/src_moz/Makefile.in @@ -61,6 +61,7 @@ REQUIRES = xpcom \ find \ appcomps \ appshell \ + xmlextras \ uconv \ unicharutil \ pref \ @@ -113,6 +114,7 @@ CPPSRCS = \ EmbedEventListener.cpp \ EventRegistrationImpl.cpp \ HttpHeaderVisitorImpl.cpp \ + AjaxListener.cpp \ NativeEventThread.cpp \ NavigationImpl.cpp \ ns_util.cpp \