Skip to content

Commit 9dd4a6c

Browse files
jreckAndroid (Google) Code Review
authored andcommitted
Merge "Save and load snapshots async"
2 parents d65c2be + ee3b562 commit 9dd4a6c

File tree

3 files changed

+84
-28
lines changed

3 files changed

+84
-28
lines changed

core/java/android/webkit/ViewStateSerializer.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ class ViewStateSerializer {
3434

3535
static final int VERSION = 1;
3636

37-
static boolean serializeViewState(OutputStream stream, WebViewClassic web)
37+
static boolean serializeViewState(OutputStream stream, DrawData draw)
3838
throws IOException {
39-
int baseLayer = web.getBaseLayer();
39+
int baseLayer = draw.mBaseLayer;
4040
if (baseLayer == 0) {
4141
return false;
4242
}
4343
DataOutputStream dos = new DataOutputStream(stream);
4444
dos.writeInt(VERSION);
45-
dos.writeInt(web.getContentWidth());
46-
dos.writeInt(web.getContentHeight());
45+
dos.writeInt(draw.mContentSize.x);
46+
dos.writeInt(draw.mContentSize.y);
4747
return nativeSerializeViewState(baseLayer, dos,
4848
new byte[WORKING_STREAM_STORAGE]);
4949
}
5050

51-
static DrawData deserializeViewState(InputStream stream, WebViewClassic web)
51+
static DrawData deserializeViewState(InputStream stream)
5252
throws IOException {
5353
DataInputStream dis = new DataInputStream(stream);
5454
int version = dis.readInt();
@@ -62,13 +62,10 @@ static DrawData deserializeViewState(InputStream stream, WebViewClassic web)
6262

6363
final WebViewCore.DrawData draw = new WebViewCore.DrawData();
6464
draw.mViewState = new WebViewCore.ViewState();
65-
int viewWidth = web.getViewWidth();
66-
int viewHeight = web.getViewHeightWithTitle() - web.getTitleHeight();
67-
draw.mViewSize = new Point(viewWidth, viewHeight);
6865
draw.mContentSize = new Point(contentWidth, contentHeight);
69-
draw.mViewState.mDefaultScale = web.getDefaultZoomScale();
7066
draw.mBaseLayer = baseLayer;
7167
draw.mInvalRegion = new Region(0, 0, contentWidth, contentHeight);
68+
stream.close();
7269
return draw;
7370
}
7471

core/java/android/webkit/WebViewClassic.java

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,34 +2437,51 @@ public void run() {
24372437
* version specific, and may not be able to be loaded by newer versions
24382438
* of WebView.
24392439
* @param stream The {@link OutputStream} to save to
2440-
* @return True if saved successfully
2440+
* @param callback The {@link ValueCallback} to call with the result
24412441
*/
2442-
public boolean saveViewState(OutputStream stream) {
2443-
try {
2444-
return ViewStateSerializer.serializeViewState(stream, this);
2445-
} catch (IOException e) {
2446-
Log.w(LOGTAG, "Failed to saveViewState", e);
2442+
public void saveViewState(OutputStream stream, ValueCallback<Boolean> callback) {
2443+
if (mWebViewCore == null) {
2444+
callback.onReceiveValue(false);
2445+
return;
24472446
}
2448-
return false;
2447+
mWebViewCore.sendMessageAtFrontOfQueue(EventHub.SAVE_VIEW_STATE,
2448+
new WebViewCore.SaveViewStateRequest(stream, callback));
24492449
}
24502450

24512451
/**
24522452
* Loads the view data from the input stream. See
24532453
* {@link #saveViewState(OutputStream)} for more information.
24542454
* @param stream The {@link InputStream} to load from
2455-
* @return True if loaded successfully
24562455
*/
2457-
public boolean loadViewState(InputStream stream) {
2458-
try {
2459-
mLoadedPicture = ViewStateSerializer.deserializeViewState(stream, this);
2460-
mBlockWebkitViewMessages = true;
2461-
setNewPicture(mLoadedPicture, true);
2462-
mLoadedPicture.mViewState = null;
2463-
return true;
2464-
} catch (IOException e) {
2465-
Log.w(LOGTAG, "Failed to loadViewState", e);
2466-
}
2467-
return false;
2456+
public void loadViewState(InputStream stream) {
2457+
mBlockWebkitViewMessages = true;
2458+
new AsyncTask<InputStream, Void, DrawData>() {
2459+
2460+
@Override
2461+
protected DrawData doInBackground(InputStream... params) {
2462+
try {
2463+
return ViewStateSerializer.deserializeViewState(params[0]);
2464+
} catch (IOException e) {
2465+
return null;
2466+
}
2467+
}
2468+
2469+
@Override
2470+
protected void onPostExecute(DrawData draw) {
2471+
if (draw == null) {
2472+
Log.e(LOGTAG, "Failed to load view state!");
2473+
return;
2474+
}
2475+
int viewWidth = getViewWidth();
2476+
int viewHeight = getViewHeightWithTitle() - getTitleHeight();
2477+
draw.mViewSize = new Point(viewWidth, viewHeight);
2478+
draw.mViewState.mDefaultScale = getDefaultZoomScale();
2479+
mLoadedPicture = draw;
2480+
setNewPicture(mLoadedPicture, true);
2481+
mLoadedPicture.mViewState = null;
2482+
}
2483+
2484+
}.execute(stream);
24682485
}
24692486

24702487
/**

core/java/android/webkit/WebViewCore.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import junit.framework.Assert;
4646

47+
import java.io.OutputStream;
4748
import java.util.ArrayList;
4849
import java.util.Collection;
4950
import java.util.Iterator;
@@ -1048,6 +1049,15 @@ public FindAllRequest(String text) {
10481049
public int mMatchIndex;
10491050
}
10501051

1052+
static class SaveViewStateRequest {
1053+
SaveViewStateRequest(OutputStream s, ValueCallback<Boolean> cb) {
1054+
mStream = s;
1055+
mCallback = cb;
1056+
}
1057+
public OutputStream mStream;
1058+
public ValueCallback<Boolean> mCallback;
1059+
}
1060+
10511061
/**
10521062
* @hide
10531063
*/
@@ -1180,6 +1190,8 @@ public class EventHub implements WebViewInputDispatcher.WebKitCallbacks {
11801190
static final int KEY_PRESS = 223;
11811191
static final int SET_INITIAL_FOCUS = 224;
11821192

1193+
static final int SAVE_VIEW_STATE = 225;
1194+
11831195
// Private handler for WebCore messages.
11841196
private Handler mHandler;
11851197
// Message queue for containing messages before the WebCore thread is
@@ -1754,8 +1766,13 @@ public void handleMessage(Message msg) {
17541766
case SET_INITIAL_FOCUS:
17551767
nativeSetInitialFocus(mNativeClass, msg.arg1);
17561768
break;
1769+
case SAVE_VIEW_STATE:
1770+
SaveViewStateRequest request = (SaveViewStateRequest) msg.obj;
1771+
saveViewState(request.mStream, request.mCallback);
1772+
break;
17571773
}
17581774
}
1775+
17591776
};
17601777
// Take all queued messages and resend them to the new handler.
17611778
synchronized (this) {
@@ -2254,6 +2271,31 @@ private void webkitDraw(DrawData draw) {
22542271
}
22552272
}
22562273

2274+
private void saveViewState(OutputStream stream,
2275+
ValueCallback<Boolean> callback) {
2276+
// TODO: Create a native method to do this better without overloading
2277+
// the draw path (and fix saving <canvas>)
2278+
DrawData draw = new DrawData();
2279+
if (DebugFlags.WEB_VIEW_CORE) Log.v(LOGTAG, "saveViewState start");
2280+
draw.mBaseLayer = nativeRecordContent(mNativeClass, draw.mInvalRegion,
2281+
draw.mContentSize);
2282+
boolean result = false;
2283+
try {
2284+
result = ViewStateSerializer.serializeViewState(stream, draw);
2285+
} catch (Throwable t) {
2286+
Log.w(LOGTAG, "Failed to save view state", t);
2287+
}
2288+
callback.onReceiveValue(result);
2289+
if (draw.mBaseLayer != 0) {
2290+
if (mDrawIsScheduled) {
2291+
mDrawIsScheduled = false;
2292+
mEventHub.removeMessages(EventHub.WEBKIT_DRAW);
2293+
}
2294+
mLastDrawData = draw;
2295+
webkitDraw(draw);
2296+
}
2297+
}
2298+
22572299
static void reducePriority() {
22582300
// remove the pending REDUCE_PRIORITY and RESUME_PRIORITY messages
22592301
sWebCoreHandler.removeMessages(WebCoreThread.REDUCE_PRIORITY);

0 commit comments

Comments
 (0)