Home | History | Annotate | Download | only in android
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/public/app/android_library_loader_hooks.h"
      6 
      7 #include "base/android/base_jni_registrar.h"
      8 #include "base/android/jni_android.h"
      9 #include "base/android/jni_registrar.h"
     10 #include "base/android/jni_string.h"
     11 #include "base/at_exit.h"
     12 #include "base/base_switches.h"
     13 #include "base/command_line.h"
     14 #include "base/debug/trace_event.h"
     15 #include "base/files/file_path.h"
     16 #include "base/logging.h"
     17 #include "base/strings/string_util.h"
     18 #include "base/tracked_objects.h"
     19 #include "content/app/android/app_jni_registrar.h"
     20 #include "content/browser/android/browser_jni_registrar.h"
     21 #include "content/child/android/child_jni_registrar.h"
     22 #include "content/common/android/command_line.h"
     23 #include "content/common/android/common_jni_registrar.h"
     24 #include "content/public/common/content_switches.h"
     25 #include "content/public/common/result_codes.h"
     26 #include "jni/LibraryLoader_jni.h"
     27 #include "media/base/android/media_jni_registrar.h"
     28 #include "net/android/net_jni_registrar.h"
     29 #include "ui/android/ui_jni_registrar.h"
     30 #include "ui/gl/android/gl_jni_registrar.h"
     31 #include "ui/shell_dialogs/android/shell_dialogs_jni_registrar.h"
     32 
     33 namespace content {
     34 
     35 namespace {
     36 
     37 base::AtExitManager* g_at_exit_manager = NULL;
     38 
     39 bool DoJniRegistration(JNIEnv* env) {
     40   static bool g_jni_init_done = false;
     41 
     42   if (!g_jni_init_done) {
     43     if (!base::android::RegisterJni(env))
     44       return false;
     45 
     46     if (!net::android::RegisterJni(env))
     47       return false;
     48 
     49     if (!ui::android::RegisterJni(env))
     50       return false;
     51 
     52     if (!ui::gl::android::RegisterJni(env))
     53       return false;
     54 
     55     if (!ui::shell_dialogs::RegisterJni(env))
     56       return false;
     57 
     58     if (!content::android::RegisterChildJni(env))
     59       return false;
     60 
     61     if (!content::android::RegisterCommonJni(env))
     62       return false;
     63 
     64     if (!content::android::RegisterBrowserJni(env))
     65       return false;
     66 
     67     if (!content::android::RegisterAppJni(env))
     68       return false;
     69 
     70     if (!media::RegisterJni(env))
     71       return false;
     72 
     73     g_jni_init_done = true;
     74   }
     75 
     76   return true;
     77 }
     78 
     79 }  // namespace
     80 
     81 static jint LibraryLoaded(JNIEnv* env, jclass clazz,
     82                           jobjectArray init_command_line) {
     83   InitNativeCommandLineFromJavaArray(env, init_command_line);
     84 
     85   CommandLine* command_line = CommandLine::ForCurrentProcess();
     86 
     87   if (command_line->HasSwitch(switches::kTraceStartup)) {
     88     base::debug::CategoryFilter category_filter(
     89         command_line->GetSwitchValueASCII(switches::kTraceStartup));
     90     base::debug::TraceLog::GetInstance()->SetEnabled(category_filter,
     91         base::debug::TraceLog::RECORD_UNTIL_FULL);
     92   }
     93 
     94   // Can only use event tracing after setting up the command line.
     95   TRACE_EVENT0("jni", "JNI_OnLoad continuation");
     96 
     97   // Note: because logging is setup here right after copying the command line
     98   // array from java to native up top of this method, any code that adds the
     99   // --enable-dcheck switch must do so on the Java side.
    100   logging::LoggingSettings settings;
    101   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
    102   settings.dcheck_state =
    103       command_line->HasSwitch(switches::kEnableDCHECK) ?
    104       logging::ENABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS :
    105       logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS;
    106   logging::InitLogging(settings);
    107   // To view log output with IDs and timestamps use "adb logcat -v threadtime".
    108   logging::SetLogItems(false,    // Process ID
    109                        false,    // Thread ID
    110                        false,    // Timestamp
    111                        false);   // Tick count
    112   VLOG(0) << "Chromium logging enabled: level = " << logging::GetMinLogLevel()
    113           << ", default verbosity = " << logging::GetVlogVerbosity();
    114 
    115   if (!DoJniRegistration(env))
    116     return RESULT_CODE_FAILED_TO_REGISTER_JNI;
    117 
    118   return 0;
    119 }
    120 
    121 void LibraryLoaderExitHook() {
    122   if (g_at_exit_manager) {
    123     delete g_at_exit_manager;
    124     g_at_exit_manager = NULL;
    125   }
    126 }
    127 
    128 bool RegisterLibraryLoaderEntryHook(JNIEnv* env, bool lazy_jni_registration) {
    129   // We need the AtExitManager to be created at the very beginning.
    130   g_at_exit_manager = new base::AtExitManager();
    131 
    132   if (!RegisterNativesImpl(env))
    133     return false;
    134 
    135   if (!lazy_jni_registration && !DoJniRegistration(env))
    136     return false;
    137 
    138   return true;
    139 }
    140 
    141 }  // namespace content
    142