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.
This commit is contained in:
edburns%acm.org 2007-03-07 21:02:54 +00:00
Родитель 8932a87cc3
Коммит 29eb9e68f1
8 изменённых файлов: 264 добавлений и 11 удалений

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -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

Просмотреть файл

@ -292,8 +292,9 @@ public class MCP {
}
public void eventDispatched(WebclientEvent webclientEvent) {
Map<String,Object> eventData =
(Map<String,Object>) 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<String,String> requestHeaders = (Map<String,String>)
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;
}

Просмотреть файл

@ -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 <edburns@acm.org>
*/
#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<nsIHttpChannel> channel = do_QueryInterface(aSubject, &rv);
nsCOMPtr<nsIRequest> request = nsnull;
nsCOMPtr<nsIInterfaceRequestor> iface = nsnull;
nsCOMPtr<nsIXMLHttpRequest> 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<nsIObserverService> 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<nsIObserverService> obsService = do_GetService("@mozilla.org/observer-service;1", &rv);
if (obsService && NS_SUCCEEDED(rv)) {
rv = obsService->RemoveObserver(this, "http-on-modify-request");
}
return rv;
}

Просмотреть файл

@ -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 <edburns@acm.org>
*/
#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

Просмотреть файл

@ -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)

Просмотреть файл

@ -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;

Просмотреть файл

@ -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 \