OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/feedback/feedback_report.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "base/files/file_enumerator.h" | |
9 #include "base/files/important_file_writer.h" | |
10 #include "base/guid.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/threading/sequenced_worker_pool.h" | |
13 #include "net/base/directory_lister.h" | |
14 | |
15 namespace { | |
16 | |
17 const base::FilePath::CharType kFeedbackReportFilenameWildcard[] = | |
18 FILE_PATH_LITERAL("Feedback Report.*"); | |
19 | |
20 const char kFeedbackReportFilenamePrefix[] = "Feedback Report."; | |
21 | |
22 void WriteReportOnBlockingPool(const base::FilePath reports_path, | |
23 const base::FilePath& file, | |
24 const std::string& data) { | |
25 DCHECK(reports_path.IsParent(file)); | |
26 if (!base::DirectoryExists(reports_path)) { | |
27 base::File::Error error; | |
28 if (!base::CreateDirectoryAndGetError(reports_path, &error)) | |
29 return; | |
30 } | |
31 base::ImportantFileWriter::WriteFileAtomically(file, data); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 namespace feedback { | |
37 | |
38 FeedbackReport::FeedbackReport( | |
39 const base::FilePath& path, | |
40 const base::Time& upload_at, | |
41 const std::string& data, | |
42 scoped_refptr<base::SequencedTaskRunner> task_runner) | |
43 : reports_path_(path), | |
44 upload_at_(upload_at), | |
45 data_(data), | |
46 reports_task_runner_(task_runner) { | |
47 if (reports_path_.empty()) | |
48 return; | |
49 file_ = reports_path_.AppendASCII( | |
50 kFeedbackReportFilenamePrefix + base::GenerateGUID()); | |
51 | |
52 reports_task_runner_->PostTask(FROM_HERE, base::Bind( | |
53 &WriteReportOnBlockingPool, reports_path_, file_, data_)); | |
54 } | |
55 | |
56 FeedbackReport::~FeedbackReport() {} | |
57 | |
58 void FeedbackReport::DeleteReportOnDisk() { | |
59 reports_task_runner_->PostTask(FROM_HERE, base::Bind( | |
60 base::IgnoreResult(&base::DeleteFile), file_, false)); | |
61 } | |
62 | |
63 // static | |
64 void FeedbackReport::LoadReportsAndQueue( | |
65 const base::FilePath& user_dir, QueueCallback callback) { | |
66 if (user_dir.empty()) | |
67 return; | |
68 | |
69 base::FileEnumerator enumerator(user_dir, | |
70 false, | |
71 base::FileEnumerator::FILES, | |
72 kFeedbackReportFilenameWildcard); | |
73 for (base::FilePath name = enumerator.Next(); | |
74 !name.empty(); | |
75 name = enumerator.Next()) { | |
76 std::string data; | |
77 if (ReadFileToString(name, &data)) | |
78 callback.Run(data); | |
79 base::DeleteFile(name, false); | |
80 } | |
81 } | |
82 | |
83 } // namespace feedback | |
OLD | NEW |