Skip to content

Commit

Permalink
Added IPNS keys manager UI to IPFS Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
spylogsster committed May 15, 2021
1 parent 33a4964 commit 0172152
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 64 deletions.
9 changes: 9 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,15 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_IPNS_KEYS_START_NODE_ERROR" desc="The error message when node was unable to start on IPFS keys manager page">
IPFS service launch error, more information is available on the <ph name="BEGIN_LINK">&lt;a target="_blank" rel="noopener noreferrer" style="text-decoration: underline;" href="chrome://ipfs-internals/"&gt;</ph>diagnostic page<ph name="END_LINK">&lt;/a&gt;</ph>
</message>
<message name="IDS_SETTINGS_IPNS_KEYS_IMPORT_ERROR" desc="The error text if ipns keys import failed">
'<ph name="KEY_NAME">$1<ex>MyCustomKey</ex></ph>' key import failed
</message>
<message name="IDS_SETTINGS_IPNS_KEYS_IMPORT_BUTTON_TITLE" desc="The title of the button to import ipns keys">
Import
</message>
<message name="IDS_SETTINGS_IPNS_KEYS_GENERATE_BUTTON_TITLE" desc="The title of the button to import ipns keys">
Generate
</message>
<message name="IDS_SETTINGS_IPNS_KEYS_START_NODE" desc="The button text to start node on IPFS keys manager page">
Start node
</message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<script type="module" src="../settings_shared_css.js"></script>

