Skip to content
This repository has been archived by the owner on Sep 6, 2019. It is now read-only.

Commit

Permalink
Hook WebView constructor to set initial user agent string
Browse files Browse the repository at this point in the history
Refs #825
  • Loading branch information
M66B committed Nov 26, 2013
1 parent 56b371a commit 2084a94
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions assets/meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,6 @@
<Hook restriction="system" method="ApplicationsProvider" permissions="" sdk="14" />

<Hook restriction="view" method="getSettings" permissions="" sdk="1" />
<Hook restriction="view" method="WebView.constructor" permissions="" sdk="1" />
<Hook restriction="view" method="android.intent.action.VIEW" permissions="" sdk="14" />
</Meta>
25 changes: 12 additions & 13 deletions src/biz/bokhorst/xprivacy/XWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Binder;
import android.util.Log;
import android.webkit.WebView;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
Expand All @@ -17,7 +17,8 @@ public class XWebView extends XHook {
private static final List<String> mWebSettings = new ArrayList<String>();

private XWebView(Methods method, String restrictionName) {
super(restrictionName, method.name(), null);
super(restrictionName, (method == Methods.constructor ? null : method.name()),
(method == Methods.constructor ? "WebView.constructor" : null));
mMethod = method;
}

Expand All @@ -35,11 +36,12 @@ public String getClassName() {
// http://developer.android.com/reference/android/webkit/WebSettings.html

private enum Methods {
getSettings
constructor, getSettings
};

public static List<XHook> getInstances() {
List<XHook> listHook = new ArrayList<XHook>();
listHook.add(new XWebView(Methods.constructor, PrivacyManager.cView));
listHook.add(new XWebView(Methods.getSettings, PrivacyManager.cView));
return listHook;
}
Expand All @@ -51,7 +53,13 @@ protected void before(MethodHookParam param) throws Throwable {

@Override
protected void after(MethodHookParam param) throws Throwable {
if (mMethod == Methods.getSettings) {
if (mMethod == Methods.constructor) {
if (isRestricted(param)) {
String ua = (String) PrivacyManager.getDefacedProp(Binder.getCallingUid(), "UA");
WebView webView = (WebView) param.thisObject;
webView.getSettings().setUserAgentString(ua);
}
} else if (mMethod == Methods.getSettings) {
if (param.getResult() != null) {
// Check web settings type
Class<?> clazzWebSettings = param.getResultOrThrowable().getClass();
Expand Down Expand Up @@ -94,13 +102,4 @@ protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
} else
Util.log(this, Log.WARN, "Unknown method=" + param.method.getName());
}

@Override
protected boolean isRestricted(MethodHookParam param) throws Throwable {
Context context = null;
if (param.args.length > 0)
context = (Context) param.args[0];
int uid = Binder.getCallingUid();
return getRestricted(context, uid, true);
}
}

0 comments on commit 2084a94

Please sign in to comment.