1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.util;
import java.util.concurrent.atomic.AtomicInteger;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
import android.util.Log;
public abstract class GeckoRequest {
private static final String LOGTAG = "GeckoRequest";
private static final AtomicInteger currentId = new AtomicInteger(0);
private final int id = currentId.getAndIncrement();
private final String name;
private final String data;
/**
* Creates a request that can be dispatched using
* {@link GeckoAppShell#sendRequestToGecko(GeckoRequest)}.
*
* @param name The name of the event associated with this request, which must have a
* Gecko-side listener registered to respond to this request.
* @param data Data to send with this request, which can be any object serializable by
* {@link JSONObject#put(String, Object)}.
*/
@RobocopTarget
public GeckoRequest(String name, Object data) {
this.name = name;
final JSONObject message = new JSONObject();
try {
message.put("id", id);
message.put("data", data);
} catch (JSONException e) {
Log.e(LOGTAG, "JSON error", e);
}
this.data = message.toString();
}
/**
* Gets the ID for this request.
*
* @return The request ID
*/
public int getId() {
return id;
}
/**
* Gets the event name associated with this request.
*
* @return The name of the event sent to Gecko
*/
public String getName() {
return name;
}
/**
* Gets the stringified data associated with this request.
*
* @return The data being sent with the request
*/
public String getData() {
return data;
}
/**
* Callback executed when the request succeeds.
*
* @param nativeJSObject The response data from Gecko
*/
@RobocopTarget
public abstract void onResponse(NativeJSObject nativeJSObject);
/**
* Callback executed when the request fails.
*
* By default, an exception is thrown. This should be overridden if the
* GeckoRequest is able to recover from the error.
*
* @throws RuntimeException
*/
@RobocopTarget
public void onError(NativeJSObject error) {
final String message = error.optString("message", "<no message>");
final String stack = error.optString("stack", "<no stack>");
throw new RuntimeException("Unhandled error for GeckoRequest " + name + ": " + message + "\nJS stack:\n" + stack);
}
}
|