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

WebTorrent Support #356

Merged
merged 6 commits into from
Aug 31, 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.tags*
/.idea/
/browser/resources/brave_extension/
/components/brave_webtorrent/extension/out/
/dist/
/out/
/vendor/requests
Expand Down Expand Up @@ -39,4 +40,4 @@ npm-debug.log
*~
CMakeLists.txt
cmake-build-debug/
coverage/
coverage/
2 changes: 2 additions & 0 deletions brave_paks.gni
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ template("brave_extra_paks") {
"$root_gen_dir/brave/brave_unscaled_resources.pak",
"$root_gen_dir/components/brave_components_resources.pak",
"$target_gen_dir/browser/resources/brave_extension.pak",
"$target_gen_dir/components/brave_webtorrent/brave_webtorrent_resources.pak",
]

deps = [
"//brave/app:brave_generated_resources_grit",
"//brave/app/theme:brave_unscaled_resources",
"//brave/components/brave_webtorrent:resources",
"//brave/components/resources:brave_components_resources_grit",
"//brave/browser/resources:brave_extension_grit",
"//brave/common/extensions/api"
Expand Down
22 changes: 22 additions & 0 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "brave/browser/brave_browser_main_extra_parts.h"
#include "brave/common/webui_url_constants.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_webtorrent/browser/content_browser_client_helper.h"
#include "brave/components/content_settings/core/browser/brave_cookie_settings.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/profiles/profile_io_data.h"
Expand Down Expand Up @@ -80,6 +81,8 @@ void BraveContentBrowserClient::BrowserURLHandlerCreated(
content::BrowserURLHandler* handler) {
// Insert handler for chrome://newtab so that we handle it
// before anything else can.
handler->AddHandlerPair(&webtorrent::HandleMagnetURLRewrite,
content::BrowserURLHandler::null_handler());
handler->AddHandlerPair(&HandleURLRewrite,
&HandleURLReverseRewrite);
ChromeContentBrowserClient::BrowserURLHandlerCreated(handler);
Expand Down Expand Up @@ -120,6 +123,25 @@ BraveContentBrowserClient::AllowWebBluetooth(
return content::ContentBrowserClient::AllowWebBluetoothResult::BLOCK_GLOBALLY_DISABLED;
}

bool BraveContentBrowserClient::HandleExternalProtocol(
const GURL& url,
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
int child_id,
content::NavigationUIData* navigation_data,
bool is_main_frame,
ui::PageTransition page_transition,
bool has_user_gesture) {

if (webtorrent::HandleMagnetProtocol(url, web_contents_getter,
page_transition, has_user_gesture)) {
return true;
}

return ChromeContentBrowserClient::HandleExternalProtocol(
url, web_contents_getter, child_id, navigation_data, is_main_frame,
page_transition, has_user_gesture);
}

bool BraveContentBrowserClient::AllowSetCookie(
const GURL& url,
const GURL& first_party,
Expand Down
9 changes: 9 additions & 0 deletions browser/brave_content_browser_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class BraveContentBrowserClient : public ChromeContentBrowserClient {
int render_process_id,
int render_frame_id) override;

bool HandleExternalProtocol(
const GURL& url,
content::ResourceRequestInfo::WebContentsGetter web_contents_getter,
int child_id,
content::NavigationUIData* navigation_data,
bool is_main_frame,
ui::PageTransition page_transition,
bool has_user_gesture) override;

content::ContentBrowserClient::AllowWebBluetoothResult AllowWebBluetooth(
content::BrowserContext* browser_context,
const url::Origin& requesting_origin,
Expand Down
79 changes: 78 additions & 1 deletion browser/brave_content_browser_client_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,60 @@

#include <vector>

#include "base/path_service.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/common/brave_paths.h"
#include "chrome/browser/ui/browser.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 "net/dns/mock_host_resolver.h"

using BraveContentBrowserClientTest = InProcessBrowserTest;
class BraveContentBrowserClientTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();

content_client_.reset(new ChromeContentClient);
content::SetContentClient(content_client_.get());
browser_content_client_.reset(new BraveContentBrowserClient());
content::SetBrowserClientForTesting(browser_content_client_.get());

host_resolver()->AddRule("*", "127.0.0.1");
content::SetupCrossSiteRedirector(embedded_test_server());

brave::RegisterPathProvider();
base::FilePath test_data_dir;
base::PathService::Get(brave::DIR_TEST_DATA, &test_data_dir);
embedded_test_server()->ServeFilesFromDirectory(test_data_dir);

ASSERT_TRUE(embedded_test_server()->Start());

url_ = embedded_test_server()->GetURL("a.com", "/magnet.html");
magnet_url_ = GURL("magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent");
extension_url_ = GURL("chrome-extension://lgjmpdmojkpocjcopdikifhejkkjglho/extension/brave_webtorrent.html?magnet%3A%3Fxt%3Durn%3Abtih%3Add8255ecdc7ca55fb0bbf81323d87062db1f6d1c%26dn%3DBig%2BBuck%2BBunny%26tr%3Dudp%253A%252F%252Fexplodie.org%253A6969%26tr%3Dudp%253A%252F%252Ftracker.coppersurfer.tk%253A6969%26tr%3Dudp%253A%252F%252Ftracker.empire-js.us%253A1337%26tr%3Dudp%253A%252F%252Ftracker.leechers-paradise.org%253A6969%26tr%3Dudp%253A%252F%252Ftracker.opentrackr.org%253A1337%26tr%3Dwss%253A%252F%252Ftracker.btorrent.xyz%26tr%3Dwss%253A%252F%252Ftracker.fastcast.nz%26tr%3Dwss%253A%252F%252Ftracker.openwebtorrent.com%26ws%3Dhttps%253A%252F%252Fwebtorrent.io%252Ftorrents%252F%26xs%3Dhttps%253A%252F%252Fwebtorrent.io%252Ftorrents%252Fbig-buck-bunny.torrent");
}

void TearDown() override {
browser_content_client_.reset();
content_client_.reset();
}

const GURL& url() { return url_; }
const GURL& magnet_url() { return magnet_url_; }
const GURL& extension_url() { return extension_url_; }

private:
GURL url_;
GURL magnet_url_;
GURL extension_url_;
ContentSettingsPattern top_level_page_pattern_;
ContentSettingsPattern empty_pattern_;
std::unique_ptr<ChromeContentClient> content_client_;
std::unique_ptr<BraveContentBrowserClient> browser_content_client_;
};

IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, CanLoadChromeURL) {
GURL chrome_settings_url("chrome://settings/");
Expand Down Expand Up @@ -41,3 +89,32 @@ IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, CanLoadCustomBravePages) {
url.spec().c_str());
});
}

IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, RewriteMagnetURLURLBar) {
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();

ui_test_utils::NavigateToURL(browser(), magnet_url());
ASSERT_TRUE(WaitForLoadStop(contents));
EXPECT_STREQ(contents->GetLastCommittedURL().spec().c_str(),
magnet_url().spec().c_str()) << "URL visible to users should stay as the magnet URL";
content::NavigationEntry* entry = contents->GetController().GetLastCommittedEntry();
EXPECT_STREQ(entry->GetURL().spec().c_str(),
extension_url().spec().c_str()) << "Real URL should be extension URL";
}

IN_PROC_BROWSER_TEST_F(BraveContentBrowserClientTest, RewriteMagnetURLLink) {
content::WebContents* contents = browser()->tab_strip_model()->GetActiveWebContents();
ui_test_utils::NavigateToURL(browser(), url());
ASSERT_TRUE(WaitForLoadStop(contents));
bool value;
EXPECT_TRUE(ExecuteScriptAndExtractBool(contents, "clickMagnetLink();",
&value));
EXPECT_TRUE(value);
ASSERT_TRUE(WaitForLoadStop(contents));

EXPECT_STREQ(contents->GetLastCommittedURL().spec().c_str(),
magnet_url().spec().c_str()) << "URL visible to users should stay as the magnet URL";
content::NavigationEntry* entry = contents->GetController().GetLastCommittedEntry();
EXPECT_STREQ(entry->GetURL().spec().c_str(),
extension_url().spec().c_str()) << "Real URL should be extension URL";
}
1 change: 1 addition & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ source_set("extensions") {

deps = [
"//brave/browser/resources:brave_extension_grit",
"//brave/components/brave_webtorrent:resources",
"//chrome/browser",
"//content/public/browser",
"//extensions/browser",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "brave/browser/extensions/brave_component_extension_resource_manager.h"

#include "brave/browser/resources/grit/brave_extension_resources_map.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources_map.h"

namespace extensions {

Expand All @@ -13,6 +14,10 @@ BraveComponentExtensionResourceManager() {
AddComponentResourceEntries(
kBraveExtension,
kBraveExtensionSize);

AddComponentResourceEntries(
kBraveWebtorrentResources,
kBraveWebtorrentResourcesSize);
}

BraveComponentExtensionResourceManager::
Expand Down
6 changes: 6 additions & 0 deletions browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/command_line.h"
#include "brave/common/brave_switches.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "components/grit/brave_components_resources.h"

namespace extensions {
Expand Down Expand Up @@ -33,6 +34,11 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
brave_extension_path.Append(FILE_PATH_LITERAL("brave_extension"));
Add(IDR_BRAVE_EXTENSON, brave_extension_path);
}

base::FilePath brave_webtorrent_path(FILE_PATH_LITERAL(""));
brave_webtorrent_path =
brave_webtorrent_path.Append(FILE_PATH_LITERAL("brave_webtorrent"));
Add(IDR_BRAVE_WEBTORRENT, brave_webtorrent_path);
}

} // namespace extensions
4 changes: 3 additions & 1 deletion browser/extensions/brave_extension_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bool IsWhitelisted(const extensions::Extension* extension) {
}
static std::vector<std::string> whitelist({
brave_extension_id,
brave_webtorrent_extension_id,
pdfjs_extension_id,
// 1Password
"aomjjhallfgjeglblehebfpbcfeobpgk",
Expand Down Expand Up @@ -126,7 +127,8 @@ bool BraveExtensionProvider::UserMayLoad(const Extension* extension,

bool BraveExtensionProvider::MustRemainInstalled(const Extension* extension,
base::string16* error) const {
return extension->id() == brave_extension_id;
return extension->id() == brave_extension_id ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be here? Do we want it always loaded by default or on user activation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer loaded by default for now if that's OK.
And address it in a follow-up issue to make it able to be turned on and off by user at anytime.

AFAIK, if we want to disable a component extension from UI, it should probably go through chrome://extensions.
First, to see component extensions listed, we'll need to pass --show-component-extension-options when run it.
But it seems I can't toggle the on&off switch even if the component extension is shown for our current version.
I would like to address this in a separate issue.
@bridiver wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, it seems it is still loaded by default even when I remove this line.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't affect default loading, it only affects the ability to uninstall

extension->id() == brave_webtorrent_extension_id;
}

} // namespace extensions
1 change: 1 addition & 0 deletions browser/resources/resource_ids
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
"brave/app/theme/brave_unscaled_resources.grd": {
"includes": [36500],
},
# brave webtorrent 37500
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* 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/. */

#define IsComponentExtensionWhitelisted IsComponentExtensionWhitelisted_ChromiumImpl
#include "../../../../../../chrome/browser/extensions/component_extensions_whitelist/whitelist.cc"
#undef IsComponentExtensionWhitelisted

#include "brave/common/extensions/extension_constants.h"
#include "components/grit/brave_components_resources.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"

namespace extensions {

bool IsComponentExtensionWhitelisted(const std::string& extension_id) {
const char* const kAllowed[] = {
brave_extension_id,
brave_webtorrent_extension_id
};

for (size_t i = 0; i < arraysize(kAllowed); ++i) {
if (extension_id == kAllowed[i])
return true;
}

return IsComponentExtensionWhitelisted_ChromiumImpl(extension_id);
}

bool IsComponentExtensionWhitelisted(int manifest_resource_id) {
switch (manifest_resource_id) {
// Please keep the list in alphabetical order.
case IDR_BRAVE_EXTENSON:
case IDR_BRAVE_WEBTORRENT:
return true;
}

return IsComponentExtensionWhitelisted_ChromiumImpl(manifest_resource_id);
}

} // namespace extensions

3 changes: 3 additions & 0 deletions chromium_src/chrome/browser/profiles/profile_io_data.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "brave/browser/net/brave_profile_network_delegate.h"

#include "../../../../../../chrome/browser/profiles/profile_io_data.cc"
1 change: 1 addition & 0 deletions common/extensions/api/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ json_features("manifest_features") {
sources = [
"//chrome/common/extensions/api/_manifest_features.json",
"//extensions/common/api/_manifest_features.json",
"_manifest_features.json",
]
}

Expand Down
17 changes: 16 additions & 1 deletion common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,20 @@
"matches": [
"chrome://newtab/*"
]
}]
}],
"sockets.tcp": {
"dependencies": ["manifest:sockets"],
"contexts": ["blessed_extension"],
"whitelist": ["3D9518A72EB02667A773B69DBA9E72E0F4A37423"]
},
"sockets.tcpServer": {
"dependencies": ["manifest:sockets"],
"contexts": ["blessed_extension"],
"whitelist": ["3D9518A72EB02667A773B69DBA9E72E0F4A37423"]
},
"sockets.udp": {
"dependencies": ["manifest:sockets"],
"contexts": ["blessed_extension"],
"whitelist": ["3D9518A72EB02667A773B69DBA9E72E0F4A37423"]
}
}
11 changes: 11 additions & 0 deletions common/extensions/api/_manifest_features.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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/.

