Search in sources :

Example 16 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class CustomTabsConnection method getSchedulerGroup.

/**
     * @return the CPU cgroup of a given process, identified by its PID, or null.
     */
@VisibleForTesting
static String getSchedulerGroup(int pid) {
    // Android uses two cgroups for the processes: the root cgroup, and the
    // "/bg_non_interactive" one for background processes. The list of
    // cgroups a process is part of can be queried by reading
    // /proc/<pid>/cgroup, which is world-readable.
    String cgroupFilename = "/proc/" + pid + "/cgroup";
    // Reading from /proc does not cause disk IO, but strict mode doesn't like it.
    // crbug.com/567143
    StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
    try {
        FileReader fileReader = new FileReader(cgroupFilename);
        BufferedReader reader = new BufferedReader(fileReader);
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                // line format: 2:cpu:/bg_non_interactive
                String[] fields = line.trim().split(":");
                if (fields.length == 3 && fields[1].equals("cpu"))
                    return fields[2];
            }
        } finally {
            reader.close();
        }
    } catch (IOException e) {
        return null;
    } finally {
        StrictMode.setThreadPolicy(oldPolicy);
    }
    return null;
}
Also used : StrictMode(android.os.StrictMode) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 17 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class PrecacheController method acquirePrecachingWakeLock.

/** Acquires the precaching {@link WakeLock}. */
@VisibleForTesting
void acquirePrecachingWakeLock() {
    assert mNonThreadSafe.calledOnValidThread();
    Log.v(TAG, "acquiring wake lock");
    if (mPrecachingWakeLock == null) {
        PowerManager pm = (PowerManager) mAppContext.getSystemService(Context.POWER_SERVICE);
        mPrecachingWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    }
    mPrecachingWakeLock.acquire();
}
Also used : PowerManager(android.os.PowerManager) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 18 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class OMADownloadHandler method parseDownloadDescriptor.

/**
     * Parses the input stream and returns the OMA information.
     *
     * @param is The input stream to the parser.
     * @return OMA information about the download content, or null if an error is found.
     */
@VisibleForTesting
static OMAInfo parseDownloadDescriptor(InputStream is) {
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(is, null);
        int eventType = parser.getEventType();
        String currentAttribute = null;
        OMAInfo info = new OMAInfo();
        StringBuilder sb = null;
        List<String> attributeList = new ArrayList<String>(Arrays.asList(OMA_TYPE, OMA_SIZE, OMA_OBJECT_URI, OMA_INSTALL_NOTIFY_URI, OMA_NEXT_URL, OMA_DD_VERSION, OMA_NAME, OMA_DESCRIPTION, OMA_VENDOR, OMA_INFO_URL, OMA_ICON_URI, OMA_INSTALL_PARAM));
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_DOCUMENT) {
                if (!info.isEmpty())
                    return null;
            } else if (eventType == XmlPullParser.START_TAG) {
                String tagName = parser.getName();
                if (attributeList.contains(tagName)) {
                    if (currentAttribute != null) {
                        Log.w(TAG, "Nested attributes was found in the download descriptor");
                        return null;
                    }
                    sb = new StringBuilder();
                    currentAttribute = tagName;
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (currentAttribute != null) {
                    if (!currentAttribute.equals(parser.getName())) {
                        Log.w(TAG, "Nested attributes was found in the download descriptor");
                        return null;
                    }
                    info.addAttributeValue(currentAttribute, sb.toString().trim());
                    currentAttribute = null;
                    sb = null;
                }
            } else if (eventType == XmlPullParser.TEXT) {
                if (currentAttribute != null) {
                    sb.append(parser.getText());
                }
            }
            eventType = parser.next();
        }
        return info;
    } catch (XmlPullParserException e) {
        Log.w(TAG, "Failed to parse download descriptor.", e);
        return null;
    } catch (IOException e) {
        Log.w(TAG, "Failed to read download descriptor.", e);
        return null;
    }
}
Also used : XmlPullParserFactory(org.xmlpull.v1.XmlPullParserFactory) XmlPullParser(org.xmlpull.v1.XmlPullParser) ArrayList(java.util.ArrayList) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 19 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class ConnectivityTask method init.

@VisibleForTesting
void init(Profile profile, int timeoutMs) {
    for (Type t : Type.values()) {
        SingleTypeTask task = new SingleTypeTask(t);
        task.start(profile, timeoutMs);
    }
}
Also used : ConnectionType(org.chromium.net.ConnectionType) VisibleForTesting(org.chromium.base.VisibleForTesting)

Example 20 with VisibleForTesting

use of org.chromium.base.VisibleForTesting in project AndroidChromium by JackyAndroid.

the class DocumentModeAssassin method migrateFromDocumentToTabbedMode.

/** Migrates the user from document mode to tabbed mode if necessary. */
@VisibleForTesting
public final void migrateFromDocumentToTabbedMode() {
    ThreadUtils.assertOnUiThread();
    // Migration is already underway.
    if (mStage != STAGE_UNINITIALIZED)
        return;
    // If migration isn't necessary, don't do anything.
    if (!isMigrationNecessary()) {
        setStage(STAGE_UNINITIALIZED, STAGE_DONE);
        return;
    }
    // If we've crashed or failed too many times, send them to tabbed mode without their data.
    // - Any incorrect or invalid files in the tabbed mode directory will be wiped out by the
    //   TabPersistentStore when the ChromeTabbedActivity starts.
    //
    // - If it crashes in the step after being migrated, then the user will simply be left
    //   with a bunch of inaccessible document mode data instead of being stuck trying to
    //   migrate, which is a lesser evil.  This case will be caught by the check above to see if
    //   migration is even necessary.
    SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
    int numMigrationAttempts = prefs.getInt(PREF_NUM_MIGRATION_ATTEMPTS, 0);
    if (numMigrationAttempts >= MAX_MIGRATION_ATTEMPTS_BEFORE_FAILURE) {
        Log.e(TAG, "Too many failures.  Migrating user to tabbed mode without data.");
        setStage(STAGE_UNINITIALIZED, STAGE_WRITE_TABMODEL_METADATA_DONE);
        return;
    }
    // Kick off the migration pipeline.
    // Using apply() instead of commit() seems to save the preference just fine, even if Chrome
    // crashes immediately afterward.
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt(PREF_NUM_MIGRATION_ATTEMPTS, numMigrationAttempts + 1);
    editor.apply();
    setStage(STAGE_UNINITIALIZED, STAGE_INITIALIZED);
}
Also used : SharedPreferences(android.content.SharedPreferences) VisibleForTesting(org.chromium.base.VisibleForTesting)

Aggregations

VisibleForTesting (org.chromium.base.VisibleForTesting)52 Intent (android.content.Intent)6 IOException (java.io.IOException)6 SharedPreferences (android.content.SharedPreferences)5 JSONObject (org.json.JSONObject)4 PendingIntent (android.app.PendingIntent)3 SpannableString (android.text.SpannableString)3 BufferedReader (java.io.BufferedReader)3 Matcher (java.util.regex.Matcher)3 CalledByNative (org.chromium.base.annotations.CalledByNative)3 AccountManagerHelper (org.chromium.components.signin.AccountManagerHelper)3 SpanInfo (org.chromium.ui.text.SpanApplier.SpanInfo)3 SuppressLint (android.annotation.SuppressLint)2 IntentFilter (android.content.IntentFilter)2 Paint (android.graphics.Paint)2 File (java.io.File)2 FileReader (java.io.FileReader)2 HttpURLConnection (java.net.HttpURLConnection)2 ArrayList (java.util.ArrayList)2 Formatter (java.util.Formatter)2