|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include <android/log.h> |
|
7 |
|
8 #include <jni.h> |
|
9 |
|
10 #include <stdlib.h> |
|
11 #include <string.h> |
|
12 #include <pthread.h> |
|
13 |
|
14 #include "nsTArray.h" |
|
15 #include "nsString.h" |
|
16 #include "nsIFile.h" |
|
17 #include "nsAppRunner.h" |
|
18 #include "AndroidBridge.h" |
|
19 #include "APKOpen.h" |
|
20 #include "nsExceptionHandler.h" |
|
21 |
|
22 #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args) |
|
23 |
|
24 // We need to put Gecko on a even more separate thread, because |
|
25 // otherwise this JNI method never returns; this leads to problems |
|
26 // with local references overrunning the local refs table, among |
|
27 // other things, since GC can't ever run on them. |
|
28 |
|
29 // Note that we don't have xpcom initialized yet, so we can't use the |
|
30 // thread manager for this. Instead, we use pthreads directly. |
|
31 |
|
32 struct AutoAttachJavaThread { |
|
33 AutoAttachJavaThread() { |
|
34 attached = mozilla_AndroidBridge_SetMainThread(pthread_self()); |
|
35 } |
|
36 ~AutoAttachJavaThread() { |
|
37 mozilla_AndroidBridge_SetMainThread(-1); |
|
38 attached = false; |
|
39 } |
|
40 |
|
41 bool attached; |
|
42 }; |
|
43 |
|
44 extern "C" NS_EXPORT void |
|
45 GeckoStart(void *data, const nsXREAppData *appData) |
|
46 { |
|
47 #ifdef MOZ_CRASHREPORTER |
|
48 const struct mapping_info *info = getLibraryMapping(); |
|
49 while (info->name) { |
|
50 CrashReporter::AddLibraryMapping(info->name, info->base, |
|
51 info->len, info->offset); |
|
52 info++; |
|
53 } |
|
54 #endif |
|
55 |
|
56 AutoAttachJavaThread attacher; |
|
57 if (!attacher.attached) |
|
58 return; |
|
59 |
|
60 if (!data) { |
|
61 LOG("Failed to get arguments for GeckoStart\n"); |
|
62 return; |
|
63 } |
|
64 |
|
65 nsTArray<char *> targs; |
|
66 char *arg = strtok(static_cast<char *>(data), " "); |
|
67 while (arg) { |
|
68 targs.AppendElement(arg); |
|
69 arg = strtok(nullptr, " "); |
|
70 } |
|
71 targs.AppendElement(static_cast<char *>(nullptr)); |
|
72 |
|
73 int result = XRE_main(targs.Length() - 1, targs.Elements(), appData, 0); |
|
74 |
|
75 if (result) |
|
76 LOG("XRE_main returned %d", result); |
|
77 |
|
78 mozilla::widget::android::GeckoAppShell::NotifyXreExit(); |
|
79 |
|
80 free(targs[0]); |
|
81 nsMemory::Free(data); |
|
82 return; |
|
83 } |