bug 1016747 - add android protocol handler to proxy input streams to Gecko r=snorp

This commit is contained in:
Brad Lassey 2014-06-04 15:28:04 -04:00
parent 07a99475ad
commit 1f635d1287
9 changed files with 359 additions and 1 deletions

View File

@ -16,6 +16,8 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLConnection;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.ByteBuffer;
@ -2720,4 +2722,31 @@ public class GeckoAppShell
toast.show();
}
@WrapElementForJNI(allowMultithread = true)
static InputStream createInputStream(URLConnection connection) throws IOException {
return connection.getInputStream();
}
@WrapElementForJNI(allowMultithread = true, narrowChars = true)
static URLConnection getConnection(String url) throws MalformedURLException, IOException {
String spec;
if (url.startsWith("android://")) {
spec = url.substring(10);
} else {
spec = url.substring(8);
}
// if the colon got stripped, put it back
int colon = spec.indexOf(':');
if (colon == -1 || colon > spec.indexOf('/')) {
spec = spec.replaceFirst("/", ":/");
}
return new URL(spec).openConnection();
}
@WrapElementForJNI(allowMultithread = true, narrowChars = true)
static String connectionGetMimeType(URLConnection connection) {
return connection.getContentType();
}
}

View File

@ -206,6 +206,16 @@ AndroidBridge::Init(JNIEnv *jEnv)
jEGLSurfacePointerField = 0;
}
jChannels = getClassGlobalRef("java/nio/channels/Channels");
jChannelCreate = jEnv->GetStaticMethodID(jChannels, "newChannel", "(Ljava/io/InputStream;)Ljava/nio/channels/ReadableByteChannel;");
jReadableByteChannel = getClassGlobalRef("java/nio/channels/ReadableByteChannel");
jByteBufferRead = jEnv->GetMethodID(jReadableByteChannel, "read", "(Ljava/nio/ByteBuffer;)I");
jInputStream = getClassGlobalRef("java/io/InputStream");
jClose = jEnv->GetMethodID(jInputStream, "close", "()V");
jAvailable = jEnv->GetMethodID(jInputStream, "available", "()I");
InitAndroidJavaWrappers(jEnv);
// jEnv should NOT be cached here by anything -- the jEnv here
@ -2093,3 +2103,41 @@ AndroidBridge::RunDelayedTasks()
}
return -1;
}
jobject AndroidBridge::ChannelCreate(jobject stream) {
JNIEnv *env = GetJNIForThread();
env->PushLocalFrame(1);
jobject channel = env->CallStaticObjectMethod(sBridge->jReadableByteChannel, sBridge->jChannelCreate, stream);
return env->PopLocalFrame(channel);
}
void AndroidBridge::InputStreamClose(jobject obj) {
JNIEnv *env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env, 1);
env->CallVoidMethod(obj, sBridge->jClose);
}
uint32_t AndroidBridge::InputStreamAvailable(jobject obj) {
JNIEnv *env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env, 1);
return env->CallIntMethod(obj, sBridge->jAvailable);
}
nsresult AndroidBridge::InputStreamRead(jobject obj, char *aBuf, uint32_t aCount, uint32_t *aRead) {
JNIEnv *env = GetJNIForThread();
AutoLocalJNIFrame jniFrame(env, 1);
jobject arr = env->NewDirectByteBuffer(aBuf, aCount);
jint read = env->CallIntMethod(obj, sBridge->jByteBufferRead, arr);
if (env->ExceptionCheck()) {
env->ExceptionClear();
return NS_ERROR_FAILURE;
}
if (read <= 0) {
*aRead = 0;
return NS_OK;
}
*aRead = read;
return NS_OK;
}

View File

