Skip to content

Commit

Permalink
Add additional details to webcompat report
Browse files Browse the repository at this point in the history
  • Loading branch information
DJAndries committed Sep 19, 2023
1 parent a8aeb07 commit 1d316b5
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 123 deletions.
7 changes: 4 additions & 3 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ source_set("ui") {
"webui/skus_internals_ui.h",
]

# It doesn't make sense to view accelerators on iOS & Android.
# It doesn't make sense to view the webcompat webui on iOS & Android.
if (!is_android && !is_ios) {
sources += [
"webui/webcompat_reporter_ui.cc",
"webui/webcompat_reporter_ui.h",
"webui/webcompat_reporter/webcompat_reporter_ui.cc",
"webui/webcompat_reporter/webcompat_reporter_ui.h",
]
}

Expand Down Expand Up @@ -553,6 +553,7 @@ source_set("ui") {
"//components/gcm_driver:gcm_buildflags",
"//components/image_fetcher/core",
"//components/infobars/content",
"//components/language/core/browser",
"//components/omnibox/browser:vector_icons",
"//components/optimization_guide/core:features",
"//components/optimization_guide/optimization_guide_internals/webui:url_constants",
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/brave_pages.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void ShowWebcompatReporter(Browser* browser) {
return;
}

OpenWebcompatReporterDialog(web_contents);
webcompat_reporter::OpenReporterDialog(web_contents);
}

