| 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 "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "net/base/directory_lister.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 |
| 18 namespace { |
| 19 |
| 20 const base::FilePath::CharType kFeedbackReportPath[] = |
| 21 FILE_PATH_LITERAL("Feedback Reports"); |
| 22 const base::FilePath::CharType kFeedbackReportFilenameWildcard[] = |
| 23 FILE_PATH_LITERAL("Feedback Report.*"); |
| 24 |
| 25 const char kFeedbackReportFilenamePrefix[] = "Feedback Report."; |
| 26 |
| 27 base::FilePath GetFeedbackReportsPath(content::BrowserContext* context) { |
| 28 if (!context) |
| 29 return base::FilePath(); |
| 30 return context->GetPath().Append(kFeedbackReportPath); |
| 31 } |
| 32 |
| 33 void WriteReportOnBlockingPool(const base::FilePath reports_path, |
| 34 const base::FilePath& file, |
| 35 const std::string& data) { |
| 36 DCHECK(reports_path.IsParent(file)); |
| 37 if (!base::DirectoryExists(reports_path)) { |
| 38 base::File::Error error; |
| 39 if (!base::CreateDirectoryAndGetError(reports_path, &error)) |
| 40 return; |
| 41 } |
| 42 base::ImportantFileWriter::WriteFileAtomically(file, data); |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
| 47 namespace feedback { |
| 48 |
| 49 FeedbackReport::FeedbackReport(content::BrowserContext* context, |
| 50 const base::Time& upload_at, |
| 51 const std::string& data) |
| 52 : upload_at_(upload_at), |
| 53 data_(data) { |
| 54 reports_path_ = GetFeedbackReportsPath(context); |
| 55 if (reports_path_.empty()) |
| 56 return; |
| 57 file_ = reports_path_.AppendASCII( |
| 58 kFeedbackReportFilenamePrefix + base::GenerateGUID()); |
| 59 |
| 60 // Uses a SKIP_ON_SHUTDOWN file task runner because losing a couple |
| 61 // associations is better than blocking shutdown. |
| 62 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); |
| 63 reports_task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( |
| 64 pool->GetSequenceToken(), |
| 65 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
| 66 |
| 67 reports_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 68 &WriteReportOnBlockingPool, reports_path_, file_, data_)); |
| 69 } |
| 70 |
| 71 FeedbackReport::~FeedbackReport() {} |
| 72 |
| 73 void FeedbackReport::DeleteReportOnDisk() { |
| 74 reports_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 75 base::IgnoreResult(&base::DeleteFile), file_, false)); |
| 76 } |
| 77 |
| 78 // static |
| 79 void FeedbackReport::LoadReportsAndQueue( |
| 80 content::BrowserContext* context, QueueCallback callback) { |
| 81 base::FilePath reports_path = GetFeedbackReportsPath(context); |
| 82 if (reports_path.empty()) |
| 83 return; |
| 84 |
| 85 base::FileEnumerator enumerator(reports_path, |
| 86 false, |
| 87 base::FileEnumerator::FILES, |
| 88 kFeedbackReportFilenameWildcard); |
| 89 for (base::FilePath name = enumerator.Next(); |
| 90 !name.empty(); |
| 91 name = enumerator.Next()) { |
| 92 std::string data; |
| 93 if (ReadFileToString(name, &data)) |
| 94 BrowserThread::PostTask( |
| 95 BrowserThread::UI, FROM_HERE, base::Bind(callback, data)); |
| 96 base::DeleteFile(name, false); |
| 97 } |
| 98 } |
| 99 |
| 100 } // namespace feedback |
| OLD | NEW |