/toolkit/xre/nsAndroidStartup.cpp

http://github.com/zpao/v8monkey · C++ · 134 lines · 67 code · 21 blank · 46 comment · 5 complexity · 273383a556e4b7c9fb3998a89bcc18ce MD5 · raw file

  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
  4. *
  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/
  9. *
  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.
  14. *
  15. * The Original Code is Android port code.
  16. *
  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.
  21. *
  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>
  27. *
  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.
  39. *
  40. * ***** END LICENSE BLOCK ***** */
  41. #include "application.ini.h"
  42. #include <android/log.h>
  43. #include <jni.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <pthread.h>
  47. #include "nsTArray.h"
  48. #include "nsString.h"
  49. #include "nsILocalFile.h"
  50. #include "nsAppRunner.h"
  51. #include "AndroidBridge.h"
  52. #include "APKOpen.h"
  53. #include "nsExceptionHandler.h"
  54. #define LOG(args...) __android_log_print(ANDROID_LOG_INFO, MOZ_APP_NAME, args)
  55. struct AutoAttachJavaThread {
  56. AutoAttachJavaThread() {
  57. attached = mozilla_AndroidBridge_SetMainThread((void*)pthread_self());
  58. }
  59. ~AutoAttachJavaThread() {
  60. mozilla_AndroidBridge_SetMainThread(nsnull);
  61. attached = false;
  62. }
  63. bool attached;
  64. };
  65. static void*
  66. GeckoStart(void *data)
  67. {
  68. #ifdef MOZ_CRASHREPORTER
  69. const struct mapping_info *info = getLibraryMapping();
  70. while (info->name) {
  71. CrashReporter::AddLibraryMapping(info->name, info->file_id, info->base,
  72. info->len, info->offset);
  73. info++;
  74. }
  75. #endif
  76. AutoAttachJavaThread attacher;
  77. if (!attacher.attached)
  78. return 0;
  79. if (!data) {
  80. LOG("Failed to get arguments for GeckoStart\n");
  81. return 0;
  82. }
  83. nsTArray<char *> targs;
  84. char *arg = strtok(static_cast<char *>(data), " ");
  85. while (arg) {
  86. targs.AppendElement(arg);
  87. arg = strtok(NULL, " ");
  88. }
  89. targs.AppendElement(static_cast<char *>(nsnull));
  90. int result = XRE_main(targs.Length() - 1, targs.Elements(), &sAppData);
  91. if (result)
  92. LOG("XRE_main returned %d", result);
  93. mozilla::AndroidBridge::Bridge()->NotifyXreExit();
  94. free(targs[0]);
  95. nsMemory::Free(data);
  96. return 0;
  97. }
  98. extern "C" NS_EXPORT void JNICALL
  99. Java_org_mozilla_gecko_GeckoAppShell_nativeRun(JNIEnv *jenv, jclass jc, jstring jargs)
  100. {
  101. // We need to put Gecko on a even more separate thread, because
  102. // otherwise this JNI method never returns; this leads to problems
  103. // with local references overrunning the local refs table, among
  104. // other things, since GC can't ever run on them.
  105. // Note that we don't have xpcom initialized yet, so we can't use the
  106. // thread manager for this. Instead, we use pthreads directly.
  107. nsAutoString wargs;
  108. int len = jenv->GetStringLength(jargs);
  109. wargs.SetLength(jenv->GetStringLength(jargs));
  110. jenv->GetStringRegion(jargs, 0, len, wargs.BeginWriting());
  111. char *args = ToNewUTF8String(wargs);
  112. GeckoStart(args);
  113. }