Home | History | Annotate | Download | only in webkit
      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  */
     16 package android.webkit;
     17 
     18 import android.graphics.Point;
     19 import android.graphics.Region;
     20 import android.webkit.WebViewCore.DrawData;
     21 
     22 import java.io.DataInputStream;
     23 import java.io.DataOutputStream;
     24 import java.io.IOException;
     25 import java.io.InputStream;
     26 import java.io.OutputStream;
     27 
     28 /**
     29  * @hide
     30  */
     31 class ViewStateSerializer {
     32 
     33     private static final int WORKING_STREAM_STORAGE = 16 * 1024;
     34 
     35     static final int VERSION = 1;
     36 
     37     static boolean serializeViewState(OutputStream stream, DrawData draw)
     38             throws IOException {
     39         int baseLayer = draw.mBaseLayer;
     40         if (baseLayer == 0) {
     41             return false;
     42         }
     43         DataOutputStream dos = new DataOutputStream(stream);
     44         dos.writeInt(VERSION);
     45         dos.writeInt(draw.mContentSize.x);
     46         dos.writeInt(draw.mContentSize.y);
     47         return nativeSerializeViewState(baseLayer, dos,
     48                 new byte[WORKING_STREAM_STORAGE]);
     49     }
     50 
     51     static DrawData deserializeViewState(InputStream stream)
     52             throws IOException {
     53         DataInputStream dis = new DataInputStream(stream);
     54         int version = dis.readInt();
     55         if (version > VERSION) {
     56             throw new IOException("Unexpected version: " + version);
     57         }
     58         int contentWidth = dis.readInt();
     59         int contentHeight = dis.readInt();
     60         int baseLayer = nativeDeserializeViewState(version, dis,
     61                 new byte[WORKING_STREAM_STORAGE]);
     62 
     63         final WebViewCore.DrawData draw = new WebViewCore.DrawData();
     64         draw.mViewState = new WebViewCore.ViewState();
     65         draw.mContentSize = new Point(contentWidth, contentHeight);
     66         draw.mBaseLayer = baseLayer;
     67         stream.close();
     68         return draw;
     69     }
     70 
     71     private static native boolean nativeSerializeViewState(int baseLayer,
     72             OutputStream stream, byte[] storage);
     73 
     74     // Returns a pointer to the BaseLayer
     75     private static native int nativeDeserializeViewState(int version,
     76             InputStream stream, byte[] storage);
     77 
     78     private ViewStateSerializer() {}
     79 }
     80