Merge m-c to b-i.
[gecko.git] / toolkit / xre / nsAndroidStartup.cpp
blob278c5591f42900889b8b727046191932cc927990
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/. */
6 #include <android/log.h>
8 #include <jni.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <pthread.h>
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"
22 #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args)
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.
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.
32 struct AutoAttachJavaThread {
33 AutoAttachJavaThread() {
34 attached = mozilla_AndroidBridge_SetMainThread(pthread_self());
36 ~AutoAttachJavaThread() {
37 mozilla_AndroidBridge_SetMainThread(-1);
38 attached = false;
41 bool attached;
44 extern "C" NS_EXPORT void
45 GeckoStart(void *data, const nsXREAppData *appData)
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++;
54 #endif
56 AutoAttachJavaThread attacher;
57 if (!attacher.attached)
58 return;
60 if (!data) {
61 LOG("Failed to get arguments for GeckoStart\n");
62 return;
65 nsTArray<char *> targs;
66 char *arg = strtok(static_cast<char *>(data), " ");
67 while (arg) {
68 targs.AppendElement(arg);
69 arg = strtok(nullptr, " ");
71 targs.AppendElement(static_cast<char *>(nullptr));
73 int result = XRE_main(targs.Length() - 1, targs.Elements(), appData, 0);
75 if (result)
76 LOG("XRE_main returned %d", result);
78 mozilla::widget::android::GeckoAppShell::NotifyXreExit();
80 free(targs[0]);
81 nsMemory::Free(data);
82 return;