Skip to content

Commit f16fc8e

Browse files
committed
Convert download-related classes to use scoped helpers (see issue chromiumembedded#167)
1 parent 66937b6 commit f16fc8e

13 files changed

Lines changed: 369 additions & 223 deletions

java/org/cef/callback/CefBeforeDownloadCallback_N.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
class CefBeforeDownloadCallback_N extends CefNativeAdapter implements CefBeforeDownloadCallback {
88
CefBeforeDownloadCallback_N() {}
99

10+
@Override
11+
protected void finalize() throws Throwable {
12+
Continue("", false);
13+
super.finalize();
14+
}
15+
1016
@Override
1117
public void Continue(String downloadPath, boolean showDialog) {
1218
try {
13-
N_Continue(downloadPath, showDialog);
19+
N_Continue(getNativeRef(null), downloadPath, showDialog);
1420
} catch (UnsatisfiedLinkError ule) {
1521
ule.printStackTrace();
1622
}
1723
}
1824

19-
private final native void N_Continue(String downloadPath, boolean showDialog);
25+
private final native void N_Continue(long self, String downloadPath, boolean showDialog);
2026
}

java/org/cef/callback/CefDownloadItemCallback.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
package org.cef.callback;
66

77
/**
8-
* Callback interface used to asynchronously cancel a download.
8+
* Callback interface used to asynchronously modify download status.
99
*/
1010
public interface CefDownloadItemCallback {
1111
/**
1212
* Call to cancel the download.
1313
*/
1414
public void cancel();
15+
16+
/**
17+
* Call to pause the download.
18+
*/
19+
public void pause();
20+
21+
/**
22+
* Call to resume the download.
23+
*/
24+
public void resume();
1525
}

java/org/cef/callback/CefDownloadItemCallback_N.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,45 @@
77
class CefDownloadItemCallback_N extends CefNativeAdapter implements CefDownloadItemCallback {
88
CefDownloadItemCallback_N() {}
99

10+
@Override
11+
protected void finalize() throws Throwable {
12+
try {
13+
N_Dispose(getNativeRef(null));
14+
} catch (UnsatisfiedLinkError ule) {
15+
ule.printStackTrace();
16+
}
17+
super.finalize();
18+
}
19+
1020
@Override
1121
public void cancel() {
1222
try {
13-
N_Cancel();
23+
N_Cancel(getNativeRef(null));
24+
} catch (UnsatisfiedLinkError ule) {
25+
ule.printStackTrace();
26+
}
27+
}
28+
29+
@Override
30+
public void pause() {
31+
try {
32+
N_Pause(getNativeRef(null));
33+
} catch (UnsatisfiedLinkError ule) {
34+
ule.printStackTrace();
35+
}
36+
}
37+
38+
@Override
39+
public void resume() {
40+
try {
41+
N_Resume(getNativeRef(null));
1442
} catch (UnsatisfiedLinkError ule) {
1543
ule.printStackTrace();
1644
}
1745
}
1846

19-
private final native void N_Cancel();
47+
private final native void N_Dispose(long self);
48+
private final native void N_Cancel(long self);
49+
private final native void N_Pause(long self);
50+
private final native void N_Resume(long self);
2051
}

java/org/cef/callback/CefDownloadItem_N.java

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
import java.util.Date;
88

99
class CefDownloadItem_N extends CefNativeAdapter implements CefDownloadItem {
10-
protected void finalize() throws Throwable {
11-
super.finalize();
12-
}
13-
1410
CefDownloadItem_N() {}
1511

1612
@Override
1713
public boolean isValid() {
1814
try {
19-
return N_IsValid();
15+
return N_IsValid(getNativeRef(null));
2016
} catch (UnsatisfiedLinkError ule) {
2117
ule.printStackTrace();
2218
}
@@ -26,7 +22,7 @@ public boolean isValid() {
2622
@Override
2723
public boolean isInProgress() {
2824
try {
29-
return N_IsInProgress();
25+
return N_IsInProgress(getNativeRef(null));
3026
} catch (UnsatisfiedLinkError ule) {
3127
ule.printStackTrace();
3228
}
@@ -36,7 +32,7 @@ public boolean isInProgress() {
3632
@Override
3733
public boolean isComplete() {
3834
try {
39-
return N_IsComplete();
35+
return N_IsComplete(getNativeRef(null));
4036
} catch (UnsatisfiedLinkError ule) {
4137
ule.printStackTrace();
4238
}
@@ -46,7 +42,7 @@ public boolean isComplete() {
4642
@Override
4743
public boolean isCanceled() {
4844
try {
49-
return N_IsCanceled();
45+
return N_IsCanceled(getNativeRef(null));
5046
} catch (UnsatisfiedLinkError ule) {
5147
ule.printStackTrace();
5248
}
@@ -56,7 +52,7 @@ public boolean isCanceled() {
5652
@Override
5753
public long getCurrentSpeed() {
5854
try {
59-
return N_GetCurrentSpeed();
55+
return N_GetCurrentSpeed(getNativeRef(null));
6056
} catch (UnsatisfiedLinkError ule) {
6157
ule.printStackTrace();
6258
}
@@ -66,7 +62,7 @@ public long getCurrentSpeed() {
6662
@Override
6763
public int getPercentComplete() {
6864
try {
69-
return N_GetPercentComplete();
65+
return N_GetPercentComplete(getNativeRef(null));
7066
} catch (UnsatisfiedLinkError ule) {
7167
ule.printStackTrace();
7268
}
@@ -76,7 +72,7 @@ public int getPercentComplete() {
7672
@Override
7773
public long getTotalBytes() {
7874
try {
79-
return N_GetTotalBytes();
75+
return N_GetTotalBytes(getNativeRef(null));
8076
} catch (UnsatisfiedLinkError ule) {
8177
ule.printStackTrace();
8278
}
@@ -86,7 +82,7 @@ public long getTotalBytes() {
8682
@Override
8783
public long getReceivedBytes() {
8884
try {
89-
return N_GetReceivedBytes();
85+
return N_GetReceivedBytes(getNativeRef(null));
9086
} catch (UnsatisfiedLinkError ule) {
9187
ule.printStackTrace();
9288
}
@@ -96,7 +92,7 @@ public long getReceivedBytes() {
9692
@Override
9793
public Date getStartTime() {
9894
try {
99-
return N_GetStartTime();
95+
return N_GetStartTime(getNativeRef(null));
10096
} catch (UnsatisfiedLinkError ule) {
10197
ule.printStackTrace();
10298
}
@@ -106,7 +102,7 @@ public Date getStartTime() {
106102
@Override
107103
public Date getEndTime() {
108104
try {
109-
return N_GetEndTime();
105+
return N_GetEndTime(getNativeRef(null));
110106
} catch (UnsatisfiedLinkError ule) {
111107
ule.printStackTrace();
112108
}
@@ -116,7 +112,7 @@ public Date getEndTime() {
116112
@Override
117113
public String getFullPath() {
118114
try {
119-
return N_GetFullPath();
115+
return N_GetFullPath(getNativeRef(null));
120116
} catch (UnsatisfiedLinkError ule) {
121117
ule.printStackTrace();
122118
}
@@ -126,7 +122,7 @@ public String getFullPath() {
126122
@Override
127123
public int getId() {
128124
try {
129-
return N_GetId();
125+
return N_GetId(getNativeRef(null));
130126
} catch (UnsatisfiedLinkError ule) {
131127
ule.printStackTrace();
132128
}
@@ -136,7 +132,7 @@ public int getId() {
136132
@Override
137133
public String getURL() {
138134
try {
139-
return N_GetURL();
135+
return N_GetURL(getNativeRef(null));
140136
} catch (UnsatisfiedLinkError ule) {
141137
ule.printStackTrace();
142138
}
@@ -146,7 +142,7 @@ public String getURL() {
146142
@Override
147143
public String getSuggestedFileName() {
148144
try {
149-
return N_GetSuggestedFileName();
145+
return N_GetSuggestedFileName(getNativeRef(null));
150146
} catch (UnsatisfiedLinkError ule) {
151147
ule.printStackTrace();
152148
}
@@ -156,7 +152,7 @@ public String getSuggestedFileName() {
156152
@Override
157153
public String getContentDisposition() {
158154
try {
159-
return N_GetContentDisposition();
155+
return N_GetContentDisposition(getNativeRef(null));
160156
} catch (UnsatisfiedLinkError ule) {
161157
ule.printStackTrace();
162158
}
@@ -166,27 +162,27 @@ public String getContentDisposition() {
166162
@Override
167163
public String getMimeType() {
168164
try {
169-
return N_GetMimeType();
165+
return N_GetMimeType(getNativeRef(null));
170166
} catch (UnsatisfiedLinkError ule) {
171167
ule.printStackTrace();
172168
}
173169
return null;
174170
}
175171

176-
private final native boolean N_IsValid();
177-
private final native boolean N_IsInProgress();
178-
private final native boolean N_IsComplete();
179-
private final native boolean N_IsCanceled();
180-
private final native long N_GetCurrentSpeed();
181-
private final native int N_GetPercentComplete();
182-
private final native long N_GetTotalBytes();
183-
private final native long N_GetReceivedBytes();
184-
private final native Date N_GetStartTime();
185-
private final native Date N_GetEndTime();
186-
private final native String N_GetFullPath();
187-
private final native int N_GetId();
188-
private final native String N_GetURL();
189-
private final native String N_GetSuggestedFileName();
190-
private final native String N_GetContentDisposition();
191-
private final native String N_GetMimeType();
172+
private final native boolean N_IsValid(long self);
173+
private final native boolean N_IsInProgress(long self);
174+
private final native boolean N_IsComplete(long self);
175+
private final native boolean N_IsCanceled(long self);
176+
private final native long N_GetCurrentSpeed(long self);
177+
private final native int N_GetPercentComplete(long self);
178+
private final native long N_GetTotalBytes(long self);
179+
private final native long N_GetReceivedBytes(long self);
180+
private final native Date N_GetStartTime(long self);
181+
private final native Date N_GetEndTime(long self);
182+
private final native String N_GetFullPath(long self);
183+
private final native int N_GetId(long self);
184+
private final native String N_GetURL(long self);
185+
private final native String N_GetSuggestedFileName(long self);
186+
private final native String N_GetContentDisposition(long self);
187+
private final native String N_GetMimeType(long self);
192188
}

java/tests/detailed/MainFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void onTakeFocus(CefBrowser browser, boolean next) {
240240

241241
menuBar.addBookmark("Binding Test", "client://tests/binding_test.html");
242242
menuBar.addBookmark("Binding Test 2", "client://tests/binding_test2.html");
243-
menuBar.addBookmark("Download Test", "http://cefbuilds.com");
243+
menuBar.addBookmark("Download Test", "http://opensource.spotify.com/cefbuilds/index.html");
244244
menuBar.addBookmark("Login Test (username:pumpkin, password:pie)",
245245
"http://www.colostate.edu/~ric/protect/your.html");
246246
menuBar.addBookmark("Certificate-error Test", "https://www.k2go.de");

native/CefBeforeDownloadCallback_N.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,31 @@
66
#include "include/cef_download_handler.h"
77
#include "jni_util.h"
88

9+
namespace {
10+
11+
CefRefPtr<CefBeforeDownloadCallback> GetSelf(jlong self) {
12+
return reinterpret_cast<CefBeforeDownloadCallback*>(self);
13+
}
14+
15+
void ClearSelf(JNIEnv* env, jobject obj) {
16+
// Clear the reference added in DownloadHandler::OnBeforeDownload.
17+
SetCefForJNIObject<CefBeforeDownloadCallback>(env, obj, NULL,
18+
"CefBeforeDownloadCallback");
19+
}
20+
21+
} // namespace
22+
923
JNIEXPORT void JNICALL
1024
Java_org_cef_callback_CefBeforeDownloadCallback_1N_N_1Continue(
1125
JNIEnv* env,
1226
jobject obj,
27+
jlong self,
1328
jstring jdownloadPath,
1429
jboolean jshowDialog) {
15-
CefRefPtr<CefBeforeDownloadCallback> callback =
16-
GetCefFromJNIObject<CefBeforeDownloadCallback>(
17-
env, obj, "CefBeforeDownloadCallback");
18-
if (!callback.get())
30+
CefRefPtr<CefBeforeDownloadCallback> callback = GetSelf(self);
31+
if (!callback)
1932
return;
20-
2133
callback->Continue(GetJNIString(env, jdownloadPath),
2234
jshowDialog != JNI_FALSE);
23-
24-
// Clear the reference added in DownloadHandler::OnBeforeDownload.
25-
SetCefForJNIObject<CefBeforeDownloadCallback>(env, obj, NULL,
26-
"CefBeforeDownloadCallback");
35+
ClearSelf(env, obj);
2736
}

native/CefBeforeDownloadCallback_N.h

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)