Skip to content

Commit

Permalink
Merge pull request #2678 from brave/dapp_detection
Browse files Browse the repository at this point in the history
Add dapp detection
  • Loading branch information
bbondy committed Jul 18, 2019
2 parents de551ba + 4e5c1e0 commit 48ad51c
Show file tree
Hide file tree
Showing 37 changed files with 721 additions and 85 deletions.
3 changes: 3 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@
<message name="IDS_WIDEVINE_PERMISSION_REQUEST_TEXT_FRAGMENT" desc="Text fragment for Widevine permission request. 'Widevine' is the name of a plugin and should not be translated.">
Install and run Widevine
</message>
<message name="IDS_WALLET_PERMISSION_REQUEST_TEXT_FRAGMENT" desc="Text fragment for wallet installation permission request.">
Install Crypto Wallets
</message>
<if expr="is_linux">
<message name="IDS_WIDEVINE_PERMISSION_REQUEST_TEXT_FRAGMENT_INSTALL" desc="Text fragment for Widevine permission request. 'Widevine' is the name of a plugin and should not be translated.">
Install Widevine
Expand Down
10 changes: 10 additions & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import("//brave/build/config.gni")
import("//brave/browser/tor/buildflags/buildflags.gni")
import("//brave/components/brave_ads/browser/buildflags/buildflags.gni")
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni")
import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni")
import("//build/buildflag_header.gni")
import("//build/config/features.gni")
Expand Down Expand Up @@ -154,6 +155,15 @@ source_set("browser_process") {
]
}

if (brave_wallet_enabled) {
sources += [
"brave_wallet/brave_wallet_utils.cc",
"brave_wallet/brave_wallet_utils.h",
"brave_wallet/wallet_installation_permission_request.cc",
"brave_wallet/wallet_installation_permission_request.h",
]
}

if (enable_tor) {
deps += [
"//brave/components/services/tor/public/cpp:manifest",
Expand Down
31 changes: 31 additions & 0 deletions browser/brave_wallet/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* Copyright (c) 2019 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/brave_wallet/brave_wallet_utils.h"

#include "base/callback.h"
#include "brave/browser/brave_wallet/wallet_installation_permission_request.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/permissions/permission_request_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"

namespace {
base::Closure g_quit_closure_for_test;
} // namespace

void RequestWalletInstallationPermission(content::WebContents* web_contents) {
DCHECK(web_contents);

if (g_quit_closure_for_test)
g_quit_closure_for_test.Run();

PermissionRequestManager::FromWebContents(web_contents)->AddRequest(
new WalletInstallationPermissionRequest(web_contents));
}

void SetQuitClosureForDappDetectionTest(const base::Closure& quit_closure) {
g_quit_closure_for_test = quit_closure;
}
20 changes: 20 additions & 0 deletions browser/brave_wallet/brave_wallet_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Copyright (c) 2019 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/. */

#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_

#include "base/callback_forward.h"

namespace content {
class BrowserContext;
class WebContents;
} // content

void RequestWalletInstallationPermission(content::WebContents* web_contents);

void SetQuitClosureForDappDetectionTest(const base::Closure& quit_closure);

#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_
59 changes: 59 additions & 0 deletions browser/brave_wallet/wallet_installation_permission_request.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* Copyright (c) 2019 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/brave_wallet/wallet_installation_permission_request.h"

#include "brave/browser/ui/brave_pages.h"
#include "brave/grit/brave_generated_resources.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ui/browser_finder.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

WalletInstallationPermissionRequest::WalletInstallationPermissionRequest(
content::WebContents* web_contents)
: web_contents_(web_contents) {
}

WalletInstallationPermissionRequest::~WalletInstallationPermissionRequest() {
}

PermissionRequest::IconId
WalletInstallationPermissionRequest::GetIconId() const {
return kExtensionIcon;
}

base::string16
WalletInstallationPermissionRequest::GetMessageTextFragment() const {
return l10n_util::GetStringUTF16(
IDS_WALLET_PERMISSION_REQUEST_TEXT_FRAGMENT);
}

GURL WalletInstallationPermissionRequest::GetOrigin() const {
return web_contents_->GetVisibleURL();
}

void WalletInstallationPermissionRequest::PermissionGranted() {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
brave::ShowBraveWallet(browser);
}

void WalletInstallationPermissionRequest::PermissionDenied() {
// Do nothing.
}

void WalletInstallationPermissionRequest::Cancelled() {
// Do nothing.
}

void WalletInstallationPermissionRequest::RequestFinished() {
delete this;
}

PermissionRequestType
WalletInstallationPermissionRequest::GetPermissionRequestType() const {
return PermissionRequestType::PERMISSION_WALLET;
}
41 changes: 41 additions & 0 deletions browser/brave_wallet/wallet_installation_permission_request.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright (c) 2019 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/. */

#ifndef BRAVE_BROWSER_BRAVE_WALLET_WALLET_INSTALLATION_PERMISSION_REQUEST_H_
#define BRAVE_BROWSER_BRAVE_WALLET_WALLET_INSTALLATION_PERMISSION_REQUEST_H_

#include "chrome/browser/permissions/permission_request.h"

namespace content {
class WebContents;
}

class GURL;

class WalletInstallationPermissionRequest : public PermissionRequest {
public:
explicit WalletInstallationPermissionRequest(
content::WebContents* web_contents);
~WalletInstallationPermissionRequest() override;

private:
// PermissionRequest overrides:
PermissionRequest::IconId GetIconId() const override;
base::string16 GetMessageTextFragment() const override;
GURL GetOrigin() const override;
void PermissionGranted() override;
void PermissionDenied() override;
void Cancelled() override;
void RequestFinished() override;
PermissionRequestType GetPermissionRequestType() const override;

// It's safe to use this raw |web_contents_| because this request is deleted
// by PermissionManager that is tied with this |web_contents_|.
content::WebContents* web_contents_;

DISALLOW_COPY_AND_ASSIGN(WalletInstallationPermissionRequest);
};

#endif // BRAVE_BROWSER_BRAVE_WALLET_WALLET_INSTALLATION_PERMISSION_REQUEST_H_
2 changes: 2 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ source_set("extensions") {

if (brave_wallet_enabled) {
sources += [
"api/brave_wallet_api.cc",
"api/brave_wallet_api.h",
"brave_wallet_navigation_throttle.cc",
"brave_wallet_navigation_throttle.h",
]
Expand Down
3 changes: 2 additions & 1 deletion browser/extensions/api/brave_shields_api.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 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/. */

Expand Down
142 changes: 72 additions & 70 deletions browser/extensions/api/brave_shields_api_browsertest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 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/. */

Expand Down Expand Up @@ -35,74 +36,74 @@ using extension_function_test_utils::RunFunctionAndReturnError;
using extension_function_test_utils::RunFunctionAndReturnSingleResult;

class BraveShieldsAPIBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
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());
extension_ = extensions::ExtensionBuilder("Test").Build();
content_settings_ =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
}

content::WebContents* active_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}

scoped_refptr<const extensions::Extension> extension() {
return extension_;
}

HostContentSettingsMap* content_settings() const {
return content_settings_;
}

void BlockScripts() {
content_settings_->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
}

bool NavigateToURLUntilLoadStop(
const std::string& origin, const std::string& path) {
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL(origin, path));

return WaitForLoadStop(active_contents());
}

