| 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 return context->GetPath().Append(kFeedbackReportPath); | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 namespace feedback { | |
| 34 | |
| 35 FeedbackReport::FeedbackReport(content::BrowserContext* context, | |
| 36 const base::Time& upload_at, | |
| 37 const std::string& data) | |
| 38 : context_(context), | |
| 39 upload_at_(upload_at), | |
| 40 data_(data) { | |
| 41 base::FilePath reports_path = GetFeedbackReportsPath(context); | |
| 42 if (reports_path.empty()) | |
| 43 return; | |
| 44 file_ = reports_path.AppendASCII( | |
| 45 kFeedbackReportFilenamePrefix + base::GenerateGUID()); | |
| 46 BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( | |
| 47 &FeedbackReport::WriteReportOnBlockingPool, this)); | |
| 48 } | |
| 49 | |
| 50 FeedbackReport::~FeedbackReport() {} | |
| 51 | |
| 52 void FeedbackReport::DeleteReportOnDisk() { | |
| 53 BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( | |
| 54 base::IgnoreResult(&base::DeleteFile), file_, false)); | |
| 55 file_.clear(); | |
| 56 } | |
| 57 | |
| 58 void FeedbackReport::WriteReportOnBlockingPool() { | |
| 59 base::FilePath reports_path = GetFeedbackReportsPath(context_); | |
| 60 if (!base::DirectoryExists(reports_path)) { | |
| 61 base::File::Error error; | |
| 62 if (!base::CreateDirectoryAndGetError(reports_path, &error)) | |
| 63 return; | |
| 64 } | |
| 65 if (!file_.empty()) | |
| 66 base::ImportantFileWriter::WriteFileAtomically(file_, data_); | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 void FeedbackReport::LoadReportsAndQueue( | |
| 71 content::BrowserContext* context, QueueCallback callback) { | |
| 72 base::FilePath reports_path = GetFeedbackReportsPath(context); | |
| 73 if (reports_path.empty()) | |
| 74 return; | |
| 75 | |
| 76 base::FileEnumerator enumerator(reports_path, | |
| 77 false, | |
| 78 base::FileEnumerator::FILES, | |
| 79 kFeedbackReportFilenameWildcard); | |
| 80 for (base::FilePath name = enumerator.Next(); | |
| 81 !name.empty(); | |
| 82 name = enumerator.Next()) { | |
| 83 std::string data; | |
| 84 if (ReadFileToString(name, &data)) | |
| 85 BrowserThread::PostTask( | |
| 86 BrowserThread::UI, FROM_HERE, base::Bind(callback, data)); | |
| 87 base::DeleteFile(name, false); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 } // namespace feedback | |
| OLD | NEW |