package com.google.firebase.perf.metrics; import com.google.firebase.perf.application.AppStateMonitor; import com.google.firebase.perf.application.AppStateUpdateHandler; import com.google.firebase.perf.logging.AndroidLogger; import com.google.firebase.perf.network.NetworkRequestMetricBuilderUtil; import com.google.firebase.perf.session.PerfSession; import com.google.firebase.perf.session.SessionAwareObject; import com.google.firebase.perf.session.SessionManager; import com.google.firebase.perf.session.gauges.GaugeManager; import com.google.firebase.perf.transport.TransportManager; import com.google.firebase.perf.util.Utils; import com.google.firebase.perf.v1.NetworkRequestMetric; import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /* loaded from: classes3.dex */ public final class NetworkRequestMetricBuilder extends AppStateUpdateHandler implements SessionAwareObject { public static final AndroidLogger logger = AndroidLogger.getInstance(); public final NetworkRequestMetric.Builder builder; public final GaugeManager gaugeManager; public boolean isManualNetworkRequestMetric; public boolean isReportSent; public final List sessions; public final TransportManager transportManager; public String userAgent; public final WeakReference weakReference; public NetworkRequestMetricBuilder setUserAgent(String str) { this.userAgent = str; return this; } @Override // com.google.firebase.perf.session.SessionAwareObject public void updateSession(PerfSession perfSession) { if (perfSession == null) { logger.warn("Unable to add new SessionId to the Network Trace. Continuing without it."); } else { if (!hasStarted() || isStopped()) { return; } this.sessions.add(perfSession); } } public static NetworkRequestMetricBuilder builder(TransportManager transportManager) { return new NetworkRequestMetricBuilder(transportManager); } public NetworkRequestMetricBuilder(TransportManager transportManager) { this(transportManager, AppStateMonitor.getInstance(), GaugeManager.getInstance()); } public NetworkRequestMetricBuilder(TransportManager transportManager, AppStateMonitor appStateMonitor, GaugeManager gaugeManager) { super(appStateMonitor); this.builder = NetworkRequestMetric.newBuilder(); this.weakReference = new WeakReference(this); this.transportManager = transportManager; this.gaugeManager = gaugeManager; this.sessions = Collections.synchronizedList(new ArrayList()); registerForAppState(); } public NetworkRequestMetricBuilder setUrl(String str) { if (str != null) { this.builder.setUrl(Utils.truncateURL(Utils.stripSensitiveInfo(str), 2000)); } return this; } public NetworkRequestMetricBuilder setHttpMethod(String str) { NetworkRequestMetric.HttpMethod httpMethod; if (str != null) { NetworkRequestMetric.HttpMethod httpMethod2 = NetworkRequestMetric.HttpMethod.HTTP_METHOD_UNKNOWN; String upperCase = str.toUpperCase(); upperCase.hashCode(); switch (upperCase) { case "OPTIONS": httpMethod = NetworkRequestMetric.HttpMethod.OPTIONS; break; case "GET": httpMethod = NetworkRequestMetric.HttpMethod.GET; break; case "PUT": httpMethod = NetworkRequestMetric.HttpMethod.PUT; break; case "HEAD": httpMethod = NetworkRequestMetric.HttpMethod.HEAD; break; case "POST": httpMethod = NetworkRequestMetric.HttpMethod.POST; break; case "PATCH": httpMethod = NetworkRequestMetric.HttpMethod.PATCH; break; case "TRACE": httpMethod = NetworkRequestMetric.HttpMethod.TRACE; break; case "CONNECT": httpMethod = NetworkRequestMetric.HttpMethod.CONNECT; break; case "DELETE": httpMethod = NetworkRequestMetric.HttpMethod.DELETE; break; default: httpMethod = NetworkRequestMetric.HttpMethod.HTTP_METHOD_UNKNOWN; break; } this.builder.setHttpMethod(httpMethod); } return this; } public NetworkRequestMetricBuilder setHttpResponseCode(int i) { this.builder.setHttpResponseCode(i); return this; } public boolean hasHttpResponseCode() { return this.builder.hasHttpResponseCode(); } public NetworkRequestMetricBuilder setRequestPayloadBytes(long j) { this.builder.setRequestPayloadBytes(j); return this; } public NetworkRequestMetricBuilder setRequestStartTimeMicros(long j) { PerfSession perfSession = SessionManager.getInstance().perfSession(); SessionManager.getInstance().registerForSessionUpdates(this.weakReference); this.builder.setClientStartTimeUs(j); updateSession(perfSession); if (perfSession.isGaugeAndEventCollectionEnabled()) { this.gaugeManager.collectGaugeMetricOnce(perfSession.getTimer()); } return this; } public NetworkRequestMetricBuilder setTimeToRequestCompletedMicros(long j) { this.builder.setTimeToRequestCompletedUs(j); return this; } public NetworkRequestMetricBuilder setTimeToResponseInitiatedMicros(long j) { this.builder.setTimeToResponseInitiatedUs(j); return this; } public long getTimeToResponseInitiatedMicros() { return this.builder.getTimeToResponseInitiatedUs(); } public NetworkRequestMetricBuilder setTimeToResponseCompletedMicros(long j) { this.builder.setTimeToResponseCompletedUs(j); if (SessionManager.getInstance().perfSession().isGaugeAndEventCollectionEnabled()) { this.gaugeManager.collectGaugeMetricOnce(SessionManager.getInstance().perfSession().getTimer()); } return this; } public NetworkRequestMetricBuilder setResponsePayloadBytes(long j) { this.builder.setResponsePayloadBytes(j); return this; } public NetworkRequestMetricBuilder setResponseContentType(String str) { if (str == null) { this.builder.clearResponseContentType(); return this; } if (isValidContentType(str)) { this.builder.setResponseContentType(str); } else { logger.warn("The content type of the response is not a valid content-type:" + str); } return this; } public NetworkRequestMetricBuilder setNetworkClientErrorReason() { this.builder.setNetworkClientErrorReason(NetworkRequestMetric.NetworkClientErrorReason.GENERIC_CLIENT_ERROR); return this; } public NetworkRequestMetric build() { SessionManager.getInstance().unregisterForSessionUpdates(this.weakReference); unregisterForAppState(); com.google.firebase.perf.v1.PerfSession[] buildAndSort = PerfSession.buildAndSort(getSessions()); if (buildAndSort != null) { this.builder.addAllPerfSessions(Arrays.asList(buildAndSort)); } NetworkRequestMetric networkRequestMetric = (NetworkRequestMetric) this.builder.build(); if (!NetworkRequestMetricBuilderUtil.isAllowedUserAgent(this.userAgent)) { logger.debug("Dropping network request from a 'User-Agent' that is not allowed"); return networkRequestMetric; } if (this.isReportSent) { if (this.isManualNetworkRequestMetric) { logger.debug("This metric has already been queued for transmission. Please create a new HttpMetric for each request/response"); } return networkRequestMetric; } this.transportManager.log(networkRequestMetric, getAppState()); this.isReportSent = true; return networkRequestMetric; } private boolean isStopped() { return this.builder.hasTimeToResponseCompletedUs(); } private boolean hasStarted() { return this.builder.hasClientStartTimeUs(); } public List getSessions() { List unmodifiableList; synchronized (this.sessions) { try { ArrayList arrayList = new ArrayList(); for (PerfSession perfSession : this.sessions) { if (perfSession != null) { arrayList.add(perfSession); } } unmodifiableList = Collections.unmodifiableList(arrayList); } catch (Throwable th) { throw th; } } return unmodifiableList; } public static boolean isValidContentType(String str) { if (str.length() > 128) { return false; } for (int i = 0; i < str.length(); i++) { char charAt = str.charAt(i); if (charAt <= 31 || charAt > 127) { return false; } } return true; } }