导航菜单

页面标题

页面副标题

Credmex v3.12.2 - EnvelopeCache.java 源代码

正在查看: Credmex v3.12.2 应用的 EnvelopeCache.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package io.sentry.cache;

import io.sentry.DateUtils;
import io.sentry.Hint;
import io.sentry.ILogger;
import io.sentry.SentryCrashLastRunState;
import io.sentry.SentryEnvelope;
import io.sentry.SentryEnvelopeItem;
import io.sentry.SentryItemType;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.Session;
import io.sentry.UncaughtExceptionHandlerIntegration;
import io.sentry.hints.AbnormalExit;
import io.sentry.hints.SessionEnd;
import io.sentry.hints.SessionStart;
import io.sentry.transport.NoOpEnvelopeCache;
import io.sentry.util.HintUtils;
import io.sentry.util.Objects;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

@ApiStatus.Internal
public class EnvelopeCache extends CacheStrategy implements IEnvelopeCache {
    private final CountDownLatch f;

    @NotNull
    private final Map<SentryEnvelope, String> g;

    public EnvelopeCache(@NotNull SentryOptions sentryOptions, @NotNull String str, int i) {
        super(sentryOptions, str, i);
        this.g = new WeakHashMap();
        this.f = new CountDownLatch(1);
    }

