Skip to content

Commit

Permalink
Publishing to IPNS should be in separate menu (#12160)
Browse files Browse the repository at this point in the history
  • Loading branch information
spylogsster authored Feb 8, 2022
1 parent 44fe0db commit 29b2e77
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 85 deletions.
8 changes: 6 additions & 2 deletions app/brave_command_ids.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
#define IDC_CONTENT_CONTEXT_IMPORT_VIDEO_IPFS 56021
#define IDC_CONTENT_CONTEXT_IMPORT_SELECTED_TEXT_IPFS 56022
#define IDC_APP_MENU_IPFS 56023
#define IDC_APP_MENU_IPFS_IMPORT_LOCAL_FILE 56024
#define IDC_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER 56025
#define IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FILE 56024
#define IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER 56025
#define IDC_SHOW_BRAVE_WALLET_PANEL 56026
#define IDC_CLOSE_BRAVE_WALLET_PANEL 56027
#define IDC_SHOW_BRAVE_VPN_PANEL 56028
Expand All @@ -45,6 +45,10 @@
#define IDC_ABOUT_BRAVE_VPN 56032
#define IDC_MANAGE_BRAVE_VPN_PLAN 56033
#define IDC_TOGGLE_BRAVE_VPN 56034
#define IDC_APP_MENU_IPFS_SHARE_LOCAL_FILE 56035
#define IDC_APP_MENU_IPFS_SHARE_LOCAL_FOLDER 56036
#define IDC_APP_MENU_IPFS_OPEN_FILES 56037
#define IDC_APP_MENU_IPFS_UPDATE_IPNS 56038

#define IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_START 56100
#define IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_END 56199
Expand Down
40 changes: 32 additions & 8 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -541,11 +541,23 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_CONTENT_CONTEXT_OPENLINKTOR_INAPP" desc="Title case: The name of the open a link in private window with Tor command">
Open Link in Brave Private Window with Tor
</message>
<message name="IDS_APP_MENU_IPFS_IMPORT_LOCAL_FILE" desc="Title case: The text label of a menu item to share local file using IPFS.">
Share Local File Using IPFS
<message name="IDS_APP_MENU_IPFS_SHARE_LOCAL_FILE" desc="Title case: The text label of a menu item to share local file using IPFS.">
Import and Share a File
</message>
<message name="IDS_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER" desc="Title case: The text label of a menu item to share local fodler using IPFS.">
Share Local Folder Using IPFS
<message name="IDS_APP_MENU_IPFS_OPEN_FILES" desc="Title case: The text label of a menu item to open IPFS webui files page.">
Open Files
</message>
<message name="IDS_APP_MENU_IPFS_SHARE_LOCAL_FOLDER" desc="Title case: The text label of a menu item to share local fodler using IPFS.">
Import and Share a Directory
</message>
<message name="IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FILE" desc="Title case: The text label of a menu item to publish local file under IPFS key.">
With a File
</message>
<message name="IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER" desc="Title case: The text label of a menu item to publish local fodler under IPFS key.">
With a Directory
</message>
<message name="IDS_APP_MENU_IPFS_UPDATE_IPNS" desc="Title case: The text label of a menu item to update IPNS.">
Update IPNS
</message>
</if>
<if expr="not use_titlecase">
Expand All @@ -555,8 +567,14 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_IMPORT_WITHOUT_PUBLISHING" desc="The name of the IPFS context menu item to import content without publishing">
Import and copy link to IPFS snapshot
</message>
<message name="IDS_APP_MENU_IPFS_IMPORT_LOCAL_FILE" desc="The text label of a menu item to share local file using IPFS.">
Share local file using IPFS
<message name="IDS_APP_MENU_IPFS_SHARE_LOCAL_FILE" desc="The text label of a menu item to share local file using IPFS.">
Import and share a file
</message>
<message name="IDS_APP_MENU_IPFS_OPEN_FILES" desc="Title case: The text label of a menu item to open IPFS webui files page.">
Open files
</message>
<message name="IDS_APP_MENU_IPFS_UPDATE_IPNS" desc="Title case: The text label of a menu item to update IPNS.">
Update IPNS
</message>
<message name="IDS_NEW_OFFTHERECORD_WINDOW_TOR" desc="The text label of a menu item to open a new off-the-record window with Tor.">
New private window with Tor
Expand All @@ -567,8 +585,14 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_CONTENT_CONTEXT_OPENLINKTOR_INAPP" desc="The name of the open a link in private window with Tor command">
Open link in Brave private window with Tor
</message>
<message name="IDS_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER" desc="The text label of a menu item to share local folder using IPFS.">
Share local folder using IPFS
<message name="IDS_APP_MENU_IPFS_SHARE_LOCAL_FOLDER" desc="The text label of a menu item to share local folder using IPFS.">
Import and share a directory
</message>
<message name="IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FILE" desc="Title case: The text label of a menu item to publish local file under IPFS key.">
With a File
</message>
<message name="IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER" desc="Title case: The text label of a menu item to publish local fodler under IPFS key.">
With a Directory
</message>
</if>
<message name="IDS_PROFILES_TOR_PROFILE_NAME" desc="Name of the Tor profile.">
Expand Down
12 changes: 6 additions & 6 deletions browser/ui/brave_browser_command_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@
#include "brave/browser/ui/sidebar/sidebar_utils.h"
#endif

#if BUILDFLAG(ENABLE_IPFS)
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "brave/components/ipfs/pref_names.h"
#endif

namespace {

bool IsBraveCommands(int id) {
Expand Down Expand Up @@ -159,6 +153,9 @@ void BraveBrowserCommandController::InitBraveCommandState() {
UpdateCommandEnabled(IDC_SPEEDREADER_ICON_ONCLICK, true);
UpdateCommandEnabled(IDC_DISTILL_PAGE, false);
}
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
UpdateCommandEnabled(IDC_APP_MENU_IPFS_OPEN_FILES, true);
#endif
}

void BraveBrowserCommandController::UpdateCommandForBraveRewards() {
Expand Down Expand Up @@ -303,6 +300,9 @@ bool BraveBrowserCommandController::ExecuteBraveCommandWithDisposition(
case IDC_MANAGE_BRAVE_VPN_PLAN:
brave::OpenBraveVPNUrls(browser_, id);
break;
case IDC_APP_MENU_IPFS_OPEN_FILES:
brave::OpenIpfsFilesWebUI(browser_);
break;
default:
LOG(WARNING) << "Received Unimplemented Command: " << id;
break;
Expand Down
19 changes: 19 additions & 0 deletions browser/ui/browser_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "brave/app/brave_command_ids.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_vpn/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/speedreader/buildflags.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "chrome/browser/browser_process.h"
Expand Down Expand Up @@ -49,6 +50,11 @@
#include "brave/components/brave_vpn/pref_names.h"
#endif

#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
#include "brave/components/ipfs/ipfs_utils.h"
#include "chrome/common/channel_info.h"
#endif

using content::WebContents;

namespace {
Expand Down Expand Up @@ -135,6 +141,19 @@ void ToggleBraveVPNButton(Browser* browser) {
#endif
}

void OpenIpfsFilesWebUI(Browser* browser) {
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
auto* prefs = browser->profile()->GetPrefs();
DCHECK(ipfs::IsLocalGatewayConfigured(prefs));
GURL gateway = ipfs::GetAPIServer(chrome::GetChannel());
GURL::Replacements replacements;
replacements.SetPathStr("/webui/");
replacements.SetRefStr("/files");
auto target_url = gateway.ReplaceComponents(replacements);
chrome::AddTabAt(browser, GURL(target_url), -1, true);
#endif
}

void OpenBraveVPNUrls(Browser* browser, int command_id) {
#if BUILDFLAG(ENABLE_BRAVE_VPN)
std::string target_url;
Expand Down
1 change: 1 addition & 0 deletions browser/ui/browser_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void MaybeDistillAndShowSpeedreaderBubble(Browser* browser);
void ShowBraveVPNBubble(Browser* browser);
void ToggleBraveVPNButton(Browser* browser);
void OpenBraveVPNUrls(Browser* browser, int command_id);
void OpenIpfsFilesWebUI(Browser* browser);

} // namespace brave

Expand Down
117 changes: 60 additions & 57 deletions browser/ui/toolbar/brave_app_menu_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/grit/generated_resources.h"

#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
#include "brave/browser/ipfs/import/ipfs_import_controller.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/browser/ipfs/ipfs_tab_helper.h"
Expand Down Expand Up @@ -109,7 +109,7 @@ class SidebarMenuModel : public ui::SimpleMenuModel,

#endif

#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
// For convenience, we show the last part of the key in the context menu item.
// The length of the key is divided to this constant and the last part is taken.
int kKeyTrimRate = 5;
Expand Down Expand Up @@ -141,9 +141,10 @@ BraveAppMenuModel::BraveAppMenuModel(
Browser* browser,
AppMenuIconController* app_menu_icon_controller)
: AppMenuModel(provider, browser, app_menu_icon_controller)
#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
,
ipfs_submenu_model_(this)
ipfs_submenu_model_(this),
ipns_submenu_model_(this)
#endif
{
}
Expand Down Expand Up @@ -258,20 +259,34 @@ void BraveAppMenuModel::InsertBraveMenuItems() {
IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER,
IDS_SHOW_BRAVE_WEBCOMPAT_REPORTER);

#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
if (IsCommandIdEnabled(IDC_APP_MENU_IPFS)) {
int keys_command_index = IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_START;
keys_command_index += AddIpfsImportMenuItem(
IDC_APP_MENU_IPFS_IMPORT_LOCAL_FILE,
IDS_APP_MENU_IPFS_IMPORT_LOCAL_FILE, keys_command_index);
keys_command_index += AddIpfsImportMenuItem(
IDC_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER,
IDS_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER, keys_command_index);
ipfs_submenu_model_.AddItemWithStringId(IDC_APP_MENU_IPFS_SHARE_LOCAL_FILE,
IDS_APP_MENU_IPFS_SHARE_LOCAL_FILE);
ipfs_submenu_model_.AddItemWithStringId(
IDC_APP_MENU_IPFS_SHARE_LOCAL_FOLDER,
IDS_APP_MENU_IPFS_SHARE_LOCAL_FOLDER);
ipfs_submenu_model_.AddItemWithStringId(IDC_APP_MENU_IPFS_OPEN_FILES,
IDS_APP_MENU_IPFS_OPEN_FILES);
if (IpnsKeysAvailable(browser()->profile())) {
ipfs_submenu_model_.InsertSubMenuWithStringIdAt(
ipfs_submenu_model_.GetItemCount(), IDC_APP_MENU_IPFS_UPDATE_IPNS,
IDS_APP_MENU_IPFS_UPDATE_IPNS, &ipns_submenu_model_);

int keys_command_index = IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_START;
keys_command_index += AddIpfsImportMenuItem(
IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FILE,
IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FILE, keys_command_index);
keys_command_index += AddIpfsImportMenuItem(
IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER,
IDS_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER, keys_command_index);
}
int index = IsCommandIdEnabled(IDC_SHOW_BRAVE_SYNC)
? GetIndexOfBraveSyncItem() + 1
: GetIndexOfBraveAdBlockItem();
InsertSubMenuWithStringIdAt(index, IDC_APP_MENU_IPFS, IDS_APP_MENU_IPFS,
&ipfs_submenu_model_);

auto& bundle = ui::ResourceBundle::GetSharedInstance();
const auto& ipfs_logo = *bundle.GetImageSkiaNamed(IDR_BRAVE_IPFS_LOGO);
ui::ImageModel model = ui::ImageModel::FromImageSkia(ipfs_logo);
Expand All @@ -281,13 +296,13 @@ void BraveAppMenuModel::InsertBraveMenuItems() {
}

void BraveAppMenuModel::ExecuteCommand(int id, int event_flags) {
#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
if (id >= IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_START &&
id <= IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_END) {
int ipfs_command = GetSelectedIPFSCommandId(id);
if (ipfs_command == -1)
return;
auto* submenu = ipns_submenu_models_[ipfs_command].get();
auto* submenu = ipns_keys_submenu_models_[ipfs_command].get();
auto command_index = submenu->GetIndexOfCommandId(id);
if (command_index == -1)
return;
Expand All @@ -297,8 +312,8 @@ void BraveAppMenuModel::ExecuteCommand(int id, int event_flags) {
return;
}
switch (id) {
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FILE:
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FILE:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FOLDER:
ExecuteIPFSCommand(id, std::string());
return;
}
Expand All @@ -307,49 +322,37 @@ void BraveAppMenuModel::ExecuteCommand(int id, int event_flags) {
}

bool BraveAppMenuModel::IsCommandIdEnabled(int id) const {
#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
content::BrowserContext* browser_context =
static_cast<content::BrowserContext*>(browser()->profile());
if (id >= IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_START &&
id <= IDC_CONTENT_CONTEXT_IMPORT_IPNS_KEYS_END) {
if (!IpnsKeysAvailable(browser_context))
return false;
if (ipns_keys_title_item_index_ != -1 &&
FindCommandIndex(id) == ipns_keys_title_item_index_) {
return false;
}
return true;
}

switch (id) {
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FILE:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FILE:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FILE:
case IDC_APP_MENU_IPFS:
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_OPEN_FILES:
case IDC_APP_MENU_IPFS_UPDATE_IPNS:
return ipfs::IsIpfsMenuEnabled(browser()->profile()->GetPrefs()) &&
IsIpfsServiceLaunched(browser_context);
}
#endif
return AppMenuModel::IsCommandIdEnabled(id);
}

#if BUILDFLAG(ENABLE_IPFS)
#if BUILDFLAG(ENABLE_IPFS_LOCAL_NODE)
int BraveAppMenuModel::AddIpnsKeysToSubMenu(ui::SimpleMenuModel* submenu,
ipfs::IpnsKeysManager* manager,
int key_command_id) {
if (!manager)
return 0;
int command_id = key_command_id + 1;

auto no_key_title = l10n_util::GetStringUTF16(IDS_IMPORT_WITHOUT_PUBLISHING);
submenu->AddItem(command_id++, no_key_title);

submenu->AddSeparator(ui::NORMAL_SEPARATOR);

auto ipns_key_title =
l10n_util::GetStringUTF16(IDS_IMPORT_USING_IPNS_KEYS_TITLE);
submenu->AddItem(command_id, ipns_key_title);
ipns_keys_title_item_index_ = command_id - key_command_id;
command_id++;
int command_id = key_command_id;

for (const auto& it : manager->GetKeys()) {
submenu->AddItem(command_id, base::ASCIIToUTF16(it.first));
Expand All @@ -365,7 +368,7 @@ int BraveAppMenuModel::AddIpnsKeysToSubMenu(ui::SimpleMenuModel* submenu,
}

int BraveAppMenuModel::FindCommandIndex(int command_id) const {
for (const auto& it : ipns_submenu_models_) {
for (const auto& it : ipns_keys_submenu_models_) {
int index = it.second->GetIndexOfCommandId(command_id);
if (index == -1)
continue;
Expand All @@ -381,18 +384,20 @@ void BraveAppMenuModel::ExecuteIPFSCommand(int id, const std::string& key) {
if (!helper)
return;
switch (id) {
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FILE:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FILE:
case IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FILE:
helper->ShowImportDialog(ui::SelectFileDialog::SELECT_OPEN_FILE, key);
break;
case IDC_APP_MENU_IPFS_IMPORT_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_SHARE_LOCAL_FOLDER:
case IDC_APP_MENU_IPFS_PUBLISH_LOCAL_FOLDER:
helper->ShowImportDialog(ui::SelectFileDialog::SELECT_EXISTING_FOLDER,
key);
break;
}
}

int BraveAppMenuModel::GetSelectedIPFSCommandId(int id) const {
for (const auto& it : ipns_submenu_models_) {
for (const auto& it : ipns_keys_submenu_models_) {
auto index = it.second->GetIndexOfCommandId(id);
if (index == -1)
continue;
Expand All @@ -405,22 +410,20 @@ int BraveAppMenuModel::AddIpfsImportMenuItem(int action_command_id,
int keys_command_id) {
content::BrowserContext* browser_context =
static_cast<content::BrowserContext*>(browser()->profile());
if (IpnsKeysAvailable(browser_context)) {
DCHECK(!ipns_submenu_models_.count(action_command_id));
ipns_submenu_models_[action_command_id] =
std::make_unique<ui::SimpleMenuModel>(this);
auto* keys_submenu = ipns_submenu_models_[action_command_id].get();
DCHECK(keys_submenu);
auto* keys_manager = GetIpnsKeysManager(browser_context);
DCHECK(keys_manager);
auto items_added =
AddIpnsKeysToSubMenu(keys_submenu, keys_manager, keys_command_id);
ipfs_submenu_model_.AddSubMenuWithStringId(action_command_id, string_id,
keys_submenu);
return items_added;
}
ipfs_submenu_model_.AddItemWithStringId(action_command_id, string_id);
return 0;
if (!IpnsKeysAvailable(browser_context))
return 0;
DCHECK(!ipns_keys_submenu_models_.count(action_command_id));
ipns_keys_submenu_models_[action_command_id] =
std::make_unique<ui::SimpleMenuModel>(this);
auto* keys_submenu = ipns_keys_submenu_models_[action_command_id].get();
DCHECK(keys_submenu);
auto* keys_manager = GetIpnsKeysManager(browser_context);
DCHECK(keys_manager);
auto items_added =
AddIpnsKeysToSubMenu(keys_submenu, keys_manager, keys_command_id);
ipns_submenu_model_.AddSubMenuWithStringId(action_command_id, string_id,
keys_submenu);
return items_added;
}
#endif
void BraveAppMenuModel::InsertAlternateProfileItems() {
Expand Down
Loading

0 comments on commit 29b2e77

Please sign in to comment.