void AllowScriptOriginOnce(const std::string& origin) {
// run extension function to temporarily allow origin
scoped_refptr<BraveShieldsAllowScriptsOnceFunction> function(
new BraveShieldsAllowScriptsOnceFunction());
function->set_extension(extension().get());
function->set_has_callback(true);

const GURL url(embedded_test_server()->GetURL(origin, "/simple.js"));
const std::string allow_origin = url.GetOrigin().spec();
int tabId = extensions::ExtensionTabUtil::GetTabId(active_contents());

RunFunctionAndReturnSingleResult(
function.get(),
"[[\"" + allow_origin + "\"], " + std::to_string(tabId) + "]",
browser());

// reload page with a.com temporarily allowed
active_contents()->GetController().Reload(content::ReloadType::NORMAL,
true);
}

private:
HostContentSettingsMap* content_settings_;
scoped_refptr<const extensions::Extension> extension_;
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
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());
extension_ = extensions::ExtensionBuilder("Test").Build();
content_settings_ =
HostContentSettingsMapFactory::GetForProfile(browser()->profile());
}

content::WebContents* active_contents() {
return browser()->tab_strip_model()->GetActiveWebContents();
}

scoped_refptr<const extensions::Extension> extension() {
return extension_;
}

HostContentSettingsMap* content_settings() const {
return content_settings_;
}

void BlockScripts() {
content_settings_->SetContentSettingCustomScope(
ContentSettingsPattern::Wildcard(), ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_JAVASCRIPT, "", CONTENT_SETTING_BLOCK);
}

bool NavigateToURLUntilLoadStop(
const std::string& origin, const std::string& path) {
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL(origin, path));

return WaitForLoadStop(active_contents());
}

void AllowScriptOriginOnce(const std::string& origin) {
// run extension function to temporarily allow origin
scoped_refptr<BraveShieldsAllowScriptsOnceFunction> function(
new BraveShieldsAllowScriptsOnceFunction());
function->set_extension(extension().get());
function->set_has_callback(true);

const GURL url(embedded_test_server()->GetURL(origin, "/simple.js"));
const std::string allow_origin = url.GetOrigin().spec();
int tabId = extensions::ExtensionTabUtil::GetTabId(active_contents());

RunFunctionAndReturnSingleResult(
function.get(),
"[[\"" + allow_origin + "\"], " + std::to_string(tabId) + "]",
browser());

// reload page with a.com temporarily allowed
active_contents()->GetController().Reload(content::ReloadType::NORMAL,
true);
}

private:
HostContentSettingsMap* content_settings_;
scoped_refptr<const extensions::Extension> extension_;
};

IN_PROC_BROWSER_TEST_F(BraveShieldsAPIBrowserTest, AllowScriptsOnce) {
Expand All @@ -129,7 +130,8 @@ IN_PROC_BROWSER_TEST_F(BraveShieldsAPIBrowserTest, AllowScriptsOnce) {
// same doc navigation
ui_test_utils::NavigateToURL(
browser(),
embedded_test_server()->GetURL("a.com", "/load_js_from_origins.html#foo"));
embedded_test_server()->GetURL("a.com",
"/load_js_from_origins.html#foo"));
EXPECT_TRUE(WaitForLoadStop(active_contents()));
EXPECT_EQ(active_contents()->GetAllFrames().size(), 2u) <<
"Scripts from a.com should be temporarily allowed for same doc navigation.";
Expand Down
Loading

0 comments on commit 48ad51c

Please sign in to comment.