summaryrefslogtreecommitdiffstats
path: root/chrome/browser/feedback/feedback_data.cc
blob: 365fbbe788c2c389ab9a58817153c2a7bb2db661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2012 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_data.h"

#include "base/file_util.h"
#include "base/json/json_string_value_serializer.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/feedback/feedback_util.h"
#include "chrome/browser/feedback/tracing_manager.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"

#if defined(USE_ASH)
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#endif

using content::BrowserThread;

namespace {

const char kMultilineIndicatorString[] = "<multiline>\n";
const char kMultilineStartString[] = "---------- START ----------\n";
const char kMultilineEndString[] = "---------- END ----------\n\n";

const size_t kFeedbackMaxLength = 4 * 1024;
const size_t kFeedbackMaxLineCount = 40;

const char kTraceFilename[] = "tracing.zip\n";
const char kPerformanceCategoryTag[] = "Performance";

const char kZipExt[] = ".zip";

const base::FilePath::CharType kLogsFilename[] =
    FILE_PATH_LITERAL("system_logs.txt");
const base::FilePath::CharType kHistogramsFilename[] =
    FILE_PATH_LITERAL("histograms.txt");

// Converts the system logs into a string that we can compress and send
// with the report. This method only converts those logs that we want in
// the compressed zip file sent with the report, hence it ignores any logs
// below the size threshold of what we want compressed.
std::string LogsToString(const FeedbackData::SystemLogsMap& sys_info) {
  std::string syslogs_string;
  for (FeedbackData::SystemLogsMap::const_iterator it = sys_info.begin();
      it != sys_info.end(); ++it) {
    std::string key = it->first;
    std::string value = it->second;

    if (FeedbackData::BelowCompressionThreshold(value))
      continue;

    TrimString(key, "\n ", &key);
    TrimString(value, "\n ", &value);

    if (value.find("\n") != std::string::npos) {
      syslogs_string.append(
          key + "=" + kMultilineIndicatorString +
          kMultilineStartString +
          value + "\n" +
          kMultilineEndString);
    } else {
      syslogs_string.append(key + "=" + value + "\n");
    }
  }
  return syslogs_string;
}

void ZipFile(const base::FilePath& filename,
             const std::string& data, std::string* compressed_data) {
  if (!feedback_util::ZipString(filename, data, compressed_data))
    compressed_data->clear();
}

void ZipLogs(const FeedbackData::SystemLogsMap& sys_info,
             std::string* compressed_logs) {
  DCHECK(compressed_logs);
  std::string logs_string = LogsToString(sys_info);
  if (logs_string.empty() ||
      !feedback_util::ZipString(
          base::FilePath(kLogsFilename), logs_string, compressed_logs)) {
    compressed_logs->clear();
  }
}

void ZipHistograms(const std::string& histograms,
                   std::string* compressed_histograms) {
  DCHECK(compressed_histograms);
  if (histograms.empty() ||
      !feedback_util::ZipString(
          base::FilePath(kHistogramsFilename),
          histograms,
          compressed_histograms)) {
    compressed_histograms->clear();
  }
}

}  // namespace

// static
bool FeedbackData::BelowCompressionThreshold(const std::string& content) {
  if (content.length() > kFeedbackMaxLength)
    return false;
  const size_t line_count = std::count(content.begin(), content.end(), '\n');
  if (line_count > kFeedbackMaxLineCount)
    return false;
  return true;
}

FeedbackData::FeedbackData() : profile_(NULL),
                               trace_id_(0),
                               feedback_page_data_complete_(false),
                               syslogs_compression_complete_(false),
                               histograms_compression_complete_(false),
                               attached_file_compression_complete_(false),
                               report_sent_(false) {
}

FeedbackData::~FeedbackData() {
}

void FeedbackData::OnFeedbackPageDataComplete() {
  feedback_page_data_complete_ = true;
  SendReport();
}

