Skip to content

Commit

Permalink
DynamiteModule: Added <init> method (#2664)
Browse files Browse the repository at this point in the history
Co-authored-by: Marvin W <[email protected]>
  • Loading branch information
DaVinci9196 and mar-v-in authored Dec 14, 2024
1 parent f0698a1 commit ad7e79e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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);
}
}
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
}
}
}

0 comments on commit ad7e79e

Please sign in to comment.