Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load magnet url instead of searching #903

Merged
merged 3 commits into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ source_set("browser_process") {
"//brave/components/brave_rewards/browser",
"//brave/components/brave_shields/browser:brave_shields",
"//brave/components/brave_sync",
"//brave/components/brave_webtorrent/browser",
"//brave/components/content_settings/core/browser",
"//chrome/common",
"//components/component_updater",
Expand Down Expand Up @@ -110,6 +111,7 @@ source_set("browser") {
"//base",
"//brave/components/brave_referrals/browser",
"//brave/components/brave_shields/browser:brave_shields",
"//brave/components/brave_webtorrent/browser",
"//brave/components/resources",
"//brave/browser/resources:brave_extension_grit",
"//chrome/browser",
Expand Down
11 changes: 9 additions & 2 deletions browser/autocomplete/brave_autocomplete_scheme_classifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

#include "base/strings/string_util.h"
#include "brave/common/url_constants.h"
#include "brave/components/brave_webtorrent/browser/webtorrent_util.h"
#include "chrome/browser/profiles/profile.h"

// See the BraveAutocompleteProviderClient why GetOriginalProfile() is fetched.
// All services except TemplateURLService exposed from AutocompleteClassifier
// uses original profile. So, |profile_| should be original profile same as
// base class does.
BraveAutocompleteSchemeClassifier::BraveAutocompleteSchemeClassifier(
Profile* profile)
: ChromeAutocompleteSchemeClassifier(profile->GetOriginalProfile()) {
: ChromeAutocompleteSchemeClassifier(profile->GetOriginalProfile()),
profile_(profile->GetOriginalProfile()) {
}

BraveAutocompleteSchemeClassifier::~BraveAutocompleteSchemeClassifier() {
Expand All @@ -25,7 +30,9 @@ BraveAutocompleteSchemeClassifier::GetInputTypeForScheme(
return metrics::OmniboxInputType::INVALID;
}
if (base::IsStringASCII(scheme) &&
base::LowerCaseEqualsASCII(scheme, kBraveUIScheme)) {
(base::LowerCaseEqualsASCII(scheme, kBraveUIScheme) ||
(webtorrent::IsWebtorrentEnabled(profile_) &&
base::LowerCaseEqualsASCII(scheme, kMagnetScheme)))) {
return metrics::OmniboxInputType::URL;
}

Expand Down
2 changes: 2 additions & 0 deletions browser/autocomplete/brave_autocomplete_scheme_classifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class BraveAutocompleteSchemeClassifier : public ChromeAutocompleteSchemeClassif
const std::string& scheme) const override;

private:
Profile* profile_;

DISALLOW_COPY_AND_ASSIGN(BraveAutocompleteSchemeClassifier);
};

Expand Down
12 changes: 12 additions & 0 deletions browser/brave_content_browser_client_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "net/dns/mock_host_resolver.h"
Expand Down Expand Up @@ -140,6 +142,16 @@ IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, RewriteMagnetURLLink) {
extension_url().spec().c_str()) << "Real URL should be extension URL";
}

IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, TypedMagnetURL) {
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
content::TestNavigationObserver observer(web_contents);
LocationBar* location_bar = browser()->window()->GetLocationBar();
ui_test_utils::SendToOmniboxAndSubmit(location_bar, magnet_url().spec());
observer.Wait();
EXPECT_EQ(magnet_url(), web_contents->GetLastCommittedURL().spec());
}

IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, ReverseRewriteTorrentURL) {
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::NavigateToURL(browser(), torrent_extension_url());
Expand Down
16 changes: 16 additions & 0 deletions components/brave_webtorrent/browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
source_set("browser") {
sources = [
"content_browser_client_helper.h",
"webtorrent_util.cc",
"webtorrent_util.h",
]

deps = [
"//base",
"//brave/common",
"//content/public/browser",
"//extensions/browser",
"//extensions/common",
"//net",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/task/post_task.h"
#include "brave/common/url_constants.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/components/brave_webtorrent/browser/webtorrent_util.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
Expand Down Expand Up @@ -52,15 +53,6 @@ static bool HandleTorrentURLReverseRewrite(GURL* url,
return false;
}

static bool IsWebtorrentEnabled(content::BrowserContext* browser_context) {
bool isTorProfile =
Profile::FromBrowserContext(browser_context)->IsTorProfile();
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(browser_context);
return !isTorProfile &&
registry->enabled_extensions().Contains(brave_webtorrent_extension_id);
}

static bool HandleTorrentURLRewrite(GURL* url,
content::BrowserContext* browser_context) {
if (!IsWebtorrentEnabled(browser_context)) return false;
Expand Down
23 changes: 23 additions & 0 deletions components/brave_webtorrent/browser/webtorrent_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* 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/components/brave_webtorrent/browser/webtorrent_util.h"

#include "brave/common/extensions/extension_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/extension_registry.h"

namespace webtorrent {

bool IsWebtorrentEnabled(content::BrowserContext* browser_context) {
bool isTorProfile =
Profile::FromBrowserContext(browser_context)->IsTorProfile();
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(browser_context);
return !isTorProfile &&
registry->enabled_extensions().Contains(brave_webtorrent_extension_id);
}

} // webtorrent

18 changes: 18 additions & 0 deletions components/brave_webtorrent/browser/webtorrent_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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/. */

#ifndef BRAVE_COMPONENTS_BRAVE_WEBTORRENT_BROWSER_WEBTORRENT_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_WEBTORRENT_BROWSER_WEBTORRENT_UTIL_H_

namespace content {
class BrowserContext;
}

namespace webtorrent {

bool IsWebtorrentEnabled(content::BrowserContext* browser_context);

} // webtorrent

#endif // BRAVE_COMPONENTS_BRAVE_WEBTORRENT_BROWSER_WEBTORRENT_UTIL_H_