-
Notifications
You must be signed in to change notification settings - Fork 873
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contents script in brave extension detects whether window.web3 is accessed or not from current tab and sends a message to background script to notify to browser. When browser gets "braveShields.dappAvailable", it shows wallet extension install guide when dapp detection pref is enabled.
- Loading branch information
Showing
21 changed files
with
325 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* 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/dapp/dapp_utils.h" | ||
|
||
#include "base/callback.h" | ||
#include "brave/browser/dapp/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 | ||
|
||
bool DappDetectionEnabled(content::BrowserContext* browser_context) { | ||
Profile* profile = Profile::FromBrowserContext(browser_context); | ||
return profile->GetPrefs()->GetBoolean(kDappDetectionEnabled); | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* 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_DAPP_DAPP_UTILS_H_ | ||
#define BRAVE_BROWSER_DAPP_DAPP_UTILS_H_ | ||
|
||
#include "base/callback_forward.h" | ||
|
||
namespace content { | ||
class BrowserContext; | ||
class WebContents; | ||
} // content | ||
|
||
bool DappDetectionEnabled(content::BrowserContext* browser_context); | ||
void RequestWalletInstallationPermission(content::WebContents* web_contents); | ||
|
||
void SetQuitClosureForDappDetectionTest(const base::Closure& quit_closure); | ||
|
||
#endif // BRAVE_BROWSER_DAPP_DAPP_UTILS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* 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/dapp/wallet_installation_permission_request.h" | ||
|
||
#include "brave/grit/brave_generated_resources.h" | ||
#include "chrome/app/vector_icons/vector_icons.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() { | ||
// TODO(shong): Install wallet. | ||
NOTIMPLEMENTED(); | ||
} | ||
|
||
void WalletInstallationPermissionRequest::PermissionDenied() { | ||
// Do nothing. | ||
} | ||
|
||
void WalletInstallationPermissionRequest::Cancelled() { | ||
// Do nothing. | ||
} | ||
|
||
void WalletInstallationPermissionRequest::RequestFinished() { | ||
delete this; | ||
} | ||
|
||
PermissionRequestType | ||
WalletInstallationPermissionRequest::GetPermissionRequestType() const { | ||
return PermissionRequestType::PERMISSION_WALLET; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_DAPP_WALLET_INSTALLATION_PERMISSION_REQUEST_H_ | ||
#define BRAVE_BROWSER_DAPP_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_DAPP_WALLET_INSTALLATION_PERMISSION_REQUEST_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
...onents/brave_extension/extension/brave_extension/background/events/dappDetectionEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* 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/. */ | ||
|
||
// content script listener for dapp detection. | ||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { | ||
switch (msg.type) { | ||
case 'dappAvailable': { | ||
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs: chrome.tabs.Tab[]) { | ||
// Only notify when this message is comes from active tab. | ||
if (msg.location === tabs[0].url) { | ||
chrome.braveShields.dappAvailable(tabs[0].id) | ||
} | ||
}) | ||
break | ||
} | ||
} | ||
}) |
Oops, something went wrong.