summaryrefslogtreecommitdiff
path: root/components/feedback/feedback_report.cc
diff options
context:
space:
mode:
authorTorne (Richard Coles) <torne@google.com>2014-05-14 12:12:37 +0100
committerTorne (Richard Coles) <torne@google.com>2014-05-14 12:12:37 +0100
commit010d83a9304c5a91596085d917d248abff47903a (patch)
tree41ef1a01862f352f9653c7a9cfa817abefe2cce2 /components/feedback/feedback_report.cc
parent08c107de54178bb0990a09adec724924e8bc9486 (diff)
downloadchromium_org-010d83a9304c5a91596085d917d248abff47903a.tar.gz
Merge from Chromium at DEPS revision 269336
This commit was generated by merge_to_master.py. Change-Id: I8b9c77f10eccd2a8b4c7ce373ffda18568af54ff
Diffstat (limited to 'components/feedback/feedback_report.cc')
-rw-r--r--components/feedback/feedback_report.cc84
1 files changed, 84 insertions, 0 deletions
diff --git a/components/feedback/feedback_report.cc b/components/feedback/feedback_report.cc
new file mode 100644
index 0000000000..af48ea63f6
--- /dev/null
+++ b/components/feedback/feedback_report.cc
@@ -0,0 +1,84 @@
+// 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/file_util.h"
+#include "base/files/file.h"
+#include "base/files/file_enumerator.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"
+#include "base/threading/sequenced_worker_pool.h"
+
+namespace {
+
+const base::FilePath::CharType kFeedbackReportFilenameWildcard[] =
+ FILE_PATH_LITERAL("Feedback Report.*");
+
+const char kFeedbackReportFilenamePrefix[] = "Feedback Report.";
+
+void WriteReportOnBlockingPool(const base::FilePath reports_path,
+ const base::FilePath& file,
+ const std::string& data) {
+ 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, data);
+}
+
+} // namespace
+
+namespace feedback {
+
+FeedbackReport::FeedbackReport(
+ const base::FilePath& path,
+ const base::Time& upload_at,
+ const std::string& data,
+ scoped_refptr<base::SequencedTaskRunner> task_runner)
+ : reports_path_(path),
+ upload_at_(upload_at),
+ data_(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::Bind(
+ &WriteReportOnBlockingPool, reports_path_, file_, data_));
+}
+
+FeedbackReport::~FeedbackReport() {}
+
+void FeedbackReport::DeleteReportOnDisk() {
+ reports_task_runner_->PostTask(FROM_HERE, base::Bind(
+ base::IgnoreResult(&base::DeleteFile), file_, false));
+}
+
+// static
+void FeedbackReport::LoadReportsAndQueue(
+ const base::FilePath& user_dir, 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()) {
+ std::string data;
+ if (ReadFileToString(name, &data))
+ callback.Run(data);
+ base::DeleteFile(name, false);
+ }
+}
+
+} // namespace feedback