Skip to content

Commit

Permalink
fix code review suggestion
Browse files Browse the repository at this point in the history
Signed-off-by: Vadym Struts <[email protected]>
  • Loading branch information
vadimstruts committed Jul 8, 2024
1 parent 76cfba8 commit fa79b0c
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 82 deletions.
17 changes: 7 additions & 10 deletions components/ipfs/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ static_library("ipfs") {
"//net",
"//url",
]
}

source_set("brave_ipfs_unit_tests") {
testonly = true

sources = [ "ipfs_utils_unittest.cc" ]

deps = [
"//base/test:test_support",
"//brave/components/ipfs",
]
if (!is_android && !is_ios) {
sources += [
"ipfs_component_cleaner.cc",
"ipfs_component_cleaner.h",
]
deps += [ "//chrome/common:constants" ]
}
}
64 changes: 64 additions & 0 deletions components/ipfs/ipfs_component_cleaner.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#include "brave/components/ipfs/ipfs_component_cleaner.h"

#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "chrome/common/chrome_paths.h"

namespace {
#if BUILDFLAG(IS_WIN)
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("lnbclahgobmjphilkalbhebakmblnbij");
#elif BUILDFLAG(IS_MAC)
#if defined(ARCH_CPU_ARM64)
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("lejaflgbgglfaomemffoaappaihfligf");
#else
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("nljcddpbnaianmglkpkneakjaapinabi");
#endif
#elif BUILDFLAG(IS_LINUX)
#if defined(ARCH_CPU_ARM64)
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("fmmldihckdnognaabhligdpckkeancng");
#else
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("oecghfpdmkjlhnfpmmjegjacfimiafjp");
#endif
#else
// Not used yet for Android/iOS
static const base::FilePath::StringPieceType kIpfsClientComponentId =
FILE_PATH_LITERAL("");
#endif
} // namespace

namespace ipfs {

base::FilePath::StringPieceType GetIpfsClientComponentId() {
return kIpfsClientComponentId;
}

base::FilePath GetIpfsClientComponentPath() {
base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
return user_data_dir.Append(GetIpfsClientComponentId());
}

void DeleteIpfsComponent(const base::FilePath& component_path) {
if (component_path.empty()) {
return;
}
// Remove IPFS component
base::ThreadPool::PostTask(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
base::BindOnce(IgnoreResult(&base::DeletePathRecursively),
component_path));
}

} // namespace ipfs
20 changes: 20 additions & 0 deletions components/ipfs/ipfs_component_cleaner.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/.

#ifndef BRAVE_COMPONENTS_IPFS_IPFS_COMPONENT_CLEANER_H_
#define BRAVE_COMPONENTS_IPFS_IPFS_COMPONENT_CLEANER_H_

#include "base/files/file_path.h"

namespace ipfs {

base::FilePath::StringPieceType GetIpfsClientComponentId();

base::FilePath GetIpfsClientComponentPath();

void DeleteIpfsComponent(const base::FilePath& component_path);
} // namespace ipfs

#endif // BRAVE_COMPONENTS_IPFS_IPFS_COMPONENT_CLEANER_H_
15 changes: 8 additions & 7 deletions components/ipfs/ipfs_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "brave/components/ipfs/ipfs_prefs.h"

#include "base/files/file_path.h"
#include "base/path_service.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "chrome/common/chrome_paths.h"
#include "build/build_config.h"
#include "components/prefs/pref_registry_simple.h"

#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
#include "brave/components/ipfs/ipfs_component_cleaner.h"
#endif

namespace {
// Used to determine which method should be used to resolve ipfs:// and ipns:///
// schemes, between:
Expand Down Expand Up @@ -127,10 +129,9 @@ void ClearDeprecatedIpfsPrefs(PrefService* registry) {
registry->ClearPref(kIPFSAutoRedirectDNSLink);
registry->ClearPref(kIPFSCompanionEnabled);

base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
ipfs::DeleteIpfsComponentAndData(user_data_dir,
ipfs::GetIpfsClientComponentId());
#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
ipfs::DeleteIpfsComponent(ipfs::GetIpfsClientComponentPath());
#endif
}

} // namespace ipfs
45 changes: 2 additions & 43 deletions components/ipfs/ipfs_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@

#include <string>
#include <string_view>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/task/thread_pool.h"
#include "brave/components/filecoin/rs/src/lib.rs.h"
#include "build/build_config.h"
#include "components/base32/base32.h"
Expand All @@ -34,25 +28,6 @@ constexpr char kIPNSScheme[] = "ipns";
const int64_t kIpfsNSCodec = 0xE3;
const int64_t kIpnsNSCodec = 0xE5;

