<commit_before>package org.ollide.xposed.incognitotab;

import android.app.Activity;
import android.os.Bundle;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class MethodHooks implements IXposedHookLoadPackage {

    public static final String EXTRA_INCOGNITO_URL = "EXTRA_INCOGNITO_URL";

    private static final String TARGET_PACKAGE = "com.android.chrome";

    private static String url = null;
    private static boolean didOpen = false;

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
        if (!loadPackageParam.packageName.equals(TARGET_PACKAGE)) {
            return;
        }

        // we are in com.android.chrome - hook methods!

        XposedHelpers.findAndHookMethod("com.google.android.apps.chrome.document.ChromeLauncherActivity", loadPackageParam.classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                Activity chromeActivity = (Activity) param.thisObject;
                String incognitoUrl = chromeActivity.getIntent().getStringExtra(EXTRA_INCOGNITO_URL);
                if (incognitoUrl != null) {
                    didOpen = false;
                    url = incognitoUrl;
                }
            }
        });

        final Class<?> chromeTabClass = XposedHelpers.findClass("com.google.android.apps.chrome.tab.ChromeTab", loadPackageParam.classLoader);
        final Class<?> chromeActivityClass = XposedHelpers.findClass("com.google.android.apps.chrome.ChromeActivity", loadPackageParam.classLoader);

        // class to create new tabs
        final Class<?> chromeTabCreatorClass = XposedHelpers.findClass("com.google.android.apps.chrome.tabmodel.ChromeTabCreator", loadPackageParam.classLoader);

        // parameter of ChromeTabCreator#createNewTab
        final Class<?> loadUrlParamsClass = XposedHelpers.findClass("org.chromium.content_public.browser.LoadUrlParams", loadPackageParam.classLoader);
        final Class<?> tabLaunchTypeClass = XposedHelpers.findClass("org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType", loadPackageParam.classLoader);
        final Class<?> tabClass = XposedHelpers.findClass("org.chromium.chrome.browser.Tab", loadPackageParam.classLoader);

        XposedHelpers.findAndHookMethod("com.google.android.apps.chrome.tab.ChromeTab", loadPackageParam.classLoader, "didStartPageLoad", String.class, boolean.class, new XC_MethodHook() {<commit_msg>hook Chrome and Chrome Beta methods<commit_after>package org.ollide.xposed.incognitotab;

import android.app.Activity;
import android.os.Bundle;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class MethodHooks implements IXposedHookLoadPackage {

    public static final String EXTRA_INCOGNITO_URL = "EXTRA_INCOGNITO_URL";

    private static final String PACKAGE_CHROME = "com.android.chrome";
    private static final String PACKAGE_CHROME_BETA = "com.chrome.beta";

    private static String url = null;
    private static boolean didOpen = false;

    @Override
    public void handleLoadPackage(final XC_LoadPackage.LoadPackageParam lpp) throws Throwable {
        if (lpp.packageName.equals(PACKAGE_CHROME) || lpp.packageName.equals(PACKAGE_CHROME_BETA)) {
            hookChromeMethods(lpp.classLoader);
        }
    }

    private void hookChromeMethods(final ClassLoader classLoader) {
        XposedHelpers.findAndHookMethod("com.google.android.apps.chrome.document.ChromeLauncherActivity", classLoader, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);

                Activity chromeActivity = (Activity) param.thisObject;
                String incognitoUrl = chromeActivity.getIntent().getStringExtra(EXTRA_INCOGNITO_URL);
                if (incognitoUrl != null) {
                    didOpen = false;
                    url = incognitoUrl;
                }
            }
        });

        final Class<?> chromeTabClass = XposedHelpers.findClass("com.google.android.apps.chrome.tab.ChromeTab", classLoader);
        final Class<?> chromeActivityClass = XposedHelpers.findClass("com.google.android.apps.chrome.ChromeActivity", classLoader);

        // class to create new tabs
        final Class<?> chromeTabCreatorClass = XposedHelpers.findClass("com.google.android.apps.chrome.tabmodel.ChromeTabCreator", classLoader);

        // parameter of ChromeTabCreator#createNewTab
        final Class<?> loadUrlParamsClass = XposedHelpers.findClass("org.chromium.content_public.browser.LoadUrlParams", classLoader);
        final Class<?> tabLaunchTypeClass = XposedHelpers.findClass("org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType", classLoader);
        final Class<?> tabClass = XposedHelpers.findClass("org.chromium.chrome.browser.Tab", classLoader);

        XposedHelpers.findAndHookMethod("com.google.android.apps.chrome.tab.ChromeTab", classLoader, "didStartPageLoad", String.class, boolean.class, new XC_MethodHook() {
{'fraction_non_alphanumeric': 0.09449093444909344, 'fraction_numerical': 0.0, 'mean_word_length': 3.7609958506224066, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1792119', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1609, 'n_words': 285}