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

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: 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 3229ee49dd272ce40ef47b21fc2606290547ecd3..5bda3d2c382a4fcb84c0fd668effa8d49b3bf460 100644
--- a/chrome/browser/android/android_protocol_adapter.cc
+++ b/chrome/browser/android/android_protocol_adapter.cc
@@ -11,7 +11,9 @@
#include "base/android/jni_string.h"
#include "base/string_util.h"
#include "chrome/browser/android/android_stream_reader_url_request_job.h"
+#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/url_constants.h"
joth 2012/08/23 18:23:28 prefer to be removing chrome/ deps from here rathe
mnaganov (inactive) 2012/08/24 12:53:05 Done.
+#include "content/public/browser/browser_thread.h"
#include "googleurl/src/gurl.h"
#include "jni/AndroidProtocolAdapter_jni.h"
#include "net/base/io_buffer.h"
@@ -19,8 +21,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 +67,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;
+ 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
@@ -75,30 +94,21 @@ static bool InitJNIBindings(JNIEnv* env) {
// static
net::URLRequestJob* AndroidProtocolAdapter::Factory(
net::URLRequest* request, 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, scheme);
- }
-
+ DCHECK(scheme == chrome::kContentScheme);
return new AndroidStreamReaderURLRequestJob(
request,
scoped_ptr<AndroidStreamReaderURLRequestJob::Delegate>(
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) {
DCHECK(env);
@@ -106,16 +116,25 @@ bool AndroidProtocolAdapter::RegisterProtocols(JNIEnv* env) {
if (!InitJNIBindings(env))
return false;
joth 2012/08/23 18:23:28 out of scope for this CL, but this should never fa
mnaganov (inactive) 2012/08/24 12:53:05 Removed.
- // 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);
+
+ // Register a file: scheme interceptor for application assets.
+ net::URLRequestContextGetter* context_getter =
+ ProfileManager::GetDefaultProfile()->GetRequestContext();
joth 2012/08/23 18:23:28 could you have the context_getter passed in as a p
mnaganov (inactive) 2012/08/24 12:53:05 Done.
+ content::BrowserThread::PostTask(
+ content::BrowserThread::IO, FROM_HERE,
+ base::Bind(&AddFileSchemeInterceptorOnIOThread,
+ base::Unretained(context_getter)));
+ // TODO(mnaganov): Add an interceptor for the incognito profile?
return true;
}
@@ -222,3 +241,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