Chromium Code Reviews| Index: chrome/browser/feedback/feedback_report.cc |
| diff --git a/chrome/browser/feedback/feedback_report.cc b/chrome/browser/feedback/feedback_report.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0a0077974e876b6ded0bc8c9127474687a6c10c7 |
| --- /dev/null |
| +++ b/chrome/browser/feedback/feedback_report.cc |
| @@ -0,0 +1,99 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/feedback/feedback_report.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/files/file_enumerator.h" |
| +#include "base/files/important_file_writer.h" |
| +#include "base/guid.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/base/directory_lister.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +const base::FilePath::CharType kFeedbackReportPath[] = |
| + FILE_PATH_LITERAL("Feedback Reports"); |
| +const base::FilePath::CharType kFeedbackReportFilenameWildcard[] = |
| + FILE_PATH_LITERAL("Feedback Report.*"); |
| + |
| +const char kFeedbackReportFilenamePrefix[] = "Feedback Report."; |
| + |
| +base::FilePath GetFeedbackReportsPath(content::BrowserContext* context) { |
| + if (!context) |
| + return base::FilePath(); |
| + return context->GetPath().Append(kFeedbackReportPath); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace feedback { |
| + |
| +FeedbackReport::FeedbackReport(content::BrowserContext* context, |
| + const base::Time& upload_at, |
| + const std::string& data) |
| + : upload_at_(upload_at), |
| + data_(data) { |
| + reports_path_ = GetFeedbackReportsPath(context); |
| + if (reports_path_.empty()) |
| + return; |
| + file_ = reports_path_.AppendASCII( |
| + 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
|
| + BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( |
| + &FeedbackReport::WriteReportOnBlockingPool, this)); |
| +} |
| + |
| +FeedbackReport::~FeedbackReport() {} |
| + |
| +void FeedbackReport::DeleteReportOnDisk() { |
| + BrowserThread::PostBlockingPoolTask(FROM_HERE, base::Bind( |
| + &FeedbackReport::DeleteReportOnBlockingPool, this)); |
| +} |
| + |
| +void FeedbackReport::WriteReportOnBlockingPool() { |
| + base::AutoLock locker(file_access_lock_); |
| + if (file_.empty()) |
| + return; |
| + |
| + if (!base::DirectoryExists(reports_path_)) { |
| + base::File::Error error; |
| + if (!base::CreateDirectoryAndGetError(reports_path_, &error)) |
| + return; |
| + } |
| + base::ImportantFileWriter::WriteFileAtomically(file_, data_); |
| +} |
| + |
| +void FeedbackReport::DeleteReportOnBlockingPool() { |
| + 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.
|
| + DeleteFile(file_, false); |
| + file_.clear(); |
| +} |
| + |
| +// static |
| +void FeedbackReport::LoadReportsAndQueue( |
| + content::BrowserContext* context, QueueCallback callback) { |
| + base::FilePath reports_path = GetFeedbackReportsPath(context); |
| + if (reports_path.empty()) |
| + return; |
| + |
| + base::FileEnumerator enumerator(reports_path, |
| + false, |
| + base::FileEnumerator::FILES, |
| + kFeedbackReportFilenameWildcard); |
| + for (base::FilePath name = enumerator.Next(); |
| + !name.empty(); |
| + name = enumerator.Next()) { |
| + std::string data; |
| + if (ReadFileToString(name, &data)) |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, base::Bind(callback, data)); |
| + base::DeleteFile(name, false); |
| + } |
| +} |
| + |
| +} // namespace feedback |