Skip to content

Commit

Permalink
[Android] Update shouldInterceptLoadRequest to Android L style
Browse files Browse the repository at this point in the history
As current API misses lots of context, The older
API:shouldInterceptRequest(WebView view, String url)
was deprecated in Android API level 21, and was replaced by
shouldInterceptRequest(WebView view, WebResourceRequest request),
so update XWalk's corresponding API.

However, this new API can be used in all Android version.

BUG=XWALK-3934
  • Loading branch information
xzhan96 committed Nov 2, 2015
1 parent f1ac4b1 commit db7728d
Show file tree
Hide file tree
Showing 31 changed files with 764 additions and 369 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.xwalk.core.internal;

import android.net.Uri;

import java.util.Map;

@XWalkAPI(impl = WebResourceRequestInternal.class, createInternally = true)
public class WebResourceRequestHandlerInternal implements WebResourceRequestInternal {
private final XWalkContentsClientBridge.XWalkWebResourceRequest mRequest;

WebResourceRequestHandlerInternal(XWalkContentsClientBridge.XWalkWebResourceRequest request) {
mRequest = request;
}

// Never use this constructor.
// It is only used in WebResourceRequestHandlerBridge.
WebResourceRequestHandlerInternal() {
mRequest = null;
}

@XWalkAPI
public Uri getUrl() {
return Uri.parse(mRequest.url);
}

@XWalkAPI
public boolean isForMainFrame() {
return mRequest.isMainFrame;
}

@XWalkAPI
public boolean hasGesture() {
return mRequest.hasUserGesture;
}

@XWalkAPI
public String getMethod() {
return mRequest.method;
}

@XWalkAPI
public Map<String, String> getRequestHeaders() {
return mRequest.requestHeaders;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.xwalk.core.internal;

import android.net.Uri;

import java.util.Map;

/**
* Encompasses parameters to the {@link XWalkResourceClient#shouldInterceptLoadRequest} method.
*/
@XWalkAPI(instance = WebResourceRequestHandlerInternal.class)
public interface WebResourceRequestInternal {
/**
* Gets the URL for which the resource request was made.
*
* @return the URL for which the resource request was made.
* @since 6.0
*/
@XWalkAPI
public Uri getUrl();

/**
* Gets whether the request was made for the main frame.
*
* @return whether the request was made for the main frame. Will be false for iframes,
* for example.
* @since 6.0
*/
@XWalkAPI
public boolean isForMainFrame();

/**
* Gets whether a gesture (such as a click) was associated with the request.
* For security reasons in certain situations this method may return false even though the
* sequence of events which caused the request to be created was initiated by a user gesture.
*
* @return whether a gesture was associated with the request.
* @since 6.0
*/
@XWalkAPI
public boolean hasGesture();

/**
* Gets the method associated with the request, for example "GET".
*
* @return the method associated with the request.
* @since 6.0
*/
@XWalkAPI
public String getMethod();

/**
* Gets the headers associated with the request. These are represented as a mapping of header
* name to header value.
*
* @return the headers associated with the request.
* @since 6.0
*/
@XWalkAPI
public Map<String, String> getRequestHeaders();
}
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ public boolean onTouchEvent(MotionEvent event) {
}

//--------------------------------------------------------------------------------------------
private class XWalkIoThreadClientImpl implements XWalkContentsIoThreadClient {
private class XWalkIoThreadClientImpl extends XWalkContentsIoThreadClient {
// All methods are called on the IO thread.

@Override
Expand All @@ -668,28 +668,26 @@ public int getCacheMode() {
}

@Override
public InterceptedRequestData shouldInterceptRequest(final String url,
boolean isMainFrame) {
public XWalkWebResourceResponse shouldInterceptRequest(
XWalkContentsClient.XWalkWebResourceRequest request) {

String url = request.url;
XWalkWebResourceResponse xwalkWebResourceResponse;
// Notify a resource load is started. This is not the best place to start the callback
// but it's a workable way.
mContentsClientBridge.getCallbackHelper().postOnResourceLoadStarted(url);

WebResourceResponse webResourceResponse = mContentsClientBridge.shouldInterceptRequest(url);
InterceptedRequestData interceptedRequestData = null;
xwalkWebResourceResponse = mContentsClientBridge.shouldInterceptRequest(request);

if (webResourceResponse == null) {
if (xwalkWebResourceResponse == null) {
mContentsClientBridge.getCallbackHelper().postOnLoadResource(url);
} else {
if (isMainFrame && webResourceResponse.getData() == null) {
if (request.isMainFrame && xwalkWebResourceResponse.getData() == null) {
mContentsClientBridge.getCallbackHelper().postOnReceivedError(
XWalkResourceClientInternal.ERROR_UNKNOWN, null, url);
XWalkResourceClientInternal.ERROR_UNKNOWN, null, url);
}
interceptedRequestData = new InterceptedRequestData(webResourceResponse.getMimeType(),
webResourceResponse.getEncoding(),
webResourceResponse.getData());
}
return interceptedRequestData;
return xwalkWebResourceResponse;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
Expand Down Expand Up @@ -133,16 +134,32 @@ final XWalkContentsClientCallbackHelper getCallbackHelper() {
}

//--------------------------------------------------------------------------------------------
// XWalkViewInternal specific methods that map directly to XWalkViewClient / XWalkWebChromeClient
// XWalkViewInternal specific methods that map directly to XWalkViewClient/XWalkWebChromeClient
//--------------------------------------------------------------------------------------------

/**
* Parameters for the {@link XWalkContentsClient#shouldInterceptRequest} method.
*/
public static class XWalkWebResourceRequest {
// Url of the request.
public String url;
// Is this for the main frame or a child iframe?
public boolean isMainFrame;
// Was a gesture associated with the request? Don't trust can easily be spoofed.
public boolean hasUserGesture;
// Method used (GET/POST/OPTIONS)
public String method;
// Headers that would have been sent to server.
public ArrayMap<String, String> requestHeaders;
}
public abstract void getVisitedHistory(ValueCallback<String[]> callback);

public abstract void doUpdateVisitedHistory(String url, boolean isReload);

public abstract void onProgressChanged(int progress);

public abstract WebResourceResponse shouldInterceptRequest(String url);
public abstract XWalkWebResourceResponse shouldInterceptRequest(
XWalkWebResourceRequest request);

public abstract void onResourceLoadStarted(String url);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.net.Uri;
import android.net.http.SslCertificate;
import android.net.http.SslError;
import android.os.Build;
import android.os.Message;
import android.os.Handler;
import android.provider.MediaStore;
Expand All @@ -30,6 +31,9 @@
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;

import javax.security.auth.x500.X500Principal;

Expand Down Expand Up @@ -244,9 +248,29 @@ public void onProgressChanged(int progress) {
}

@Override
public WebResourceResponse shouldInterceptRequest(String url) {
public XWalkWebResourceResponse shouldInterceptRequest(XWalkWebResourceRequest request) {
if (isOwnerActivityRunning()) {
return mXWalkResourceClient.shouldInterceptLoadRequest(mXWalkView, url);
WebResourceResponse response = mXWalkResourceClient.shouldInterceptLoadRequest(
mXWalkView, new WebResourceRequestHandlerInternal(request));
if (response == null) return null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
Map<String, String> responseHeaders = response.getResponseHeaders();
if (responseHeaders == null) responseHeaders = new HashMap<String, String>();

return new XWalkWebResourceResponse(
response.getMimeType(),
response.getEncoding(),
response.getData(),
response.getStatusCode(),
response.getReasonPhrase(),
responseHeaders);
} else {
return new XWalkWebResourceResponse(
response.getMimeType(),
response.getEncoding(),
response.getData());
}
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,56 @@

package org.xwalk.core.internal;

import android.util.ArrayMap;

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;

/**
* Delegate for handling callbacks. All methods are called on the IO thread.
*/
@JNINamespace("xwalk")
interface XWalkContentsIoThreadClient {
public abstract class XWalkContentsIoThreadClient {
@CalledByNative
public int getCacheMode();
public abstract int getCacheMode();

@CalledByNative
public InterceptedRequestData shouldInterceptRequest(String url, boolean isMainFrame);
public abstract boolean shouldBlockContentUrls();

@CalledByNative
public boolean shouldBlockContentUrls();
public abstract boolean shouldBlockFileUrls();

@CalledByNative
public boolean shouldBlockFileUrls();
public abstract boolean shouldBlockNetworkLoads();

@CalledByNative
public boolean shouldBlockNetworkLoads();
public abstract void onDownloadStart(String url,
String userAgent,
String contentDisposition,
String mimeType,
long contentLength);

@CalledByNative
public void onDownloadStart(String url,
String userAgent,
String contentDisposition,
String mimeType,
long contentLength);
public abstract void newLoginRequest(String realm, String account, String args);

public abstract XWalkWebResourceResponse shouldInterceptRequest(
XWalkContentsClient.XWalkWebResourceRequest request);

// Protected methods ---------------------------------------------------------------------------
@CalledByNative
public void newLoginRequest(String realm, String account, String args);
protected XWalkWebResourceResponse shouldInterceptRequest(String url, boolean isMainFrame,
boolean hasUserGesture, String method, String[] requestHeaderNames,
String[] requestHeaderValues) {
XWalkContentsClient.XWalkWebResourceRequest request =
new XWalkContentsClient.XWalkWebResourceRequest();
request.url = url;
request.isMainFrame = isMainFrame;
request.hasUserGesture = hasUserGesture;
request.method = method;
request.requestHeaders = new ArrayMap<String, String>(requestHeaderNames.length);
for (int i = 0; i < requestHeaderNames.length; ++i) {
request.requestHeaders.put(requestHeaderNames[i], requestHeaderValues[i]);
}
return shouldInterceptRequest(request);
}
}
Loading

0 comments on commit db7728d

Please sign in to comment.