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

DynamiteModule: Added <init> method #2664

Merged
merged 2 commits into from
Dec 14, 2024
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 @@ -5,10 +5,27 @@

package com.google.android.gms.dynamite.descriptors.com.google.android.gms.ads.dynamite;

import android.content.Context;
import android.content.ContextWrapper;
import android.webkit.WebSettings;
import androidx.annotation.Keep;

@Keep
public class ModuleDescriptor {
public static final String MODULE_ID = "com.google.android.gms.ads.dynamite";
public static final int MODULE_VERSION = 230500001;

/**
* The ads module might try to access the user agent, requiring initialization on the main thread,
* which may result in deadlocks when invoked from any other thread. This only happens with microG,
* because we don't use the highly privileged SELinux Sandbox that regular Play Services uses
* (which allows apps to read the user-agent from Play Services instead of the WebView). To prevent
* the issue we pre-emptively initialize the WebView.
*/
public static void init(Context context) {
if (context instanceof ContextWrapper) {
context = ((ContextWrapper) context).getBaseContext();
}
WebSettings.getDefaultUserAgent(context);
mar-v-in marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ public static DynamiteContext create(String moduleId, Context originalContext) {
DynamiteModuleInfo moduleInfo = new DynamiteModuleInfo(moduleId);
Context gmsContext = originalContext.createPackageContext(Constants.GMS_PACKAGE_NAME, 0);
Context originalAppContext = originalContext.getApplicationContext();
DynamiteContext dynamiteContext;
if (originalAppContext == null || originalAppContext == originalContext) {
return new DynamiteContext(moduleInfo, originalContext, gmsContext, null);
dynamiteContext = new DynamiteContext(moduleInfo, originalContext, gmsContext, null);
} else {
return new DynamiteContext(moduleInfo, originalContext, gmsContext, new DynamiteContext(moduleInfo, originalAppContext, gmsContext, null));
dynamiteContext = new DynamiteContext(moduleInfo, originalContext, gmsContext, new DynamiteContext(moduleInfo, originalAppContext, gmsContext, null));
}
moduleInfo.init(dynamiteContext);
return dynamiteContext;
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
import java.util.Collection;
import java.util.Collections;

import static android.content.Context.CONTEXT_IGNORE_SECURITY;
import static android.content.Context.CONTEXT_INCLUDE_CODE;
import android.content.Context;

public class DynamiteModuleInfo {
private Class<?> descriptor;
Expand Down Expand Up @@ -51,4 +50,12 @@ public Collection<String> getMergedClasses() {
return Collections.emptySet();
}
}

public void init(Context dynamiteContext) {
try {
descriptor.getMethod("init", Context.class).invoke(null, dynamiteContext);
} catch (Exception e) {
// Ignore
}
}
}
Loading