Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / android / net / android_protocol_handler.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "xwalk/runtime/browser/android/net/android_protocol_handler.h"
6
7 #include <string>
8
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_helper.h"
11 #include "base/android/jni_string.h"
12 #include "base/strings/string_util.h"
13 #include "content/public/common/url_constants.h"
14 #include "jni/AndroidProtocolHandler_jni.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/mime_util.h"
17 #include "net/base/net_errors.h"
18 #include "net/base/net_util.h"
19 #include "net/http/http_util.h"
20 #include "net/url_request/protocol_intercept_job_factory.h"
21 #include "net/url_request/url_request.h"
22 #include "url/gurl.h"
23 #include "xwalk/runtime/browser/android/net/android_stream_reader_url_request_job.h"
24 #include "xwalk/runtime/browser/android/net/input_stream_impl.h"
25 #include "xwalk/runtime/browser/android/net/url_constants.h"
26 #include "xwalk/runtime/browser/runtime_context.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
28
29 using base::android::AttachCurrentThread;
30 using base::android::ClearException;
31 using base::android::ConvertUTF8ToJavaString;
32 using base::android::ScopedJavaGlobalRef;
33 using base::android::ScopedJavaLocalRef;
34 using xwalk::InputStream;
35 using xwalk::InputStreamImpl;
36
37 namespace {
38
39 // Override resource context for reading resource and asset files. Used for
40 // testing.
41 JavaObjectWeakGlobalRef* g_resource_context = NULL;
42
43 void ResetResourceContext(JavaObjectWeakGlobalRef* ref) {
44   if (g_resource_context)
45     delete g_resource_context;
46
47   g_resource_context = ref;
48 }
49
50 void* kPreviouslyFailedKey = &kPreviouslyFailedKey;
51
52 void MarkRequestAsFailed(net::URLRequest* request) {
53   request->SetUserData(kPreviouslyFailedKey,
54                        new base::SupportsUserData::Data());
55 }
56
57 bool HasRequestPreviouslyFailed(net::URLRequest* request) {
58   return request->GetUserData(kPreviouslyFailedKey) != NULL;
59 }
60
61 class AndroidStreamReaderURLRequestJobDelegateImpl
62     : public AndroidStreamReaderURLRequestJob::Delegate {
63  public:
64   AndroidStreamReaderURLRequestJobDelegateImpl();
65
66   virtual scoped_ptr<InputStream> OpenInputStream(
67       JNIEnv* env,
68       const GURL& url) OVERRIDE;
69
70   virtual void OnInputStreamOpenFailed(net::URLRequest* request,
71                                        bool* restart) OVERRIDE;
72
73   virtual bool GetMimeType(JNIEnv* env,
74                            net::URLRequest* request,
75                            InputStream* stream,
76                            std::string* mime_type) OVERRIDE;
77
78   virtual bool GetCharset(JNIEnv* env,
79                           net::URLRequest* request,
80                           InputStream* stream,
81                           std::string* charset) OVERRIDE;
82
83   virtual bool GetPackageName(JNIEnv* env,
84                               std::string* name) OVERRIDE;
85
86   virtual ~AndroidStreamReaderURLRequestJobDelegateImpl();
87 };
88
89 class AndroidProtocolHandlerBase
90     : public net::URLRequestJobFactory::ProtocolHandler {
91  public:
92   virtual net::URLRequestJob* MaybeCreateJob(
93       net::URLRequest* request,
94       net::NetworkDelegate* network_delegate) const OVERRIDE;
95
96   virtual bool CanHandleRequest(const net::URLRequest* request) const = 0;
97 };
98
99 class AssetFileProtocolHandler : public AndroidProtocolHandlerBase {
100  public:
101   AssetFileProtocolHandler();
102
103   virtual ~AssetFileProtocolHandler() OVERRIDE;
104   virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
105
106  private:
107   // file:///android_asset/
108   const std::string asset_prefix_;
109   // file:///android_res/
110   const std::string resource_prefix_;
111 };
112
113 // Protocol handler for app:// scheme requests.
114 class AppSchemeProtocolHandler : public AndroidProtocolHandlerBase {
115  public:
116   AppSchemeProtocolHandler();
117   virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
118 };
119
120 // Protocol handler for content:// scheme requests.
121 class ContentSchemeProtocolHandler : public AndroidProtocolHandlerBase {
122  public:
123   ContentSchemeProtocolHandler();
124   virtual bool CanHandleRequest(const net::URLRequest* request) const OVERRIDE;
125 };
126
127 static ScopedJavaLocalRef<jobject> GetResourceContext(JNIEnv* env) {
128   if (g_resource_context)
129     return g_resource_context->get(env);
130   ScopedJavaLocalRef<jobject> context;
131   // We have to reset as GetApplicationContext() returns a jobject with a
132   // global ref. The constructor that takes a jobject would expect a local ref
133   // and would assert.
134   context.Reset(env, base::android::GetApplicationContext());
135   return context;
136 }
137
138 // AndroidStreamReaderURLRequestJobDelegateImpl -------------------------------
139
140 AndroidStreamReaderURLRequestJobDelegateImpl::
141     AndroidStreamReaderURLRequestJobDelegateImpl() {}
142
143 AndroidStreamReaderURLRequestJobDelegateImpl::
144 ~AndroidStreamReaderURLRequestJobDelegateImpl() {
145 }
146
147 scoped_ptr<InputStream>
148 AndroidStreamReaderURLRequestJobDelegateImpl::OpenInputStream(
149     JNIEnv* env, const GURL& url) {
150   DCHECK(url.is_valid());
151   DCHECK(env);
152
153   // Open the input stream.
154   ScopedJavaLocalRef<jstring> jurl =
155       ConvertUTF8ToJavaString(env, url.spec());
156   ScopedJavaLocalRef<jobject> stream =
157       xwalk::Java_AndroidProtocolHandler_open(
158           env,
159           GetResourceContext(env).obj(),
160           jurl.obj());
161
162   // Check and clear pending exceptions.
163   if (ClearException(env) || stream.is_null()) {
164     DLOG(ERROR) << "Unable to open input stream for Android URL";
165     return scoped_ptr<InputStream>();
166   }
167   return make_scoped_ptr<InputStream>(new InputStreamImpl(stream));
168 }
169
170 void AndroidStreamReaderURLRequestJobDelegateImpl::OnInputStreamOpenFailed(
171     net::URLRequest* request,
172     bool* restart) {
173   DCHECK(!HasRequestPreviouslyFailed(request));
174   MarkRequestAsFailed(request);
175   *restart = true;
176 }
177
178 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetMimeType(
179     JNIEnv* env,
180     net::URLRequest* request,
181     xwalk::InputStream* stream,
182     std::string* mime_type) {
183   DCHECK(env);
184   DCHECK(request);
185   DCHECK(mime_type);
186
187   // Query the mime type from the Java side. It is possible for the query to
188   // fail, as the mime type cannot be determined for all supported schemes.
189   ScopedJavaLocalRef<jstring> url =
190       ConvertUTF8ToJavaString(env, request->url().spec());
191   const InputStreamImpl* stream_impl =
192       InputStreamImpl::FromInputStream(stream);
193   ScopedJavaLocalRef<jstring> returned_type =
194       xwalk::Java_AndroidProtocolHandler_getMimeType(
195           env,
196           GetResourceContext(env).obj(),
197           stream_impl->jobj(), url.obj());
198   if (ClearException(env) || returned_type.is_null())
199     return false;
200
201   *mime_type = base::android::ConvertJavaStringToUTF8(returned_type);
202   return true;
203 }
204
205 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetCharset(
206     JNIEnv* env,
207     net::URLRequest* request,
208     xwalk::InputStream* stream,
209     std::string* charset) {
210   // TODO(shouqun): We should probably be getting this from the managed side.
211   return false;
212 }
213
214 bool AndroidStreamReaderURLRequestJobDelegateImpl::GetPackageName(
215     JNIEnv* env,
216     std::string* name) {
217   ScopedJavaLocalRef<jstring> returned_name =
218       xwalk::Java_AndroidProtocolHandler_getPackageName(
219           env,
220           GetResourceContext(env).obj());
221
222   if (ClearException(env) || returned_name.is_null())
223     return false;
224
225   *name = base::android::ConvertJavaStringToUTF8(returned_name);
226   return true;
227 }
228
229 // AndroidProtocolHandlerBase -------------------------------------------------
230
231 net::URLRequestJob* AndroidProtocolHandlerBase::MaybeCreateJob(
232     net::URLRequest* request,
233     net::NetworkDelegate* network_delegate) const {
234   if (!CanHandleRequest(request)) return NULL;
235
236   // For WebViewClassic compatibility this job can only accept URLs that can be
237   // opened. URLs that cannot be opened should be resolved by the next handler.
238   //
239   // If a request is initially handled here but the job fails due to it being
240   // unable to open the InputStream for that request the request is marked as
241   // previously failed and restarted.
242   // Restarting a request involves creating a new job for that request. This
243   // handler will ignore requests know to have previously failed to 1) prevent
244   // an infinite loop, 2) ensure that the next handler in line gets the
245   // opportunity to create a job for the request.
246   if (HasRequestPreviouslyFailed(request)) return NULL;
247
248   scoped_ptr<AndroidStreamReaderURLRequestJobDelegateImpl> reader_delegate(
249       new AndroidStreamReaderURLRequestJobDelegateImpl());
250
251   xwalk::RuntimeContext* runtime_context =
252       xwalk::XWalkRunner::GetInstance()->runtime_context();
253   std::string content_security_policy = runtime_context->GetCSPString();
254
255   return new AndroidStreamReaderURLRequestJob(
256       request,
257       network_delegate,
258       reader_delegate.PassAs<AndroidStreamReaderURLRequestJob::Delegate>(),
259       content_security_policy);
260 }
261
262 // AssetFileProtocolHandler ---------------------------------------------------
263
264 AssetFileProtocolHandler::AssetFileProtocolHandler()
265     : asset_prefix_(std::string(content::kFileScheme) +
266                     std::string(content::kStandardSchemeSeparator) +
267                     xwalk::kAndroidAssetPath),
268       resource_prefix_(std::string(content::kFileScheme) +
269                        std::string(content::kStandardSchemeSeparator) +
270                        xwalk::kAndroidResourcePath) {
271 }
272
273 AssetFileProtocolHandler::~AssetFileProtocolHandler() {
274 }
275
276 bool AssetFileProtocolHandler::CanHandleRequest(
277     const net::URLRequest* request) const {
278   if (!request->url().SchemeIsFile())
279     return false;
280
281   const std::string& url = request->url().spec();
282   if (!StartsWithASCII(url, asset_prefix_, /*case_sensitive=*/ true) &&
283       !StartsWithASCII(url, resource_prefix_, /*case_sensitive=*/ true)) {
284     return false;
285   }
286
287   return true;
288 }
289
290 // ContentSchemeProtocolHandler
291 ContentSchemeProtocolHandler::ContentSchemeProtocolHandler() {
292 }
293
294 bool ContentSchemeProtocolHandler::CanHandleRequest(
295     const net::URLRequest* request) const {
296   return request->url().SchemeIs(xwalk::kContentScheme);
297 }
298
299 // AppSchemeProtocolHandler
300 AppSchemeProtocolHandler::AppSchemeProtocolHandler() {
301 }
302
303 bool AppSchemeProtocolHandler::CanHandleRequest(
304     const net::URLRequest* request) const {
305   return request->url().SchemeIs(xwalk::kAppScheme);
306 }
307
308 }  // namespace
309
310 namespace xwalk {
311
312 bool RegisterAndroidProtocolHandler(JNIEnv* env) {
313   return RegisterNativesImpl(env);
314 }
315
316 // static
317 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
318 CreateContentSchemeProtocolHandler() {
319   return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
320       new ContentSchemeProtocolHandler());
321 }
322
323 // static
324 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
325 CreateAssetFileProtocolHandler() {
326   return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
327       new AssetFileProtocolHandler());
328 }
329 // static
330 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>
331 CreateAppSchemeProtocolHandler() {
332   return make_scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(
333       new AppSchemeProtocolHandler());
334 }
335
336
337 // Set a context object to be used for resolving resource queries. This can
338 // be used to override the default application context and redirect all
339 // resource queries to a specific context object, e.g., for the purposes of
340 // testing.
341 //
342 // |context| should be a android.content.Context instance or NULL to enable
343 // the use of the standard application context.
344 static void SetResourceContextForTesting(JNIEnv* env, jclass /*clazz*/,
345                                          jobject context) {
346   if (context) {
347     ResetResourceContext(new JavaObjectWeakGlobalRef(env, context));
348   } else {
349     ResetResourceContext(NULL);
350   }
351 }
352
353 static jstring GetAndroidAssetPath(JNIEnv* env, jclass /*clazz*/) {
354   // OK to release, JNI binding.
355   return ConvertUTF8ToJavaString(
356       env, xwalk::kAndroidAssetPath).Release();
357 }
358
359 static jstring GetAndroidResourcePath(JNIEnv* env, jclass /*clazz*/) {
360   // OK to release, JNI binding.
361   return ConvertUTF8ToJavaString(
362       env, xwalk::kAndroidResourcePath).Release();
363 }
364
365 }  // namespace xwalk