1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.webkit;
17
18import android.graphics.Point;
19import android.webkit.WebViewCore.DrawData;
20
21import java.io.DataInputStream;
22import java.io.DataOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26
27/**
28 * @hide
29 */
30class ViewStateSerializer {
31
32    private static final int WORKING_STREAM_STORAGE = 16 * 1024;
33
34    static final int VERSION = 1;
35
36    static boolean serializeViewState(OutputStream stream, DrawData draw)
37            throws IOException {
38        int baseLayer = draw.mBaseLayer;
39        if (baseLayer == 0) {
40            return false;
41        }
42        DataOutputStream dos = new DataOutputStream(stream);
43        dos.writeInt(VERSION);
44        dos.writeInt(draw.mContentSize.x);
45        dos.writeInt(draw.mContentSize.y);
46        return nativeSerializeViewState(baseLayer, dos,
47                new byte[WORKING_STREAM_STORAGE]);
48    }
49
50    static DrawData deserializeViewState(InputStream stream)
51            throws IOException {
52        DataInputStream dis = new DataInputStream(stream);
53        int version = dis.readInt();
54        if (version > VERSION) {
55            throw new IOException("Unexpected version: " + version);
56        }
57        int contentWidth = dis.readInt();
58        int contentHeight = dis.readInt();
59        int baseLayer = nativeDeserializeViewState(version, dis,
60                new byte[WORKING_STREAM_STORAGE]);
61
62        final WebViewCore.DrawData draw = new WebViewCore.DrawData();
63        draw.mViewState = new WebViewCore.ViewState();
64        draw.mContentSize = new Point(contentWidth, contentHeight);
65        draw.mBaseLayer = baseLayer;
66        stream.close();
67        return draw;
68    }
69
70    public static void dumpLayerHierarchy(int baseLayer, OutputStream out, int level) {
71        nativeDumpLayerHierarchy(baseLayer, level, out,
72                new byte[WORKING_STREAM_STORAGE]);
73    }
74
75
76    private static native void nativeDumpLayerHierarchy(int baseLayer, int level,
77            OutputStream out, byte[] storage);
78
79    private static native boolean nativeSerializeViewState(int baseLayer,
80            OutputStream stream, byte[] storage);
81
82    // Returns a pointer to the BaseLayer
83    private static native int nativeDeserializeViewState(int version,
84            InputStream stream, byte[] storage);
85
86    private ViewStateSerializer() {}
87}
88