-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
show_feedback_page.cc
351 lines (312 loc) · 14.1 KB
/
show_feedback_page.cc
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/json/json_writer.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/escape.h"
#include "base/strings/string_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/feedback/feedback_dialog_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/feedback/feedback_dialog.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/base/consent_level.h"
#include "extensions/browser/api/feedback_private/feedback_private_api.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "ash/constants/ash_features.h"
#include "ash/webui/os_feedback_ui/url_constants.h"
#include "ash/webui/system_apps/public/system_web_app_type.h"
#include "base/functional/bind.h"
#include "base/strings/strcat.h"
#include "chrome/browser/ash/crosapi/browser_manager.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chromeos/components/kiosk/kiosk_utils.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "google_apis/gaia/gaia_auth_util.h"
#endif
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/crosapi/mojom/feedback.mojom.h"
#include "chromeos/lacros/lacros_service.h"
#endif
namespace feedback_private = extensions::api::feedback_private;
namespace chrome {
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
constexpr char kExtraDiagnosticsQueryParam[] = "extra_diagnostics";
constexpr char kDescriptionTemplateQueryParam[] = "description_template";
constexpr char kDescriptionPlaceholderQueryParam[] =
"description_placeholder_text";
constexpr char kFromAssistantQueryParam[] = "from_assistant";
constexpr char kSettingsSearchFeedbackQueryParam[] = "from_settings_search";
constexpr char kCategoryTagParam[] = "category_tag";
constexpr char kPageURLParam[] = "page_url";
constexpr char kQueryParamSeparator[] = "&";
constexpr char kQueryParamKeyValueSeparator[] = "=";
constexpr char kFromAssistantQueryParamValue[] = "true";
constexpr char kSettingsSearchFeedbackQueryParamValue[] = "true";
constexpr char kFromAutofillQueryParam[] = "from_autofill";
constexpr char kFromAutofillParamValue[] = "true";
constexpr char kAutofillMetadataQueryParam[] = "autofill_metadata";
// Concat query parameter with escaped value.
std::string StrCatQueryParam(const std::string query_param,
const std::string value) {
return base::StrCat({query_param, kQueryParamKeyValueSeparator,
base::EscapeQueryParamValue(value, /*use_plus=*/false)});
}
// Returns URL for OS Feedback with additional data passed as query parameters.
GURL BuildFeedbackUrl(const std::string extra_diagnostics,
const std::string description_template,
const std::string description_placeholder_text,
const std::string category_tag,
const GURL page_url,
FeedbackSource source,
base::Value::Dict autofill_metadata) {
std::vector<std::string> query_params;
if (!extra_diagnostics.empty()) {
query_params.emplace_back(
StrCatQueryParam(kExtraDiagnosticsQueryParam, extra_diagnostics));
}
if (!description_template.empty()) {
query_params.emplace_back(
StrCatQueryParam(kDescriptionTemplateQueryParam, description_template));
}
if (!description_placeholder_text.empty()) {
query_params.emplace_back(StrCatQueryParam(
kDescriptionPlaceholderQueryParam, description_placeholder_text));
}
if (!category_tag.empty()) {
query_params.emplace_back(
StrCatQueryParam(kCategoryTagParam, category_tag));
}
if (!page_url.is_empty()) {
query_params.emplace_back(StrCatQueryParam(kPageURLParam, page_url.spec()));
}
if (source == kFeedbackSourceAssistant) {
query_params.emplace_back(StrCatQueryParam(kFromAssistantQueryParam,
kFromAssistantQueryParamValue));
}
if (source == kFeedbackSourceOsSettingsSearch) {
query_params.emplace_back(
StrCatQueryParam(kSettingsSearchFeedbackQueryParam,
kSettingsSearchFeedbackQueryParamValue));
}
if (source == kFeedbackSourceAutofillContextMenu) {
query_params.emplace_back(
StrCatQueryParam(kFromAutofillQueryParam, kFromAutofillParamValue));
std::string autofill_metadata_json;
base::JSONWriter::Write(autofill_metadata, &autofill_metadata_json);
query_params.emplace_back(
StrCatQueryParam(kAutofillMetadataQueryParam, autofill_metadata_json));
}
// Use default URL if no extra parameters to be added.
if (query_params.empty()) {
return GURL(ash::kChromeUIOSFeedbackUrl);
}
return GURL(
base::StrCat({ash::kChromeUIOSFeedbackUrl, "/?",
base::JoinString(query_params, kQueryParamSeparator)}));
}
// Returns whether the user has an internal Google account (e.g. @google.com).
bool IsGoogleInternalAccount(Profile* profile) {
auto* identity_manager = IdentityManagerFactory::GetForProfile(profile);
if (!identity_manager) // Non-GAIA account, e.g. guest mode.
return false;
// Browser sync consent is not required to use feedback.
CoreAccountInfo account_info =
identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSignin);
return gaia::IsGoogleInternalAccountEmail(account_info.email);
}
// Returns if the feedback page is considered to be triggered from user
// interaction.
bool IsFromUserInteraction(FeedbackSource source) {
switch (source) {
case kFeedbackSourceArcApp:
case kFeedbackSourceAsh:
case kFeedbackSourceAssistant:
case kFeedbackSourceAutofillContextMenu:
case kFeedbackSourceBrowserCommand:
case kFeedbackSourceConnectivityDiagnostics:
case kFeedbackSourceDesktopTabGroups:
case kFeedbackSourceNetworkHealthPage:
case kFeedbackSourceMdSettingsAboutPage:
case kFeedbackSourceOldSettingsAboutPage:
case kFeedbackSourceOsSettingsSearch:
case kFeedbackSourceQuickAnswers:
case kFeedbackSourceQuickOffice:
case kFeedbackSourceSettingsPerformancePage:
return true;
default:
return false;
}
}
void OnLacrosActiveTabUrlFetched(
Profile* profile,
chrome::FeedbackSource source,
const std::string& description_template,
const std::string& description_placeholder_text,
const std::string& category_tag,
const std::string& extra_diagnostics,
base::Value::Dict autofill_metadata,
const absl::optional<GURL>& active_tab_url) {
GURL page_url;
if (active_tab_url)
page_url = *active_tab_url;
chrome::ShowFeedbackPage(page_url, profile, source, description_template,
description_placeholder_text, category_tag,
extra_diagnostics, std::move(autofill_metadata));
}
void LaunchFeedbackSWA(Profile* profile, const GURL& url) {
ash::SystemAppLaunchParams params;
params.url = url;
ash::LaunchSystemWebAppAsync(profile, ash::SystemWebAppType::OS_FEEDBACK,
std::move(params));
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
#if !BUILDFLAG(IS_CHROMEOS_LACROS)
feedback_private::FeedbackFlow GetFeedbackFlowFromSource(
FeedbackSource source) {
switch (source) {
case kFeedbackSourceSadTabPage:
return feedback_private::FeedbackFlow::kSadTabCrash;
case kFeedbackSourceAutofillContextMenu:
return feedback_private::FeedbackFlow::kGoogleInternal;
default:
return feedback_private::FeedbackFlow::kRegular;
}
}
// Calls feedback private api to show Feedback ui.
void RequestFeedbackFlow(const GURL& page_url,
Profile* profile,
FeedbackSource source,
const std::string& description_template,
const std::string& description_placeholder_text,
const std::string& category_tag,
const std::string& extra_diagnostics,
base::Value::Dict autofill_metadata) {
feedback_private::FeedbackFlow flow = GetFeedbackFlowFromSource(source);
bool include_bluetooth_logs = false;
bool show_questionnaire = false;
#if BUILDFLAG(IS_CHROMEOS_ASH)
if (IsGoogleInternalAccount(profile)) {
flow = feedback_private::FeedbackFlow::kGoogleInternal;
include_bluetooth_logs = IsFromUserInteraction(source);
show_questionnaire = IsFromUserInteraction(source);
}
// Disable the new feedback tool for kiosk, because SWAs are disabled there.
if (!chromeos::IsKioskSession() &&
base::FeatureList::IsEnabled(ash::features::kOsFeedback)) {
// TODO(crbug.com/1407646): Include autofill metadata into CrOS new feedback
// tool.
GURL url = BuildFeedbackUrl(extra_diagnostics, description_template,
description_placeholder_text, category_tag,
page_url, source, std::move(autofill_metadata));
// Wait for all SWAs to be registered before continuing.
ash::SystemWebAppManager::Get(profile)->on_apps_synchronized().Post(
FROM_HERE, base::BindOnce(&LaunchFeedbackSWA, profile, url));
return;
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
extensions::FeedbackPrivateAPI* api =
extensions::FeedbackPrivateAPI::GetFactoryInstance()->Get(profile);
auto info = api->CreateFeedbackInfo(
description_template, description_placeholder_text, category_tag,
extra_diagnostics, page_url, flow, source == kFeedbackSourceAssistant,
include_bluetooth_logs, show_questionnaire,
source == kFeedbackSourceChromeLabs ||
source == kFeedbackSourceKaleidoscope,
source == kFeedbackSourceAutofillContextMenu, autofill_metadata);
FeedbackDialog::CreateOrShow(profile, *info);
}
#endif // !BUILDFLAG(IS_CHROMEOS_LACROS)
} // namespace
#if BUILDFLAG(IS_CHROMEOS_LACROS)
namespace internal {
// Requests to show Feedback ui remotely in ash via crosapi mojo call.
void ShowFeedbackPageLacros(const GURL& page_url,
FeedbackSource source,
const std::string& description_template,
const std::string& description_placeholder_text,
const std::string& category_tag,
const std::string& extra_diagnostics,
base::Value::Dict autofill_metadata);
} // namespace internal
#endif
void ShowFeedbackPage(const Browser* browser,
FeedbackSource source,
const std::string& description_template,
const std::string& description_placeholder_text,
const std::string& category_tag,
const std::string& extra_diagnostics,
base::Value::Dict autofill_metadata) {
GURL page_url;
if (browser) {
page_url = GetTargetTabUrl(browser->session_id(),
browser->tab_strip_model()->active_index());
}
Profile* profile = GetFeedbackProfile(browser);
#if BUILDFLAG(IS_CHROMEOS_ASH)
// When users invoke the feedback dialog by pressing alt-shift-i without
// an active ash window, we need to check if there is an active lacros window
// and show its Url in the feedback dialog if there is any.
if (!browser && crosapi::BrowserManager::Get()->IsRunning() &&
crosapi::BrowserManager::Get()->GetActiveTabUrlSupported()) {
crosapi::BrowserManager::Get()->GetActiveTabUrl(base::BindOnce(
&OnLacrosActiveTabUrlFetched, profile, source, description_template,
description_placeholder_text, category_tag, extra_diagnostics,
std::move(autofill_metadata)));
} else {
ShowFeedbackPage(page_url, profile, source, description_template,
description_placeholder_text, category_tag,
extra_diagnostics, std::move(autofill_metadata));
}
#else
ShowFeedbackPage(page_url, profile, source, description_template,
description_placeholder_text, category_tag,
extra_diagnostics, std::move(autofill_metadata));
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
}
void ShowFeedbackPage(const GURL& page_url,
Profile* profile,
FeedbackSource source,
const std::string& description_template,
const std::string& description_placeholder_text,
const std::string& category_tag,
const std::string& extra_diagnostics,
base::Value::Dict autofill_metadata) {
if (!profile) {
LOG(ERROR) << "Cannot invoke feedback: No profile found!";
return;
}
if (!profile->GetPrefs()->GetBoolean(prefs::kUserFeedbackAllowed)) {
return;
}
// Record an UMA histogram to know the most frequent feedback request source.
UMA_HISTOGRAM_ENUMERATION("Feedback.RequestSource", source,
kFeedbackSourceCount);
#if BUILDFLAG(IS_CHROMEOS_LACROS)
// After M87 beta, Feedback API should be supported in crosapi with all ash
// versions on chromeOS platform where lacros is deployed.
DCHECK(
chromeos::LacrosService::Get()->IsAvailable<crosapi::mojom::Feedback>());
// Send request to ash via crosapi mojo to show Feedback ui from ash.
internal::ShowFeedbackPageLacros(
page_url, source, description_template, description_placeholder_text,
category_tag, extra_diagnostics, std::move(autofill_metadata));
#else
// Show feedback dialog using feedback extension API.
RequestFeedbackFlow(page_url, profile, source, description_template,
description_placeholder_text, category_tag,
extra_diagnostics, std::move(autofill_metadata));
#endif // BUILDFLAG(IS_CHROMEOS_LACROS)
}
} // namespace chrome