Skip to content

Commit 975b9b7

Browse files
jreckAndroid (Google) Code Review
authored andcommitted
Merge "Delay creating Java objects for WebHistoryItem" into jb-dev
2 parents 89139d7 + 69adc53 commit 975b9b7

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

core/java/android/webkit/WebHistoryItem.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,8 @@ public class WebHistoryItem implements Cloneable {
3232
private static int sNextId = 0;
3333
// Unique identifier.
3434
private final int mId;
35-
// The title of this item's document.
36-
private String mTitle;
37-
// The base url of this item.
38-
private String mUrl;
39-
// The original requested url of this item.
40-
private String mOriginalUrl;
35+
// A point to a native WebHistoryItem instance which contains the actual data
36+
private int mNativeBridge;
4137
// The favicon for this item.
4238
private Bitmap mFavicon;
4339
// The pre-flattened data used for saving the state.
@@ -55,18 +51,26 @@ public class WebHistoryItem implements Cloneable {
5551
* Basic constructor that assigns a unique id to the item. Called by JNI
5652
* only.
5753
*/
58-
private WebHistoryItem() {
54+
private WebHistoryItem(int nativeBridge) {
5955
synchronized (WebHistoryItem.class) {
6056
mId = sNextId++;
6157
}
58+
mNativeBridge = nativeBridge;
59+
nativeRef(mNativeBridge);
60+
}
61+
62+
protected void finalize() throws Throwable {
63+
if (mNativeBridge != 0) {
64+
nativeUnref(mNativeBridge);
65+
mNativeBridge = 0;
66+
}
6267
}
6368

6469
/**
6570
* Construct a new WebHistoryItem with initial flattened data.
6671
* @param data The pre-flattened data coming from restoreState.
6772
*/
6873
/*package*/ WebHistoryItem(byte[] data) {
69-
mUrl = null; // This will be updated natively
7074
mFlattenedData = data;
7175
synchronized (WebHistoryItem.class) {
7276
mId = sNextId++;
@@ -78,12 +82,14 @@ private WebHistoryItem() {
7882
* @param item The history item to clone.
7983
*/
8084
private WebHistoryItem(WebHistoryItem item) {
81-
mUrl = item.mUrl;
82-
mTitle = item.mTitle;
8385
mFlattenedData = item.mFlattenedData;
84-
mFavicon = item.mFavicon;
8586
mId = item.mId;
86-
}
87+
mFavicon = item.mFavicon;
88+
mNativeBridge = item.mNativeBridge;
89+
if (mNativeBridge != 0) {
90+
nativeRef(mNativeBridge);
91+
}
92+
}
8793

8894
/**
8995
* Return an identifier for this history item. If an item is a copy of
@@ -106,7 +112,8 @@ public int getId() {
106112
* to synchronize this method.
107113
*/
108114
public String getUrl() {
109-
return mUrl;
115+
if (mNativeBridge == 0) return null;
116+
return nativeGetUrl(mNativeBridge);
110117
}
111118

112119
/**
@@ -116,7 +123,8 @@ public String getUrl() {
116123
* @return The original url of this history item.
117124
*/
118125
public String getOriginalUrl() {
119-
return mOriginalUrl;
126+
if (mNativeBridge == 0) return null;
127+
return nativeGetOriginalUrl(mNativeBridge);
120128
}
121129

122130
/**
@@ -126,7 +134,8 @@ public String getOriginalUrl() {
126134
* to synchronize this method.
127135
*/
128136
public String getTitle() {
129-
return mTitle;
137+
if (mNativeBridge == 0) return null;
138+
return nativeGetTitle(mNativeBridge);
130139
}
131140

132141
/**
@@ -136,6 +145,9 @@ public String getTitle() {
136145
* to synchronize this method.
137146
*/
138147
public Bitmap getFavicon() {
148+
if (mFavicon == null && mNativeBridge != 0) {
149+
mFavicon = nativeGetFavicon(mNativeBridge);
150+
}
139151
return mFavicon;
140152
}
141153

@@ -156,7 +168,7 @@ public String getTouchIconUrl() {
156168
}
157169

158170
try {
159-
URL url = new URL(mOriginalUrl);
171+
URL url = new URL(getOriginalUrl());
160172
mTouchIconUrlServerDefault = new URL(url.getProtocol(), url.getHost(), url.getPort(),
161173
"/apple-touch-icon.png").toString();
162174
} catch (MalformedURLException e) {
@@ -214,6 +226,9 @@ public void setCustomData(Object data) {
214226
* to synchronize this method.
215227
*/
216228
/*package*/ byte[] getFlattenedData() {
229+
if (mNativeBridge != 0) {
230+
return nativeGetFlattenedData(mNativeBridge);
231+
}
217232
return mFlattenedData;
218233
}
219234

@@ -223,7 +238,8 @@ public void setCustomData(Object data) {
223238
* to synchronize this method.
224239
*/
225240
/*package*/ void inflate(int nativeFrame) {
226-
inflate(nativeFrame, mFlattenedData);
241+
mNativeBridge = inflate(nativeFrame, mFlattenedData);
242+
mFlattenedData = null;
227243
}
228244

229245
/**
@@ -235,15 +251,13 @@ protected synchronized WebHistoryItem clone() {
235251

236252
/* Natively inflate this item, this method is called in the WebCore thread.
237253
*/
238-
private native void inflate(int nativeFrame, byte[] data);
254+
private native int inflate(int nativeFrame, byte[] data);
255+
private native void nativeRef(int nptr);
256+
private native void nativeUnref(int nptr);
257+
private native String nativeGetTitle(int nptr);
258+
private native String nativeGetUrl(int nptr);
259+
private native String nativeGetOriginalUrl(int nptr);
260+
private native byte[] nativeGetFlattenedData(int nptr);
261+
private native Bitmap nativeGetFavicon(int nptr);
239262

240-
/* Called by jni when the item is updated */
241-
private void update(String url, String originalUrl, String title,
242-
Bitmap favicon, byte[] data) {
243-
mUrl = url;
244-
mOriginalUrl = originalUrl;
245-
mTitle = title;
246-
mFavicon = favicon;
247-
mFlattenedData = data;
248-
}
249263
}

0 commit comments

Comments
 (0)