#if BUILDFLAG(IS_WIN)
static const char kIpfsClientComponentId[] = "lnbclahgobmjphilkalbhebakmblnbij";
#elif BUILDFLAG(IS_MAC)
#if defined(ARCH_CPU_ARM64)
static const char kIpfsClientComponentId[] = "lejaflgbgglfaomemffoaappaihfligf";
#else
static const char kIpfsClientComponentId[] = "nljcddpbnaianmglkpkneakjaapinabi";
#endif
#elif BUILDFLAG(IS_LINUX)
#if defined(ARCH_CPU_ARM64)
static const char kIpfsClientComponentId[] = "fmmldihckdnognaabhligdpckkeancng";
#else
static const char kIpfsClientComponentId[] = "oecghfpdmkjlhnfpmmjegjacfimiafjp";
#endif
#else
// Not used yet for Android/iOS
static const char kIpfsClientComponentId[] = "";
#endif

// Decodes a varint from the given string piece into the given int64_t. Returns
// remaining span if the string had a valid varint (where a byte was found with
// it's top bit set).
Expand All @@ -62,8 +37,9 @@ base::span<const uint8_t> DecodeVarInt(base::span<const uint8_t> from,
int shift = 0;
uint64_t ret = 0;
do {
if (it == from.end())
if (it == from.end()) {
return {};
}

// Shifting 64 or more bits is undefined behavior.
DCHECK_LT(shift, 64);
Expand Down Expand Up @@ -119,23 +95,6 @@ bool IsValidCID(const std::string& cid) {
} // namespace

namespace ipfs {

std::string GetIpfsClientComponentId() {
return kIpfsClientComponentId;
}

void DeleteIpfsComponentAndData(const base::FilePath& user_data_dir,
const std::string& ipfs_client_component_id) {
if (user_data_dir.empty() || !base::PathExists(user_data_dir)) {
return;
}
// Remove IPFS component
base::ThreadPool::PostTask(
FROM_HERE, {base::TaskPriority::BEST_EFFORT, base::MayBlock()},
base::GetDeletePathRecursivelyCallback(
user_data_dir.Append(FILE_PATH_LITERAL(ipfs_client_component_id))));
}

bool TranslateIPFSURI(const GURL& url, GURL* new_url, bool use_subdomain) {
const GURL gateway_url{kDefaultPublicGateway};
std::string cid, path;
Expand Down
14 changes: 2 additions & 12 deletions components/ipfs/ipfs_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,17 @@
#ifndef BRAVE_COMPONENTS_IPFS_IPFS_UTILS_H_
#define BRAVE_COMPONENTS_IPFS_IPFS_UTILS_H_

#include <string>

#include "base/files/file_path.h"
#include "build/build_config.h"
#include "url/gurl.h"

namespace ipfs {

inline constexpr char kIPFSScheme[] = "ipfs";
inline constexpr char kDefaultPublicGateway[] = "https://ipfs.io";

bool TranslateIPFSURI(const GURL& url,
GURL* new_url,
bool use_subdomain);
bool TranslateIPFSURI(const GURL& url, GURL* new_url, bool use_subdomain);

GURL ContentHashToCIDv1URL(base::span<const uint8_t> contenthash);

std::string GetIpfsClientComponentId();

void DeleteIpfsComponentAndData(const base::FilePath& user_data_dir,
const std::string& ipfs_client_component_id);

} // namespace ipfs

#endif // BRAVE_COMPONENTS_IPFS_IPFS_UTILS_H_
55 changes: 48 additions & 7 deletions components/ipfs/ipfs_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@
#include "brave/components/ipfs/ipfs_utils.h"

#include <fstream>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "content/public/test/browser_task_environment.h"
#include "net/base/url_util.h"
#include "testing/gtest/include/gtest/gtest.h"

#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
#include "base/path_service.h"
#include "brave/components/ipfs/ipfs_component_cleaner.h"
#include "chrome/common/chrome_paths.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
namespace {
#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
// Simple function to dump some text into a new file.
void CreateTextFile(const base::FilePath& filename,
const std::wstring& contents) {
Expand All @@ -31,6 +45,21 @@ void CreateTextFile(const base::FilePath& filename,
file << contents;
file.close();
}
std::unique_ptr<Profile> CreateProfile(const base::FilePath& path) {
SyncServiceFactory::GetInstance();

sync_preferences::PrefServiceMockFactory factory;
auto registry = base::MakeRefCounted<user_prefs::PrefRegistrySyncable>();
std::unique_ptr<sync_preferences::PrefServiceSyncable> prefs(
factory.CreateSyncable(registry.get()));
RegisterUserProfilePrefs(registry.get());

TestingProfile::Builder profile_builder;
profile_builder.SetPrefService(std::move(prefs));
profile_builder.SetPath(path);
return profile_builder.Build();
}
#endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)

GURL GetDefaultIPFSGateway() {
return GURL(ipfs::kDefaultPublicGateway);
Expand All @@ -43,15 +72,27 @@ class IpfsUtilsUnitTest : public testing::Test {
~IpfsUtilsUnitTest() override = default;

protected:
void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
void SetUp() override {
#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
profile_ = CreateProfile(temp_dir_.GetPath());
EXPECT_TRUE(profile_);
#endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
}

content::BrowserTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
std::unique_ptr<Profile> profile_;
base::ScopedTempDir temp_dir_;
#endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
};

#if !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)
TEST_F(IpfsUtilsUnitTest, DeleteIpfsComponentAndDataTest) {
auto user_data_dir = temp_dir_.GetPath();
base::FilePath user_data_dir;
DCHECK(base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir));

base::FilePath cache_folder =
user_data_dir.Append(FILE_PATH_LITERAL("brave_ipfs"));
base::CreateDirectory(cache_folder);
Expand All @@ -65,23 +106,23 @@ TEST_F(IpfsUtilsUnitTest, DeleteIpfsComponentAndDataTest) {
CreateTextFile(cache_folder_subdir_file_01, L"12345678901234567890");

base::FilePath component_id_folder =
user_data_dir.Append(FILE_PATH_LITERAL(ipfs::GetIpfsClientComponentId()));
user_data_dir.Append(base::FilePath(ipfs::GetIpfsClientComponentId()));
base::CreateDirectory(component_id_folder);
EXPECT_TRUE(base::PathExists(component_id_folder));
base::FilePath component_id_folde_subdir =
cache_folder.Append(FILE_PATH_LITERAL("subdir1"));
component_id_folder.Append(FILE_PATH_LITERAL("subdir1"));
base::CreateDirectory(component_id_folde_subdir);
EXPECT_TRUE(base::PathExists(component_id_folde_subdir));
base::FilePath component_id_folde_subdir_file_01 =
cache_folder_subdir.Append(FILE_PATH_LITERAL("The file 01.txt"));
component_id_folde_subdir.Append(FILE_PATH_LITERAL("The file 01.txt"));
CreateTextFile(component_id_folde_subdir_file_01, L"12345678901234567890");

ipfs::DeleteIpfsComponentAndData(user_data_dir,
ipfs::GetIpfsClientComponentId());
ipfs::DeleteIpfsComponent(ipfs::GetIpfsClientComponentPath());
task_environment_.RunUntilIdle();
EXPECT_TRUE(base::PathExists(cache_folder));
EXPECT_FALSE(base::PathExists(component_id_folder));
}
#endif // !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_ANDROID)

TEST_F(IpfsUtilsUnitTest, TranslateIPFSURINotIPFSScheme) {
GURL url(
Expand Down
25 changes: 25 additions & 0 deletions components/ipfs/test/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2020 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/. */

#import("//brave/build/config.gni")
#import("//brave/components/ipfs/buildflags/buildflags.gni")
import("//testing/test.gni")
source_set("brave_ipfs_unit_tests") {
testonly = true

sources = [ "//brave/components/ipfs/ipfs_utils_unittest.cc" ]

deps = [
"//base/test:test_support",
"//brave/components/ipfs",
"//chrome/browser:browser",
"//chrome/browser/sync:factories",
"//chrome/browser/sync:sync",
"//chrome/test:test_support",
"//components/sync_preferences:test_support",
"//content/test:test_support",
"//net",
]
} # source_set("brave_ipfs_unit_tests")
2 changes: 1 addition & 1 deletion patches/chrome-browser-resources-settings-router.ts.patch
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ index 7e03316d8975291611bb095bd3b87c5133f1e33b..6b604189c90f73f0bdffcfcf05edfc73
IMPORT_DATA: Route;
SIGN_OUT: Route;
// </if>
+ GET_STARTED: Route; SHIELDS: Route; SOCIAL_BLOCKING: Route; EXTENSIONS: Route; EXTENSIONS_V2: Route; BRAVE_SYNC: Route; BRAVE_SYNC_SETUP: Route; BRAVE_IPFS: Route; BRAVE_WALLET: Route; BRAVE_WEB3: Route; BRAVE_NEW_TAB: Route; THEMES: Route;
+ GET_STARTED: Route; SHIELDS: Route; SOCIAL_BLOCKING: Route; EXTENSIONS: Route; EXTENSIONS_V2: Route; BRAVE_SYNC: Route; BRAVE_SYNC_SETUP: Route; BRAVE_WALLET: Route; BRAVE_WEB3: Route; BRAVE_NEW_TAB: Route; THEMES: Route;
}

/** Class for navigable routes. */
Loading

0 comments on commit fa79b0c

Please sign in to comment.