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, WebView web)
     38             throws IOException {
     39         int baseLayer = web.getBaseLayer();
     40         if (baseLayer == 0) {
     41             return false;
     42         }
     43         DataOutputStream dos = new DataOutputStream(stream);
     44         dos.writeInt(VERSION);
     45         dos.writeInt(web.getContentWidth());
     46         dos.writeInt(web.getContentHeight());
     47         return nativeSerializeViewState(baseLayer, dos,
     48                 new byte[WORKING_STREAM_STORAGE]);
     49     }
     50 
     51     static DrawData deserializeViewState(InputStream stream, WebView web)
     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(dis,
     61                 new byte[WORKING_STREAM_STORAGE]);
     62 
     63         final WebViewCore.DrawData draw = new WebViewCore.DrawData();
     64         draw.mViewState = new WebViewCore.ViewState();
     65         int viewWidth = web.getViewWidth();
     66         int viewHeight = web.getViewHeightWithTitle() - web.getTitleHeight();
     67         draw.mViewSize = new Point(viewWidth, viewHeight);
     68         draw.mContentSize = new Point(contentWidth, contentHeight);
     69         draw.mViewState.mDefaultScale = web.getDefaultZoomScale();
     70         draw.mBaseLayer = baseLayer;
     71         draw.mInvalRegion = new Region(0, 0, contentWidth, contentHeight);
     72         return draw;
     73     }
     74 
     75     private static native boolean nativeSerializeViewState(int baseLayer,
     76             OutputStream stream, byte[] storage);
     77 
     78     // Returns a pointer to the BaseLayer
     79     private static native int nativeDeserializeViewState(
     80             InputStream stream, byte[] storage);
     81 
     82     private ViewStateSerializer() {}
     83 }
     84