From 14faf9d16c1082a2d8f2f65e4eb69a23db49e5e1 Mon Sep 17 00:00:00 2001 From: Fredy Wijaya Date: Fri, 2 Feb 2024 12:17:03 -0600 Subject: [PATCH] mobile: Update Cronvoy API to allow setting log level and logger (#32153) Signed-off-by: Fredy Wijaya --- .../net/impl/CronvoyEngineBuilderImpl.java | 22 +++++++++++++++++++ .../org/chromium/net/impl/CronvoyLogger.java | 2 +- .../net/impl/CronvoyUrlRequestContext.java | 7 +++--- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/mobile/library/java/org/chromium/net/impl/CronvoyEngineBuilderImpl.java b/mobile/library/java/org/chromium/net/impl/CronvoyEngineBuilderImpl.java index 7813bb95592a..300050049da7 100644 --- a/mobile/library/java/org/chromium/net/impl/CronvoyEngineBuilderImpl.java +++ b/mobile/library/java/org/chromium/net/impl/CronvoyEngineBuilderImpl.java @@ -20,6 +20,8 @@ import org.chromium.net.ICronetEngineBuilder; import org.chromium.net.impl.Annotations.HttpCacheType; +import io.envoyproxy.envoymobile.engine.EnvoyEngine; + /** Implementation of {@link ICronetEngineBuilder} that builds Envoy-Mobile based Cronet engine. */ public abstract class CronvoyEngineBuilderImpl extends ICronetEngineBuilder { @@ -66,6 +68,8 @@ final static class Pkp { private String mExperimentalOptions; private boolean mNetworkQualityEstimatorEnabled; private int mThreadPriority = INVALID_THREAD_PRIORITY; + private EnvoyEngine.LogLevel mLogLevel = EnvoyEngine.LogLevel.OFF; + private CronvoyLogger mCronvoyLogger = new CronvoyLogger(); /** * Default config enables SPDY and QUIC, disables SDCH and HTTP cache. @@ -357,4 +361,22 @@ int threadPriority(int defaultThreadPriority) { * @return {@link Context} for builder. */ Context getContext() { return mApplicationContext; } + + /** Sets the log level. */ + public CronvoyEngineBuilderImpl setLogLevel(EnvoyEngine.LogLevel logLevel) { + this.mLogLevel = logLevel; + return this; + } + + /** Gets the log level. It defaults to `OFF` when not set. */ + public EnvoyEngine.LogLevel getLogLevel() { return mLogLevel; } + + /** Sets the {@link io.envoyproxy.envoymobile.engine.types.EnvoyLogger}. */ + public CronvoyEngineBuilderImpl setLogger(CronvoyLogger cronvoyLogger) { + this.mCronvoyLogger = cronvoyLogger; + return this; + } + + /** Gets the {@link org.chromium.net.impl.CronvoyLogger} implementation. */ + public CronvoyLogger getLogger() { return mCronvoyLogger; } } diff --git a/mobile/library/java/org/chromium/net/impl/CronvoyLogger.java b/mobile/library/java/org/chromium/net/impl/CronvoyLogger.java index 34c0d2b0487c..07d238f60af7 100644 --- a/mobile/library/java/org/chromium/net/impl/CronvoyLogger.java +++ b/mobile/library/java/org/chromium/net/impl/CronvoyLogger.java @@ -22,7 +22,7 @@ * the desired file until CronvoyUrlRequestContext.stopNetLog disables Envoy logging. * */ -final class CronvoyLogger implements EnvoyLogger { +public class CronvoyLogger implements EnvoyLogger { private int mFilesize = 0; private String mFileName = null; private FileWriter mWriter = null; diff --git a/mobile/library/java/org/chromium/net/impl/CronvoyUrlRequestContext.java b/mobile/library/java/org/chromium/net/impl/CronvoyUrlRequestContext.java index 175314abdb49..bf6e5ae60643 100644 --- a/mobile/library/java/org/chromium/net/impl/CronvoyUrlRequestContext.java +++ b/mobile/library/java/org/chromium/net/impl/CronvoyUrlRequestContext.java @@ -47,7 +47,7 @@ public final class CronvoyUrlRequestContext extends CronvoyEngineBase { private final Object mLock = new Object(); private final ConditionVariable mInitCompleted = new ConditionVariable(false); private final AtomicInteger mActiveRequestCount = new AtomicInteger(0); - private EnvoyEngine.LogLevel mLogLevel = EnvoyEngine.LogLevel.OFF; + private EnvoyEngine.LogLevel mLogLevel; @GuardedBy("mLock") private EnvoyEngine mEngine; /** @@ -61,7 +61,7 @@ public final class CronvoyUrlRequestContext extends CronvoyEngineBase { private final String mUserAgent; private final AtomicReference mInitializationCompleter = new AtomicReference<>(); - private final CronvoyLogger mCronvoyLogger = new CronvoyLogger(); + private final CronvoyLogger mCronvoyLogger; /** * Locks operations on the list of RequestFinishedInfo.Listeners, because operations can happen @@ -84,8 +84,9 @@ public CronvoyUrlRequestContext(NativeCronvoyEngineBuilderImpl builder) { final int threadPriority = builder.threadPriority(THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY_MORE_FAVORABLE); mUserAgent = builder.getUserAgent(); + mLogLevel = builder.getLogLevel(); + mCronvoyLogger = builder.getLogger(); synchronized (mLock) { - mEngine = builder.createEngine(() -> { mNetworkThread = Thread.currentThread(); android.os.Process.setThreadPriority(threadPriority);