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 1b326ee
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 35 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,10 @@ export class BraveIPFSBrowserProxyImpl {
chrome.send('setIPFSStorageMax', [value])
}

importIpnsKey (value) {
chrome.send('importIpnsKey', [value])
}

launchIPFSService () {
return new Promise(resolve => {
if (!chrome.ipfs) {
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
21 changes: 20 additions & 1 deletion 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 @@ -64,13 +68,28 @@ Polymer({
this.addWebUIListener('brave-ipfs-keys-loaded', (launched) => {
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});
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
}
Expand Down
58 changes: 58 additions & 0 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 Down Expand Up @@ -106,6 +109,10 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"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,
Expand Down Expand Up @@ -509,6 +516,57 @@ void BraveDefaultExtensionsHandler::GetDecentralizedDnsResolveMethodList(
}

#if BUILDFLAG(IPFS_ENABLED)
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::OnIpfsLaunched(bool result, int64_t pid) {
if (!IsJavascriptAllowed())
return;
Expand Down
16 changes: 15 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,21 @@ 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;
std::string dialog_key_;
scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
#endif

#if BUILDFLAG(ENABLE_WIDEVINE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
{"ipfsKeysNodeNotLaunched", IDS_SETTINGS_IPNS_KEYS_NODE_NOT_LAUNCHED},
{"ipfsKeysStartNode", IDS_SETTINGS_IPNS_KEYS_START_NODE},
{"ipfsKeysNodeLaunchError", IDS_SETTINGS_IPNS_KEYS_START_NODE_ERROR},
{"ipfsKeyImport", IDS_SETTINGS_IPNS_KEYS_IMPORT_BUTTON_TITLE},
{"ipfsKeyGenerate", IDS_SETTINGS_IPNS_KEYS_GENERATE_BUTTON_TITLE},
{"ipfsImporKeysError", IDS_SETTINGS_IPNS_KEYS_IMPORT_ERROR},
};
html_source->AddLocalizedStrings(localized_strings);
html_source->AddString("webRTCLearnMoreURL",
Expand Down
18 changes: 0 additions & 18 deletions components/ipfs/ipfs_network_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,6 @@ std::unique_ptr<storage::BlobDataBuilder> BuildBlobWithFolder(

namespace ipfs {

<<<<<<< HEAD
=======

>>>>>>> 8ba05e73c8 (Implemented IPFS import refactoring)
std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
const GURL& gurl,
const std::string& method,
Expand Down Expand Up @@ -252,34 +248,20 @@ void CreateRequestForFile(const base::FilePath& upload_file_path,
size_t file_size) {
std::string mime_boundary = net::GenerateMimeMultipartBoundary();
auto blob_builder_callback =
<<<<<<< HEAD
base::BindOnce(&BuildBlobWithFile, upload_file_path, mime_type, filename,
mime_boundary, file_size);
=======
base::BindOnce(&BuildBlobWithFile, upload_file_path, mime_type,
filename, mime_boundary, file_size);
>>>>>>> 8ba05e73c8 (Implemented IPFS import refactoring)
std::string content_type = ipfs::kIPFSImportMultipartContentType;
content_type += " boundary=";
content_type += mime_boundary;

base::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock(), content::BrowserThread::IO},
base::BindOnce(&CreateResourceRequest, std::move(blob_builder_callback),
<<<<<<< HEAD
content_type, std::move(storage)),
std::move(request_callback));
}

std::vector<ImportFileInfo> EnumerateDirectoryFiles(base::FilePath dir_path) {
=======
content_type, std::move(storage)),
std::move(request_callback));
}

std::vector<ImportFileInfo> EnumerateDirectoryFiles(
base::FilePath dir_path) {
>>>>>>> 8ba05e73c8 (Implemented IPFS import refactoring)
std::vector<ImportFileInfo> files;
base::FileEnumerator file_enum(
dir_path, true,
Expand Down
4 changes: 1 addition & 3 deletions components/ipfs/ipfs_network_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
#include "services/network/public/cpp/data_element.h"
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_data_item.h"

#include "services/network/public/cpp/data_element.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ipfs {

class IpfsNetwrokUtilsUnitTest : public ChromeRenderViewHostTestHarness {
class IpfsNetwrokUtilsUnitTest : public testing::Test {
public:
IpfsNetwrokUtilsUnitTest() {}
~IpfsNetwrokUtilsUnitTest() override = default;
Expand Down
Loading

0 comments on commit 1b326ee

Please sign in to comment.