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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
// 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_util.h"
#include <sstream>
#include <string>
#include <vector>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/file_version_info.h"
#include "base/memory/singleton.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/feedback_private/feedback_private_api.h"
#include "chrome/browser/feedback/feedback_data.h"
#include "chrome/browser/metrics/variations/variations_http_header_provider.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/safe_browsing/safe_browsing_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/metrics/metrics_log_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_client.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "grit/theme_resources.h"
#include "net/base/load_flags.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
#include "third_party/icu/source/common/unicode/locid.h"
#include "third_party/zlib/google/zip.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"
#if defined(OS_CHROMEOS)
#include "ash/shell.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#endif
namespace {
void DispatchFeedback(Profile* profile, std::string* post_body, int64 delay);
GURL GetTargetTabUrl(int session_id, int index) {
Browser* browser = chrome::FindBrowserWithID(session_id);
// Sanity checks.
if (!browser || index >= browser->tab_strip_model()->count())
return GURL();
if (index >= 0) {
content::WebContents* target_tab =
browser->tab_strip_model()->GetWebContentsAt(index);
if (target_tab)
return target_tab->GetURL();
}
return GURL();
}
// URL to post bug reports to.
const char kFeedbackPostUrl[] =
"https://www.google.com/tools/feedback/chrome/__submit";
const char kProtBufMimeType[] = "application/x-protobuf";
const char kPngMimeType[] = "image/png";
const int kHttpPostSuccessNoContent = 204;
const int kHttpPostFailNoConnection = -1;
const int kHttpPostFailClientError = 400;
const int kHttpPostFailServerError = 500;
const int64 kInitialRetryDelay = 900000; // 15 minutes
const int64 kRetryDelayIncreaseFactor = 2;
const int64 kRetryDelayLimit = 14400000; // 4 hours
const char kArbitraryMimeType[] = "application/octet-stream";
const char kHistogramsAttachmentName[] = "histograms.zip";
const char kLogsAttachmentName[] = "system_logs.zip";
#if defined(OS_CHROMEOS)
const int kChromeOSProductId = 208;
#else
const int kChromeBrowserProductId = 237;
#endif
// Simple net::URLFetcherDelegate to clean up URLFetcher on completion.
class PostCleanup : public net::URLFetcherDelegate {
public:
PostCleanup(Profile* profile,
std::string* post_body,
int64 previous_delay) : profile_(profile),
post_body_(post_body),
previous_delay_(previous_delay) { }
// Overridden from net::URLFetcherDelegate.
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
protected:
virtual ~PostCleanup() {}
private:
Profile* profile_;
std::string* post_body_;
int64 previous_delay_;
DISALLOW_COPY_AND_ASSIGN(PostCleanup);
};
// Don't use the data parameter, instead use the pointer we pass into every
// post cleanup object - that pointer will be deleted and deleted only on a
// successful post to the feedback server.
void PostCleanup::OnURLFetchComplete(
const net::URLFetcher* source) {
std::stringstream error_stream;
int response_code = source->GetResponseCode();
if (response_code == kHttpPostSuccessNoContent) {
// We've sent our report, delete the report data
delete post_body_;
error_stream << "Success";
} else {
// Uh oh, feedback failed, send it off to retry
if (previous_delay_) {
if (previous_delay_ < kRetryDelayLimit)
previous_delay_ *= kRetryDelayIncreaseFactor;
} else {
previous_delay_ = kInitialRetryDelay;
}
DispatchFeedback(profile_, post_body_, previous_delay_);
// Process the error for debug output
if (response_code == kHttpPostFailNoConnection) {
error_stream << "No connection to server.";
} else if ((response_code > kHttpPostFailClientError) &&
(response_code < kHttpPostFailServerError)) {
error_stream << "Client error: HTTP response code " << response_code;
} else if (response_code > kHttpPostFailServerError) {
error_stream << "Server error: HTTP response code " << response_code;
} else {
error_stream << "Unknown error: HTTP response code " << response_code;
}
}
LOG(WARNING) << "FEEDBACK: Submission to feedback server (" <<
source->GetURL() << ") status: " << error_stream.str();
// Delete the URLFetcher.
delete source;
// And then delete ourselves.
delete this;
}
void SendFeedback(Profile* profile,
std::string* post_body,
int64 previous_delay) {
DCHECK(post_body);
GURL post_url;
if (CommandLine::ForCurrentProcess()->
HasSwitch(switches::kFeedbackServer))
post_url = GURL(CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kFeedbackServer));
else
post_url = GURL(kFeedbackPostUrl);
net::URLFetcher* fetcher = net::URLFetcher::Create(
post_url, net::URLFetcher::POST,
new PostCleanup(profile, post_body, previous_delay));
fetcher->SetRequestContext(profile->GetRequestContext());
fetcher->SetLoadFlags(
net::LOAD_DO_NOT_SAVE_COOKIES | net::LOAD_DO_NOT_SEND_COOKIES);
net::HttpRequestHeaders headers;
chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders(
fetcher->GetOriginalURL(), profile->IsOffTheRecord(), false, &headers);
fetcher->SetExtraRequestHeaders(headers.ToString());
fetcher->SetUploadData(std::string(kProtBufMimeType), *post_body);
fetcher->Start();
}
void DispatchFeedback(Profile* profile, std::string* post_body, int64 delay) {
DCHECK(post_body);
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&SendFeedback, profile, post_body, delay),
base::TimeDelta::FromMilliseconds(delay));
}
void AddFeedbackData(userfeedback::ExtensionSubmit* feedback_data,
const std::string& key, const std::string& value) {
// Don't bother with empty keys or values
if (key == "" || value == "") return;
// Create log_value object and add it to the web_data object
userfeedback::ProductSpecificData log_value;
log_value.set_key(key);
log_value.set_value(value);
userfeedback::WebData* web_data = feedback_data->mutable_web_data();
*(web_data->add_product_specific_data()) = log_value;
}
// Adds data as an attachment to feedback_data if the data is non-empty.
void AddAttachment(userfeedback::ExtensionSubmit* feedback_data,
const char* name,
std::string* data) {
if (data == NULL || data->empty())
return;
userfeedback::ProductSpecificBinaryData* attachment =
feedback_data->add_product_specific_binary_data();
attachment->set_mime_type(kArbitraryMimeType);
attachment->set_name(name);
attachment->set_data(*data);
}
} // namespace
namespace chrome {
const char kAppLauncherCategoryTag[] = "AppLauncher";
void ShowFeedbackPage(Browser* browser,
const std::string& description_template,
const std::string& category_tag) {
GURL page_url;
if (browser) {
page_url = GetTargetTabUrl(browser->session_id().id(),
browser->tab_strip_model()->active_index());
}
Profile* profile = NULL;
if (browser) {
profile = browser->profile();
} else {
profile = ProfileManager::GetLastUsedProfileAllowedByPolicy();
}
if (!profile) {
LOG(ERROR) << "Cannot invoke feedback: No profile found!";
return;
}
extensions::FeedbackPrivateAPI* api =
extensions::FeedbackPrivateAPI::GetFactoryInstance()->GetForProfile(
profile);
api->RequestFeedback(description_template,
category_tag,
page_url);
}
} // namespace chrome
namespace feedback_util {
void SendReport(scoped_refptr<FeedbackData> data) {
if (!data.get()) {
LOG(ERROR) << "SendReport called with NULL data!";
NOTREACHED();
return;
}
userfeedback::ExtensionSubmit feedback_data;
// Unused field, needs to be 0 though.
feedback_data.set_type_id(0);
userfeedback::CommonData* common_data = feedback_data.mutable_common_data();
// We're not using gaia ids, we're using the e-mail field instead.
common_data->set_gaia_id(0);
common_data->set_user_email(data->user_email());
common_data->set_description(data->description());
std::string chrome_locale = g_browser_process->GetApplicationLocale();
common_data->set_source_description_language(chrome_locale);
userfeedback::WebData* web_data = feedback_data.mutable_web_data();
web_data->set_url(data->page_url());
web_data->mutable_navigator()->set_user_agent(content::GetUserAgent(GURL()));
gfx::Rect screen_size;
if (data->sys_info()) {
for (FeedbackData::SystemLogsMap::const_iterator i =
data->sys_info()->begin(); i != data->sys_info()->end(); ++i) {
if (FeedbackData::BelowCompressionThreshold(i->second))
AddFeedbackData(&feedback_data, i->first, i->second);
}
AddAttachment(&feedback_data, kLogsAttachmentName, data->compressed_logs());
}
if (data->histograms()) {
AddAttachment(&feedback_data,
kHistogramsAttachmentName,
data->compressed_histograms());
}
if (!data->attached_filename().empty()) {
// We need to use the UTF8Unsafe methods here to accomodate Windows, which
// uses wide strings to store filepaths.
std::string name = base::FilePath::FromUTF8Unsafe(
data->attached_filename()).BaseName().AsUTF8Unsafe();
AddAttachment(&feedback_data, name.c_str(), data->attached_filedata());
}
// NOTE: Screenshot needs to be processed after system info since we'll get
// the screenshot dimensions from system info.
if (data->image() && data->image()->size()) {
userfeedback::PostedScreenshot screenshot;
screenshot.set_mime_type(kPngMimeType);
// Set that we 'have' dimensions of the screenshot. These dimensions are
// ignored by the server but are a 'required' field in the protobuf.
userfeedback::Dimensions dimensions;
dimensions.set_width(0.0);
dimensions.set_height(0.0);
*(screenshot.mutable_dimensions()) = dimensions;
screenshot.set_binary_content(*data->image());
*(feedback_data.mutable_screenshot()) = screenshot;
}
if (data->category_tag().size())
feedback_data.set_bucket(data->category_tag());
// Set whether we're reporting from ChromeOS or Chrome on another platform.
userfeedback::ChromeData chrome_data;
#if defined(OS_CHROMEOS)
chrome_data.set_chrome_platform(
userfeedback::ChromeData_ChromePlatform_CHROME_OS);
userfeedback::ChromeOsData chrome_os_data;
chrome_os_data.set_category(
userfeedback::ChromeOsData_ChromeOsCategory_OTHER);
*(chrome_data.mutable_chrome_os_data()) = chrome_os_data;
feedback_data.set_product_id(kChromeOSProductId);
#else
chrome_data.set_chrome_platform(
userfeedback::ChromeData_ChromePlatform_CHROME_BROWSER);
userfeedback::ChromeBrowserData chrome_browser_data;
chrome_browser_data.set_category(
userfeedback::ChromeBrowserData_ChromeBrowserCategory_OTHER);
*(chrome_data.mutable_chrome_browser_data()) = chrome_browser_data;
feedback_data.set_product_id(kChromeBrowserProductId);
#endif
*(feedback_data.mutable_chrome_data()) = chrome_data;
// This pointer will eventually get deleted by the PostCleanup class, after
// we've either managed to successfully upload the report or died trying.
std::string* post_body = new std::string;
feedback_data.SerializeToString(post_body);
DispatchFeedback(data->profile(), post_body, 0);
}
bool ZipString(const base::FilePath& filename,
const std::string& data, std::string* compressed_logs) {
base::FilePath temp_path;
// Create a temporary directory, put the logs into a file in it.
if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL(""), &temp_path))
return false;
base::FilePath temp_file = temp_path.Append(filename);
if (file_util::WriteFile(temp_file, data.c_str(), data.size()) == -1)
return false;
return ZipFile(temp_file, compressed_logs);
}
bool ZipFile(const base::FilePath& filename, std::string* compressed_logs) {
base::FilePath zip_file;
// Create a temporary file to receive the zip file in it.
if (!file_util::CreateTemporaryFile(&zip_file))
return false;
if (!zip::Zip(filename, zip_file, false))
return false;
if (!base::ReadFileToString(zip_file, compressed_logs))
return false;
base::DeleteFile(zip_file, false);
return true;
}
} // namespace feedback_util
|