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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // 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.
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/strings/string_number_conversions.h"
11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "net/base/directory_lister.h"
14
15 using content::BrowserThread;
16
17 namespace {
18
19 const base::FilePath::CharType kFeedbackReportPath[] =
20 FILE_PATH_LITERAL("Feedback Reports");
21 const base::FilePath::CharType kFeedbackReportFilenameWildcard[] =
22 FILE_PATH_LITERAL("Feedback Report.*");
23
24 const char kFeedbackReportFilenamePrefix[] = "Feedback Report.";
25
26 base::FilePath GetFeedbackReportsPath(content::BrowserContext* context) {
27 return context->GetPath().Append(kFeedbackReportPath);
28 }
29
30 // We need this function to post a task to delete file (which returns a bool
31 // causing Bind to fail compile).
32 void DeleteFileNoReturn(base::FilePath file, bool recursive) {
33 base::DeleteFile(file, recursive);
34 }
35
36 } // namespace
37
38 namespace feedback {
39
40 FeedbackReport::FeedbackReport(
41 content::BrowserContext* context,
42 const base::Time& upload_at,
43 const std::string& data) : context_(context),
44 upload_at_(upload_at),
Zachary Kuznia 2014/01/23 05:21:01 nit: indent
rkc 2014/01/23 21:48:00 Done.
45 data_(data) {
46 base::FilePath reports_path = GetFeedbackReportsPath(context);
47 if (reports_path.empty())
48 return;
49
50 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,
51 file_ = reports_path.AppendASCII(
52 kFeedbackReportFilenamePrefix + upload_time);
53 content::BrowserThread::PostBlockingPoolTask(
54 FROM_HERE, base::Bind(
55 &FeedbackReport::WriteReportOnBlockingPool, this));
56 }
57
58 FeedbackReport::~FeedbackReport() {}
59
60 void FeedbackReport::DeleteReportOnDisk() {
61 content::BrowserThread::PostBlockingPoolTask(
62 FROM_HERE, base::Bind(
63 &DeleteFileNoReturn, file_, false));
64 file_.clear();
65 }
66
67 void FeedbackReport::WriteReportOnBlockingPool() {
68 base::FilePath reports_path = GetFeedbackReportsPath(context_);
69 if (!base::DirectoryExists(reports_path)) {
70 base::File::Error error;
71 if (!base::CreateDirectoryAndGetError(reports_path, &error))
72 return;
73 }
74 if (!file_.empty())
75 base::ImportantFileWriter::WriteFileAtomically(file_, data_);
76 }
77
78 // static
79 void FeedbackReport::LoadReportsAndQueue(
80 content::BrowserContext* context, QueueCallback callback) {
81 base::FilePath reports_path = GetFeedbackReportsPath(context);
82 if (reports_path.empty())
83 return;
84
85 base::FileEnumerator enumerator(reports_path,
86 false,
87 base::FileEnumerator::FILES,
88 kFeedbackReportFilenameWildcard);
89 for (base::FilePath name = enumerator.Next();
90 !name.empty();
Zachary Kuznia 2014/01/23 05:21:01 nit: indent.
rkc 2014/01/23 21:48:00 Done.
91 name = enumerator.Next()) {
92 std::string data;
93 if (ReadFileToString(name, &data)) {
94 content::BrowserThread::PostTask(
95 BrowserThread::UI,
96 FROM_HERE,
97 base::Bind(callback, data));
98 }
99 base::DeleteFile(name, false);
100 }
101 }
102
103 } // namespace feedback
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698