This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
forked from cscott/skeleton-addon-fxandroid
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.js
262 lines (216 loc) · 7.98 KB
/
bootstrap.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Test values.
* Change these to whatever you want, then build and deploy the XPI.
*/
const ANNO_URL = "http://CampaignManager-Stage-272259297.us-east-1.elb.amazonaws.com/announce/";
const ANNO_INTERVAL = 15000;
/**
* Actual code follows.
*/
const RSRC = "basicnative-mobile"; // resource prefix
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
Cu.import("resource://gre/modules/Services.jsm");
function isNativeUI() {
return (Services.appinfo.ID == "{aa3c5121-dab2-40e2-81ca-7ea25febc110}");
}
// Tell the JNI module about the signatures of the Java methods
// we want to use.
function loadJNI() {
let jenv = JNI.GetForThread();
let Object = JNI.LoadClass(jenv, "java.lang.Object", {
methods: [
{ name: "toString", sig: "()Ljava/lang/String;" }
],
});
let GeckoApp = JNI.LoadClass(jenv, "org.mozilla.gecko.GeckoApp", {
static_fields: [
{ name: "mAppContext", sig: "Lorg/mozilla/gecko/GeckoApp;" }
],
});
let GeckoPreferences = JNI.LoadClass(jenv, "org.mozilla.gecko.GeckoPreferences", {
static_methods: [
{ name: "broadcastAnnouncementsPref", sig: "(Landroid/content/Context;)V" },
],
});
let SharedPreferences = JNI.LoadClass(jenv, "android.content.SharedPreferences", {
methods: [
{ name: "edit", sig: "()Landroid/content/SharedPreferences$Editor;" },
{ name: "getLong", sig: "(Ljava/lang/String;J)J" },
{ name: "getString", sig: "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;" },
],
});
let SharedPreferencesEditor = JNI.LoadClass(jenv, "android.content.SharedPreferences$Editor", {
methods: [
{ name: "commit", sig: "()Z" },
{ name: "putString", sig: "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/SharedPreferences$Editor;" },
{ name: "putLong", sig: "(Ljava/lang/String;J)Landroid/content/SharedPreferences$Editor;" },
],
});
try {
let AnnouncementsConstants = JNI.LoadClass(jenv, "org.mozilla.gecko.background.announcements.AnnouncementsConstants", {
static_fields: [
{ name: "DEFAULT_BACKOFF_MSEC", sig: "J" },
{ name: "DISABLED", sig: "Z" },
{ name: "MINIMUM_FETCH_INTERVAL_MSEC", sig: "J" },
],
});
} catch (ex) {
}
let Context = JNI.LoadClass(jenv, "android.content.Context", {
methods: [
{ name: "getSharedPreferences",
sig: "(Ljava/lang/String;I)Landroid/content/SharedPreferences;" },
{ name: "getSystemService",
sig: "(Ljava/lang/String;)Ljava/lang/Object;" }
],
});
}
function unloadJNI() {
let jenv = JNI.GetForThread();
JNI.UnloadClasses(jenv);
}
function setupJNI() {
let jenv = JNI.GetForThread();
jenv.contents.contents.PushLocalFrame(jenv, 100);
return jenv;
}
function teardownJNI(jenv) {
// Clean up memory allocated by JNI.
jenv.contents.contents.PopLocalFrame(jenv, null);
}
function _broadcastAnnouncementsPref(context) {
let GeckoPreferences = JNI.classes.org.mozilla.gecko.GeckoPreferences;
GeckoPreferences.broadcastAnnouncementsPref(context);
android_log(3, "GeckoSetPrefs", "Announcement broadcast.");
}
function increaseIdle() {
let jenv = setupJNI();
android_log(3, "GeckoSetPrefs", "Pushing last launch back.");
let Context = JNI.classes.android.content.Context;
let GeckoApp = JNI.classes.org.mozilla.gecko.GeckoApp;
let context = GeckoApp.mAppContext;
let sharedPrefs = context.getSharedPreferences("background", 0);
let lastLaunch = sharedPrefs.getLong("last_firefox_launch", Date.now());
let older = lastLaunch - (24 * 60 * 60 * 1000);
android_log(3, "GeckoSetPrefs", "Setting last launch to " + older);
let editor = sharedPrefs.edit();
editor.putLong("last_firefox_launch", older);
editor.commit();
android_log(3, "GeckoSetPrefs", "Committed.");
teardownJNI(jenv);
}
function setAnnouncementsPrefs(url, interval) {
let jenv = setupJNI();
android_log(3, "GeckoSetPrefs", "Set prefs to " + url + ", " + interval);
let Context = JNI.classes.android.content.Context;
let GeckoApp = JNI.classes.org.mozilla.gecko.GeckoApp;
let context = GeckoApp.mAppContext;
let editor = context.getSharedPreferences("background", 0).edit();
editor.putString("announce_server_base_url", url);
editor.putLong("announce_fetch_interval_msec", interval);
editor.putLong("earliest_next_announce_fetch", 0);
editor.commit();
android_log(3, "GeckoSetPrefs", "Committed.");
// Set the minimum to our interval.
try {
let AnnouncementsConstants = JNI.classes.org.mozilla.gecko.background.announcements.AnnouncementsConstants;
android_log(3, "GeckoSetPrefs", "Setting MINIMUM_FETCH_INTERVAL_MSEC to " + interval);
AnnouncementsConstants.DEFAULT_BACKOFF_MSEC = 100; // So we retry on error.
AnnouncementsConstants.DISABLED = false;
AnnouncementsConstants.MINIMUM_FETCH_INTERVAL_MSEC = interval;
} catch (ex) {
android_log(3, "GeckoSetPrefs", "Error setting AnnouncementsConstants.MINIMUM_FETCH_INTERVAL_MSEC.");
}
// Now broadcast so that we refresh.
_broadcastAnnouncementsPref(context);
teardownJNI(jenv);
}
let prefMenuID = null;
let ageMenuID = null;
function loadIntoWindow(window) {
android_log(3, "GeckoSetPrefs", "Loading into window.");
if (!window || !isNativeUI()) {
return;
}
// Always enable.
let AnnouncementsConstants = JNI.classes.org.mozilla.gecko.background.announcements.AnnouncementsConstants;
AnnouncementsConstants.DISABLED = false;
prefMenuID = window.NativeWindow.menu.add("Set announcements prefs", null,
function() {
setAnnouncementsPrefs(ANNO_URL, ANNO_INTERVAL);
});
ageMenuID = window.NativeWindow.menu.add("Increase idle record", null,
function() {
increaseIdle();
});
android_log(3, "GeckoSetPrefs", "Done loading.");
}
function unloadFromWindow(window) {
if (!window || !isNativeUI()) {
return;
}
window.NativeWindow.menu.remove(prefMenuID);
window.NativeWindow.menu.remove(ageMenuID);
}
/**
* bootstrap.js API
*/
var windowListener = {
onOpenWindow: function(aWindow) {
// Wait for the window to finish loading.
let domWindow = aWindow.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
domWindow.addEventListener("load", function() {
domWindow.removeEventListener("load", arguments.callee, false);
loadIntoWindow(domWindow);
}, false);
},
onCloseWindow: function(aWindow) {
},
onWindowTitleChange: function(aWindow, aTitle) {
}
};
function startup(aData, aReason) {
let resource = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
let alias = Services.io.newFileURI(aData.installPath);
if (!aData.installPath.isDirectory()) {
alias = Services.io.newURI("jar:" + alias.spec + "!/", null, null);
}
resource.setSubstitution(RSRC, alias);
Cu.import("resource://" + RSRC + "/jni.jsm");
loadJNI();
// Load into any existing windows.
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
loadIntoWindow(domWindow);
}
// Load into any new windows.
Services.wm.addListener(windowListener);
}
function shutdown(aData, aReason) {
// When the application is shutting down we normally don't have to clean
// up any UI changes made.
if (aReason == APP_SHUTDOWN) {
return;
}
// Unload JNI module.
unloadJNI();
Cu.unload("resource://" + RSRC + "/jni.jsm");
// Teardown resource: alias.
let resource = Services.io.getProtocolHandler("resource").QueryInterface(Ci.nsIResProtocolHandler);
resource.setSubstitution(RSRC, null);
// Stop listening for new windows.
Services.wm.removeListener(windowListener);
// Unload from any existing windows.
let windows = Services.wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
unloadFromWindow(domWindow);
}
}
function install(aData, aReason) {
}
function uninstall(aData, aReason) {
}