Index: android_webview/native/android_protocol_handler.cc |
diff --git a/android_webview/native/android_protocol_handler.cc b/android_webview/native/android_protocol_handler.cc |
index 907881bae85822301eb9d1d435e0c8d1e23f7443..ef16af61459b0155e41d5d189041dd7d43d0f2be 100644 |
--- a/android_webview/native/android_protocol_handler.cc |
+++ b/android_webview/native/android_protocol_handler.cc |
@@ -49,6 +49,8 @@ class AndroidStreamReaderURLRequestJobDelegateImpl |
public: |
AndroidStreamReaderURLRequestJobDelegateImpl(); |
+ bool CanOpenInputStream(JNIEnv* env, net::URLRequest* request); |
+ |
virtual scoped_ptr<InputStream> OpenInputStream( |
JNIEnv* env, |
net::URLRequest* request) OVERRIDE; |
@@ -64,21 +66,26 @@ class AndroidStreamReaderURLRequestJobDelegateImpl |
std::string* charset) OVERRIDE; |
virtual ~AndroidStreamReaderURLRequestJobDelegateImpl(); |
+ |
+ private: |
+ scoped_ptr<InputStream> DoOpenInputStream(JNIEnv* env, |
+ net::URLRequest* request); |
+ |
+ net::URLRequest* cached_input_stream_request_; |
+ scoped_ptr<InputStream> cached_input_stream_; |
}; |
-class AssetFileProtocolInterceptor : |
+class AssetFileProtocolHandler : |
public net::URLRequestJobFactory::ProtocolHandler { |
public: |
- virtual ~AssetFileProtocolInterceptor() OVERRIDE; |
- static scoped_ptr<net::URLRequestJobFactory> CreateURLRequestJobFactory( |
- scoped_ptr<net::URLRequestJobFactory> base_job_factory); |
+ AssetFileProtocolHandler(); |
+ |
+ virtual ~AssetFileProtocolHandler() OVERRIDE; |
virtual net::URLRequestJob* MaybeCreateJob( |
net::URLRequest* request, |
net::NetworkDelegate* network_delegate) const OVERRIDE; |
private: |
- AssetFileProtocolInterceptor(); |
- |
// file:///android_asset/ |
const std::string asset_prefix_; |
// file:///android_res/ |
@@ -89,18 +96,10 @@ class AssetFileProtocolInterceptor : |
class ContentSchemeProtocolHandler : |
public net::URLRequestJobFactory::ProtocolHandler { |
public: |
- ContentSchemeProtocolHandler() {} |
- |
+ ContentSchemeProtocolHandler(); |
virtual net::URLRequestJob* MaybeCreateJob( |
net::URLRequest* request, |
- net::NetworkDelegate* network_delegate) const OVERRIDE { |
- DCHECK(request->url().SchemeIs(android_webview::kContentScheme)); |
- return new AndroidStreamReaderURLRequestJob( |
- request, |
- network_delegate, |
- scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
- new AndroidStreamReaderURLRequestJobDelegateImpl())); |
- } |
+ net::NetworkDelegate* network_delegate) const OVERRIDE; |
}; |
static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) { |
@@ -114,17 +113,28 @@ static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) { |
return context; |
} |
+// AndroidStreamReaderURLRequestJobDelegateImpl ------------------------------- |
+ |
AndroidStreamReaderURLRequestJobDelegateImpl:: |
-AndroidStreamReaderURLRequestJobDelegateImpl() { |
-} |
+ AndroidStreamReaderURLRequestJobDelegateImpl() |
+ : cached_input_stream_request_(NULL) {} |
AndroidStreamReaderURLRequestJobDelegateImpl:: |
~AndroidStreamReaderURLRequestJobDelegateImpl() { |
} |
+bool AndroidStreamReaderURLRequestJobDelegateImpl::CanOpenInputStream( |
+ JNIEnv* env, |
+ net::URLRequest* request) { |
+ cached_input_stream_request_ = request; |
+ cached_input_stream_ = DoOpenInputStream(env, request); |
+ return cached_input_stream_.get() != NULL; |
+} |
+ |
scoped_ptr<InputStream> |
-AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream( |
- JNIEnv* env, net::URLRequest* request) { |
+AndroidStreamReaderURLRequestJobDelegateImpl::DoOpenInputStream( |
+ JNIEnv* env, |
+ net::URLRequest* request) { |
DCHECK(request); |
DCHECK(env); |
@@ -145,6 +155,16 @@ AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream( |
return make_scoped_ptr<InputStream>(new InputStreamImpl(stream)); |
} |
+scoped_ptr<InputStream> |
+AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream( |
+ JNIEnv* env, net::URLRequest* request) { |
+ if (cached_input_stream_request_ == request) { |
+ cached_input_stream_request_ = NULL; |
+ return cached_input_stream_.Pass(); |
+ } |
+ return DoOpenInputStream(env, request); |
+} |
+ |
bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType( |
JNIEnv* env, |
net::URLRequest* request, |
@@ -181,7 +201,9 @@ bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset( |
return false; |
} |
-AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() |
+// AssetFileProtocolHandler ----------------------------------------------- |
+ |
+AssetFileProtocolHandler::AssetFileProtocolHandler() |
: asset_prefix_(std::string(chrome::kFileScheme) + |
std::string(content::kStandardSchemeSeparator) + |
android_webview::kAndroidAssetPath), |
@@ -190,22 +212,10 @@ AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() |
android_webview::kAndroidResourcePath) { |
} |
-AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() { |
+AssetFileProtocolHandler::~AssetFileProtocolHandler() { |
} |
-// static |
-scoped_ptr<net::URLRequestJobFactory> |
-AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
- scoped_ptr<net::URLRequestJobFactory> base_job_factory) { |
- scoped_ptr<net::URLRequestJobFactory> top_job_factory( |
- new net::ProtocolInterceptJobFactory( |
- base_job_factory.Pass(), |
- scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
- new AssetFileProtocolInterceptor()))); |
- return top_job_factory.Pass(); |
-} |
- |
-net::URLRequestJob* AssetFileProtocolInterceptor::MaybeCreateJob( |
+net::URLRequestJob* AssetFileProtocolHandler::MaybeCreateJob( |
net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
if (!request->url().SchemeIsFile()) return NULL; |
@@ -215,11 +225,46 @@ net::URLRequestJob* AssetFileProtocolInterceptor::MaybeCreateJob( |
return NULL; |
} |
+ scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate( |
+ new AndroidStreamReaderURLRequestJobDelegateImpl()); |
+ |
+ // For WebViewClassic compatibility this job can only accept URLs that can be |
+ // opened. |
+ if (!reader_delegate->CanOpenInputStream(AttachCurrentThread(), request)) { |
mkosiba (inactive)
2013/03/01 18:21:00
Just thought of the right way to solve this - use
mkosiba (inactive)
2013/03/05 16:34:43
and turns out doing this is a major pain. opted to
|
+ return NULL; |
+ } |
+ |
+ return new AndroidStreamReaderURLRequestJob( |
+ request, |
+ network_delegate, |
+ reader_delegate.PassAs<AndroidStreamReaderURLRequestJob::Delegate>()); |
+} |
+ |
+// ContentSchemeProtocolHandler ----------------------------------------------- |
+ |
+ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() { |
+} |
+ |
+net::URLRequestJob* ContentSchemeProtocolHandler::MaybeCreateJob( |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) const { |
+ if (!request->url().SchemeIs(android_webview::kContentScheme)) { |
+ return NULL; |
+ } |
+ |
+ scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate( |
+ new AndroidStreamReaderURLRequestJobDelegateImpl()); |
+ |
+ // For WebViewClassic compatibility this job can only accept URLs that can be |
+ // opened. |
+ if (!reader_delegate->CanOpenInputStream(AttachCurrentThread(), request)) { |
+ return NULL; |
+ } |
+ |
return new AndroidStreamReaderURLRequestJob( |
request, |
network_delegate, |
- scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>( |
- new AndroidStreamReaderURLRequestJobDelegateImpl())); |
+ reader_delegate.PassAs<AndroidStreamReaderURLRequestJob::Delegate>()); |
} |
} // namespace |
@@ -231,18 +276,17 @@ bool RegisterAndroidProtocolHandler(JNIEnv* env) { |
} |
// static |
-scoped_ptr<net::URLRequestJobFactory> CreateAndroidRequestJobFactory( |
- scoped_ptr<AwURLRequestJobFactory> job_factory) { |
- // Register content://. Note that even though a scheme is |
- // registered here, it cannot be used by child processes until access to it is |
- // granted via ChildProcessSecurityPolicy::GrantScheme(). This is done in |
- // AwContentBrowserClient. |
- // The job factory takes ownership of the handler. |
- bool set_protocol = job_factory->SetProtocolHandler( |
- android_webview::kContentScheme, new ContentSchemeProtocolHandler()); |
- DCHECK(set_protocol); |
- return AssetFileProtocolInterceptor::CreateURLRequestJobFactory( |
- job_factory.PassAs<net::URLRequestJobFactory>()); |
+scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
+CreateContentSchemeProtocolHandler() { |
+ return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
+ new ContentSchemeProtocolHandler()); |
+} |
+ |
+// static |
+scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> |
+CreateAssetFileProtocolHandler() { |
+ return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>( |
+ new AssetFileProtocolHandler()); |
} |
// Set a context object to be used for resolving resource queries. This can |