Chromium Code Reviews| 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) { | |
|
Lei Zhang
2014/02/03 22:26:51
I'd DCHECK(reports_path.IsParent(file));
rkc
2014/02/03 22:41:27
Done.
| |
| 36 if (!base::DirectoryExists(reports_path)) { | |
| 37 base::File::Error error; | |
| 38 if (!base::CreateDirectoryAndGetError(reports_path, &error)) | |
| 39 return; | |
| 40 } | |
| 41 base::ImportantFileWriter::WriteFileAtomically(file, data); | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 namespace feedback { | |
| 47 | |
| 48 FeedbackReport::FeedbackReport(content::BrowserContext* context, | |
| 49 const base::Time& upload_at, | |
| 50 const std::string& data) | |
| 51 : upload_at_(upload_at), | |
| 52 data_(data) { | |
| 53 reports_path_ = GetFeedbackReportsPath(context); | |
| 54 if (reports_path_.empty()) | |
| 55 return; | |
| 56 file_ = reports_path_.AppendASCII( | |
| 57 kFeedbackReportFilenamePrefix + base::GenerateGUID()); | |
| 58 | |
| 59 // Uses a SKIP_ON_SHUTDOWN file task runner because losing a couple | |
| 60 // associations is better than blocking shutdown. | |
| 61 base::SequencedWorkerPool* pool = BrowserThread::GetBlockingPool(); | |
| 62 reports_task_runner_ = pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
| 63 pool->GetSequenceToken(), | |
| 64 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | |
| 65 | |
| 66 reports_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 67 &WriteReportOnBlockingPool, reports_path_, file_, data_)); | |
| 68 } | |
| 69 | |
| 70 FeedbackReport::~FeedbackReport() {} | |
| 71 | |
| 72 void FeedbackReport::DeleteReportOnDisk() { | |
| 73 reports_task_runner_->PostTask(FROM_HERE, base::Bind( | |
| 74 base::IgnoreResult(&base::DeleteFile), file_, false)); | |
| 75 } | |
| 76 | |
| 77 // static | |
| 78 void FeedbackReport::LoadReportsAndQueue( | |
| 79 content::BrowserContext* context, QueueCallback callback) { | |
| 80 base::FilePath reports_path = GetFeedbackReportsPath(context); | |
| 81 if (reports_path.empty()) | |
| 82 return; | |
| 83 | |
| 84 base::FileEnumerator enumerator(reports_path, | |
| 85 false, | |
| 86 base::FileEnumerator::FILES, | |
| 87 kFeedbackReportFilenameWildcard); | |
| 88 for (base::FilePath name = enumerator.Next(); | |
| 89 !name.empty(); | |
| 90 name = enumerator.Next()) { | |
| 91 std::string data; | |
| 92 if (ReadFileToString(name, &data)) | |
| 93 BrowserThread::PostTask( | |
| 94 BrowserThread::UI, FROM_HERE, base::Bind(callback, data)); | |
| 95 base::DeleteFile(name, false); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 } // namespace feedback | |
| OLD | NEW |