@ -117,7 +117,6 @@ private:
TimeStamp mRunTime;
};
class AndroidBridge MOZ_FINAL : public mozilla::layers::GeckoContentController
{
public:
@ -344,6 +343,13 @@ public:
static jfieldID GetStaticFieldID(JNIEnv* env, jclass jClass, const char* fieldName, const char* fieldType);
static jmethodID GetMethodID(JNIEnv* env, jclass jClass, const char* methodName, const char* methodType);
static jmethodID GetStaticMethodID(JNIEnv* env, jclass jClass, const char* methodName, const char* methodType);
static jobject ChannelCreate(jobject);
static void InputStreamClose(jobject obj);
static uint32_t InputStreamAvailable(jobject obj);
static nsresult InputStreamRead(jobject obj, char *aBuf, uint32_t aCount, uint32_t *aRead);
protected:
static StaticRefPtr<AndroidBridge> sBridge;
nsTArray<nsCOMPtr<nsIMobileMessageCallback> > mSmsRequests;
@ -378,6 +384,16 @@ protected:
bool QueueSmsRequest(nsIMobileMessageCallback* aRequest, uint32_t* aRequestIdOut);
// intput stream
jclass jReadableByteChannel;
jclass jChannels;
jmethodID jChannelCreate;
jmethodID jByteBufferRead;
jclass jInputStream;
jmethodID jClose;
jmethodID jAvailable;
// other things
jmethodID jNotifyAppShellReady;
jmethodID jGetOutstandingDrawEvents;

View File

@ -20,6 +20,8 @@ jmethodID GeckoAppShell::jCheckURIVisited = 0;
jmethodID GeckoAppShell::jClearMessageList = 0;
jmethodID GeckoAppShell::jCloseCamera = 0;
jmethodID GeckoAppShell::jCloseNotification = 0;
jmethodID GeckoAppShell::jConnectionGetMimeType = 0;
jmethodID GeckoAppShell::jCreateInputStream = 0;
jmethodID GeckoAppShell::jCreateMessageListWrapper = 0;
jmethodID GeckoAppShell::jCreateShortcut = 0;
jmethodID GeckoAppShell::jDeleteMessageWrapper = 0;
@ -34,6 +36,7 @@ jmethodID GeckoAppShell::jEnableNetworkNotifications = 0;
jmethodID GeckoAppShell::jEnableScreenOrientationNotifications = 0;
jmethodID GeckoAppShell::jEnableSensor = 0;
jmethodID GeckoAppShell::jGamepadAdded = 0;
jmethodID GeckoAppShell::jGetConnection = 0;
jmethodID GeckoAppShell::jGetContext = 0;
jmethodID GeckoAppShell::jGetCurrentBatteryInformationWrapper = 0;
jmethodID GeckoAppShell::jGetCurrentNetworkInformationWrapper = 0;
@ -102,6 +105,8 @@ void GeckoAppShell::InitStubs(JNIEnv *jEnv) {
jClearMessageList = getStaticMethod("clearMessageList", "(I)V");
jCloseCamera = getStaticMethod("closeCamera", "()V");
jCloseNotification = getStaticMethod("closeNotification", "(Ljava/lang/String;)V");
jConnectionGetMimeType = getStaticMethod("connectionGetMimeType", "(Ljava/net/URLConnection;)Ljava/lang/String;");
jCreateInputStream = getStaticMethod("createInputStream", "(Ljava/net/URLConnection;)Ljava/io/InputStream;");
jCreateMessageListWrapper = getStaticMethod("createMessageList", "(JJ[Ljava/lang/String;IIZI)V");
jCreateShortcut = getStaticMethod("createShortcut", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
jDeleteMessageWrapper = getStaticMethod("deleteMessage", "(II)V");
@ -116,6 +121,7 @@ void GeckoAppShell::InitStubs(JNIEnv *jEnv) {
jEnableScreenOrientationNotifications = getStaticMethod("enableScreenOrientationNotifications", "()V");
jEnableSensor = getStaticMethod("enableSensor", "(I)V");
jGamepadAdded = getStaticMethod("gamepadAdded", "(II)V");
jGetConnection = getStaticMethod("getConnection", "(Ljava/lang/String;)Ljava/net/URLConnection;");
jGetContext = getStaticMethod("getContext", "()Landroid/content/Context;");
jGetCurrentBatteryInformationWrapper = getStaticMethod("getCurrentBatteryInformation", "()[D");
jGetCurrentNetworkInformationWrapper = getStaticMethod("getCurrentNetworkInformation", "()[D");
@ -295,6 +301,32 @@ void GeckoAppShell::CloseNotification(const nsAString& a0) {
env->PopLocalFrame(nullptr);
}
jstring GeckoAppShell::ConnectionGetMimeType(jobject a0) {
JNIEnv *env = GetJNIForThread();
if (env->PushLocalFrame(2) != 0) {
AndroidBridge::HandleUncaughtException(env);
MOZ_CRASH("Exception should have caused crash.");
}
jobject temp = env->CallStaticObjectMethod(mGeckoAppShellClass, jConnectionGetMimeType, a0);
AndroidBridge::HandleUncaughtException(env);
jstring ret = static_cast<jstring>(env->PopLocalFrame(temp));
return ret;
}
jobject GeckoAppShell::CreateInputStream(jobject a0) {
JNIEnv *env = GetJNIForThread();
if (env->PushLocalFrame(2) != 0) {
AndroidBridge::HandleUncaughtException(env);
MOZ_CRASH("Exception should have caused crash.");
}
jobject temp = env->CallStaticObjectMethod(mGeckoAppShellClass, jCreateInputStream, a0);
AndroidBridge::HandleUncaughtException(env);
jobject ret = static_cast<jobject>(env->PopLocalFrame(temp));
return ret;
}
void GeckoAppShell::CreateMessageListWrapper(int64_t a0, int64_t a1, jobjectArray a2, int32_t a3, int32_t a4, bool a5, int32_t a6) {
JNIEnv *env = AndroidBridge::GetJNIEnv();
if (env->PushLocalFrame(1) != 0) {
@ -478,6 +510,21 @@ void GeckoAppShell::GamepadAdded(int32_t a0, int32_t a1) {
env->PopLocalFrame(nullptr);
}
jobject GeckoAppShell::GetConnection(const nsACString& a0) {
JNIEnv *env = GetJNIForThread();
if (env->PushLocalFrame(2) != 0) {
AndroidBridge::HandleUncaughtException(env);
MOZ_CRASH("Exception should have caused crash.");
}
jstring j0 = AndroidBridge::NewJavaString(env, a0);
jobject temp = env->CallStaticObjectMethod(mGeckoAppShellClass, jGetConnection, j0);
AndroidBridge::HandleUncaughtException(env);
jobject ret = static_cast<jobject>(env->PopLocalFrame(temp));
return ret;
}
jobject GeckoAppShell::GetContext() {
JNIEnv *env = GetJNIForThread();
if (env->PushLocalFrame(1) != 0) {

View File

@ -27,6 +27,8 @@ public:
static void ClearMessageList(int32_t a0);
static void CloseCamera();
static void CloseNotification(const nsAString& a0);
static jstring ConnectionGetMimeType(jobject a0);
static jobject CreateInputStream(jobject a0);
static void CreateMessageListWrapper(int64_t a0, int64_t a1, jobjectArray a2, int32_t a3, int32_t a4, bool a5, int32_t a6);
static void CreateShortcut(const nsAString& a0, const nsAString& a1, const nsAString& a2, const nsAString& a3);
static void DeleteMessageWrapper(int32_t a0, int32_t a1);
@ -41,6 +43,7 @@ public:
static void EnableScreenOrientationNotifications();
static void EnableSensor(int32_t a0);
static void GamepadAdded(int32_t a0, int32_t a1);
static jobject GetConnection(const nsACString& a0);
static jobject GetContext();
static jdoubleArray GetCurrentBatteryInformationWrapper();
static jdoubleArray GetCurrentNetworkInformationWrapper();
@ -108,6 +111,8 @@ protected:
static jmethodID jClearMessageList;
static jmethodID jCloseCamera;
static jmethodID jCloseNotification;
static jmethodID jConnectionGetMimeType;
static jmethodID jCreateInputStream;
static jmethodID jCreateMessageListWrapper;
static jmethodID jCreateShortcut;
static jmethodID jDeleteMessageWrapper;
@ -122,6 +127,7 @@ protected:
static jmethodID jEnableScreenOrientationNotifications;
static jmethodID jEnableSensor;
static jmethodID jGamepadAdded;
static jmethodID jGetConnection;
static jmethodID jGetContext;
static jmethodID jGetCurrentBatteryInformationWrapper;
static jmethodID jGetCurrentNetworkInformationWrapper;

View File

@ -27,6 +27,7 @@ SOURCES += [
'GeneratedJNIWrappers.cpp',
'GfxInfo.cpp',
'NativeJSContainer.cpp',
'nsAndroidProtocolHandler.cpp',
'nsAppShell.cpp',
'nsClipboard.cpp',
'nsDeviceContextAndroid.cpp',
@ -51,6 +52,7 @@ LOCAL_INCLUDES += [
'/docshell/base',
'/dom/base',
'/dom/system/android',
'/netwerk/base/src',
'/netwerk/cache',
'/widget/android/android',
'/widget/shared',

View File

@ -0,0 +1,170 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim:set ts=4 sw=4 sts=4 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsAndroidProtocolHandler.h"
#include "nsCOMPtr.h"
#include "nsIChannel.h"
#include "nsIIOService.h"
#include "nsNetUtil.h"
#include "android/log.h"
#include "nsBaseChannel.h"
#include "AndroidBridge.h"
#include "GeneratedJNIWrappers.h"
using namespace mozilla;
using namespace mozilla::widget::android;
class AndroidInputStream : public nsIInputStream
{
public:
AndroidInputStream(jobject connection) {
JNIEnv *env = GetJNIForThread();
mBridgeInputStream = env->NewGlobalRef(GeckoAppShell::CreateInputStream(connection));
mBridgeChannel = env->NewGlobalRef(AndroidBridge::ChannelCreate(mBridgeInputStream));
}
virtual ~AndroidInputStream() {
JNIEnv *env = GetJNIForThread();
env->DeleteGlobalRef(mBridgeInputStream);
env->DeleteGlobalRef(mBridgeChannel);
}
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIINPUTSTREAM
private:
jobject mBridgeInputStream;
jobject mBridgeChannel;
};
NS_IMPL_ISUPPORTS(AndroidInputStream, nsIInputStream)
NS_IMETHODIMP AndroidInputStream::Close(void) {
mozilla::AndroidBridge::InputStreamClose(mBridgeInputStream);
return NS_OK;
}
NS_IMETHODIMP AndroidInputStream::Available(uint64_t *_retval) {
*_retval = mozilla::AndroidBridge::InputStreamAvailable(mBridgeInputStream);
return NS_OK;
}
NS_IMETHODIMP AndroidInputStream::Read(char *aBuf, uint32_t aCount, uint32_t *_retval) {
return mozilla::AndroidBridge::InputStreamRead(mBridgeChannel, aBuf, aCount, _retval);
}
NS_IMETHODIMP AndroidInputStream::ReadSegments(nsWriteSegmentFun aWriter, void *aClosure, uint32_t aCount, uint32_t *_retval) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP AndroidInputStream::IsNonBlocking(bool *_retval) {
*_retval = false;
return NS_OK;
}
class AndroidChannel : public nsBaseChannel
{
private:
AndroidChannel(nsIURI *aURI, jobject aConnection) {
JNIEnv *env = GetJNIForThread();
mConnection = env->NewGlobalRef(aConnection);
mURI = aURI;
nsCString type;
jstring jtype = GeckoAppShell::ConnectionGetMimeType(mConnection);
if (jtype)
SetContentType(nsJNICString(jtype, env));
}
public:
static AndroidChannel* CreateChannel(nsIURI *aURI) {
nsCString spec;
aURI->GetSpec(spec);
jobject connection = GeckoAppShell::GetConnection(spec);
if (!connection)
return NULL;
return new AndroidChannel(aURI, connection);
}
~AndroidChannel() {
JNIEnv *env = GetJNIForThread();
env->DeleteGlobalRef(mConnection);
}
virtual nsresult OpenContentStream(bool async, nsIInputStream **result,
nsIChannel** channel) {
nsCOMPtr<nsIInputStream> stream = new AndroidInputStream(mConnection);
NS_ADDREF(*result = stream);
return NS_OK;
}
private:
jobject mConnection;
};
NS_IMPL_ISUPPORTS(nsAndroidProtocolHandler,
nsIProtocolHandler,
nsISupportsWeakReference)
NS_IMETHODIMP
nsAndroidProtocolHandler::GetScheme(nsACString &result)
{
result.AssignLiteral("android");
return NS_OK;
}
NS_IMETHODIMP
nsAndroidProtocolHandler::GetDefaultPort(int32_t *result)
{
*result = -1; // no port for android: URLs
return NS_OK;
}
NS_IMETHODIMP
nsAndroidProtocolHandler::AllowPort(int32_t port, const char *scheme, bool *_retval)
{
// don't override anything.
*_retval = false;
return NS_OK;
}
NS_IMETHODIMP
nsAndroidProtocolHandler::GetProtocolFlags(uint32_t *result)
{
*result = URI_STD | URI_IS_UI_RESOURCE | URI_IS_LOCAL_RESOURCE | URI_NORELATIVE | URI_DANGEROUS_TO_LOAD;
return NS_OK;
}
NS_IMETHODIMP
nsAndroidProtocolHandler::NewURI(const nsACString &aSpec,
const char *aCharset,
nsIURI *aBaseURI,
nsIURI **result)
{
nsresult rv;
nsCOMPtr<nsIStandardURL> surl(do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = surl->Init(nsIStandardURL::URLTYPE_STANDARD, -1, aSpec, aCharset, aBaseURI);
if (NS_FAILED(rv))
return rv;
nsCOMPtr<nsIURL> url(do_QueryInterface(surl, &rv));
NS_ENSURE_SUCCESS(rv, rv);
surl->SetMutable(false);
NS_ADDREF(*result = url);
return NS_OK;
}
NS_IMETHODIMP
nsAndroidProtocolHandler::NewChannel(nsIURI* aURI,
nsIChannel* *aResult)
{
nsCOMPtr<nsIChannel> channel = AndroidChannel::CreateChannel(aURI);
if (!channel)
return NS_ERROR_FAILURE;
NS_ADDREF(*aResult = channel);
return NS_OK;
}

View File

@ -0,0 +1,35 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsAndroidProtocolHandler_h___
#define nsAndroidProtocolHandler_h___
#include "nsIProtocolHandler.h"
#include "nsWeakReference.h"
#include "mozilla/Attributes.h"
#define NS_ANDROIDPROTOCOLHANDLER_CID \
{ /* e9cd2b7f-8386-441b-aaf5-0b371846bfd0 */ \
0xe9cd2b7f, \
0x8386, \
0x441b, \
{0x0b, 0x37, 0x18, 0x46, 0xbf, 0xd0} \
}
class nsAndroidProtocolHandler MOZ_FINAL : public nsIProtocolHandler,
public nsSupportsWeakReference
{
public:
NS_DECL_THREADSAFE_ISUPPORTS
// nsIProtocolHandler methods:
NS_DECL_NSIPROTOCOLHANDLER
// nsAndroidProtocolHandler methods:
nsAndroidProtocolHandler() {}
~nsAndroidProtocolHandler() {}
};
#endif /* nsAndroidProtocolHandler_h___ */

View File

@ -25,6 +25,7 @@
#include "nsHTMLFormatConverter.h"
#include "nsIMEPicker.h"
#include "nsXULAppAPI.h"
#include "nsAndroidProtocolHandler.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsWindow)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsScreenManagerAndroid)
@ -38,6 +39,7 @@ NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecAndroid)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsHTMLFormatConverter)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsIMEPicker)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAndroidBridge)
NS_GENERIC_FACTORY_CONSTRUCTOR(nsAndroidProtocolHandler)
#include "GfxInfo.h"
namespace mozilla {
@ -62,6 +64,7 @@ NS_DEFINE_NAMED_CID(NS_HTMLFORMATCONVERTER_CID);
NS_DEFINE_NAMED_CID(NS_IMEPICKER_CID);
NS_DEFINE_NAMED_CID(NS_GFXINFO_CID);
NS_DEFINE_NAMED_CID(NS_ANDROIDBRIDGE_CID);
NS_DEFINE_NAMED_CID(NS_ANDROIDPROTOCOLHANDLER_CID);
static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{ &kNS_WINDOW_CID, false, nullptr, nsWindowConstructor },
@ -79,6 +82,7 @@ static const mozilla::Module::CIDEntry kWidgetCIDs[] = {
{ &kNS_IMEPICKER_CID, false, nullptr, nsIMEPickerConstructor },
{ &kNS_GFXINFO_CID, false, nullptr, mozilla::widget::GfxInfoConstructor },
{ &kNS_ANDROIDBRIDGE_CID, false, nullptr, nsAndroidBridgeConstructor },
{ &kNS_ANDROIDPROTOCOLHANDLER_CID, false, nullptr, nsAndroidProtocolHandlerConstructor },
{ nullptr }
};
@ -98,6 +102,7 @@ static const mozilla::Module::ContractIDEntry kWidgetContracts[] = {
{ "@mozilla.org/imepicker;1", &kNS_IMEPICKER_CID },
{ "@mozilla.org/gfx/info;1", &kNS_GFXINFO_CID },
{ "@mozilla.org/android/bridge;1", &kNS_ANDROIDBRIDGE_CID },
{ NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "android", &kNS_ANDROIDPROTOCOLHANDLER_CID },
{ nullptr }
};