Bug 575195 - Send size mode events to dom from web shell window, and insure new windo...
[mozilla-central.git] / toolkit / xre / nsAndroidStartup.cpp
blobdafd593504914db16353ab3829fa05ac1e830682
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
13 * License.
15 * The Original Code is Android port code.
17 * The Initial Developer of the Original Code is
18 * Mozilla Foundation
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
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>
44 #include <jni.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <pthread.h>
50 #include "nsTArray.h"
51 #include "nsString.h"
52 #include "nsILocalFile.h"
53 #include "nsAppRunner.h"
54 #include "AndroidBridge.h"
56 #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args)
58 static pthread_t gGeckoThread = 0;
60 struct AutoAttachJavaThread {
61 AutoAttachJavaThread() {
62 attached = mozilla_AndroidBridge_SetMainThread((void*)pthread_self());
64 ~AutoAttachJavaThread() {
65 mozilla_AndroidBridge_SetMainThread(nsnull);
66 attached = PR_FALSE;
69 PRBool attached;
72 static void*
73 GeckoStart(void *data)
75 AutoAttachJavaThread attacher;
76 if (!attacher.attached)
77 return 0;
79 if (!data) {
80 LOG("Failed to get arguments for GeckoStart\n");
81 return 0;
84 nsresult rv;
85 nsCOMPtr<nsILocalFile> appini;
86 rv = NS_NewLocalFile(NS_LITERAL_STRING("/data/data/org.mozilla." MOZ_APP_NAME "/application.ini"),
87 PR_FALSE,
88 getter_AddRefs(appini));
89 if (NS_FAILED(rv)) {
90 LOG("Failed to create nsILocalFile for appdata\n");
91 return 0;
94 nsXREAppData *appData;
95 rv = XRE_CreateAppData(appini, &appData);
96 if (NS_FAILED(rv)) {
97 LOG("Failed to load application.ini from /data/data/org.mozilla." MOZ_APP_NAME "/application.ini\n");
98 return 0;
101 nsCOMPtr<nsILocalFile> xreDir;
102 rv = NS_NewLocalFile(NS_LITERAL_STRING("/data/data/org.mozilla." MOZ_APP_NAME),
103 PR_FALSE,
104 getter_AddRefs(xreDir));
105 if (NS_FAILED(rv)) {
106 LOG("Failed to create nsIFile for xreDirectory");
107 return 0;
110 appData->xreDirectory = xreDir.get();
113 nsTArray<char *> targs;
114 char *arg = strtok(static_cast<char *>(data), " ");
115 while (arg) {
116 targs.AppendElement(arg);
117 arg = strtok(NULL, " ");
119 targs.AppendElement(static_cast<char *>(nsnull));
121 int result = XRE_main(targs.Length() - 1, targs.Elements(), appData);
123 if (result)
124 LOG("XRE_main returned %d", result);
126 XRE_FreeAppData(appData);
128 mozilla::AndroidBridge::Bridge()->NotifyXreExit();
130 free(targs[0]);
131 nsMemory::Free(data);
132 return 0;
135 extern "C" NS_EXPORT void JNICALL
136 Java_org_mozilla_gecko_GeckoAppShell_nativeRun(JNIEnv *jenv, jclass jc, jstring jargs)
138 // We need to put Gecko on a even more separate thread, because
139 // otherwise this JNI method never returns; this leads to problems
140 // with local references overrunning the local refs table, among
141 // other things, since GC can't ever run on them.
143 // Note that we don't have xpcom initialized yet, so we can't use the
144 // thread manager for this. Instead, we use pthreads directly.
146 nsAutoString wargs;
147 int len = jenv->GetStringLength(jargs);
148 wargs.SetLength(jenv->GetStringLength(jargs));
149 jenv->GetStringRegion(jargs, 0, len, wargs.BeginWriting());
150 char *args = ToNewUTF8String(wargs);
152 if (pthread_create(&gGeckoThread, NULL, GeckoStart, args) != 0) {
153 LOG("pthread_create failed!");