Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 57217f23 authored by Jonathan Dixon's avatar Jonathan Dixon Committed by Android (Google) Code Review
Browse files

Merge "Separate interface and implementation of 2 more WebView classes"

parents cbb987e1 fcc1f75b
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ class CallbackProxy extends Handler {
    // Start with 100 to indicate it is not in load for the empty page.
    private volatile int mLatestProgress = 100;
    // Back/Forward list
    private final WebBackForwardList mBackForwardList;
    private final WebBackForwardListClassic mBackForwardList;
    // Back/Forward list client
    private volatile WebBackForwardListClient mWebBackForwardListClient;
    // Used to call startActivity during url override.
@@ -188,7 +188,7 @@ class CallbackProxy extends Handler {
        // Used to start a default activity.
        mContext = context;
        mWebView = w;
        mBackForwardList = new WebBackForwardList(this);
        mBackForwardList = new WebBackForwardListClassic(this);
    }

    protected synchronized void blockMessages() {
@@ -249,7 +249,7 @@ class CallbackProxy extends Handler {
     * Get the Back/Forward list to return to the user or to update the cached
     * history list.
     */
    public WebBackForwardList getBackForwardList() {
    public WebBackForwardListClassic getBackForwardList() {
        return mBackForwardList;
    }

@@ -1301,7 +1301,7 @@ class CallbackProxy extends Handler {
    public void onReceivedIcon(Bitmap icon) {
        // The current item might be null if the icon was already stored in the
        // database and this is a new WebView.
        WebHistoryItem i = mBackForwardList.getCurrentItem();
        WebHistoryItemClassic i = mBackForwardList.getCurrentItem();
        if (i != null) {
            i.setFavicon(icon);
        }
@@ -1316,7 +1316,7 @@ class CallbackProxy extends Handler {
    /* package */ void onReceivedTouchIconUrl(String url, boolean precomposed) {
        // We should have a current item but we do not want to crash so check
        // for null.
        WebHistoryItem i = mBackForwardList.getCurrentItem();
        WebHistoryItemClassic i = mBackForwardList.getCurrentItem();
        if (i != null) {
            i.setTouchIconUrl(url, precomposed);
        }
+7 −125
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package android.webkit;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * This class contains the back/forward list for a WebView.
@@ -25,22 +24,11 @@ import java.util.ArrayList;
 * inspect the entries in the list.
 */
public class WebBackForwardList implements Cloneable, Serializable {
    // Current position in the list.
    private int mCurrentIndex;
    // ArrayList of WebHistoryItems for maintaining our copy.
    private ArrayList<WebHistoryItem> mArray;
    // Flag to indicate that the list is invalid
    private boolean mClearPending;
    // CallbackProxy to issue client callbacks.
    private final CallbackProxy mCallbackProxy;

    /**
     * Construct a back/forward list used by clients of WebView.
     *  @hide
     */
    /*package*/ WebBackForwardList(CallbackProxy proxy) {
        mCurrentIndex = -1;
        mArray = new ArrayList<WebHistoryItem>();
        mCallbackProxy = proxy;
    public WebBackForwardList() {
    }

    /**
@@ -49,7 +37,7 @@ public class WebBackForwardList implements Cloneable, Serializable {
     * @return The current history item.
     */
    public synchronized WebHistoryItem getCurrentItem() {
        return getItemAtIndex(mCurrentIndex);
        throw new MustOverrideException();
    }

    /**
@@ -58,7 +46,7 @@ public class WebBackForwardList implements Cloneable, Serializable {
     * @return The current index from 0...n or -1 if the list is empty.
     */
    public synchronized int getCurrentIndex() {
        return mCurrentIndex;
        throw new MustOverrideException();
    }

    /**
@@ -67,10 +55,7 @@ public class WebBackForwardList implements Cloneable, Serializable {
     * @param index The index to retrieve.
     */
    public synchronized WebHistoryItem getItemAtIndex(int index) {
        if (index < 0 || index >= getSize()) {
            return null;
        }
        return mArray.get(index);
        throw new MustOverrideException();
    }

    /**
@@ -78,78 +63,7 @@ public class WebBackForwardList implements Cloneable, Serializable {
     * @return The size of the list.
     */
    public synchronized int getSize() {
        return mArray.size();
    }

    /**
     * Mark the back/forward list as having a pending clear. This is used on the
     * UI side to mark the list as being invalid during the clearHistory method.
     */
    /*package*/ synchronized void setClearPending() {
        mClearPending = true;
    }

    /**
     * Return the status of the clear flag. This is used on the UI side to
     * determine if the list is valid for checking things like canGoBack.
     */
    /*package*/ synchronized boolean getClearPending() {
        return mClearPending;
    }

    /**
     * Add a new history item to the list. This will remove all items after the
     * current item and append the new item to the end of the list. Called from
     * the WebCore thread only. Synchronized because the UI thread may be
     * reading the array or the current index.
     * @param item A new history item.
     */
    /*package*/ synchronized void addHistoryItem(WebHistoryItem item) {
        // Update the current position because we are going to add the new item
        // in that slot.
        ++mCurrentIndex;
        // If the current position is not at the end, remove all history items
        // after the current item.
        final int size = mArray.size();
        final int newPos = mCurrentIndex;
        if (newPos != size) {
            for (int i = size - 1; i >= newPos; i--) {
                final WebHistoryItem h = mArray.remove(i);
            }
        }
        // Add the item to the list.
        mArray.add(item);
        if (mCallbackProxy != null) {
            mCallbackProxy.onNewHistoryItem(item);
        }
    }

    /**
     * Clear the back/forward list. Called from the WebCore thread.
     */
    /*package*/ synchronized void close(int nativeFrame) {
        // Clear the array first because nativeClose will call addHistoryItem
        // with the current item.
        mArray.clear();
        mCurrentIndex = -1;
        nativeClose(nativeFrame);
        // Reset the clear flag
        mClearPending = false;
    }

    /* Remove the item at the given index. Called by JNI only. */
    private synchronized void removeHistoryItem(int index) {
        // XXX: This is a special case. Since the callback is only triggered
        // when removing the first item, we can assert that the index is 0.
        // This lets us change the current index without having to query the
        // native BackForwardList.
        if (DebugFlags.WEB_BACK_FORWARD_LIST && (index != 0)) {
            throw new AssertionError();
        }
        final WebHistoryItem h = mArray.remove(index);
        // XXX: If we ever add another callback for removing history items at
        // any index, this will no longer be valid.
        mCurrentIndex--;
        throw new MustOverrideException();
    }

    /**
@@ -158,39 +72,7 @@ public class WebBackForwardList implements Cloneable, Serializable {
     * webkit package classes.
     */
    protected synchronized WebBackForwardList clone() {
        WebBackForwardList l = new WebBackForwardList(null);
        if (mClearPending) {
            // If a clear is pending, return a copy with only the current item.
            l.addHistoryItem(getCurrentItem());
            return l;
        }
        l.mCurrentIndex = mCurrentIndex;
        int size = getSize();
        l.mArray = new ArrayList<WebHistoryItem>(size);
        for (int i = 0; i < size; i++) {
            // Add a copy of each WebHistoryItem
            l.mArray.add(mArray.get(i).clone());
        }
        return l;
        throw new MustOverrideException();
    }

    /**
     * Set the new history index.
     * @param newIndex The new history index.
     */
    /*package*/ synchronized void setCurrentIndex(int newIndex) {
        mCurrentIndex = newIndex;
        if (mCallbackProxy != null) {
            mCallbackProxy.onIndexChanged(getItemAtIndex(newIndex), newIndex);
        }
    }

    /**
     * Restore the history index.
     */
    /*package*/ static native synchronized void restoreIndex(int nativeFrame,
            int index);

    /* Close the native list. */
    private static native void nativeClose(int nativeFrame);
}
+166 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2012 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 java.io.Serializable;
import java.util.ArrayList;

/* package */ class WebBackForwardListClassic extends WebBackForwardList implements Cloneable,
        Serializable {

    // Current position in the list.
    private int mCurrentIndex;
    // ArrayList of WebHistoryItems for maintaining our copy.
    private ArrayList<WebHistoryItemClassic> mArray;
    // Flag to indicate that the list is invalid
    private boolean mClearPending;
    // CallbackProxy to issue client callbacks.
    private final CallbackProxy mCallbackProxy;

    /*package*/ WebBackForwardListClassic(CallbackProxy proxy) {
        mCurrentIndex = -1;
        mArray = new ArrayList<WebHistoryItemClassic>();
        mCallbackProxy = proxy;
    }

    public synchronized WebHistoryItemClassic getCurrentItem() {
        return getItemAtIndex(mCurrentIndex);
    }

    public synchronized int getCurrentIndex() {
        return mCurrentIndex;
    }

    public synchronized WebHistoryItemClassic getItemAtIndex(int index) {
        if (index < 0 || index >= getSize()) {
            return null;
        }
        return mArray.get(index);
    }

    public synchronized int getSize() {
        return mArray.size();
    }

    /**
     * Mark the back/forward list as having a pending clear. This is used on the
     * UI side to mark the list as being invalid during the clearHistory method.
     */
    /*package*/ synchronized void setClearPending() {
        mClearPending = true;
    }

    /**
     * Return the status of the clear flag. This is used on the UI side to
     * determine if the list is valid for checking things like canGoBack.
     */
    /*package*/ synchronized boolean getClearPending() {
        return mClearPending;
    }

    /**
     * Add a new history item to the list. This will remove all items after the
     * current item and append the new item to the end of the list. Called from
     * the WebCore thread only. Synchronized because the UI thread may be
     * reading the array or the current index.
     * @param item A new history item.
     */
    /*package*/ synchronized void addHistoryItem(WebHistoryItem item) {
        // Update the current position because we are going to add the new item
        // in that slot.
        ++mCurrentIndex;
        // If the current position is not at the end, remove all history items
        // after the current item.
        final int size = mArray.size();
        final int newPos = mCurrentIndex;
        if (newPos != size) {
            for (int i = size - 1; i >= newPos; i--) {
                final WebHistoryItem h = mArray.remove(i);
            }
        }
        // Add the item to the list.
        mArray.add((WebHistoryItemClassic) item);
        if (mCallbackProxy != null) {
            mCallbackProxy.onNewHistoryItem(item);
        }
    }

    /**
     * Clear the back/forward list. Called from the WebCore thread.
     */
    /*package*/ synchronized void close(int nativeFrame) {
        // Clear the array first because nativeClose will call addHistoryItem
        // with the current item.
        mArray.clear();
        mCurrentIndex = -1;
        nativeClose(nativeFrame);
        // Reset the clear flag
        mClearPending = false;
    }

    /* Remove the item at the given index. Called by JNI only. */
    private synchronized void removeHistoryItem(int index) {
        // XXX: This is a special case. Since the callback is only triggered
        // when removing the first item, we can assert that the index is 0.
        // This lets us change the current index without having to query the
        // native BackForwardList.
        if (DebugFlags.WEB_BACK_FORWARD_LIST && (index != 0)) {
            throw new AssertionError();
        }
        final WebHistoryItem h = mArray.remove(index);
        // XXX: If we ever add another callback for removing history items at
        // any index, this will no longer be valid.
        mCurrentIndex--;
    }

    public synchronized WebBackForwardListClassic clone() {
        WebBackForwardListClassic l = new WebBackForwardListClassic(null);
        if (mClearPending) {
            // If a clear is pending, return a copy with only the current item.
            l.addHistoryItem(getCurrentItem());
            return l;
        }
        l.mCurrentIndex = mCurrentIndex;
        int size = getSize();
        l.mArray = new ArrayList<WebHistoryItemClassic>(size);
        for (int i = 0; i < size; i++) {
            // Add a copy of each WebHistoryItem
            l.mArray.add(mArray.get(i).clone());
        }
        return l;
    }

    /**
     * Set the new history index.
     * @param newIndex The new history index.
     */
    /*package*/ synchronized void setCurrentIndex(int newIndex) {
        mCurrentIndex = newIndex;
        if (mCallbackProxy != null) {
            mCallbackProxy.onIndexChanged(getItemAtIndex(newIndex), newIndex);
        }
    }

    /**
     * Restore the history index.
     */
    /*package*/ static native synchronized void restoreIndex(int nativeFrame,
            int index);

    /* Close the native list. */
    private static native void nativeClose(int nativeFrame);
}
+7 −177
Original line number Diff line number Diff line
@@ -18,9 +18,6 @@ package android.webkit;

import android.graphics.Bitmap;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * A convenience class for accessing fields in an entry in the back/forward list
 * of a WebView. Each WebHistoryItem is a snapshot of the requested history
@@ -28,67 +25,8 @@ import java.net.URL;
 * @see WebBackForwardList
 */
public class WebHistoryItem implements Cloneable {
    // Global identifier count.
    private static int sNextId = 0;
    // Unique identifier.
    private final int mId;
    // A point to a native WebHistoryItem instance which contains the actual data
    private int mNativeBridge;
    // The favicon for this item.
    private Bitmap mFavicon;
    // The pre-flattened data used for saving the state.
    private byte[] mFlattenedData;
    // The apple-touch-icon url for use when adding the site to the home screen,
    // as obtained from a <link> element in the page.
    private String mTouchIconUrlFromLink;
    // If no <link> is specified, this holds the default location of the
    // apple-touch-icon.
    private String mTouchIconUrlServerDefault;
    // Custom client data that is not flattened or read by native code.
    private Object mCustomData;

    /**
     * Basic constructor that assigns a unique id to the item. Called by JNI
     * only.
     */
    private WebHistoryItem(int nativeBridge) {
        synchronized (WebHistoryItem.class) {
            mId = sNextId++;
        }
        mNativeBridge = nativeBridge;
        nativeRef(mNativeBridge);
    }

    protected void finalize() throws Throwable {
        if (mNativeBridge != 0) {
            nativeUnref(mNativeBridge);
            mNativeBridge = 0;
        }
    }

    /**
     * Construct a new WebHistoryItem with initial flattened data.
     * @param data The pre-flattened data coming from restoreState.
     */
    /*package*/ WebHistoryItem(byte[] data) {
        mFlattenedData = data;
        synchronized (WebHistoryItem.class) {
            mId = sNextId++;
        }
    }

    /**
     * Construct a clone of a WebHistoryItem from the given item.
     * @param item The history item to clone.
     */
    private WebHistoryItem(WebHistoryItem item) {
        mFlattenedData = item.mFlattenedData;
        mId = item.mId;
        mFavicon = item.mFavicon;
        mNativeBridge = item.mNativeBridge;
        if (mNativeBridge != 0) {
            nativeRef(mNativeBridge);
        }
    /* package */ WebHistoryItem() {
    }

    /**
@@ -100,7 +38,7 @@ public class WebHistoryItem implements Cloneable {
     */
    @Deprecated
    public int getId() {
        return mId;
        throw new MustOverrideException();
    }

    /**
@@ -112,8 +50,7 @@ public class WebHistoryItem implements Cloneable {
     * to synchronize this method.
     */
    public String getUrl() {
        if (mNativeBridge == 0) return null;
        return nativeGetUrl(mNativeBridge);
        throw new MustOverrideException();
    }

    /**
@@ -123,8 +60,7 @@ public class WebHistoryItem implements Cloneable {
     * @return The original url of this history item.
     */
    public String getOriginalUrl() {
        if (mNativeBridge == 0) return null;
        return nativeGetOriginalUrl(mNativeBridge);
        throw new MustOverrideException();
    }
    
    /**
@@ -134,8 +70,7 @@ public class WebHistoryItem implements Cloneable {
     * to synchronize this method.
     */
    public String getTitle() {
        if (mNativeBridge == 0) return null;
        return nativeGetTitle(mNativeBridge);
        throw new MustOverrideException();
    }

    /**
@@ -145,119 +80,14 @@ public class WebHistoryItem implements Cloneable {
     * to synchronize this method.
     */
    public Bitmap getFavicon() {
        if (mFavicon == null && mNativeBridge != 0) {
            mFavicon = nativeGetFavicon(mNativeBridge);
        }
        return mFavicon;
    }

    /**
     * Return the touch icon url.
     * If no touch icon <link> tag was specified, returns
     * <host>/apple-touch-icon.png. The DownloadTouchIcon class that
     * attempts to retrieve the touch icon will handle the case where
     * that file does not exist. An icon set by a <link> tag is always
     * used in preference to an icon saved on the server.
     * @hide
     */
    public String getTouchIconUrl() {
        if (mTouchIconUrlFromLink != null) {
            return mTouchIconUrlFromLink;
        } else if (mTouchIconUrlServerDefault != null) {
            return mTouchIconUrlServerDefault;
        }

        try {
            URL url = new URL(getOriginalUrl());
            mTouchIconUrlServerDefault = new URL(url.getProtocol(), url.getHost(), url.getPort(),
                    "/apple-touch-icon.png").toString();
        } catch (MalformedURLException e) {
            return null;
        }
        return mTouchIconUrlServerDefault;
    }

    /**
     * Return the custom data provided by the client.
     * @hide
     */
    public Object getCustomData() {
        return mCustomData;
    }

    /**
     * Set the custom data field.
     * @param data An Object containing any data the client wishes to associate
     *             with the item.
     * @hide
     */
    public void setCustomData(Object data) {
        // NOTE: WebHistoryItems are used in multiple threads. However, the
        // public facing apis are all getters with the exception of this one
        // api. Since this api is exclusive to clients, we don't make any
        // promises about thread safety.
        mCustomData = data;
    }

    /**
     * Set the favicon.
     * @param icon A Bitmap containing the favicon for this history item.
     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
     * to synchronize this method.
     */
    /*package*/ void setFavicon(Bitmap icon) {
        mFavicon = icon;
    }

    /**
     * Set the touch icon url. Will not overwrite an icon that has been
     * set already from a <link> tag, unless the new icon is precomposed.
     * @hide
     */
    /*package*/ void setTouchIconUrl(String url, boolean precomposed) {
        if (precomposed || mTouchIconUrlFromLink == null) {
            mTouchIconUrlFromLink = url;
        }
    }

    /**
     * Get the pre-flattened data.
     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
     * to synchronize this method.
     */
    /*package*/ byte[] getFlattenedData() {
        if (mNativeBridge != 0) {
            return nativeGetFlattenedData(mNativeBridge);
        }
        return mFlattenedData;
    }

    /**
     * Inflate this item.
     * Note: The VM ensures 32-bit atomic read/write operations so we don't have
     * to synchronize this method.
     */
    /*package*/ void inflate(int nativeFrame) {
        mNativeBridge = inflate(nativeFrame, mFlattenedData);
        mFlattenedData = null;
        throw new MustOverrideException();
    }

    /**
     * Clone the history item for use by clients of WebView.
     */
    protected synchronized WebHistoryItem clone() {
        return new WebHistoryItem(this);
        throw new MustOverrideException();
    }

    /* Natively inflate this item, this method is called in the WebCore thread.
     */
    private native int inflate(int nativeFrame, byte[] data);
    private native void nativeRef(int nptr);
    private native void nativeUnref(int nptr);
    private native String nativeGetTitle(int nptr);
    private native String nativeGetUrl(int nptr);
    private native String nativeGetOriginalUrl(int nptr);
    private native byte[] nativeGetFlattenedData(int nptr);
    private native Bitmap nativeGetFavicon(int nptr);

}
+221 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading