summaryrefslogtreecommitdiffstats
path: root/core/java/android/webkit/ViewStateSerializer.java
blob: 1d44b96c8e2eec4b5810036537227afac74e0398 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright (C) 2011 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 android.webkit;

import android.graphics.Point;
import android.webkit.WebViewCore.DrawData;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @hide
 */
class ViewStateSerializer {

    private static final int WORKING_STREAM_STORAGE = 16 * 1024;

    // VERSION = 1 was for pictures encoded using a previous copy of libskia
    static final int VERSION = 2;

    static boolean serializeViewState(OutputStream stream, DrawData draw)
            throws IOException {
        int baseLayer = draw.mBaseLayer;
        if (baseLayer == 0) {
            return false;
        }
        DataOutputStream dos = new DataOutputStream(stream);
        dos.writeInt(VERSION);
        dos.writeInt(draw.mContentSize.x);
        dos.writeInt(draw.mContentSize.y);
        return nativeSerializeViewState(baseLayer, dos,
                new byte[WORKING_STREAM_STORAGE]);
    }

    static DrawData deserializeViewState(InputStream stream)
            throws IOException {
        DataInputStream dis = new DataInputStream(stream);
        int version = dis.readInt();
        if (version > VERSION) {
            throw new IOException("Unexpected version: " + version);
        }
        int contentWidth = dis.readInt();
        int contentHeight = dis.readInt();
        int baseLayer = nativeDeserializeViewState(version, dis,
                new byte[WORKING_STREAM_STORAGE]);

        final WebViewCore.DrawData draw = new WebViewCore.DrawData();
        draw.mViewState = new WebViewCore.ViewState();
        draw.mContentSize = new Point(contentWidth, contentHeight);
        draw.mBaseLayer = baseLayer;
        stream.close();
        return draw;
    }

    public static void dumpLayerHierarchy(int baseLayer, OutputStream out, int level) {
        nativeDumpLayerHierarchy(baseLayer, level, out,
                new byte[WORKING_STREAM_STORAGE]);
    }


    private static native void nativeDumpLayerHierarchy(int baseLayer, int level,
            OutputStream out, byte[] storage);

    private static native boolean nativeSerializeViewState(int baseLayer,
            OutputStream stream, byte[] storage);

    // Returns a pointer to the BaseLayer
    private static native int nativeDeserializeViewState(int version,
            InputStream stream, byte[] storage);

    private ViewStateSerializer() {}
}