This source file includes following definitions.
- ResetResourceContext
- MarkRequestAsFailed
- HasRequestPreviouslyFailed
- GetResourceContext
- AndroidStreamReaderURLRequestJobDelegateImpl
- AndroidStreamReaderURLRequestJobDelegateImpl
- OpenInputStream
- OnInputStreamOpenFailed
- GetMimeType
- GetCharset
- MaybeCreateJob
- resource_prefix_
- CanHandleRequest
- CanHandleRequest
- RegisterAndroidProtocolHandler
- CreateContentSchemeProtocolHandler
- CreateAssetFileProtocolHandler
- SetResourceContextForTesting
- GetAndroidAssetPath
- GetAndroidResourcePath
#include "android_webview/native/android_protocol_handler.h"
#include "android_webview/browser/net/android_stream_reader_url_request_job.h"
#include "android_webview/browser/net/aw_url_request_job_factory.h"
#include "android_webview/common/url_constants.h"
#include "android_webview/native/input_stream_impl.h"
#include "base/android/jni_android.h"
#include "base/android/jni_helper.h"
#include "base/android/jni_string.h"
#include "base/strings/string_util.h"
#include "content/public/common/url_constants.h"
#include "jni/AndroidProtocolHandler_jni.h"
#include "net/base/io_buffer.h"
#include "net/base/mime_util.h"
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/http/http_util.h"
#include "net/url_request/protocol_intercept_job_factory.h"
#include "net/url_request/url_request.h"
#include "url/gurl.h"
using android_webview::InputStream;
using android_webview::InputStreamImpl;
using base::android::AttachCurrentThread;
using base::android::ClearException;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
namespace {
JavaObjectWeakGlobalRef* g_resource_context = NULL;
void ResetResourceContext(JavaObjectWeakGlobalRef* ref) {
if (g_resource_context)
delete g_resource_context;
g_resource_context = ref;
}
void* kPreviouslyFailedKey = &kPreviouslyFailedKey;
void MarkRequestAsFailed(net::URLRequest* request) {
request->SetUserData(kPreviouslyFailedKey,
new base::SupportsUserData::Data());
}
bool HasRequestPreviouslyFailed(net::URLRequest* request) {
return request->GetUserData(kPreviouslyFailedKey) != NULL;
}
class AndroidStreamReaderURLRequestJobDelegateImpl
: public AndroidStreamReaderURLRequestJob::Delegate {
public:
AndroidStreamReaderURLRequestJobDelegateImpl();
virtual scoped_ptr<InputStream> OpenInputStream(
JNIEnv* env,
const GURL& url) OVERRIDE;
virtual void OnInputStreamOpenFailed(net::URLRequest* request,
bool* restart) OVERRIDE;
virtual bool GetMimeType(JNIEnv* env,
net::URLRequest* request,
InputStream* stream,
std::string* mime_type) OVERRIDE;
virtual bool GetCharset(JNIEnv* env,
net::URLRequest* request,
InputStream* stream,
std::string* charset) OVERRIDE;
virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
};
class AndroidProtocolHandlerBase :
public net::URLRequestJobFactory::ProtocolHandler {
public:
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE;
virtual bool CanHandleRequest(const net::URLRequest* request) const = 0;
};
class AssetFileProtocolHandler : public AndroidProtocolHandlerBase {
public:
AssetFileProtocolHandler();
virtual ~AssetFileProtocolHandler() OVERRIDE;
virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
private:
const std::string asset_prefix_;
const std::string resource_prefix_;
};
class ContentSchemeProtocolHandler : public AndroidProtocolHandlerBase {
public:
ContentSchemeProtocolHandler();
virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
};
static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) {
if (g_resource_context)
return g_resource_context->get(env);
ScopedJavaLocalRef<jobject> context;
context.Reset(env, base::android::GetApplicationContext());
return context;
}
AndroidStreamReaderURLRequestJobDelegateImpl::
AndroidStreamReaderURLRequestJobDelegateImpl() {}
AndroidStreamReaderURLRequestJobDelegateImpl::
~AndroidStreamReaderURLRequestJobDelegateImpl() {
}
scoped_ptr<InputStream>
AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
JNIEnv* env, const GURL& url) {
DCHECK(url.is_valid());
DCHECK(env);
ScopedJavaLocalRef<jstring> jurl =
ConvertUTF8ToJavaString(env, url.spec());
ScopedJavaLocalRef<jobject> stream =
android_webview::Java_AndroidProtocolHandler_open(
env,
GetResourceContext(env).obj(),
jurl.obj());
if (stream.is_null()) {
DLOG(ERROR) << "Unable to open input stream for Android URL";
return scoped_ptr<InputStream>();
}
return make_scoped_ptr<InputStream>(new InputStreamImpl(stream));
}
void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
net::URLRequest* request,
bool* restart) {
DCHECK(!HasRequestPreviouslyFailed(request));
MarkRequestAsFailed(request);
*restart = true;
}
bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
JNIEnv* env,
net::URLRequest* request,
android_webview::InputStream* stream,
std::string* mime_type) {
DCHECK(env);
DCHECK(request);
DCHECK(mime_type);
ScopedJavaLocalRef<jstring> url =
ConvertUTF8ToJavaString(env, request->url().spec());
const InputStreamImpl* stream_impl =
InputStreamImpl::FromInputStream(stream);
ScopedJavaLocalRef<jstring> returned_type =
android_webview::Java_AndroidProtocolHandler_getMimeType(
env,
GetResourceContext(env).obj(),
stream_impl->jobj(), url.obj());
if (returned_type.is_null())
return false;
*mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
return true;
}
bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
JNIEnv* env,
net::URLRequest* request,
android_webview::InputStream* stream,
std::string* charset) {
return false;
}
net::URLRequestJob* AndroidProtocolHandlerBase::MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const {
if (!CanHandleRequest(request)) return NULL;
if (HasRequestPreviouslyFailed(request)) return NULL;
scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate(
new AndroidStreamReaderURLRequestJobDelegateImpl());
return new AndroidStreamReaderURLRequestJob(
request,
network_delegate,
reader_delegate.PassAs<AndroidStreamReaderURLRequestJob::Delegate>());
}
AssetFileProtocolHandler::AssetFileProtocolHandler()
: asset_prefix_(std::string(content::kFileScheme) +
std::string(content::kStandardSchemeSeparator) +
android_webview::kAndroidAssetPath),
resource_prefix_(std::string(content::kFileScheme) +
std::string(content::kStandardSchemeSeparator) +
android_webview::kAndroidResourcePath) {
}
AssetFileProtocolHandler::~AssetFileProtocolHandler() {
}
bool AssetFileProtocolHandler::CanHandleRequest(
const net::URLRequest* request) const {
if (!request->url().SchemeIsFile())
return false;
const std::string& url = request->url().spec();
if (!StartsWithASCII(url, asset_prefix_, true) &&
!StartsWithASCII(url, resource_prefix_, true)) {
return false;
}
return true;
}
ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() {
}
bool ContentSchemeProtocolHandler::CanHandleRequest(
const net::URLRequest* request) const {
return request->url().SchemeIs(android_webview::kContentScheme);
}
}
namespace android_webview {
bool RegisterAndroidProtocolHandler(JNIEnv* env) {
return RegisterNativesImpl(env);
}
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
CreateContentSchemeProtocolHandler() {
return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
new ContentSchemeProtocolHandler());
}
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
CreateAssetFileProtocolHandler() {
return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
new AssetFileProtocolHandler());
}
static void SetResourceContextForTesting(JNIEnv* env, jclass ,
jobject context) {
if (context) {
ResetResourceContext(new JavaObjectWeakGlobalRef(env, context));
} else {
ResetResourceContext(NULL);
}
}
static jstring GetAndroidAssetPath(JNIEnv* env, jclass ) {
return ConvertUTF8ToJavaString(
env, android_webview::kAndroidAssetPath).Release();
}
static jstring GetAndroidResourcePath(JNIEnv* env, jclass ) {
return ConvertUTF8ToJavaString(
env, android_webview::kAndroidResourcePath).Release();
}
}