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_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/api/feedback_private/feedback_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/feedback/tracing_manager.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "extensions/browser/event_router.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/webui/web_ui_util.h"
24 #include "url/url_util.h"
26 using feedback::FeedbackData
;
30 // Getting the filename of a blob prepends a "C:\fakepath" to the filename.
31 // This is undesirable, strip it if it exists.
32 std::string
StripFakepath(const std::string
& path
) {
33 const char kFakePathStr
[] = "C:\\fakepath\\";
34 if (base::StartsWith(path
, kFakePathStr
,
35 base::CompareCase::INSENSITIVE_ASCII
))
36 return path
.substr(arraysize(kFakePathStr
) - 1);
42 namespace extensions
{
44 namespace feedback_private
= api::feedback_private
;
46 using feedback_private::SystemInformation
;
47 using feedback_private::FeedbackInfo
;
49 char kFeedbackExtensionId
[] = "gfdkimpbcpahaombhbimeihdjnejgicl";
51 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<FeedbackPrivateAPI
> >
52 g_factory
= LAZY_INSTANCE_INITIALIZER
;
55 BrowserContextKeyedAPIFactory
<FeedbackPrivateAPI
>*
56 FeedbackPrivateAPI::GetFactoryInstance() {
57 return g_factory
.Pointer();
60 FeedbackPrivateAPI::FeedbackPrivateAPI(content::BrowserContext
* context
)
61 : browser_context_(context
), service_(new FeedbackService()) {
64 FeedbackPrivateAPI::~FeedbackPrivateAPI() {
69 FeedbackService
* FeedbackPrivateAPI::GetService() const {
73 void FeedbackPrivateAPI::RequestFeedback(
74 const std::string
& description_template
,
75 const std::string
& category_tag
,
76 const GURL
& page_url
) {
77 if (browser_context_
&& EventRouter::Get(browser_context_
)) {
79 info
.description
= description_template
;
80 info
.category_tag
= make_scoped_ptr(new std::string(category_tag
));
81 info
.page_url
= make_scoped_ptr(new std::string(page_url
.spec()));
82 info
.system_information
.reset(new SystemInformationList
);
83 // The manager is only available if tracing is enabled.
84 if (TracingManager
* manager
= TracingManager::Get()) {
85 info
.trace_id
.reset(new int(manager
->RequestTrace()));
88 scoped_ptr
<base::ListValue
> args(new base::ListValue());
89 args
->Append(info
.ToValue().release());
91 scoped_ptr
<Event
> event(new Event(
92 events::FEEDBACK_PRIVATE_ON_FEEDBACK_REQUESTED
,
93 feedback_private::OnFeedbackRequested::kEventName
, args
.Pass()));
94 event
->restrict_to_browser_context
= browser_context_
;
96 EventRouter::Get(browser_context_
)
97 ->DispatchEventToExtension(kFeedbackExtensionId
, event
.Pass());
102 base::Closure
* FeedbackPrivateGetStringsFunction::test_callback_
= NULL
;
104 bool FeedbackPrivateGetStringsFunction::RunSync() {
105 base::DictionaryValue
* dict
= new base::DictionaryValue();
108 #define SET_STRING(id, idr) \
109 dict->SetString(id, l10n_util::GetStringUTF16(idr))
110 SET_STRING("page-title", IDS_FEEDBACK_REPORT_PAGE_TITLE
);
111 SET_STRING("page-url", IDS_FEEDBACK_REPORT_URL_LABEL
);
112 SET_STRING("screenshot", IDS_FEEDBACK_SCREENSHOT_LABEL
);
113 SET_STRING("user-email", IDS_FEEDBACK_USER_EMAIL_LABEL
);
114 #if defined(OS_CHROMEOS)
115 SET_STRING("sys-info",
116 IDS_FEEDBACK_INCLUDE_SYSTEM_INFORMATION_AND_METRICS_CHKBOX
);
118 SET_STRING("sys-info", IDS_FEEDBACK_INCLUDE_SYSTEM_INFORMATION_CHKBOX
);
120 SET_STRING("attach-file-label", IDS_FEEDBACK_ATTACH_FILE_LABEL
);
121 SET_STRING("attach-file-note", IDS_FEEDBACK_ATTACH_FILE_NOTE
);
122 SET_STRING("attach-file-to-big", IDS_FEEDBACK_ATTACH_FILE_TO_BIG
);
123 SET_STRING("reading-file", IDS_FEEDBACK_READING_FILE
);
124 SET_STRING("send-report", IDS_FEEDBACK_SEND_REPORT
);
125 SET_STRING("cancel", IDS_CANCEL
);
126 SET_STRING("no-description", IDS_FEEDBACK_NO_DESCRIPTION
);
127 SET_STRING("privacy-note", IDS_FEEDBACK_PRIVACY_NOTE
);
128 SET_STRING("performance-trace",
129 IDS_FEEDBACK_INCLUDE_PERFORMANCE_TRACE_CHECKBOX
);
132 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
133 webui::SetLoadTimeDataDefaults(app_locale
, dict
);
135 if (test_callback_
&& !test_callback_
->is_null())
136 test_callback_
->Run();
141 bool FeedbackPrivateGetUserEmailFunction::RunSync() {
142 SigninManagerBase
* signin_manager
=
143 SigninManagerFactory::GetForProfile(GetProfile());
144 SetResult(new base::StringValue(
145 signin_manager
? signin_manager
->GetAuthenticatedUsername()
150 bool FeedbackPrivateGetSystemInformationFunction::RunAsync() {
151 FeedbackService
* service
=
152 FeedbackPrivateAPI::GetFactoryInstance()->Get(GetProfile())->GetService();
154 service
->GetSystemInformation(
156 &FeedbackPrivateGetSystemInformationFunction::OnCompleted
, this));
160 void FeedbackPrivateGetSystemInformationFunction::OnCompleted(
161 const SystemInformationList
& sys_info
) {
162 results_
= feedback_private::GetSystemInformation::Results::Create(
167 bool FeedbackPrivateSendFeedbackFunction::RunAsync() {
168 scoped_ptr
<feedback_private::SendFeedback::Params
> params(
169 feedback_private::SendFeedback::Params::Create(*args_
));
170 EXTENSION_FUNCTION_VALIDATE(params
.get());
172 const FeedbackInfo
&feedback_info
= params
->feedback
;
174 std::string attached_file_uuid
;
175 if (feedback_info
.attached_file_blob_uuid
.get() &&
176 !feedback_info
.attached_file_blob_uuid
->empty())
177 attached_file_uuid
= *feedback_info
.attached_file_blob_uuid
;
179 std::string screenshot_uuid
;
180 if (feedback_info
.screenshot_blob_uuid
.get() &&
181 !feedback_info
.screenshot_blob_uuid
->empty())
182 screenshot_uuid
= *feedback_info
.screenshot_blob_uuid
;
184 // Populate feedback data.
185 scoped_refptr
<FeedbackData
> feedback_data(new FeedbackData());
186 feedback_data
->set_context(GetProfile());
187 feedback_data
->set_description(feedback_info
.description
);
189 if (feedback_info
.category_tag
.get())
190 feedback_data
->set_category_tag(*feedback_info
.category_tag
.get());
191 if (feedback_info
.page_url
.get())
192 feedback_data
->set_page_url(*feedback_info
.page_url
.get());
193 if (feedback_info
.email
.get())
194 feedback_data
->set_user_email(*feedback_info
.email
.get());
196 if (!attached_file_uuid
.empty()) {
197 feedback_data
->set_attached_filename(
198 StripFakepath((*feedback_info
.attached_file
.get()).name
));
199 feedback_data
->set_attached_file_uuid(attached_file_uuid
);
202 if (!screenshot_uuid
.empty())
203 feedback_data
->set_screenshot_uuid(screenshot_uuid
);
205 if (feedback_info
.trace_id
.get()) {
206 feedback_data
->set_trace_id(*feedback_info
.trace_id
.get());
209 scoped_ptr
<FeedbackData::SystemLogsMap
> sys_logs(
210 new FeedbackData::SystemLogsMap
);
211 SystemInformationList
* sys_info
= feedback_info
.system_information
.get();
213 for (SystemInformationList::iterator it
= sys_info
->begin();
214 it
!= sys_info
->end(); ++it
)
215 (*sys_logs
.get())[it
->get()->key
] = it
->get()->value
;
217 feedback_data
->SetAndCompressSystemInfo(sys_logs
.Pass());
219 FeedbackService
* service
=
220 FeedbackPrivateAPI::GetFactoryInstance()->Get(GetProfile())->GetService();
223 if (feedback_info
.send_histograms
) {
224 scoped_ptr
<std::string
> histograms(new std::string
);
225 *histograms
= base::StatisticsRecorder::ToJSON(std::string());
226 if (!histograms
->empty())
227 feedback_data
->SetAndCompressHistograms(histograms
.Pass());
230 service
->SendFeedback(
233 base::Bind(&FeedbackPrivateSendFeedbackFunction::OnCompleted
, this));
238 void FeedbackPrivateSendFeedbackFunction::OnCompleted(
240 results_
= feedback_private::SendFeedback::Results::Create(
241 success
? feedback_private::STATUS_SUCCESS
:
242 feedback_private::STATUS_DELAYED
);
246 } // namespace extensions