summaryrefslogtreecommitdiff
path: root/chromium/components/feedback/feedback_report.cc
blob: ced788fedf0dfe1247adc53cf2439700558c034a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/feedback/feedback_report.h"

#include "base/files/file.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/guid.h"
#include "base/sequenced_task_runner.h"
#include "base/strings/string_number_conversions.h"

namespace feedback {

namespace {

constexpr base::FilePath::CharType kFeedbackReportFilenameWildcard[] =
    FILE_PATH_LITERAL("Feedback Report.*");

constexpr char kFeedbackReportFilenamePrefix[] = "Feedback Report.";

void WriteReportOnBlockingPool(const base::FilePath reports_path,
                               const base::FilePath& file,
                               scoped_refptr<FeedbackReport> report) {
  DCHECK(reports_path.IsParent(file));
  if (!base::DirectoryExists(reports_path)) {
    base::File::Error error;
    if (!base::CreateDirectoryAndGetError(reports_path, &error))
      return;
  }
  base::ImportantFileWriter::WriteFileAtomically(file, report->data(),
                                                 "FeedbackReport");
}

}  // namespace

FeedbackReport::FeedbackReport(
    const base::FilePath& path,
    const base::Time& upload_at,
    std::unique_ptr<std::string> data,
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : reports_path_(path),
      upload_at_(upload_at),
      data_(std::move(data)),
      reports_task_runner_(task_runner) {
  if (reports_path_.empty())
    return;
  file_ = reports_path_.AppendASCII(
      kFeedbackReportFilenamePrefix + base::GenerateGUID());

  reports_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&WriteReportOnBlockingPool, reports_path_, file_,
                     base::WrapRefCounted<FeedbackReport>(this)));
}

// static
const char FeedbackReport::kCrashReportIdsKey[]  = "crash_report_ids";

// static
void FeedbackReport::LoadReportsAndQueue(const base::FilePath& user_dir,
                                         const QueueCallback& callback) {
  if (user_dir.empty())
    return;

  base::FileEnumerator enumerator(user_dir,
                                  false,
                                  base::FileEnumerator::FILES,
                                  kFeedbackReportFilenameWildcard);
  for (base::FilePath name = enumerator.Next();
       !name.empty();
       name = enumerator.Next()) {
    auto data = std::make_unique<std::string>();
    if (ReadFileToString(name, data.get()))
      callback.Run(std::move(data));
    base::DeleteFile(name, false);
  }
}

void FeedbackReport::DeleteReportOnDisk() {
  reports_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(base::IgnoreResult(&base::DeleteFile), file_, false));
}

FeedbackReport::~FeedbackReport() {}

}  // namespace feedback