Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3088)

Unified Diff: chrome/browser/android/android_protocol_adapter.cc

Issue 10880025: [Android] Fix handling of asset files loading. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments addressed Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/android_protocol_adapter.cc
diff --git a/chrome/browser/android/android_protocol_adapter.cc b/chrome/browser/android/android_protocol_adapter.cc
index aea709200073d5f5cd0a09c3e0502cf58347cf01..80f85304b9c807466ab171dd5a1602c1e64d854e 100644
--- a/chrome/browser/android/android_protocol_adapter.cc
+++ b/chrome/browser/android/android_protocol_adapter.cc
@@ -19,8 +19,9 @@
#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/http/http_util.h"
-#include "net/url_request/url_request_error_job.h"
-#include "net/url_request/url_request_file_job.h"
+#include "net/url_request/url_request_context.h"
+#include "net/url_request/url_request_context_getter.h"
+#include "net/url_request/url_request_job_factory.h"
#include "net/url_request/url_request_job_manager.h"
using base::android::AttachCurrentThread;
@@ -64,6 +65,22 @@ class AndroidStreamReaderURLRequestJobDelegateImpl
virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
};
+class AssetFileProtocolInterceptor :
+ public net::URLRequestJobFactory::Interceptor {
+ public:
+ AssetFileProtocolInterceptor();
+ virtual ~AssetFileProtocolInterceptor() OVERRIDE;
+ virtual net::URLRequestJob* MaybeIntercept(net::URLRequest* request) const OVERRIDE;
Sami 2012/08/28 11:10:50 Line length?
mnaganov (inactive) 2012/08/28 13:08:00 Done.
+ virtual net::URLRequestJob* MaybeInterceptRedirect(
+ const GURL& location,
+ net::URLRequest* request) const OVERRIDE;
+ virtual net::URLRequestJob* MaybeInterceptResponse(
+ net::URLRequest* request) const OVERRIDE;
+
+ private:
+ const std::string kAssetPrefix;
+ const std::string kResourcePrefix;
+};
} // namespace
@@ -74,27 +91,8 @@ static bool InitJNIBindings(JNIEnv* env) {
// static
net::URLRequestJob* AndroidProtocolAdapter::Factory(
- net::URLRequest* request,
- net::NetworkDelegate* network_delegate,
- const std::string& scheme) {
- DCHECK(scheme == chrome::kFileScheme ||
- scheme == chrome::kContentScheme);
- JNIEnv* env = AttachCurrentThread();
- DCHECK(env);
- // If this is a file:// URL we cannot handle, fall back to the default
- // handler.
- const std::string& url = request->url().spec();
- std::string assetPrefix = std::string(chrome::kFileScheme) + "://" +
- chrome::kAndroidAssetPath;
- std::string resourcePrefix = std::string(chrome::kFileScheme) + "://" +
- chrome::kAndroidResourcePath;
-
- if (scheme == chrome::kFileScheme &&
- !StartsWithASCII(url, assetPrefix, /*case_sensitive=*/ true) &&
- !StartsWithASCII(url, resourcePrefix, /*case_sensitive=*/ true)) {
- return net::URLRequestFileJob::Factory(request, network_delegate, scheme);
- }
-
+ net::URLRequest* request, const std::string& scheme) {
+ DCHECK(scheme == chrome::kContentScheme);
return new AndroidStreamReaderURLRequestJob(
request,
network_delegate,
@@ -102,25 +100,37 @@ net::URLRequestJob* AndroidProtocolAdapter::Factory(
new AndroidStreamReaderURLRequestJobDelegateImpl()));
}
+static void AddFileSchemeInterceptorOnIOThread(
+ net::URLRequestContextGetter* context_getter) {
+ // The job factory takes ownership of the interceptor.
+ const_cast<net::URLRequestJobFactory*>(
+ context_getter->GetURLRequestContext()->job_factory())->AddInterceptor(
+ new AssetFileProtocolInterceptor());
+}
+
// static
-bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) {
+void AndroidProtocolAdapter::RegisterProtocols(
+ JNIEnv* env, net::URLRequestContextGetter* context_getter) {
DCHECK(env);
+ InitJNIBindings(env);
Sami 2012/08/28 11:10:50 Maybe add a DCHECK for the result here? Otherwise
mnaganov (inactive) 2012/08/28 13:08:00 Good idea. But wrapping the in a DCHECK will mean
- if (!InitJNIBindings(env))
- return false;
-
- // Register content:// and file://. Note that even though a scheme is
+ // 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
// RenderViewHost.
+ //
+ // TODO(mnaganov): Convert into a ProtocolHandler.
net::URLRequestJobManager* job_manager =
net::URLRequestJobManager::GetInstance();
job_manager->RegisterProtocolFactory(chrome::kContentScheme,
&AndroidProtocolAdapter::Factory);
- job_manager->RegisterProtocolFactory(
- chrome::kFileScheme, &AndroidProtocolAdapter::Factory);
- return true;
+ // Register a file: scheme interceptor for application assets.
+ context_getter->GetNetworkTaskRunner()->PostTask(
+ FROM_HERE,
+ base::Bind(&AddFileSchemeInterceptorOnIOThread,
+ base::Unretained(context_getter)));
+ // TODO(mnaganov): Add an interceptor for the incognito profile?
}
// Set a context object to be used for resolving resource queries. This can
@@ -225,3 +235,42 @@ bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
// TODO: We should probably be getting this from the managed side.
return false;
}
+
+AssetFileProtocolInterceptor::AssetFileProtocolInterceptor() :
+ kAssetPrefix(std::string(chrome::kFileScheme) +
+ std::string(content::kStandardSchemeSeparator) +
+ chrome::kAndroidAssetPath),
+ kResourcePrefix(std::string(chrome::kFileScheme) +
+ std::string(content::kStandardSchemeSeparator) +
+ chrome::kAndroidResourcePath) {
+}
+
+AssetFileProtocolInterceptor::~AssetFileProtocolInterceptor() {
+}
+
+net::URLRequestJob* AssetFileProtocolInterceptor::MaybeIntercept(
+ net::URLRequest* request) const {
+ if (!request->url().SchemeIsFile()) return NULL;
+
+ const std::string& url = request->url().spec();
+ if (!StartsWithASCII(url, kAssetPrefix, /*case_sensitive=*/ true) &&
+ !StartsWithASCII(url, kResourcePrefix, /*case_sensitive=*/ true)) {
+ return NULL;
+ }
+
+ return new AndroidStreamReaderURLRequestJob(
+ request,
+ scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
+ new AndroidStreamReaderURLRequestJobDelegateImpl()));
+}
+
+net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptRedirect(
+ const GURL& location,
+ net::URLRequest* request) const {
+ return NULL;
+}
+
+net::URLRequestJob* AssetFileProtocolInterceptor::MaybeInterceptResponse(
+ net::URLRequest* request) const {
+ return NULL;
+}

Powered by Google App Engine
This is Rietveld 408576698