Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3506)

Unified Diff: chrome/browser/feedback/feedback_report.cc

Issue 141433011: Cache feedback reports to disk in case of send failure. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..b98ff44b2aecc16b6b34fbb59cb1b3c88d2ce114
--- /dev/null
+++ b/chrome/browser/feedback/feedback_report.cc
@@ -0,0 +1,103 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
Zachary Kuznia 2014/01/23 05:21:01 2014
rkc 2014/01/23 21:48:00 Done.
+// 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/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) {
+ return context->GetPath().Append(kFeedbackReportPath);
+}
+
+// We need this function to post a task to delete file (which returns a bool
+// causing Bind to fail compile).
+void DeleteFileNoReturn(base::FilePath file, bool recursive) {
+ base::DeleteFile(file, recursive);
+}
+
+} // namespace
+
+namespace feedback {
+
+FeedbackReport::FeedbackReport(
+ content::BrowserContext* context,
+ const base::Time& upload_at,
+ const std::string& data) : context_(context),
+ upload_at_(upload_at),
Zachary Kuznia 2014/01/23 05:21:01 nit: indent
rkc 2014/01/23 21:48:00 Done.
+ data_(data) {
+ base::FilePath reports_path = GetFeedbackReportsPath(context);
+ if (reports_path.empty())
+ return;
+
+ std::string upload_time = base::Int64ToString(upload_at.ToInternalValue());
Zachary Kuznia 2014/01/23 05:21:01 Is there a reason to use the upload time, like to
rkc 2014/01/23 21:48:00 It's unique and it can help in diagnosing issues,
+ file_ = reports_path.AppendASCII(
+ kFeedbackReportFilenamePrefix + upload_time);
+ content::BrowserThread::PostBlockingPoolTask(
+ FROM_HERE, base::Bind(
+ &FeedbackReport::WriteReportOnBlockingPool, this));
+}
+
+FeedbackReport::~FeedbackReport() {}
+
+void FeedbackReport::DeleteReportOnDisk() {
+ content::BrowserThread::PostBlockingPoolTask(
+ FROM_HERE, base::Bind(
+ &DeleteFileNoReturn, file_, false));
+ file_.clear();
+}
+
+void FeedbackReport::WriteReportOnBlockingPool() {
+ base::FilePath reports_path = GetFeedbackReportsPath(context_);
+ if (!base::DirectoryExists(reports_path)) {
+ base::File::Error error;
+ if (!base::CreateDirectoryAndGetError(reports_path, &error))
+ return;
+ }
+ if (!file_.empty())
+ base::ImportantFileWriter::WriteFileAtomically(file_, data_);
+}
+
+// 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();
Zachary Kuznia 2014/01/23 05:21:01 nit: indent.
rkc 2014/01/23 21:48:00 Done.
+ name = enumerator.Next()) {
+ std::string data;
+ if (ReadFileToString(name, &data)) {
+ content::BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(callback, data));
+ }
+ base::DeleteFile(name, false);
+ }
+}
+
+} // namespace feedback

Powered by Google App Engine
This is Rietveld 408576698