diff options
author | dtrainor <dtrainor@chromium.org> | 2015-10-05 11:01:48 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-10-05 18:02:43 +0000 |
commit | ac84a37c07ae9377fcb7baed8791b76640f341a5 (patch) | |
tree | 8ecdfc5921b3c5c535583351672f0786be92d664 /base/android | |
parent | baa79511a1d30f587ebf9c8fa60bc6e1957e7137 (diff) | |
download | chromium_src-ac84a37c07ae9377fcb7baed8791b76640f341a5.zip chromium_src-ac84a37c07ae9377fcb7baed8791b76640f341a5.tar.gz chromium_src-ac84a37c07ae9377fcb7baed8791b76640f341a5.tar.bz2 |
Move ChromeCommandLineInitUtil to base/
- Make ChromeCommandLineInitUtil accessible to other folders than chrome/.
- Pull out the command line file name to be passed as a parameter.
BUG=526219
Review URL: https://codereview.chromium.org/1364143002
Cr-Commit-Position: refs/heads/master@{#352349}
Diffstat (limited to 'base/android')
-rw-r--r-- | base/android/java/src/org/chromium/base/CommandLineInitUtil.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/base/android/java/src/org/chromium/base/CommandLineInitUtil.java b/base/android/java/src/org/chromium/base/CommandLineInitUtil.java new file mode 100644 index 0000000..6aa227c --- /dev/null +++ b/base/android/java/src/org/chromium/base/CommandLineInitUtil.java @@ -0,0 +1,107 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.base; + +import android.annotation.SuppressLint; +import android.content.Context; +import android.os.Build; +import android.provider.Settings; + +import org.chromium.base.annotations.SuppressFBWarnings; + +import java.io.File; + +/** + * Provides implementation of command line initialization for Android. + */ +public final class CommandLineInitUtil { + + private static final String TAG = "CommandLineInitUtil"; + + /** + * The location of the command line file needs to be in a protected + * directory so requires root access to be tweaked, i.e., no other app in a + * regular (non-rooted) device can change this file's contents. + * See below for debugging on a regular (non-rooted) device. + */ + private static final String COMMAND_LINE_FILE_PATH = "/data/local"; + + /** + * This path (writable by the shell in regular non-rooted "user" builds) is used when: + * 1) The "debug app" is set to the application calling this. + * and + * 2) ADB is enabled. + * + */ + private static final String COMMAND_LINE_FILE_PATH_DEBUG_APP = "/data/local/tmp"; + + private CommandLineInitUtil() { + } + + /** + * Initializes the CommandLine class, pulling command line arguments from {@code fileName}. + * @param context The {@link Context} to use to query whether or not this application is being + * debugged, and whether or not the publicly writable command line file should + * be used. + * @param fileName The name of the command line file to pull arguments from. + */ + @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") + public static void initCommandLine(Context context, String fileName) { + if (!CommandLine.isInitialized()) { + File commandLineFile = getAlternativeCommandLinePath(context, fileName); + if (commandLineFile == null) { + commandLineFile = new File(COMMAND_LINE_FILE_PATH, fileName); + } + CommandLine.initFromFile(commandLineFile.getPath()); + } + } + + /** + * Use an alternative path if adb is enabled and this is the debug app. + */ + @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") + private static File getAlternativeCommandLinePath(Context context, String fileName) { + File alternativeCommandLineFile = + new File(COMMAND_LINE_FILE_PATH_DEBUG_APP, fileName); + if (!alternativeCommandLineFile.exists()) return null; + try { + String debugApp = Build.VERSION.SDK_INT < 17 + ? getDebugAppPreJBMR1(context) : getDebugAppJBMR1(context); + + if (debugApp != null + && debugApp.equals(context.getApplicationContext().getPackageName())) { + Log.i(TAG, "Using alternative command line file in " + + alternativeCommandLineFile.getPath()); + return alternativeCommandLineFile; + } + } catch (RuntimeException e) { + Log.e(TAG, "Unable to detect alternative command line file"); + } + + return null; + } + + @SuppressLint("NewApi") + private static String getDebugAppJBMR1(Context context) { + boolean adbEnabled = Settings.Global.getInt(context.getContentResolver(), + Settings.Global.ADB_ENABLED, 0) == 1; + if (adbEnabled) { + return Settings.Global.getString(context.getContentResolver(), + Settings.Global.DEBUG_APP); + } + return null; + } + + @SuppressWarnings("deprecation") + private static String getDebugAppPreJBMR1(Context context) { + boolean adbEnabled = Settings.System.getInt(context.getContentResolver(), + Settings.System.ADB_ENABLED, 0) == 1; + if (adbEnabled) { + return Settings.System.getString(context.getContentResolver(), + Settings.System.DEBUG_APP); + } + return null; + } +} |