Skip to content

Commit

Permalink
fix issue[90]
Browse files Browse the repository at this point in the history
1. fix issue 90 which will cause deadlock in low version webview kernel;

2.remove application attributes from sdk manifest.
  • Loading branch information
lovekidan committed Sep 14, 2017
1 parent 6d84ba8 commit 68fe611
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 30 deletions.
6 changes: 3 additions & 3 deletions sonic-android/sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
versionName "2.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -26,10 +26,10 @@ dependencies {
})

// compile sdk from jcenter
compile 'com.tencent.sonic:sdk:1.0.0'
// compile 'com.tencent.sonic:sdk:1.0.0'

// compile sonic-sdk from local path
// compile project(path: ':sdk')
compile project(path: ':sdk')

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
Expand Down
7 changes: 1 addition & 6 deletions sonic-android/sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
<manifest

package="com.tencent.sonic.sdk">

<application android:allowBackup="true" android:label="@string/app_name"
android:supportsRtl="true">

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ public boolean onClientReady() {
return false;
}

public Object onClientRequestResource(String url) {
protected Object onRequestResource(String url) {
if (wasInterceptInvoked.get() || !isMatchCurrentUrl(url)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public boolean shouldLog(int level) {
public abstract String getCookie(String url);

/**
* Det cookies to webview after session connection response with cookies in it's headers.
* Set cookies to webview after session connection response with cookies in it's headers.
*
* @param url The url which need to set cookies
* @param cookies The cookies for current input url
Expand Down
125 changes: 107 additions & 18 deletions sonic-android/sdk/src/main/java/com/tencent/sonic/sdk/SonicSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public class SonicSession implements SonicSessionStream.Callback, Handler.Callba

public static final String WEB_RESPONSE_LOCAL_REFRESH_TIME = "local_refresh_time";


/**
* Name of chrome file thread
*/
public static final String CHROME_FILE_THREAD = "Chrome_FileThread";

/**
* Session state : original.
* <p>
Expand Down Expand Up @@ -214,6 +220,22 @@ public class SonicSession implements SonicSessionStream.Callback, Handler.Callba

protected static final int COMMON_MSG_END = COMMON_MSG_BEGIN + 4;


/**
* Resource Intercept State : none
*/
protected static final int RESOURCE_INTERCEPT_STATE_NONE = 0;

/**
* Resource Intercept State : intercepting in file thread
*/
protected static final int RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD = 1;

/**
* Resource Intercept State : intercepting in other thread(may be IOThread or other else)
*/
protected static final int RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD = 2;

/**
* Session state, include <code>STATE_NONE</code>, <code>STATE_RUNNING</code>,
* <code>STATE_READY</code> and <code>STATE_DESTROY</code>.
Expand Down Expand Up @@ -252,13 +274,29 @@ public class SonicSession implements SonicSessionStream.Callback, Handler.Callba
*/
protected final AtomicBoolean isWaitingForSessionThread = new AtomicBoolean(false);


/**
* Whether the local html is loaded, it is used only the template changes.
*/
protected final AtomicBoolean wasOnPageFinishInvoked = new AtomicBoolean(false);


/**
* Resource intercept state, include <code>RESOURCE_INTERCEPT_STATE_NONE</code>,
* <code>RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD</code>,
* <code>RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD</code>
* More about it at {https://codereview.chromium.org/1350553005/#ps20001}
*/
protected final AtomicInteger resourceInterceptState = new AtomicInteger(RESOURCE_INTERCEPT_STATE_NONE);

/**
* Session statics var
*/
protected final SonicSessionStatistics statistics = new SonicSessionStatistics();

/**
* Session Connection impl var
*/
protected volatile SonicSessionConnection sessionConnection;

/**
Expand Down Expand Up @@ -467,23 +505,11 @@ protected void handleFlow_Connection(String htmlString) {

// If the page has set cookie, sonic will set the cookie to kernel.
startTime = System.currentTimeMillis();
Map<String, List<String>> HeaderFieldsMap = sessionConnection.getResponseHeaderFields();
Map<String, List<String>> headerFieldsMap = sessionConnection.getResponseHeaderFields();
if (SonicUtils.shouldLog(Log.DEBUG)) {
SonicUtils.log(TAG, Log.DEBUG, "session(" + sId + ") connection get header fields cost = " + (System.currentTimeMillis() - startTime) + " ms.");
}

if (null != HeaderFieldsMap) {
String keyOfSetCookie = null;
if (HeaderFieldsMap.containsKey("Set-Cookie")) {
keyOfSetCookie = "Set-Cookie";
} else if (HeaderFieldsMap.containsKey("set-cookie")) {
keyOfSetCookie = "set-cookie";
}
if (!TextUtils.isEmpty(keyOfSetCookie)) {
List<String> cookieList = HeaderFieldsMap.get(keyOfSetCookie);
SonicEngine.getInstance().getRuntime().setCookie(getCurrentUrl(), cookieList);
}
}
setCookiesFromHeaders(headerFieldsMap, shouldSetCookieAsynchronous());
}

SonicUtils.log(TAG, Log.INFO, "session(" + sId + ") handleFlow_Connection: respCode = " + responseCode + ", cost " + (System.currentTimeMillis() - statistics.connectionFlowStartTime) + " ms.");
Expand Down Expand Up @@ -933,7 +959,70 @@ public boolean bindClient(SonicSessionClient client) {
*
* @return True if it is set for the first time
*/
protected boolean onClientReady() {
public boolean onClientReady() {
return false;
}

/**
* When the webview initiates a resource interception, the client invokes the method to retrieve the data
*
* @param url The url of this session
* @return Return the data to kernel
*/
public final Object onClientRequestResource(String url) {
String currentThreadName = Thread.currentThread().getName();
if (CHROME_FILE_THREAD.equals(currentThreadName)) {
resourceInterceptState.set(RESOURCE_INTERCEPT_STATE_IN_FILE_THREAD);
} else {
resourceInterceptState.set(RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD);
if (SonicUtils.shouldLog(Log.DEBUG)) {
SonicUtils.log(TAG, Log.DEBUG, "onClientRequestResource called in " + currentThreadName + ".");
}
}
Object object = onRequestResource(url);
resourceInterceptState.set(RESOURCE_INTERCEPT_STATE_NONE);
return object;
}

/**
* Whether should set cookie asynchronous or not , if {@code onClientRequestResource} is calling
* in IOThread, it should not call set cookie synchronous which will handle in IOThread as it may
* cause deadlock
* More about it see {https://issuetracker.google.com/issues/36989494#c8}
* Fix VasSonic issue {https://github.com/Tencent/VasSonic/issues/90}
*
* @param url The url of this session
* @return Return the data to kernel
*/
protected boolean shouldSetCookieAsynchronous() {
return RESOURCE_INTERCEPT_STATE_IN_OTHER_THREAD == resourceInterceptState.get();
}

/**
* Set cookies to webview from headers
*
* @param headers headers from server response
* @param executeInNewThread whether execute in new thread or not
* @return Set cookie success or not
*/
protected boolean setCookiesFromHeaders(Map<String, List<String>> headers, boolean executeInNewThread) {
if (null != headers) {
final List<String> cookies = headers.get(SonicSessionConnection.HTTP_HEAD_FILED_SET_COOKIE);
if (null != cookies && 0 != cookies.size()) {
if (!executeInNewThread) {
return SonicEngine.getInstance().getRuntime().setCookie(getCurrentUrl(), cookies);
} else {
SonicUtils.log(TAG, Log.INFO, "setCookiesFromHeaders asynchronous in new thread.");
SonicEngine.getInstance().getRuntime().postTaskToThread(new Runnable() {
@Override
public void run() {
SonicEngine.getInstance().getRuntime().setCookie(getCurrentUrl(), cookies);
}
}, 0L);
}
return true;
}
}
return false;
}

Expand All @@ -943,7 +1032,7 @@ protected boolean onClientReady() {
* @param url The url of this session
* @return Return the data to kernel
*/
protected Object onClientRequestResource(String url) {
protected Object onRequestResource(String url) {
return null;
}

Expand All @@ -953,11 +1042,11 @@ protected Object onClientRequestResource(String url) {
* @param diffDataCallback Sonic provides the latest data to the page through this callback
* @return The result
*/
protected boolean onWebReady(SonicDiffDataCallback diffDataCallback) {
public boolean onWebReady(SonicDiffDataCallback diffDataCallback) {
return false;
}

protected boolean onClientPageFinished(String url) {
public boolean onClientPageFinished(String url) {
if (isMatchCurrentUrl(url)) {
SonicUtils.log(TAG, Log.INFO, "session(" + sId + ") onClientPageFinished:url=" + url + ".");
wasOnPageFinishInvoked.set(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public abstract class SonicSessionConnection {
*/
public final static String HTTP_HEAD_CSP_REPORT_ONLY = "Content-Security-Policy-Report-Only";


/**
* HTTP Header:Set-Cookie. <br>
* This header represents the HTML Set-Cookie.
*/
public final static String HTTP_HEAD_FILED_SET_COOKIE = "Set-Cookie";

/**
* SonicSession Object used by SonicSessionConnection.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public boolean onWebReady(SonicDiffDataCallback callback) {
return true;
}

public Object onClientRequestResource(String url) {
protected Object onRequestResource(String url) {
if (!isMatchCurrentUrl(url)) {
return null;
}
Expand Down

0 comments on commit 68fe611

Please sign in to comment.