Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up patches with asm #5127

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,24 @@ public BraveAndroidSyncSettingsClassAdapter(ClassVisitor visitor) {
"notifyObservers");
makePublicMethod(sAndroidSyncSettingsClassName,
"notifyObservers");

makePublicMethod(sAndroidSyncSettingsClassName,
"updateCachedSettings");
addMethodAnnotation(sBraveAndroidSyncSettingsClassName,
"updateCachedSettings", "Ljava/lang/Override;");

makePublicMethod(sAndroidSyncSettingsClassName,
"setChromeSyncEnabled");
addMethodAnnotation(sBraveAndroidSyncSettingsClassName,
"setChromeSyncEnabled", "Ljava/lang/Override;");

deleteField(sBraveAndroidSyncSettingsClassName, "mIsSyncable");
makeProtectedField(sAndroidSyncSettingsClassName, "mIsSyncable");

deleteField(sBraveAndroidSyncSettingsClassName, "mChromeSyncEnabled");
makeProtectedField(sAndroidSyncSettingsClassName, "mChromeSyncEnabled");

deleteField(sBraveAndroidSyncSettingsClassName, "mMasterSyncEnabled");
makeProtectedField(sAndroidSyncSettingsClassName, "mMasterSyncEnabled");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@

package org.brave.bytecode;

import static org.objectweb.asm.Opcodes.ASM5;
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM5;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.ClassNode;
Expand Down Expand Up @@ -46,14 +50,45 @@ public void makePublic() {
}
}