<style include="settings-shared">
#submit {
margin-left : 10px;
}
</style>
<cr-dialog show-close-button show-on-attach>
<div slot="title">$i18n{ipfsAddKeyDialogTitle}</div>
Expand All @@ -14,9 +17,12 @@
</cr-input>
</div>
<div slot="button-container">
<cr-button class="action-button" on-click="onImportKeyTap_" disabled="[[!isSubmitButtonEnabled_]]">
$i18n{ipfsKeyImport}
</cr-button>
<cr-button class="action-button" id="submit" on-click="handleSubmit_"
disabled="[[!isSubmitButtonEnabled_]]">
Submit
$i18n{ipfsKeyGenerate}
</cr-button>
</div>
</cr-dialog>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ Polymer({
this.isNameValid_ = value;
this.isSubmitButtonEnabled_ = value;
},

onImportKeyTap_: function(item) {
this.browserProxy_.importIpnsKey(this.$.key.value)
this.fire('close');
},
handleSubmit_: function() {
var name = this.$.key.value
this.browserProxy_.addIpnsKey(name).then(json => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class BraveIPFSBrowserProxy {
getIPFSResolveMethodList () {}
getIPFSEnabled () {}
setIPFSStorageMax (value) {}
importIpnsKey () {}
}

/**
Expand All @@ -27,6 +28,12 @@ export class BraveIPFSBrowserProxyImpl {
chrome.send('setIPFSStorageMax', [value])
}

importIpnsKey (value) {
chrome.send('importIpnsKey', [value])
}
notifyIpfsNodeStatus () {
chrome.send('notifyIpfsNodeStatus', [])
}
launchIPFSService () {
return new Promise(resolve => {
if (!chrome.ipfs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ Polymer({
});
this.browserProxy_.getIPFSEnabled().then(enabled => {
this.ipfsEnabled_ = enabled

});

this.addWebUIListener('brave-ipfs-node-status-changed', (launched) => {
this.isLocalNodeLaunched_ = launched
})

this.browserProxy_.notifyIpfsNodeStatus();
window.addEventListener('load', this.onLoad_.bind(this));
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@
<div class="settings-row">
<div class="flex cr-padded-text">
$i18n{ipfsKeysListTitle}
<div class="secondary error-text" id="key-import-error" hidden="[[!importKeysError_]]">
</div>
</div>
<cr-button on-click="onAddKeyTap_" >
$i18n{add}
</cr-button>
</div>
<div class="flex" id="listContainer">
<iron-list items="[[keys_]]" class="flex"
<iron-list items="[[keys_]]" class="flex" id="keysList"
preserve-focus risk-selection>
<template>
<div class="settings-box">
Expand Down
38 changes: 29 additions & 9 deletions browser/resources/settings/brave_ipfs_page/p2p_keys_subpage.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Polymer({
type: Boolean,
value: false,
},
importKeysError_: {
type: Boolean,
value: false,
},
showAddp2pKeyDialog_: {
type: Boolean,
value: false,
Expand All @@ -61,23 +65,43 @@ Polymer({
this.addWebUIListener('brave-ipfs-node-status-changed', (launched) => {
this.onServiceLaunched(launched)
})
this.addWebUIListener('brave-ipfs-keys-loaded', (launched) => {
this.addWebUIListener('brave-ipfs-keys-loaded', (success) => {
this.updateKeys()
})
this.addWebUIListener('brave-ipfs-key-imported', (key, value, success) => {
this.importKeysError_ = !success
if (this.importKeysError_ ) {
const errorLabel = (this.$$('#key-import-error'));
errorLabel.textContent = this.i18n('ipfsImporKeysError', key)
return;
}
this.keys_.push({ "name" : key, "value" : value});
this.notifyKeylist();
})
},
notifyKeylist: function() {
const keysList =
/** @type {IronListElement} */ (this.$$('#keysList'));
if (keysList) {
keysList.notifyResize();
}
},

toggleUILayout: function(launched) {
this.launchNodeButtonEnabled_ = !launched;
this.localNodeLaunched = launched
this.importKeysError_ = false
if (launched) {
this.localNodeLaunchError_ = false;
this.localNodeLaunchError_ = false
} else {
this.showAddp2pKeyDialog_ = false
}
},

onServiceLaunched: function(success) {
this.toggleUILayout(success)
if (success) {
this.updateKeys();
}
},

onStartNodeKeyTap_: function() {
Expand All @@ -91,12 +115,7 @@ Polymer({
/*++++++
* @override */
ready: function() {
this.onServiceLaunched(this.localNodeLaunched)
window.addEventListener('load', this.onLoad_.bind(this));
},

onLoad_: function() {
this.updateKeys();
this.browserProxy_.notifyIpfsNodeStatus();
},

isDefaultKey_: function(name) {
Expand All @@ -113,6 +132,7 @@ Polymer({
return;
this.keys_ = JSON.parse(keys);
this.toggleUILayout(true)
this.notifyKeylist();
});
},

Expand Down
106 changes: 87 additions & 19 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/browser/ui/webui/settings/brave_default_extensions_handler.h"

#include <memory>
#include <string>

#include "base/bind.h"
Expand All @@ -25,6 +26,7 @@
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/flags_ui/flags_ui_constants.h"
Expand Down Expand Up @@ -57,6 +59,7 @@
#if BUILDFLAG(IPFS_ENABLED)
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/keys/ipns_keys_manager.h"
#include "brave/components/ipfs/pref_names.h"
#endif

Expand All @@ -77,6 +80,26 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
if (service) {
ipfs_service_observer_.Observe(service);
}
web_ui()->RegisterMessageCallback(
"notifyIpfsNodeStatus",
base::BindRepeating(&BraveDefaultExtensionsHandler::CheckIpfsNodeStatus,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setIPFSStorageMax",
base::BindRepeating(&BraveDefaultExtensionsHandler::SetIPFSStorageMax,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"importIpnsKey",
base::BindRepeating(&BraveDefaultExtensionsHandler::ImportIpnsKey,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"launchIPFSService",
base::BindRepeating(&BraveDefaultExtensionsHandler::LaunchIPFSService,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"shutdownIPFSService",
base::BindRepeating(&BraveDefaultExtensionsHandler::ShutdownIPFSService,
base::Unretained(this)));
#endif

web_ui()->RegisterMessageCallback(
Expand All @@ -102,19 +125,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"setMediaRouterEnabled",
base::BindRepeating(&BraveDefaultExtensionsHandler::SetMediaRouterEnabled,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setIPFSStorageMax",
base::BindRepeating(&BraveDefaultExtensionsHandler::SetIPFSStorageMax,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"launchIPFSService",
base::BindRepeating(&BraveDefaultExtensionsHandler::LaunchIPFSService,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"shutdownIPFSService",
base::BindRepeating(&BraveDefaultExtensionsHandler::ShutdownIPFSService,
base::Unretained(this)));

// TODO(petemill): If anything outside this handler is responsible for causing
// restart-neccessary actions, then this should be moved to a generic handler
// and the flag should be moved to somewhere more static / singleton-like.
Expand Down Expand Up @@ -509,21 +519,79 @@ void BraveDefaultExtensionsHandler::GetDecentralizedDnsResolveMethodList(
}

#if BUILDFLAG(IPFS_ENABLED)
void BraveDefaultExtensionsHandler::OnIpfsLaunched(bool result, int64_t pid) {
if (!IsJavascriptAllowed())
void BraveDefaultExtensionsHandler::FileSelected(const base::FilePath& path,
int index,
void* params) {
ipfs::IpfsService* service =
ipfs::IpfsServiceFactory::GetForContext(profile_);
if (!service)
return;
service->GetIpnsKeysManager()->ImportKey(
path, dialog_key_,
base::BindOnce(&BraveDefaultExtensionsHandler::OnKeyImported,
base::Unretained(this)));
select_file_dialog_.reset();
dialog_key_.clear();
}

void BraveDefaultExtensionsHandler::OnKeyImported(const std::string& key,
const std::string& value,
bool success) {
FireWebUIListener("brave-ipfs-key-imported", base::Value(key),
base::Value(value), base::Value(success));
}

void BraveDefaultExtensionsHandler::FileSelectionCanceled(void* params) {
select_file_dialog_.reset();
dialog_key_.clear();
}

void BraveDefaultExtensionsHandler::ImportIpnsKey(const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
CHECK(profile_);
std::string key_name;
args->GetString(0, &key_name);
auto* web_contents = web_ui()->GetWebContents();
select_file_dialog_ = ui::SelectFileDialog::Create(
this, std::make_unique<ChromeSelectFilePolicy>(web_contents));
if (!select_file_dialog_) {
VLOG(1) << "Export already in progress";
return;
}
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
const base::FilePath directory = profile->last_selected_directory();
gfx::NativeWindow parent_window = web_contents->GetTopLevelNativeWindow();
ui::SelectFileDialog::FileTypeInfo file_types;
file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_PATH;
dialog_key_ = key_name;
select_file_dialog_->SelectFile(
ui::SelectFileDialog::SELECT_OPEN_FILE, std::u16string(), directory,
&file_types, 0, FILE_PATH_LITERAL("key"), parent_window, nullptr);
}

void BraveDefaultExtensionsHandler::CheckIpfsNodeStatus(
const base::ListValue* args) {
NotifyNodeStatus();
}

void BraveDefaultExtensionsHandler::NotifyNodeStatus() {
ipfs::IpfsService* service =
ipfs::IpfsServiceFactory::GetForContext(profile_);
bool launched = service && service->IsDaemonLaunched();
FireWebUIListener("brave-ipfs-node-status-changed", base::Value(launched));
}

void BraveDefaultExtensionsHandler::OnIpfsLaunched(bool result, int64_t pid) {
if (!IsJavascriptAllowed())
return;
NotifyNodeStatus();
}

void BraveDefaultExtensionsHandler::OnIpfsShutdown() {
if (!IsJavascriptAllowed())
return;
ipfs::IpfsService* service =
ipfs::IpfsServiceFactory::GetForContext(profile_);
bool launched = service && service->IsDaemonLaunched();
FireWebUIListener("brave-ipfs-node-status-changed", base::Value(launched));
NotifyNodeStatus();
}
void BraveDefaultExtensionsHandler::OnIpnsKeysLoaded(bool success) {
if (!IsJavascriptAllowed())
Expand Down
19 changes: 18 additions & 1 deletion browser/ui/webui/settings/brave_default_extensions_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
#if BUILDFLAG(IPFS_ENABLED)
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/ipfs_service_observer.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#endif

class Profile;

class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler
#if BUILDFLAG(IPFS_ENABLED)
,
ipfs::IpfsServiceObserver
ipfs::IpfsServiceObserver,
public ui::SelectFileDialog::Listener
#endif
{
public:
Expand All @@ -46,6 +48,7 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler
void SetIPFSCompanionEnabled(const base::ListValue* args);
void SetMediaRouterEnabled(const base::ListValue* args);
void SetIPFSStorageMax(const base::ListValue* args);
void ImportIpnsKey(const base::ListValue* args);
void LaunchIPFSService(const base::ListValue* args);
void ShutdownIPFSService(const base::ListValue* args);
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
Expand Down Expand Up @@ -74,10 +77,24 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler
bool IsRestartNeeded();

#if BUILDFLAG(IPFS_ENABLED)
// ui::SelectFileDialog::Listener
void FileSelected(const base::FilePath& path,
int index,
void* params) override;
void FileSelectionCanceled(void* params) override;

void OnKeyImported(const std::string& key,
const std::string& value,
bool success);
// ipfs::IpfsServiceObserver
void OnIpfsLaunched(bool result, int64_t pid) override;
void OnIpfsShutdown() override;
void OnIpnsKeysLoaded(bool success) override;
void CheckIpfsNodeStatus(const base::ListValue* args);
void NotifyNodeStatus();

std::string dialog_key_;
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
#endif

#if BUILDFLAG(ENABLE_WIDEVINE)
Expand Down
Loading

0 comments on commit 0172152

Please sign in to comment.