1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Android port code.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
23 * Vladimir Vukicevic <vladimir@pobox.com>
24 * Michael Wu <mwu@mozilla.com>
25 * Brad Lassey <blassey@mozilla.com>
26 * Alex Pakhotin <alexp@mozilla.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #include <android/log.h>
52 #include "nsILocalFile.h"
53 #include "nsAppRunner.h"
54 #include "AndroidBridge.h"
56 #include "nsExceptionHandler.h"
58 #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args)
60 static pthread_t gGeckoThread
= 0;
62 struct AutoAttachJavaThread
{
63 AutoAttachJavaThread() {
64 attached
= mozilla_AndroidBridge_SetMainThread((void*)pthread_self());
66 ~AutoAttachJavaThread() {
67 mozilla_AndroidBridge_SetMainThread(nsnull
);
75 GeckoStart(void *data
)
77 #ifdef MOZ_CRASHREPORTER
78 const struct mapping_info
*info
= getLibraryMapping();
80 CrashReporter::AddLibraryMapping(info
->name
, info
->file_id
, info
->base
,
81 info
->len
, info
->offset
);
86 AutoAttachJavaThread attacher
;
87 if (!attacher
.attached
)
91 LOG("Failed to get arguments for GeckoStart\n");
96 nsCOMPtr
<nsILocalFile
> appini
;
97 rv
= NS_NewLocalFile(NS_LITERAL_STRING("/data/data/" ANDROID_PACKAGE_NAME
"/application.ini"),
99 getter_AddRefs(appini
));
101 LOG("Failed to create nsILocalFile for appdata\n");
105 nsXREAppData
*appData
;
106 rv
= XRE_CreateAppData(appini
, &appData
);
108 LOG("Failed to load application.ini from /data/data/" ANDROID_PACKAGE_NAME
"/application.ini\n");
112 nsCOMPtr
<nsILocalFile
> xreDir
;
113 rv
= NS_NewLocalFile(NS_LITERAL_STRING("/data/data/" ANDROID_PACKAGE_NAME
),
115 getter_AddRefs(xreDir
));
117 LOG("Failed to create nsIFile for xreDirectory");
121 appData
->xreDirectory
= xreDir
.get();
123 nsTArray
<char *> targs
;
124 char *arg
= strtok(static_cast<char *>(data
), " ");
126 targs
.AppendElement(arg
);
127 arg
= strtok(NULL
, " ");
129 targs
.AppendElement(static_cast<char *>(nsnull
));
131 int result
= XRE_main(targs
.Length() - 1, targs
.Elements(), appData
);
134 LOG("XRE_main returned %d", result
);
136 XRE_FreeAppData(appData
);
138 mozilla::AndroidBridge::Bridge()->NotifyXreExit();
141 nsMemory::Free(data
);
145 extern "C" NS_EXPORT
void JNICALL
146 Java_org_mozilla_gecko_GeckoAppShell_nativeRun(JNIEnv
*jenv
, jclass jc
, jstring jargs
)
148 // We need to put Gecko on a even more separate thread, because
149 // otherwise this JNI method never returns; this leads to problems
150 // with local references overrunning the local refs table, among
151 // other things, since GC can't ever run on them.
153 // Note that we don't have xpcom initialized yet, so we can't use the
154 // thread manager for this. Instead, we use pthreads directly.
157 int len
= jenv
->GetStringLength(jargs
);
158 wargs
.SetLength(jenv
->GetStringLength(jargs
));
159 jenv
->GetStringRegion(jargs
, 0, len
, wargs
.BeginWriting());
160 char *args
= ToNewUTF8String(wargs
);
162 if (pthread_create(&gGeckoThread
, NULL
, GeckoStart
, args
) != 0) {
163 LOG("pthread_create failed!");