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

Java patches rebase #4716

Merged
merged 4 commits into from
Mar 5, 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 @@ -2378,7 +2378,7 @@ public void run() {
mBookmarkItems.add(bookmarkItem);
} else if (mAction.equals(DELETE_RECORD)) {
long defaultFolderId = (null != mDefaultFolder ? mDefaultFolder.getId() : 0);
mBookmarkItems.add(BookmarkBridge.createBookmarkItem(id, BookmarkType.NORMAL, "", "", false,
mBookmarkItems.add(BraveBookmarkModel.createBookmarkItem(id, BookmarkType.NORMAL, "", "", false,
SergeyZhukovsky marked this conversation as resolved.
Show resolved Hide resolved
defaultFolderId, BookmarkType.NORMAL, true, true));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,33 @@

package org.chromium.chrome.browser.bookmarks;

import org.chromium.chrome.browser.bookmarks.BookmarkBridge;
import org.chromium.chrome.browser.bookmarks.BookmarkBridge.BookmarkItem;
import org.chromium.components.bookmarks.BookmarkId;


// see org.brave.bytecode.BraveBookmarkModelClassAdapter
public class BraveBookmarkModel extends BookmarkModel {

public void deleteBookmarkSilently(BookmarkId bookmark) {
assert null != bookmark;
deleteBookmark(bookmark);
}

public void extensiveBookmarkChangesBeginning() {
assert false;
}

public void extensiveBookmarkChangesEnded() {
assert false;
}

public static BookmarkItem createBookmarkItem(long id, int type, String title, String url,
boolean isFolder, long parentId, int parentIdType, boolean isEditable,
boolean isManaged) {
assert false;
return null;
}

/**
* Calls {@link BookmarkBridge#moveBookmark(BookmarkId, BookmarkId, int)} for the given
* bookmark. The bookmark is appended at the end. Call that method from Brave's sync only
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.brave.bytecode;

import org.objectweb.asm.ClassVisitor;

public class BraveBookmarkModelClassAdapter extends BraveClassVisitor {

static String sBraveBookmarkModelClassName =
"org/chromium/chrome/browser/bookmarks/BraveBookmarkModel";

static String sBookmarksBridgeClassName =
"org/chromium/chrome/browser/bookmarks/BookmarkBridge";

public BraveBookmarkModelClassAdapter(ClassVisitor visitor) {
super(visitor);
deleteMethod(sBraveBookmarkModelClassName,
"extensiveBookmarkChangesBeginning");
deleteMethod(sBraveBookmarkModelClassName,
"extensiveBookmarkChangesEnded");
deleteMethod(sBraveBookmarkModelClassName,
"createBookmarkItem");
makePublicMethod(sBookmarksBridgeClassName,
"extensiveBookmarkChangesBeginning");
makePublicMethod(sBookmarksBridgeClassName,
"extensiveBookmarkChangesEnded");
makePublicMethod(sBookmarksBridgeClassName,
"createBookmarkItem");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2020 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.brave.bytecode;

import org.objectweb.asm.ClassVisitor;

public class BraveClassAdapter {
public static ClassVisitor createAdapter(ClassVisitor chain) {
chain = new BraveBookmarkModelClassAdapter(chain);
chain = new BraveMainPreferenceBaseClassAdapter(chain);
SergeyZhukovsky marked this conversation as resolved.
Show resolved Hide resolved
return chain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.brave.bytecode;

import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM5;

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

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

class BraveClassVisitor extends ClassVisitor {

class Method {
public int access;
public String name;
public String desc;
public String signature;
public String[] exceptions;

public Method(final int access, final String name, String desc,
String signature, String[] exceptions) {
this.access = access;
this.name = name;
this.desc = desc;
this.signature = signature;
this.exceptions = exceptions;
}

public void makePublic() {
access &= ~ACC_PRIVATE;
access |= ACC_PUBLIC;
}
}

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>> mMakePublicMethods =
new HashMap<String, ArrayList<String>>();

public BraveClassVisitor(ClassVisitor visitor) {
super(ASM5, visitor);
}

protected void changeSuperName(String className, String superName) {
mSuperNames.put(className, superName);
}

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

return false;
}

protected void deleteMethod(String className, String methodName) {
ArrayList methods = mDeleteMethods.get(className);
if (methods == null) {
methods = new ArrayList<String>();
mDeleteMethods.put(className, methods);
}
methods.add(methodName);
}

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

return false;
}

protected void makePublicMethod(String className, String methodName) {
ArrayList methods = mMakePublicMethods.get(className);
if (methods == null) {
methods = new ArrayList<String>();
mMakePublicMethods.put(className, methods);
}
methods.add(methodName);
}

@Override
public void visit(int version,
int access,
String name,
String signature,
String superName,
String[] interfaces) {
mName = name;
if (mSuperNames.containsKey(name)) {
superName = mSuperNames.get(name);
System.out.println("change superclass of " + name + " to " + superName);
}

visitImpl(version, access, name, signature, superName, interfaces);
}

protected void visitImpl(int version,
int access,
String name,
String signature,
String superName,
String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
}

@Override
public MethodVisitor visitMethod(final int access,
final String name,
String desc,
String signature,
String[] exceptions) {
Method method = new Method(access, name, desc, signature, exceptions);
if (shouldDeleteMethod(name)) {
System.out.println("delete " + name + " from " + mName);
return null;
}

if (shouldMakePublicMethod(name)) {
System.out.println("make " + name + " public in " + mName);
method.makePublic();
}

return visitMethodImpl(method);
}

protected MethodVisitor visitMethodImpl(Method method) {
return super.visitMethod(method.access,
method.name,
method.desc,
method.signature,
method.exceptions);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2020 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.brave.bytecode;

import org.objectweb.asm.ClassVisitor;

public class BraveMainPreferenceBaseClassAdapter extends BraveClassVisitor {

static String sMainPreferencesClassName =
"org/chromium/chrome/browser/preferences/MainPreferences";

static String sBraveMainPreferencesBaseClassName =
"org/chromium/chrome/browser/preferences/BraveMainPreferencesBase";

public BraveMainPreferenceBaseClassAdapter(ClassVisitor visitor) {
super(visitor);
changeSuperName(sMainPreferencesClassName,
sBraveMainPreferencesBaseClassName);
}
}
6 changes: 6 additions & 0 deletions build/android/bytecode/java_sources.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
brave_java_bytecode_files = [
"../../../brave/build/android/bytecode/java/org/brave/bytecode/BraveClassAdapter.java",
"../../../brave/build/android/bytecode/java/org/brave/bytecode/BraveClassVisitor.java",
"../../../brave/build/android/bytecode/java/org/brave/bytecode/BraveBookmarkModelClassAdapter.java",
"../../../brave/build/android/bytecode/java/org/brave/bytecode/BraveMainPreferenceBaseClassAdapter.java",
]
1 change: 1 addition & 0 deletions build/android/config.gni
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import("//brave/android/brave_java_sources.gni")
import("//brave/build/android/bytecode/java_sources.gni")

declare_args() {
brave_android_manifest_includes = []
Expand Down
12 changes: 12 additions & 0 deletions patches/build-android-bytecode-BUILD.gn.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/build/android/bytecode/BUILD.gn b/build/android/bytecode/BUILD.gn
index 88517537f42e5bcea3aa7763dca9ce95b3e4d552..136410363466f1d717f1faa22bf308180e1f326e 100644
--- a/build/android/bytecode/BUILD.gn
+++ b/build/android/bytecode/BUILD.gn
@@ -16,6 +16,7 @@ java_binary("java_bytecode_rewriter") {
"java/org/chromium/bytecode/ThreadAssertionClassAdapter.java",
"java/org/chromium/bytecode/TypeUtils.java",
]
+ java_files += brave_java_bytecode_files
main_class = "org.chromium.bytecode.ByteCodeProcessor"
deps = [
"//third_party/ow2_asm:asm_java",
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java b/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java
index 4a56332c061a775dae854a1d2946c855c2f4f66c..682225913081bafd1f393ed8327240012304dfc6 100644
--- a/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java
+++ b/build/android/bytecode/java/org/chromium/bytecode/ByteCodeProcessor.java
@@ -136,6 +136,7 @@ class ByteCodeProcessor {
chain = new CustomResourcesClassAdapter(
chain, reader.getClassName(), reader.getSuperName(), sFullClassPathClassLoader);
}
+ chain = org.brave.bytecode.BraveClassAdapter.createAdapter(chain);
reader.accept(chain, 0);
byte[] patchedByteCode = writer.toByteArray();
return EntryDataPair.create(entry.getName(), patchedByteCode);

This file was deleted.