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 "components/feedback/feedback_util.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/files/file_util.h"
11 #include "components/feedback/feedback_data.h"
12 #include "components/feedback/feedback_uploader.h"
13 #include "components/feedback/feedback_uploader_factory.h"
14 #include "components/feedback/proto/common.pb.h"
15 #include "components/feedback/proto/dom.pb.h"
16 #include "components/feedback/proto/extension.pb.h"
17 #include "components/feedback/proto/math.pb.h"
18 #include "third_party/zlib/google/zip.h"
19
20 using feedback::FeedbackData;
21
22 namespace {
23
24 #if defined(OS_CHROMEOS)
25 const int kChromeOSProductId = 208;
26 #else
27 const int kChromeBrowserProductId = 237;
28 #endif
29
30 } // namespace
31
32 namespace feedback_util {
33
SendReport(scoped_refptr<FeedbackData> data)34 void SendReport(scoped_refptr<FeedbackData> data) {
35 if (!data.get()) {
36 LOG(ERROR) << "SendReport called with NULL data!";
37 NOTREACHED();
38 return;
39 }
40
41 userfeedback::ExtensionSubmit feedback_data;
42 data->PrepareReport(&feedback_data);
43
44 // Set whether we're reporting from ChromeOS or Chrome on another platform.
45 userfeedback::ChromeData chrome_data;
46 #if defined(OS_CHROMEOS)
47 chrome_data.set_chrome_platform(
48 userfeedback::ChromeData_ChromePlatform_CHROME_OS);
49 userfeedback::ChromeOsData chrome_os_data;
50 chrome_os_data.set_category(
51 userfeedback::ChromeOsData_ChromeOsCategory_OTHER);
52 *(chrome_data.mutable_chrome_os_data()) = chrome_os_data;
53 feedback_data.set_product_id(kChromeOSProductId);
54 #else
55 chrome_data.set_chrome_platform(
56 userfeedback::ChromeData_ChromePlatform_CHROME_BROWSER);
57 userfeedback::ChromeBrowserData chrome_browser_data;
58 chrome_browser_data.set_category(
59 userfeedback::ChromeBrowserData_ChromeBrowserCategory_OTHER);
60 *(chrome_data.mutable_chrome_browser_data()) = chrome_browser_data;
61 feedback_data.set_product_id(kChromeBrowserProductId);
62 #endif
63
64 *(feedback_data.mutable_chrome_data()) = chrome_data;
65
66 // This pointer will eventually get deleted by the PostCleanup class, after
67 // we've either managed to successfully upload the report or died trying.
68 std::string post_body;
69 feedback_data.SerializeToString(&post_body);
70
71 feedback::FeedbackUploader *uploader =
72 feedback::FeedbackUploaderFactory::GetForBrowserContext(data->context());
73 uploader->QueueReport(post_body);
74 }
75
ZipString(const base::FilePath & filename,const std::string & data,std::string * compressed_logs)76 bool ZipString(const base::FilePath& filename,
77 const std::string& data, std::string* compressed_logs) {
78 base::FilePath temp_path;
79 base::FilePath zip_file;
80
81 // Create a temporary directory, put the logs into a file in it. Create
82 // another temporary file to receive the zip file in.
83 if (!base::CreateNewTempDirectory(base::FilePath::StringType(), &temp_path))
84 return false;
85 if (base::WriteFile(temp_path.Append(filename), data.c_str(), data.size()) ==
86 -1)
87 return false;
88
89 bool succeed = base::CreateTemporaryFile(&zip_file) &&
90 zip::Zip(temp_path, zip_file, false) &&
91 base::ReadFileToString(zip_file, compressed_logs);
92
93 base::DeleteFile(temp_path, true);
94 base::DeleteFile(zip_file, false);
95
96 return succeed;
97 }
98
99 } // namespace feedback_util
100