{
"sockets": {
"channel": "stable",
"extension_types": ["extension", "platform_app"],
"whitelist": ["3D9518A72EB02667A773B69DBA9E72E0F4A37423"]
}
}
1 change: 1 addition & 0 deletions common/extensions/extension_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include "brave/common/extensions/extension_constants.h"

const char brave_extension_id[] = "mnojpmjdmbbfmejpflffifhffcmidifd";
const char brave_webtorrent_extension_id[] = "lgjmpdmojkpocjcopdikifhejkkjglho";
const char pdfjs_extension_id[] = "oemmndcbldboiebfnladdacbdfmadadm";
const char widevine_extension_id[] = "oimompecagnajdejgnnjijobebaeigek";
1 change: 1 addition & 0 deletions common/extensions/extension_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

extern const char brave_extension_id[];
extern const char brave_webtorrent_extension_id[];
extern const char pdfjs_extension_id[];
extern const char widevine_extension_id[];
1 change: 1 addition & 0 deletions common/url_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
#include "brave/common/url_constants.h"

const char kBraveUIScheme[] = "brave";
const char kMagnetScheme[] = "magnet";
const char kWidevineMoreInfoURL[] = "https://www.eff.org/issues/drm";
const char kWidevineTOS[] = "https://policies.google.com/terms";
1 change: 1 addition & 0 deletions common/url_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define BRAVE_COMMON_URL_CONSTANTS_H_

extern const char kBraveUIScheme[];
extern const char kMagnetScheme[];
extern const char kWidevineMoreInfoURL[];
extern const char kWidevineTOS[];

Expand Down
2 changes: 1 addition & 1 deletion components/brave_adblock_ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ transpile_web_ui("brave_adblock_ui") {
"reducers/adblock_reducer.ts",
]

bundle_name = "brave_adblock.bundle.js"
bundle_names = ["brave_adblock.bundle.js"]
}
3 changes: 2 additions & 1 deletion components/brave_new_tab_ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ transpile_web_ui("brave_new_tab_ui") {
"reducers/index.ts",
"reducers/new_tab_reducer.tsx",
]
bundle_name = "brave_new_tab.bundle.js"

bundle_names = ["brave_new_tab.bundle.js"]
}
2 changes: 1 addition & 1 deletion components/brave_rewards_ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ transpile_web_ui("brave_rewards_ui") {
"reducers/rewards_reducer.ts",
]

bundle_name = "brave_rewards.bundle.js"
bundle_names = ["brave_rewards.bundle.js"]
}
Loading