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.
5 #include "components/feedback/feedback_data.h"
8 #include "base/files/file_util.h"
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "components/feedback/feedback_util.h"
15 #include "components/feedback/tracing_manager.h"
16 #include "content/public/browser/browser_thread.h"
18 using content::BrowserThread
;
23 const char kTraceFilename
[] = "tracing.zip";
24 const char kPerformanceCategoryTag
[] = "Performance";
26 const base::FilePath::CharType kHistogramsFilename
[] =
27 FILE_PATH_LITERAL("histograms.txt");
29 const char kHistogramsAttachmentName
[] = "histograms.zip";
33 FeedbackData::FeedbackData()
34 : send_report_(base::Bind(&feedback_util::SendReport
)), context_(NULL
),
35 trace_id_(0), pending_op_count_(1), report_sent_(false) {}
37 FeedbackData::~FeedbackData() {
40 void FeedbackData::OnFeedbackPageDataComplete() {
45 void FeedbackData::SetAndCompressSystemInfo(
46 scoped_ptr
<FeedbackData::SystemLogsMap
> sys_info
) {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
50 TracingManager
* manager
= TracingManager::Get();
53 !manager
->GetTraceData(
55 base::Bind(&FeedbackData::OnGetTraceData
, this, trace_id_
))) {
63 AddLogs(sys_info
.Pass());
64 BrowserThread::PostBlockingPoolTaskAndReply(
66 base::Bind(&FeedbackCommon::CompressLogs
, this),
67 base::Bind(&FeedbackData::OnCompressComplete
, this));
71 void FeedbackData::SetAndCompressHistograms(
72 scoped_ptr
<std::string
> histograms
) {
73 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
75 if (!histograms
.get())
78 BrowserThread::PostBlockingPoolTaskAndReply(
80 base::Bind(&FeedbackCommon::CompressFile
,
82 base::FilePath(kHistogramsFilename
),
83 kHistogramsAttachmentName
,
84 base::Passed(&histograms
)),
85 base::Bind(&FeedbackData::OnCompressComplete
, this));
88 void FeedbackData::AttachAndCompressFileData(
89 scoped_ptr
<std::string
> attached_filedata
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 if (!attached_filedata
.get() || attached_filedata
->empty())
95 base::FilePath attached_file
=
96 base::FilePath::FromUTF8Unsafe(attached_filename_
);
97 BrowserThread::PostBlockingPoolTaskAndReply(
99 base::Bind(&FeedbackCommon::CompressFile
,
103 base::Passed(&attached_filedata
)),
104 base::Bind(&FeedbackData::OnCompressComplete
, this));
107 void FeedbackData::OnGetTraceData(
109 scoped_refptr
<base::RefCountedString
> trace_data
) {
110 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
111 TracingManager
* manager
= TracingManager::Get();
113 manager
->DiscardTraceData(trace_id
);
115 scoped_ptr
<std::string
> data(new std::string
);
116 data
->swap(trace_data
->data());
118 AddFile(kTraceFilename
, data
.Pass());
120 set_category_tag(kPerformanceCategoryTag
);
126 void FeedbackData::OnCompressComplete() {
127 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
132 bool FeedbackData::IsDataComplete() {
133 return pending_op_count_
== 0;
136 void FeedbackData::SendReport() {
137 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
138 if (IsDataComplete() && !report_sent_
) {
140 send_report_
.Run(this);
144 } // namespace feedback