2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2018-11-30 18:39:55 +03:00
|
|
|
/* vim:set ts=4 sw=2 sts=2 et cin: */
|
2014-06-04 23:28:04 +04:00
|
|
|
/* 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 "android/log.h"
|
|
|
|
#include "nsBaseChannel.h"
|
|
|
|
#include "AndroidBridge.h"
|
|
|
|
#include "GeneratedJNIWrappers.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
class AndroidInputStream : public nsIInputStream {
|
|
|
|
public:
|
2018-03-27 17:51:32 +03:00
|
|
|
explicit AndroidInputStream(jni::Object::Param connection) {
|
2016-07-21 20:49:04 +03:00
|
|
|
mBridgeInputStream = java::GeckoAppShell::CreateInputStream(connection);
|
2015-01-10 03:33:57 +03:00
|
|
|
mBridgeChannel = AndroidBridge::ChannelCreate(mBridgeInputStream);
|
2014-06-04 23:28:04 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-26 23:07:59 +04:00
|
|
|
private:
|
2014-06-04 23:28:04 +04:00
|
|
|
virtual ~AndroidInputStream() {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-08-26 23:07:59 +04:00
|
|
|
public:
|
2014-06-04 23:28:04 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
|
|
|
NS_DECL_NSIINPUTSTREAM
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-10 03:33:57 +03:00
|
|
|
private:
|
|
|
|
jni::Object::GlobalRef mBridgeInputStream;
|
|
|
|
jni::Object::GlobalRef mBridgeChannel;
|
2014-06-04 23:28:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(AndroidInputStream, nsIInputStream)
|
|
|
|
|
|
|
|
NS_IMETHODIMP AndroidInputStream::Close(void) {
|
2015-01-10 03:33:57 +03:00
|
|
|
AndroidBridge::InputStreamClose(mBridgeInputStream);
|
2014-06-04 23:28:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP AndroidInputStream::Available(uint64_t* _retval) {
|
2015-01-10 03:33:57 +03:00
|
|
|
*_retval = AndroidBridge::InputStreamAvailable(mBridgeInputStream);
|
2014-06-04 23:28:04 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP AndroidInputStream::Read(char* aBuf, uint32_t aCount,
|
|
|
|
uint32_t* _retval) {
|
2015-01-10 03:33:57 +03:00
|
|
|
return AndroidBridge::InputStreamRead(mBridgeChannel, aBuf, aCount, _retval);
|
2014-06-04 23:28:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP AndroidInputStream::ReadSegments(nsWriteSegmentFun aWriter,
|
2019-05-01 11:47:10 +03:00
|
|
|
void* aClosure, uint32_t aCount,
|
|
|
|
uint32_t* _retval) {
|
2014-06-04 23:28:04 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
NS_IMETHODIMP AndroidInputStream::IsNonBlocking(bool* _retval) {
|
2014-06-04 23:28:04 +04:00
|
|
|
*_retval = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AndroidChannel : public nsBaseChannel {
|
2018-11-30 13:46:48 +03:00
|
|
|
private:
|
2019-05-01 11:47:10 +03:00
|
|
|
AndroidChannel(nsIURI* aURI, jni::Object::Param aConnection) {
|
2015-01-10 03:33:57 +03:00
|
|
|
mConnection = aConnection;
|
2015-10-21 23:53:08 +03:00
|
|
|
SetURI(aURI);
|
2014-06-04 23:28:04 +04:00
|
|
|
|
2016-07-21 20:49:04 +03:00
|
|
|
auto type = java::GeckoAppShell::ConnectionGetMimeType(mConnection);
|
2015-01-10 03:33:57 +03:00
|
|
|
if (type) {
|
2016-02-10 01:27:28 +03:00
|
|
|
SetContentType(type->ToCString());
|
2014-06-04 23:28:04 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2015-01-10 03:33:57 +03:00
|
|
|
|
2014-06-04 23:28:04 +04:00
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
static AndroidChannel* CreateChannel(nsIURI* aURI) {
|
2014-06-04 23:28:04 +04:00
|
|
|
nsCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
2015-01-10 03:33:57 +03:00
|
|
|
|
2016-07-21 20:49:04 +03:00
|
|
|
auto connection = java::GeckoAppShell::GetConnection(spec);
|
2015-01-10 03:33:57 +03:00
|
|
|
return connection ? new AndroidChannel(aURI, connection) : nullptr;
|
2014-06-04 23:28:04 +04:00
|
|
|
}
|
2015-01-10 03:33:57 +03:00
|
|
|
|
|
|
|
virtual ~AndroidChannel() {}
|
2014-06-04 23:28:04 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
virtual nsresult OpenContentStream(bool async, nsIInputStream** result,
|
|
|
|
nsIChannel** channel) {
|
2014-06-04 23:28:04 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream = new AndroidInputStream(mConnection);
|
|
|
|
NS_ADDREF(*result = stream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-01-10 03:33:57 +03:00
|
|
|
|
2014-06-04 23:28:04 +04:00
|
|
|
private:
|
2015-01-10 03:33:57 +03:00
|
|
|
jni::Object::GlobalRef mConnection;
|
2014-06-04 23:28:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsAndroidProtocolHandler, nsIProtocolHandler,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAndroidProtocolHandler::GetScheme(nsACString& result) {
|
2014-06-04 23:28:04 +04:00
|
|
|
result.AssignLiteral("android");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAndroidProtocolHandler::GetDefaultPort(int32_t* result) {
|
2014-06-04 23:28:04 +04:00
|
|
|
*result = -1; // no port for android: URLs
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAndroidProtocolHandler::AllowPort(int32_t port, const char* scheme,
|
|
|
|
bool* _retval) {
|
2014-06-04 23:28:04 +04:00
|
|
|
// don't override anything.
|
|
|
|
*_retval = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAndroidProtocolHandler::GetProtocolFlags(uint32_t* result) {
|
2014-06-04 23:28:04 +04:00
|
|
|
*result = URI_STD | URI_IS_UI_RESOURCE | URI_IS_LOCAL_RESOURCE |
|
|
|
|
URI_NORELATIVE | URI_DANGEROUS_TO_LOAD;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2019-05-01 11:47:10 +03:00
|
|
|
nsAndroidProtocolHandler::NewChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo,
|
|
|
|
nsIChannel** aResult) {
|
2014-06-04 23:28:04 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel = AndroidChannel::CreateChannel(aURI);
|
|
|
|
if (!channel) return NS_ERROR_FAILURE;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-12-12 20:08:14 +03:00
|
|
|
// set the loadInfo on the new channel
|
|
|
|
nsresult rv = channel->SetLoadInfo(aLoadInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-06-04 23:28:04 +04:00
|
|
|
NS_ADDREF(*aResult = channel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|