void FeedbackData::SetAndCompressSystemInfo(
    scoped_ptr<FeedbackData::SystemLogsMap> sys_info) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (trace_id_ != 0) {
    TracingManager* manager = TracingManager::Get();
    if (!manager ||
        !manager->GetTraceData(
            trace_id_,
            base::Bind(&FeedbackData::OnGetTraceData, this, trace_id_))) {
      trace_id_ = 0;
    }
  }

  sys_info_ = sys_info.Pass();
  if (sys_info_.get()) {
    std::string* compressed_logs_ptr = new std::string;
    scoped_ptr<std::string> compressed_logs(compressed_logs_ptr);
    BrowserThread::PostBlockingPoolTaskAndReply(
        FROM_HERE,
        base::Bind(&ZipLogs,
                   *sys_info_,
                   compressed_logs_ptr),
        base::Bind(&FeedbackData::OnCompressLogsComplete,
                   this,
                   base::Passed(&compressed_logs)));
  }
}

void FeedbackData::SetAndCompressHistograms(
    scoped_ptr<std::string> histograms) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  histograms_ = histograms.Pass();
  if (histograms_.get()) {
    std::string* compressed_histograms_ptr = new std::string;
    scoped_ptr<std::string> compressed_histograms(compressed_histograms_ptr);
    BrowserThread::PostBlockingPoolTaskAndReply(
        FROM_HERE,
        base::Bind(&ZipHistograms,
                   *histograms_,
                   compressed_histograms_ptr),
        base::Bind(&FeedbackData::OnCompressHistogramsComplete,
                   this,
                   base::Passed(&compressed_histograms)));
  }
}

void FeedbackData::AttachAndCompressFileData(
    scoped_ptr<std::string> attached_filedata) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  attached_filedata_ = attached_filedata.Pass();

  if (!attached_filename_.empty() && attached_filedata_.get()) {
    std::string* compressed_file_ptr = new std::string;
    scoped_ptr<std::string> compressed_file(compressed_file_ptr);
#if defined(OS_WIN)
    base::FilePath attached_file(base::UTF8ToWide(attached_filename_));
#else
    base::FilePath attached_file(attached_filename_);
#endif
    BrowserThread::PostBlockingPoolTaskAndReply(
        FROM_HERE,
        base::Bind(&ZipFile,
                   attached_file,
                   *(attached_filedata_.get()),
                   compressed_file_ptr),
        base::Bind(&FeedbackData::OnCompressFileComplete,
                   this,
                   base::Passed(&compressed_file)));
  }
}

void FeedbackData::OnGetTraceData(
    int trace_id,
    scoped_refptr<base::RefCountedString> trace_data) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  TracingManager* manager = TracingManager::Get();
  if (manager)
    manager->DiscardTraceData(trace_id);

  scoped_ptr<std::string> data(new std::string);
  data->swap(trace_data->data());

  attached_filename_ = kTraceFilename;
  attached_filedata_ = data.Pass();
  attached_file_compression_complete_ = true;
  trace_id_ = 0;

  set_category_tag(kPerformanceCategoryTag);

  SendReport();
}

void FeedbackData::OnCompressLogsComplete(
    scoped_ptr<std::string> compressed_logs) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  compressed_logs_ = compressed_logs.Pass();
  syslogs_compression_complete_ = true;

  SendReport();
}

void FeedbackData::OnCompressHistogramsComplete(
    scoped_ptr<std::string> compressed_histograms) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  compressed_histograms_ = compressed_histograms.Pass();
  histograms_compression_complete_ = true;

  SendReport();
}

void FeedbackData::OnCompressFileComplete(
    scoped_ptr<std::string> compressed_file) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  if (compressed_file.get()) {
    attached_filedata_ = compressed_file.Pass();
    attached_filename_.append(kZipExt);
    attached_file_compression_complete_ = true;
  } else {
    attached_filename_.clear();
    attached_filedata_.reset(NULL);
  }

  SendReport();
}

bool FeedbackData::IsDataComplete() {
  return (!sys_info_.get() || syslogs_compression_complete_) &&
      (!histograms_.get() || histograms_compression_complete_) &&
      (!attached_filedata_.get() || attached_file_compression_complete_) &&
      !trace_id_ &&
      feedback_page_data_complete_;
}

void FeedbackData::SendReport() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (IsDataComplete() && !report_sent_) {
    report_sent_ = true;
    feedback_util::SendReport(this);
  }
}