Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 864f9abf authored by Samer Naoura's avatar Samer Naoura Committed by Mehmet Murat Sevim
Browse files

BugreportManager takes screenshot even though screenshotFd is null

Bug: 385709501
Fix: 385709501
Flag: android.os.bugreport_deferred_consent_screenshot_fix
Test: atest dumpstate_test
(cherry picked from https://partner-android-review.googlesource.com/q/commit:43f497f573e627e910da037a2a763e7fccec2679)
Merged-In: I6bccb0eb4a005df558ce17ee2d7e323c873c66ad
Change-Id: I6bccb0eb4a005df558ce17ee2d7e323c873c66ad
parent 91ae097a
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -3459,6 +3459,7 @@ Dumpstate::RunStatus Dumpstate::RunInternal(int32_t calling_uid,
    }

    MaybeTakeEarlyScreenshot();
    MaybeSavePlaceholderScreenshot();
    MaybeWaitForSnapshotSystemTrace(std::move(snapshot_system_trace));
    onUiIntensiveBugreportDumpsFinished(calling_uid);
    MaybeCheckUserConsent(calling_uid, calling_package);
@@ -3558,6 +3559,26 @@ void Dumpstate::MaybeTakeEarlyScreenshot() {
    TakeScreenshot();
}

void Dumpstate::MaybeSavePlaceholderScreenshot() {
    if (options_->do_screenshot) {
        // No need to save a placeholder screenshot if a real one will be taken.
        return;
    }
    if (!options_->is_consent_deferred) {
        return;
    }

    // When consent is deferred, a screenshot used to be taken even when one
    // was not requested. The screenshot is not taken any more but a placeholder
    // is saved for backwards compatibility.
    std::string path = ds.GetPath(ds.CalledByApi() ? "-png.tmp" : ".png");
    if (android::os::CopyFileToFile(DEFAULT_SCREENSHOT_PATH, path)) {
        MYLOGD("Saved fallback screenshot on %s\n", path.c_str());
    } else {
        MYLOGE("Failed to save fallback screenshot on %s\n", path.c_str());
    };
}

std::future<std::string> Dumpstate::MaybeSnapshotSystemTraceAsync() {
    // When capturing traces via bugreport handler (BH), this function will be invoked twice:
    // 1) When BH invokes IDumpstate::PreDumpUiData()
+1 −0
Original line number Diff line number Diff line
@@ -573,6 +573,7 @@ class Dumpstate {
    RunStatus dumpstate();

    void MaybeTakeEarlyScreenshot();
    void MaybeSavePlaceholderScreenshot();
    std::future<std::string> MaybeSnapshotSystemTraceAsync();
    void MaybeWaitForSnapshotSystemTrace(std::future<std::string> task);
    void MaybeSnapshotUiTraces();
+32 −0
Original line number Diff line number Diff line
@@ -1082,6 +1082,38 @@ TEST_F(ZippedBugReportStreamTest, DISABLED_StreamLimitedOnlyReport) {
    VerifyEntry(handle_, bugreport_txt_name, &entry);
}

TEST_F(ZippedBugReportStreamTest, ScreenShotFileCreated) {
    std::string out_path = kTestDataPath + "ScreenShotCapturedOut.zip";
    android::base::unique_fd out_fd;
    CreateFd(out_path, &out_fd);
    ds_.options_->limited_only = true;
    ds_.options_->stream_to_socket = true;
    ds_.options_->do_screenshot = false;
    ds_.options_->is_consent_deferred = true;
    RedirectOutputToFd(out_fd);

    GenerateBugreport();

    std::string screenshot = ds_.GetPath(ds_.CalledByApi() ? "-png.tmp" : ".png");
    EXPECT_TRUE(std::filesystem::exists(screenshot)) << screenshot << " was not created.";
}

TEST_F(ZippedBugReportStreamTest, ScreenShotFileIsNotCreated) {
    std::string out_path = kTestDataPath + "ScreenShotCapturedOut.zip";
    android::base::unique_fd out_fd;
    CreateFd(out_path, &out_fd);
    ds_.options_->limited_only = true;
    ds_.options_->stream_to_socket = true;
    ds_.options_->do_screenshot = false;
    ds_.options_->is_consent_deferred = false;
    RedirectOutputToFd(out_fd);

    GenerateBugreport();

    std::string screenshot = ds_.GetPath(ds_.CalledByApi() ? "-png.tmp" : ".png");
    EXPECT_FALSE(std::filesystem::exists(screenshot)) << screenshot << " was created.";
}

class ProgressTest : public DumpstateBaseTest {
  public:
    Progress GetInstance(int32_t max, double growth_factor, const std::string& path = "") {