    private void C(@NotNull Hint hint) {
        Date date;
        Object g = HintUtils.g(hint);
        if (g instanceof AbnormalExit) {
            File y2 = y(this.c.getAbsolutePath());
            if (!y2.exists()) {
                this.a.getLogger().c(SentryLevel.DEBUG, "No previous session file to end.", new Object[0]);
                return;
            }
            ILogger logger = this.a.getLogger();
            SentryLevel sentryLevel = SentryLevel.WARNING;
            logger.c(sentryLevel, "Previous session is not ended, we'd need to end it.", new Object[0]);
            try {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(y2), CacheStrategy.e));
                try {
                    Session session = (Session) this.b.c(bufferedReader, Session.class);
                    if (session != null) {
                        AbnormalExit abnormalExit = (AbnormalExit) g;
                        Long c = abnormalExit.c();
                        if (c != null) {
                            date = DateUtils.d(c.longValue());
                            Date k = session.k();
                            if (k == null || date.before(k)) {
                                this.a.getLogger().c(sentryLevel, "Abnormal exit happened before previous session start, not ending the session.", new Object[0]);
                                bufferedReader.close();
                                return;
                            }
                        } else {
                            date = null;
                        }
                        session.q(Session.State.Abnormal, null, true, abnormalExit.f());
                        session.d(date);
                        H(y2, session);
                    }
                    bufferedReader.close();
                } finally {
                }
            } catch (Throwable th) {
                this.a.getLogger().b(SentryLevel.ERROR, "Error processing previous session.", th);
            }
        }
    }

    private void D(@NotNull File file, @NotNull SentryEnvelope sentryEnvelope) {
        Iterable<SentryEnvelopeItem> c = sentryEnvelope.c();
        if (!c.iterator().hasNext()) {
            this.a.getLogger().c(SentryLevel.INFO, "Current envelope %s is empty", file.getAbsolutePath());
            return;
        }
        SentryEnvelopeItem next = c.iterator().next();
        if (!SentryItemType.Session.equals(next.x().b())) {
            this.a.getLogger().c(SentryLevel.INFO, "Current envelope has a different envelope type %s", next.x().b());
            return;
        }
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(next.w()), CacheStrategy.e));
            try {
                Session session = (Session) this.b.c(bufferedReader, Session.class);
                if (session == null) {
                    this.a.getLogger().c(SentryLevel.ERROR, "Item of type %s returned null by the parser.", next.x().b());
                } else {
                    H(file, session);
                }
                bufferedReader.close();
            } finally {
            }
        } catch (Throwable th) {
            this.a.getLogger().b(SentryLevel.ERROR, "Item failed to process.", th);
        }
    }

    private void F() {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(new File(this.a.getCacheDirPath(), "last_crash"));
            try {
                fileOutputStream.write(DateUtils.g(DateUtils.c()).getBytes(CacheStrategy.e));
                fileOutputStream.flush();
                fileOutputStream.close();
            } finally {
            }
        } catch (Throwable th) {
            this.a.getLogger().b(SentryLevel.ERROR, "Error writing the crash marker file to the disk", th);
        }
    }

    private void G(@NotNull File file, @NotNull SentryEnvelope sentryEnvelope) {
        if (file.exists()) {
            this.a.getLogger().c(SentryLevel.DEBUG, "Overwriting envelope to offline storage: %s", file.getAbsolutePath());
            if (!file.delete()) {
                this.a.getLogger().c(SentryLevel.ERROR, "Failed to delete: %s", file.getAbsolutePath());
            }
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            try {
                this.b.b(sentryEnvelope, fileOutputStream);
                fileOutputStream.close();
            } finally {
            }
        } catch (Throwable th) {
            this.a.getLogger().a(SentryLevel.ERROR, th, "Error writing Envelope %s to offline storage", file.getAbsolutePath());
        }
    }

    private void H(@NotNull File file, @NotNull Session session) {
        if (file.exists()) {
            this.a.getLogger().c(SentryLevel.DEBUG, "Overwriting session to offline storage: %s", session.j());
            if (!file.delete()) {
                this.a.getLogger().c(SentryLevel.ERROR, "Failed to delete: %s", file.getAbsolutePath());
            }
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, CacheStrategy.e));
                try {
                    this.b.a(session, bufferedWriter);
                    bufferedWriter.close();
                    fileOutputStream.close();
                } finally {
                }
            } finally {
            }
        } catch (Throwable th) {
            this.a.getLogger().a(SentryLevel.ERROR, th, "Error writing Session to offline storage: %s", session.j());
        }
    }

    @NotNull
    private File[] s() {
        File[] listFiles;
        return (!e() || (listFiles = this.c.listFiles(new FilenameFilter() {
            @Override
            public final boolean accept(File file, String str) {
                boolean z2;
                z2 = EnvelopeCache.z(file, str);
                return z2;
            }
        })) == null) ? new File[0] : listFiles;
    }

    @NotNull
    public static IEnvelopeCache t(@NotNull SentryOptions sentryOptions) {
        String cacheDirPath = sentryOptions.getCacheDirPath();
        int maxCacheItems = sentryOptions.getMaxCacheItems();
        if (cacheDirPath != null) {
            return new EnvelopeCache(sentryOptions, cacheDirPath, maxCacheItems);
        }
        sentryOptions.getLogger().c(SentryLevel.WARNING, "cacheDirPath is null, returning NoOpEnvelopeCache", new Object[0]);
        return NoOpEnvelopeCache.a();
    }

    @NotNull
    public static File v(@NotNull String str) {
        return new File(str, "session.json");
    }

    @NotNull
    private synchronized File w(@NotNull SentryEnvelope sentryEnvelope) {
        String str;
        if (this.g.containsKey(sentryEnvelope)) {
            str = this.g.get(sentryEnvelope);
        } else {
            String str2 = (sentryEnvelope.b().a() != null ? sentryEnvelope.b().a().toString() : UUID.randomUUID().toString()) + ".envelope";
            this.g.put(sentryEnvelope, str2);
            str = str2;
        }
        return new File(this.c.getAbsolutePath(), str);
    }

    @NotNull
    public static File y(@NotNull String str) {
        return new File(str, "previous_session.json");
    }

    public static boolean z(File file, String str) {
        return str.endsWith(".envelope");
    }

    public boolean E() {
        try {
            return this.f.await(this.a.getFlushTimeoutMillis(), TimeUnit.MILLISECONDS);
        } catch (InterruptedException unused) {
            Thread.currentThread().interrupt();
            this.a.getLogger().c(SentryLevel.DEBUG, "Timed out waiting for previous session to flush.", new Object[0]);
            return false;
        }
    }

    public void O(@NotNull SentryEnvelope sentryEnvelope, @NotNull Hint hint) {
        Objects.c(sentryEnvelope, "Envelope is required.");
        o(s());
        File v = v(this.c.getAbsolutePath());
        File y2 = y(this.c.getAbsolutePath());
        if (HintUtils.h(hint, SessionEnd.class) && !v.delete()) {
            this.a.getLogger().c(SentryLevel.WARNING, "Current envelope doesn't exist.", new Object[0]);
        }
        if (HintUtils.h(hint, AbnormalExit.class)) {
            C(hint);
        }
        if (HintUtils.h(hint, SessionStart.class)) {
            if (v.exists()) {
                this.a.getLogger().c(SentryLevel.WARNING, "Current session is not ended, we'd need to end it.", new Object[0]);
                try {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(v), CacheStrategy.e));
                    try {
                        Session session = (Session) this.b.c(bufferedReader, Session.class);
                        if (session != null) {
                            H(y2, session);
                        }
                        bufferedReader.close();
                    } finally {
                    }
                } catch (Throwable th) {
                    this.a.getLogger().b(SentryLevel.ERROR, "Error processing session.", th);
                }
            }
            D(v, sentryEnvelope);
            boolean exists = new File(this.a.getCacheDirPath(), ".sentry-native/last_crash").exists();
            if (!exists) {
                File file = new File(this.a.getCacheDirPath(), "last_crash");
                if (file.exists()) {
                    this.a.getLogger().c(SentryLevel.INFO, "Crash marker file exists, crashedLastRun will return true.", new Object[0]);
                    if (!file.delete()) {
                        this.a.getLogger().c(SentryLevel.ERROR, "Failed to delete the crash marker file. %s.", file.getAbsolutePath());
                    }
                    exists = true;
                }
            }
            SentryCrashLastRunState.a().b(exists);
            u();
        }
        File w = w(sentryEnvelope);
        if (w.exists()) {
            this.a.getLogger().c(SentryLevel.WARNING, "Not adding Envelope to offline storage because it already exists: %s", w.getAbsolutePath());
            return;
        }
        this.a.getLogger().c(SentryLevel.DEBUG, "Adding Envelope to offline storage: %s", w.getAbsolutePath());
        G(w, sentryEnvelope);
        if (HintUtils.h(hint, UncaughtExceptionHandlerIntegration.UncaughtExceptionHint.class)) {
            F();
        }
    }

    @Override
    public void i(@NotNull SentryEnvelope sentryEnvelope) {
        Objects.c(sentryEnvelope, "Envelope is required.");
        File w = w(sentryEnvelope);
        if (!w.exists()) {
            this.a.getLogger().c(SentryLevel.DEBUG, "Envelope was not cached: %s", w.getAbsolutePath());
            return;
        }
        this.a.getLogger().c(SentryLevel.DEBUG, "Discarding envelope from cache: %s", w.getAbsolutePath());
        if (w.delete()) {
            return;
        }
        this.a.getLogger().c(SentryLevel.ERROR, "Failed to delete envelope: %s", w.getAbsolutePath());
    }

    @Override
    @NotNull
    public Iterator<SentryEnvelope> iterator() {
        File[] s = s();
        ArrayList arrayList = new ArrayList(s.length);
        for (File file : s) {
            try {
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                try {
                    arrayList.add(this.b.d(bufferedInputStream));
                    bufferedInputStream.close();
                } catch (Throwable th) {
                    try {
                        bufferedInputStream.close();
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                    throw th;
                }
            } catch (FileNotFoundException unused) {
                this.a.getLogger().c(SentryLevel.DEBUG, "Envelope file '%s' disappeared while converting all cached files to envelopes.", file.getAbsolutePath());
            } catch (IOException e) {
                this.a.getLogger().b(SentryLevel.ERROR, String.format("Error while reading cached envelope from file %s", file.getAbsolutePath()), e);
            }
        }
        return arrayList.iterator();
    }

    public void u() {
        this.f.countDown();
    }
}