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

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 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 // We need this function to post a task to delete file (which returns a bool
Lei Zhang 2014/01/23 22:28:41 I think the correct way to do this is with base::I
rkc 2014/01/23 22:57:21 Ah, I knew there had to be a better way to do this
32 // causing Bind to fail compile).
33 void DeleteFileNoReturn(base::FilePath file, bool recursive) {
34 base::DeleteFile(file, recursive);
35 }
36
37 } // namespace
38
39 namespace feedback {
40
41 FeedbackReport::FeedbackReport(
42 content::BrowserContext* context,
Lei Zhang 2014/01/23 22:28:41 nit: this fits on the previous line; align the oth
rkc 2014/01/23 22:57:21 Done.
43 const base::Time& upload_at,
44 const std::string& data) : context_(context),
45 upload_at_(upload_at),
46 data_(data) {
47 base::FilePath reports_path = GetFeedbackReportsPath(context);
48 if (reports_path.empty())
49 return;
50 file_ = reports_path.AppendASCII(
51 kFeedbackReportFilenamePrefix + base::GenerateGUID());
52 content::BrowserThread::PostBlockingPoolTask(
Lei Zhang 2014/01/23 22:28:41 since you already declared "using content::Browser
rkc 2014/01/23 22:57:21 Done.
53 FROM_HERE, base::Bind(
54 &FeedbackReport::WriteReportOnBlockingPool, this));
55 }
56
57 FeedbackReport::~FeedbackReport() {}
58
59 void FeedbackReport::DeleteReportOnDisk() {
60 content::BrowserThread::PostBlockingPoolTask(
61 FROM_HERE, base::Bind(
62 &DeleteFileNoReturn, file_, false));
63 file_.clear();
64 }
65
66 void FeedbackReport::WriteReportOnBlockingPool() {
67 base::FilePath reports_path = GetFeedbackReportsPath(context_);
68 if (!base::DirectoryExists(reports_path)) {
69 base::File::Error error;
70 if (!base::CreateDirectoryAndGetError(reports_path, &error))
71 return;
72 }
73 if (!file_.empty())
74 base::ImportantFileWriter::WriteFileAtomically(file_, data_);
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 content::BrowserThread::PostTask(
94 BrowserThread::UI,
95 FROM_HERE,
96 base::Bind(callback, data));
97 }
98 base::DeleteFile(name, false);
99 }
100 }
101
102 } // namespace feedback
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698