class BraveMethodVisitor extends MethodVisitor {

private Method mMethod;

public BraveMethodVisitor(Method method, MethodVisitor mv) {
super(ASM5, mv);
this.mMethod = method;
}

@Override
public void visitMethodInsn(int opcode,
java.lang.String owner,
java.lang.String name,
java.lang.String descriptor,
boolean isInterface) {
if (shouldMakePublicMethod(owner, name) &&
opcode == INVOKESPECIAL) {
System.out.println("use invoke virtual for call to method " +
owner + "." + name);
// use invoke virtual because other classes can override
// the method now
opcode = INVOKEVIRTUAL;
}
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
}

protected String mName = "";
protected String mSuperName = "";

private Map<String, String> mSuperNames = new HashMap<String, String>();
private Map<String, ArrayList<String>> mDeleteMethods =
new HashMap<String, ArrayList<String>>();
private Map<String, ArrayList<String>> mDeleteFields =
new HashMap<String, ArrayList<String>>();
private Map<String, ArrayList<String>> mMakePublicMethods =
new HashMap<String, ArrayList<String>>();
private Map<String, ArrayList<String>> mMakeProtectedFields =
new HashMap<String, ArrayList<String>>();
private Map<String, Map<String, ArrayList<String>>> mAddAnnotations =
new HashMap<String, Map<String, ArrayList<String>>>();

Expand Down Expand Up @@ -87,18 +122,22 @@ protected void deleteMethod(String className, String methodName) {
methods.add(methodName);
}

private boolean shouldMakePublicMethod(String methodName) {
private boolean shouldMakePublicMethod(String className, String methodName) {
for(Map.Entry<String, ArrayList<String>> entry :
mMakePublicMethods.entrySet()) {
String className = entry.getKey();
String entryClassName = entry.getKey();
ArrayList<String> methodNames = entry.getValue();
return mName.contains(className) &&
return className.contains(entryClassName) &&
methodNames.contains(methodName);
}

return false;
}

private boolean shouldMakePublicMethod(String methodName) {
return shouldMakePublicMethod(mName, methodName);
}

protected void makePublicMethod(String className, String methodName) {
ArrayList methods = mMakePublicMethods.get(className);
if (methods == null) {
Expand All @@ -108,6 +147,52 @@ protected void makePublicMethod(String className, String methodName) {
methods.add(methodName);
}

private boolean shouldDeleteField(String fieldName) {
for(Map.Entry<String, ArrayList<String>> entry :
mDeleteFields.entrySet()) {
String className = entry.getKey();
ArrayList<String> fieldNames = entry.getValue();
return mName.contains(className) &&
fieldNames.contains(fieldName);
}

return false;
}

protected void deleteField(String className, String fieldName) {
ArrayList fields = mDeleteFields.get(className);
if (fields == null) {
fields = new ArrayList<String>();
mDeleteFields.put(className, fields);
}
fields.add(fieldName);
}

private boolean shouldMakeProtectedField(String className, String fieldName) {
for(Map.Entry<String, ArrayList<String>> entry :
mMakeProtectedFields.entrySet()) {
String entryClassName = entry.getKey();
ArrayList<String> fieldNames = entry.getValue();
return className.contains(entryClassName) &&
fieldNames.contains(fieldName);
}

return false;
}

private boolean shouldMakeProtectedField(String fieldName) {
return shouldMakeProtectedField(mName, fieldName);
}

protected void makeProtectedField(String className, String fieldName) {
ArrayList fields = mMakeProtectedFields.get(className);
if (fields == null) {
fields = new ArrayList<String>();
mMakeProtectedFields.put(className, fields);
}
fields.add(fieldName);
}

protected void addMethodAnnotation(String className, String methodName, String annotationType) {
Map<String, ArrayList<String>> annotations = mAddAnnotations.get(className);
if (annotations == null) {
Expand Down Expand Up @@ -150,6 +235,25 @@ protected void visitImpl(int version,
super.visit(version, access, name, signature, superName, interfaces);
}

@Override
public FieldVisitor visitField(int access,
java.lang.String name,
java.lang.String descriptor,
java.lang.String signature,
java.lang.Object value) {
if (shouldDeleteField(name)) {
System.out.println("delete " + name + " from " + mName);
return null;
}

if (shouldMakeProtectedField(name)) {
System.out.println("make " + name + " public in " + mName);
access &= ~ACC_PRIVATE;
access |= ACC_PROTECTED;
}
return super.visitField(access, name, descriptor, signature, value);
}

@Override
public MethodVisitor visitMethod(final int access,
final String name,
Expand All @@ -171,11 +275,11 @@ public MethodVisitor visitMethod(final int access,
}

protected MethodVisitor visitMethodImpl(Method method) {
return super.visitMethod(method.access,
method.name,
method.desc,
method.signature,
method.exceptions);
return new BraveMethodVisitor(method, super.visitMethod(method.access,
method.name,
method.desc,
method.signature,
method.exceptions));
}

protected ClassNode process(ClassNode source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@

package org.chromium.components.sync;


// see org.brave.bytecode.BraveAndroidSyncSettingsAdapter
public class BraveAndroidSyncSettings extends AndroidSyncSettings {
private boolean mIsSyncable;

private boolean mChromeSyncEnabled;

private boolean mMasterSyncEnabled;

public BraveAndroidSyncSettings(SyncContentResolverDelegate syncContentResolverDelegate) {
super(syncContentResolverDelegate, null);
}

@Override
protected void setChromeSyncEnabled(boolean value) {
public void setChromeSyncEnabled(boolean value) {
mChromeSyncEnabled = false;
notifyObservers();
}

@Override
protected boolean updateCachedSettings() {
public boolean updateCachedSettings() {
boolean oldChromeSyncEnabled = mChromeSyncEnabled;
boolean oldMasterSyncEnabled = mMasterSyncEnabled;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
diff --git a/components/sync/android/java/src/org/chromium/components/sync/AndroidSyncSettings.java b/components/sync/android/java/src/org/chromium/components/sync/AndroidSyncSettings.java
index 84a8b66912a2f6847771486e62556b772c192fd2..57e756941d38433c21fb7aaa4bcd472044894cb9 100644
index 84a8b66912a2f6847771486e62556b772c192fd2..b66f68589fb11cda455a86d8400feb3ac2a0f083 100644
--- a/components/sync/android/java/src/org/chromium/components/sync/AndroidSyncSettings.java
+++ b/components/sync/android/java/src/org/chromium/components/sync/AndroidSyncSettings.java
@@ -55,11 +55,11 @@ public class AndroidSyncSettings {

private Account mAccount;

- private boolean mIsSyncable;
+ protected boolean mIsSyncable;

- private boolean mChromeSyncEnabled;
+ protected boolean mChromeSyncEnabled;

- private boolean mMasterSyncEnabled;
+ protected boolean mMasterSyncEnabled;

private final ObserverList<AndroidSyncSettingsObserver> mObservers =
new ObserverList<AndroidSyncSettingsObserver>();
@@ -77,7 +77,7 @@ public class AndroidSyncSettings {
if (sInstance == null) {
SyncContentResolverDelegate contentResolver =
Expand All @@ -35,21 +20,3 @@ index 84a8b66912a2f6847771486e62556b772c192fd2..57e756941d38433c21fb7aaa4bcd4720
@Nullable Callback<Boolean> callback) {
mContractAuthority = ContextUtils.getApplicationContext().getPackageName();
mSyncContentResolverDelegate = syncContentResolverDelegate;
@@ -211,7 +211,7 @@ public class AndroidSyncSettings {
}
}

- private void setChromeSyncEnabled(boolean value) {
+ protected void setChromeSyncEnabled(boolean value) {
synchronized (mLock) {
updateSyncability(null);
if (value == mChromeSyncEnabled || mAccount == null) return;
@@ -295,7 +295,7 @@ public class AndroidSyncSettings {
*
* @return Whether either chromeSyncEnabled or masterSyncEnabled changed.
*/
- private boolean updateCachedSettings() {
+ protected boolean updateCachedSettings() {
synchronized (mLock) {
boolean oldChromeSyncEnabled = mChromeSyncEnabled;
boolean oldMasterSyncEnabled = mMasterSyncEnabled;