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 } // namespace | |
| 34 | |
| 35 namespace feedback { | |
| 36 | |
| 37 FeedbackReport::FeedbackReport(content::BrowserContext* context, | |
| 38 const base::Time& upload_at, | |
| 39 const std::string& data) | |
| 40 : upload_at_(upload_at), | |
| 41 data_(data) { | |
| 42 reports_path_ = GetFeedbackReportsPath(context); | |
| 43 if (reports_path_.empty()) | |
| 44 return; | |
| 45 file_ = reports_path_.AppendASCII( | |
| 46 kFeedbackReportFilenamePrefix + base::GenerateGUID()); | |
| 
 
Lei Zhang
2014/02/01 01:37:22
Consider using file_util::GetUniquePathNumber() in
 
rkc
2014/02/03 22:13:11
That would involve moving getting the filename ont
 
 | |
| 47 BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( | |
| 48 &FeedbackReport::WriteReportOnBlockingPool, this)); | |
| 49 } | |
| 50 | |
| 51 FeedbackReport::~FeedbackReport() {} | |
| 52 | |
| 53 void FeedbackReport::DeleteReportOnDisk() { | |
| 54 BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( | |
| 55 &FeedbackReport::DeleteReportOnBlockingPool, this)); | |
| 56 } | |
| 57 | |
| 58 void FeedbackReport::WriteReportOnBlockingPool() { | |
| 59 base::AutoLock locker(file_access_lock_); | |
| 60 if (file_.empty()) | |
| 61 return; | |
| 62 | |
| 63 if (!base::DirectoryExists(reports_path_)) { | |
| 64 base::File::Error error; | |
| 65 if (!base::CreateDirectoryAndGetError(reports_path_, &error)) | |
| 66 return; | |
| 67 } | |
| 68 base::ImportantFileWriter::WriteFileAtomically(file_, data_); | |
| 69 } | |
| 70 | |
| 71 void FeedbackReport::DeleteReportOnBlockingPool() { | |
| 72 base::AutoLock locker(file_access_lock_); | |
| 
 
Lei Zhang
2014/02/01 01:37:22
You shouldn't need a lock. Instead, get a Sequence
 
rkc
2014/02/03 22:13:11
Done.
 
 | |
| 73 DeleteFile(file_, false); | |
| 74 file_.clear(); | |
| 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 |