void ShowBraveWallet(Browser* browser) {
Expand Down
3 changes: 2 additions & 1 deletion browser/ui/webui/brave_shields/shields_panel_data_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ void ShieldsPanelDataHandler::OpenWebCompatWindow() {
if (!active_shields_data_controller_)
return;

OpenWebcompatReporterDialog(active_shields_data_controller_->web_contents());
webcompat_reporter::OpenReporterDialog(
active_shields_data_controller_->web_contents());
}

void ShieldsPanelDataHandler::UpdateFavicon() {
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/webui/brave_web_ui_controller_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_ui.h"
#include "brave/browser/ui/webui/private_new_tab_page/brave_private_new_tab_ui.h"
#include "brave/browser/ui/webui/speedreader/speedreader_toolbar_ui.h"
#include "brave/browser/ui/webui/webcompat_reporter_ui.h"
#include "brave/browser/ui/webui/webcompat_reporter/webcompat_reporter_ui.h"
#include "brave/browser/ui/webui/welcome_page/brave_welcome_ui.h"
#include "brave/components/brave_news/common/features.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
Expand Down Expand Up @@ -108,7 +108,7 @@ WebUIController* NewWebUI(WebUI* web_ui, const GURL& url) {
return new SkusInternalsUI(web_ui, url.host());
#if !BUILDFLAG(IS_ANDROID)
} else if (host == kWebcompatReporterHost) {
return new WebcompatReporterUI(web_ui, url.host());
return new webcompat_reporter::WebcompatReporterUI(web_ui, url.host());
#endif
#if BUILDFLAG(ENABLE_IPFS_INTERNALS_WEBUI)
} else if (host == kIPFSWebUIHost &&
Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import("//brave/components/playlist/common/buildflags/buildflags.gni")
brave_browser_ui_webui_configs_deps = [
"//base",
"//brave/browser/ui",
"//brave/components/brave_shields/browser",
"//brave/components/brave_vpn/common/buildflags",
"//brave/components/playlist/common/buildflags",
]
Expand Down
39 changes: 37 additions & 2 deletions browser/ui/webui/webcompat_reporter/webcompat_reporter_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

#include "base/json/json_writer.h"
#include "base/values.h"
#include "brave/browser/ui/brave_shields_data_controller.h"
#include "brave/components/brave_shields/common/brave_shields_panel.mojom-shared.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/webcompat_reporter/browser/fields.h"
#include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
Expand All @@ -24,10 +27,16 @@
using content::WebContents;
using content::WebUIMessageHandler;

namespace webcompat_reporter {

namespace {

constexpr int kDialogMinHeight = 100;
constexpr int kDialogMaxHeight = 700;
constexpr int kDialogWidth = 375;

} // namespace

// A ui::WebDialogDelegate that specifies the webcompat reporter's appearance.
class WebcompatReporterDialogDelegate : public ui::WebDialogDelegate {
public:
Expand Down Expand Up @@ -102,9 +111,33 @@ bool WebcompatReporterDialogDelegate::ShouldShowDialogTitle() const {
return false;
}

void OpenWebcompatReporterDialog(content::WebContents* initiator) {
void OpenReporterDialog(content::WebContents* initiator) {
bool shields_enabled = false;
brave_shields::mojom::FingerprintMode fp_block_mode =
brave_shields::mojom::FingerprintMode::STANDARD;
brave_shields::mojom::AdBlockMode ad_block_mode =
brave_shields::mojom::AdBlockMode::STANDARD;
brave_shields::BraveShieldsDataController* shields_data_controller =
brave_shields::BraveShieldsDataController::FromWebContents(initiator);
if (shields_data_controller != nullptr) {
shields_enabled = shields_data_controller->GetBraveShieldsEnabled();
fp_block_mode = shields_data_controller->GetFingerprintMode();
ad_block_mode = shields_data_controller->GetAdBlockMode();
}

// Remove query and fragments from reported URL.
GURL::Replacements replacements;
replacements.ClearQuery();
replacements.ClearRef();
GURL report_url =
initiator->GetLastCommittedURL().ReplaceComponents(replacements);

base::Value::Dict params_dict;
params_dict.Set("siteUrl", initiator->GetLastCommittedURL().spec());
params_dict.Set(kSiteURLField, report_url.spec());
params_dict.Set(kShieldsEnabledField, shields_enabled);
params_dict.Set(kAdBlockSettingField, GetAdBlockModeString(ad_block_mode));
params_dict.Set(kFPBlockSettingField,
GetFingerprintModeString(fp_block_mode));

gfx::Size min_size(kDialogWidth, kDialogMinHeight);
gfx::Size max_size(kDialogWidth, kDialogMaxHeight);
Expand All @@ -113,3 +146,5 @@ void OpenWebcompatReporterDialog(content::WebContents* initiator) {
std::make_unique<WebcompatReporterDialogDelegate>(std::move(params_dict)),
initiator, min_size, max_size);
}

} // namespace webcompat_reporter
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ namespace content {
class WebContents;
}

void OpenWebcompatReporterDialog(content::WebContents* initiator);
namespace webcompat_reporter {

void OpenReporterDialog(content::WebContents* initiator);

} // namespace webcompat_reporter

#endif // BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_WEBCOMPAT_REPORTER_DIALOG_H_
178 changes: 178 additions & 0 deletions browser/ui/webui/webcompat_reporter/webcompat_reporter_ui.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/webui/webcompat_reporter/webcompat_reporter_ui.h"

#include <memory>
#include <utility>
#include <vector>

#include "base/strings/string_util.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/ui/brave_shields_data_controller.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/filter_list_catalog_entry.h"
#include "brave/components/brave_shields/common/pref_names.h"
#include "brave/components/brave_vpn/common/buildflags/buildflags.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/webcompat_reporter/browser/fields.h"
#include "brave/components/webcompat_reporter/browser/webcompat_report_uploader.h"
#include "brave/components/webcompat_reporter/resources/grit/webcompat_reporter_generated_map.h"
#include "chrome/browser/profiles/profile.h"
#include "components/grit/brave_components_resources.h"
#include "components/language/core/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/web_dialogs/web_dialog_delegate.h"
#include "url/gurl.h"

#if BUILDFLAG(ENABLE_BRAVE_VPN)
#include "brave/browser/brave_vpn/brave_vpn_service_factory.h"
#include "brave/components/brave_vpn/browser/brave_vpn_service.h"
#endif

namespace webcompat_reporter {

namespace {

class WebcompatReporterDOMHandler : public content::WebUIMessageHandler {
public:
explicit WebcompatReporterDOMHandler(Profile* profile);
WebcompatReporterDOMHandler(const WebcompatReporterDOMHandler&) = delete;
WebcompatReporterDOMHandler& operator=(const WebcompatReporterDOMHandler&) =
delete;
~WebcompatReporterDOMHandler() override;

// WebUIMessageHandler implementation.
void RegisterMessages() override;

private:
void InitAdditionalParameters(Profile* profile);
void HandleSubmitReport(const base::Value::List& args);

std::unique_ptr<webcompat_reporter::WebcompatReportUploader> uploader_;
std::string ad_block_list_names_;
std::string languages_;
bool language_farbling_enabled_ = true;
bool brave_vpn_connected_ = false;
};

WebcompatReporterDOMHandler::WebcompatReporterDOMHandler(Profile* profile)
: uploader_(std::make_unique<webcompat_reporter::WebcompatReportUploader>(
profile->GetURLLoaderFactory())) {
InitAdditionalParameters(profile);
}

void WebcompatReporterDOMHandler::InitAdditionalParameters(Profile* profile) {
std::vector<std::string> ad_block_list_names;

// Collect all enabled adblock list names
brave_shields::AdBlockService* ad_block_service =
g_brave_browser_process->ad_block_service();
if (ad_block_service != nullptr) {
brave_shields::AdBlockRegionalServiceManager* regional_service_manager =
ad_block_service->regional_service_manager();
CHECK(regional_service_manager);
for (const brave_shields::FilterListCatalogEntry& entry :
regional_service_manager->GetFilterListCatalog()) {
if (regional_service_manager->IsFilterListEnabled(entry.uuid)) {
ad_block_list_names.push_back(entry.title);
}
}
}

ad_block_list_names_ = base::JoinString(ad_block_list_names, ",");

#if BUILDFLAG(ENABLE_BRAVE_VPN)
brave_vpn::BraveVpnService* vpn_service =
brave_vpn::BraveVpnServiceFactory::GetForProfile(profile);
if (vpn_service != nullptr) {
brave_vpn_connected_ = vpn_service->IsConnected();
}
#endif

PrefService* profile_prefs = profile->GetPrefs();
languages_ = profile_prefs->GetString(language::prefs::kAcceptLanguages);
language_farbling_enabled_ =
profile_prefs->GetBoolean(brave_shields::prefs::kReduceLanguageEnabled);
}

WebcompatReporterDOMHandler::~WebcompatReporterDOMHandler() = default;

void WebcompatReporterDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"webcompat_reporter.submitReport",
base::BindRepeating(&WebcompatReporterDOMHandler::HandleSubmitReport,
base::Unretained(this)));
}

void WebcompatReporterDOMHandler::HandleSubmitReport(
const base::Value::List& args) {
DCHECK_EQ(args.size(), 1U);
if (!args[0].is_dict()) {
return;
}

const base::Value::Dict& submission_args = args[0].GetDict();

const std::string* url_arg = submission_args.FindString(kSiteURLField);
const std::string* ad_block_setting_arg =
submission_args.FindString(kAdBlockSettingField);
const std::string* fp_block_setting_arg =
submission_args.FindString(kFPBlockSettingField);
const base::Value* details_arg = submission_args.Find(kDetailsField);
const base::Value* contact_arg = submission_args.Find(kContactField);
bool shields_enabled =
submission_args.FindBool(kShieldsEnabledField).value_or(false);

std::string url;
std::string ad_block_setting;
std::string fp_block_setting;
base::Value details;
base::Value contact;

if (url_arg != nullptr) {
url = *url_arg;
}
if (ad_block_setting_arg != nullptr) {
ad_block_setting = *ad_block_setting_arg;
}
if (fp_block_setting_arg != nullptr) {
fp_block_setting = *fp_block_setting_arg;
}
if (details_arg != nullptr) {
details = details_arg->Clone();
}
if (contact_arg != nullptr) {
contact = contact_arg->Clone();
}

uploader_->SubmitReport(GURL(url), shields_enabled, ad_block_setting,
fp_block_setting, ad_block_list_names_, languages_,
language_farbling_enabled_, brave_vpn_connected_,
details, contact);
}

} // namespace

WebcompatReporterUI::WebcompatReporterUI(content::WebUI* web_ui,
const std::string& name)
: ConstrainedWebDialogUI(web_ui) {
CreateAndAddWebUIDataSource(web_ui, name, kWebcompatReporterGenerated,
kWebcompatReporterGeneratedSize,
IDR_WEBCOMPAT_REPORTER_HTML);
Profile* profile = Profile::FromWebUI(web_ui);

web_ui->AddMessageHandler(
std::make_unique<WebcompatReporterDOMHandler>(profile));
}

WebcompatReporterUI::~WebcompatReporterUI() = default;

} // namespace webcompat_reporter
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
/* Copyright (c) 2023 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_UI_H_
#ifndef BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_WEBCOMPAT_REPORTER_UI_H_
#define BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_WEBCOMPAT_REPORTER_UI_H_

#include <string>

Expand All @@ -14,6 +14,8 @@ namespace content {
class WebUI;
}

namespace webcompat_reporter {

class WebcompatReporterUI : public ConstrainedWebDialogUI {
public:
WebcompatReporterUI(content::WebUI* web_ui, const std::string& host);
Expand All @@ -22,4 +24,6 @@ class WebcompatReporterUI : public ConstrainedWebDialogUI {
~WebcompatReporterUI() override;
};

#endif // BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_UI_H_
} // namespace webcompat_reporter

#endif // BRAVE_BROWSER_UI_WEBUI_WEBCOMPAT_REPORTER_WEBCOMPAT_REPORTER_UI_H_
Loading

0 comments on commit 1d316b5

Please sign in to comment.