Skip to content

Commit

Permalink
Redirect bug submittion details to github.
Browse files Browse the repository at this point in the history
On Crashes page after submitting a crash a button appears that allows
submit a bug with additional details. Instead of directing the user to
bugs.chromium.org we will redirect them to
github.com/brave/brave-browser/issues/new and prefill some of the issue
info.

Fixes brave/brave-browser#10185
  • Loading branch information
mkarolin committed Jun 10, 2020
1 parent 045ffc1 commit d049e3e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "brave/common/brave_features.h"
#include "brave/common/brave_switches.h"
#include "brave/common/network_constants.h"
Expand All @@ -24,6 +26,8 @@

namespace brave {

namespace {

// Update server checks happen from the profile context for admin policy
// installed extensions. Update server checks happen from the system context for
// normal update operations.
Expand All @@ -45,6 +49,32 @@ bool IsUpdaterURL(const GURL& gurl) {
[&gurl](URLPattern pattern) { return pattern.MatchesURL(gurl); });
}

bool RewriteBugReportingURL(const GURL& request_url, GURL* new_url) {
GURL url("https://github.com/brave/brave-browser/issues/new");
std::string query = "title=Crash%20Report&labels=crash";
// We are expecting 3 query keys: comment, template, and labels
base::StringPairs pairs;
if (!base::SplitStringIntoKeyValuePairs(request_url.query(), '=', '&',
&pairs) || pairs.size() != 3) {
return false;
}
for (const auto& pair : pairs) {
if (pair.first == "comment") {
query += "&body=" + pair.second;
base::ReplaceSubstringsAfterOffset(&query, 0, "Chrome", "Brave");
} else if (pair.first != "template" && pair.first != "labels") {
return false;
}
}

GURL::Replacements replacements;
replacements.SetQueryStr(query);
*new_url = url.ReplaceComponents(replacements);
return true;
}

} // namespace

int OnBeforeURLRequest_CommonStaticRedirectWork(
const ResponseCallback& next_callback,
std::shared_ptr<BraveRequestInfo> ctx) {
Expand All @@ -67,6 +97,9 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS, kChromeCastPrefix);
static URLPattern clients4_pattern(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS, kClients4Prefix);
static URLPattern bugsChromium_pattern(
URLPattern::SCHEME_HTTP | URLPattern::SCHEME_HTTPS,
"*://bugs.chromium.org/p/chromium/issues/entry?*");

if (IsUpdaterURL(request_url)) {
replacements.SetQueryStr(request_url.query_piece());
Expand Down Expand Up @@ -97,6 +130,11 @@ int OnBeforeURLRequest_CommonStaticRedirectWorkForGURL(
return net::OK;
}

if (bugsChromium_pattern.MatchesURL(request_url)) {
if (RewriteBugReportingURL(request_url, new_url))
return net::OK;
}

return net::OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,44 @@ TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
EXPECT_EQ(redirect.path(), url.path());
EXPECT_EQ(rc, net::OK);
}

TEST(BraveCommonStaticRedirectNetworkDelegateHelperTest,
RedirectBugsChromium) {
// Check when we will redirect.
const GURL url(
"https://bugs.chromium.org/p/chromium/issues/"
"entry?template=Crash%20Report&comment=IMPORTANT%20Chrome&labels="
"Restrict-View-"
"EditIssue%2CStability-Crash%2CUser-Submitted");
auto request_info = std::make_shared<brave::BraveRequestInfo>(url);

int rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
const GURL redirect = GURL(request_info->new_url_spec);
EXPECT_EQ(redirect.host(), "github.com");
EXPECT_TRUE(redirect.SchemeIs(url::kHttpsScheme));
EXPECT_EQ(redirect.path(), "/brave/brave-browser/issues/new");
EXPECT_EQ(redirect.query(),
"title=Crash%20Report&labels=crash&body=IMPORTANT%20Brave");
EXPECT_EQ(rc, net::OK);

// Check when we should not redirect: wrong query keys count
request_info.reset();
const GURL url_fewer_keys(
"https://bugs.chromium.org/p/chromium/issues/entry?template=A");
request_info = std::make_shared<brave::BraveRequestInfo>(url_fewer_keys);
rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);

// Check when we should not redirect: wrong query keys
request_info.reset();
const GURL url_wrong_keys(
"https://bugs.chromium.org/p/chromium/issues/entry?t=A&l=B&c=C");
request_info = std::make_shared<brave::BraveRequestInfo>(url_wrong_keys);
rc = OnBeforeURLRequest_CommonStaticRedirectWork(ResponseCallback(),
request_info);
EXPECT_TRUE(request_info->new_url_spec.empty());
EXPECT_EQ(rc, net::OK);
}
10 changes: 10 additions & 0 deletions chromium_src/chrome/browser/ui/webui/crashes_ui.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* Copyright (c) 2020 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/. */

#include "brave/browser/version_info.h"

#define GetVersionNumber GetBraveVersionNumberForDisplay
#include "../../../../../../../chrome/browser/ui/webui/crashes_ui.cc"
#undef GetVersionNumber

0 comments on commit d049e3e

Please sign in to comment.