1 // Copyright 2013 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 "chrome/browser/extensions/api/feedback_private/feedback_service.h"
7 #include "base/callback.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_content_client.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread
;
16 using feedback::FeedbackData
;
20 void PopulateSystemInfo(
21 extensions::SystemInformationList
* sys_info_list
,
22 const std::string
& key
,
23 const std::string
& value
) {
24 base::DictionaryValue sys_info_value
;
25 sys_info_value
.Set("key", new base::StringValue(key
));
26 sys_info_value
.Set("value", new base::StringValue(value
));
28 linked_ptr
<SystemInformation
> sys_info(new SystemInformation());
29 SystemInformation::Populate(sys_info_value
, sys_info
.get());
31 sys_info_list
->push_back(sys_info
);
36 namespace extensions
{
38 FeedbackService::FeedbackService() {
41 FeedbackService::~FeedbackService() {
44 void FeedbackService::SendFeedback(
46 scoped_refptr
<FeedbackData
> feedback_data
,
47 const SendFeedbackCallback
& callback
) {
48 send_feedback_callback_
= callback
;
49 feedback_data_
= feedback_data
;
50 feedback_data_
->set_locale(g_browser_process
->GetApplicationLocale());
51 feedback_data_
->set_user_agent(GetUserAgent());
53 if (!feedback_data_
->attached_file_uuid().empty()) {
54 // Self-deleting object.
55 BlobReader
* attached_file_reader
= new BlobReader(
56 profile
, feedback_data_
->attached_file_uuid(),
57 base::Bind(&FeedbackService::AttachedFileCallback
, AsWeakPtr()));
58 attached_file_reader
->Start();
61 if (!feedback_data_
->screenshot_uuid().empty()) {
62 // Self-deleting object.
63 BlobReader
* screenshot_reader
= new BlobReader(
64 profile
, feedback_data_
->screenshot_uuid(),
65 base::Bind(&FeedbackService::ScreenshotCallback
, AsWeakPtr()));
66 screenshot_reader
->Start();
69 CompleteSendFeedback();
72 void FeedbackService::AttachedFileCallback(scoped_ptr
<std::string
> data
,
73 int64
/* total_blob_length */) {
74 feedback_data_
->set_attached_file_uuid(std::string());
76 feedback_data_
->AttachAndCompressFileData(data
.Pass());
78 CompleteSendFeedback();
81 void FeedbackService::ScreenshotCallback(scoped_ptr
<std::string
> data
,
82 int64
/* total_blob_length */) {
83 feedback_data_
->set_screenshot_uuid(std::string());
85 feedback_data_
->set_image(data
.Pass());
87 CompleteSendFeedback();
90 void FeedbackService::GetSystemInformation(
91 const GetSystemInformationCallback
& callback
) {
92 system_information_callback_
= callback
;
94 system_logs::ScrubbedSystemLogsFetcher
* fetcher
=
95 new system_logs::ScrubbedSystemLogsFetcher();
97 base::Bind(&FeedbackService::OnSystemLogsFetchComplete
, AsWeakPtr()));
101 void FeedbackService::OnSystemLogsFetchComplete(
102 scoped_ptr
<system_logs::SystemLogsResponse
> sys_info_map
) {
103 SystemInformationList sys_info_list
;
104 if (!sys_info_map
.get()) {
105 system_information_callback_
.Run(sys_info_list
);
109 for (system_logs::SystemLogsResponse::iterator it
= sys_info_map
->begin();
110 it
!= sys_info_map
->end(); ++it
)
111 PopulateSystemInfo(&sys_info_list
, it
->first
, it
->second
);
113 system_information_callback_
.Run(sys_info_list
);
116 void FeedbackService::CompleteSendFeedback() {
117 // A particular data collection is considered completed if,
118 // a.) The blob URL is invalid - this will either happen because we never had
119 // a URL and never needed to read this data, or that the data read failed
120 // and we set it to invalid in the data read callback.
121 // b.) The associated data object exists, meaning that the data has been read
122 // and the read callback has updated the associated data on the feedback
124 bool attached_file_completed
= feedback_data_
->attached_file_uuid().empty();
125 bool screenshot_completed
= feedback_data_
->screenshot_uuid().empty();
127 if (screenshot_completed
&& attached_file_completed
) {
128 // Signal the feedback object that the data from the feedback page has been
129 // filled - the object will manage sending of the actual report.
130 feedback_data_
->OnFeedbackPageDataComplete();
131 // TODO(rkc): Change this once we have FeedbackData/Util refactored to
132 // report the status of the report being sent.
133 send_feedback_callback_
.Run(true);
137 } // namespace extensions