Skip to content

Commit

Permalink
Merge pull request #9058 from brave/ios-build
Browse files Browse the repository at this point in the history
Added IPFS_LOCAL_NODE_ENABLED to separate local node deps
  • Loading branch information
spylogsster authored Jun 10, 2021
2 parents bda6571 + 712941b commit 5a4a600
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 63 deletions.
49 changes: 27 additions & 22 deletions components/ipfs/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import("//brave/components/ipfs/buildflags/buildflags.gni")
import("//extensions/buildflags/buildflags.gni")

source_set("ipfs") {
sources = [
"addresses_config.cc",
"addresses_config.h",
"blob_context_getter_factory.h",
"brave_ipfs_client_updater.cc",
"brave_ipfs_client_updater.h",
"features.cc",
"features.h",
"import/imported_data.cc",
"import/imported_data.h",
"import/ipfs_import_worker_base.cc",
"import/ipfs_import_worker_base.h",
"import/ipfs_link_import_worker.cc",
"import/ipfs_link_import_worker.h",
"ipfs_constants.cc",
"ipfs_constants.h",
"ipfs_interstitial_controller_client.cc",
"ipfs_interstitial_controller_client.h",
"ipfs_json_parser.cc",
"ipfs_json_parser.h",
"ipfs_navigation_throttle.cc",
"ipfs_navigation_throttle.h",
"ipfs_network_utils.cc",
"ipfs_network_utils.h",
"ipfs_not_connected_page.cc",
"ipfs_not_connected_page.h",
"ipfs_onboarding_page.cc",
"ipfs_onboarding_page.h",
"ipfs_p3a.cc",
"ipfs_p3a.h",
"ipfs_ports.cc",
Expand All @@ -38,8 +24,6 @@ source_set("ipfs") {
"ipfs_service_observer.h",
"ipfs_utils.cc",
"ipfs_utils.h",
"keys/ipns_keys_manager.cc",
"keys/ipns_keys_manager.h",
"node_info.cc",
"node_info.h",
"pref_names.cc",
Expand All @@ -61,21 +45,42 @@ source_set("ipfs") {
"//components/infobars/core",
"//components/keyed_service/core",
"//components/prefs",
"//components/security_interstitials/content:security_interstitial_page",
"//components/security_interstitials/core",
"//components/user_prefs",
"//components/version_info",
"//content/public/browser",
"//content/public/common",
"//extensions/buildflags",
"//net",
"//services/network/public/cpp",
"//storage/browser",
"//third_party/re2",
"//ui/base",
"//ui/native_theme:native_theme",
"//url",
]
if (ipfs_local_node_enabled) {
sources += [
"import/imported_data.cc",
"import/imported_data.h",
"import/ipfs_import_worker_base.cc",
"import/ipfs_import_worker_base.h",
"import/ipfs_link_import_worker.cc",
"import/ipfs_link_import_worker.h",
"ipfs_interstitial_controller_client.cc",
"ipfs_interstitial_controller_client.h",
"ipfs_navigation_throttle.cc",
"ipfs_navigation_throttle.h",
"ipfs_not_connected_page.cc",
"ipfs_not_connected_page.h",
"ipfs_onboarding_page.cc",
"ipfs_onboarding_page.h",
"keys/ipns_keys_manager.cc",
"keys/ipns_keys_manager.h",
]
deps += [
"//components/security_interstitials/content:security_interstitial_page",
"//content/public/browser",
"//content/public/common",
"//ui/native_theme:native_theme",
]
}

if (enable_extensions) {
deps += [ "//extensions/browser" ]
Expand Down
4 changes: 2 additions & 2 deletions components/ipfs/brave_ipfs_client_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ static const char kIpfsClientComponentBase64PublicKey[] =
"J9HIuxTzVft5v5Ys0S0Kqorn2xo+lFpVzZT7sV2orDHaLiVB5uqCMWhXehVixfRp"
"BuPGdwSuzJsNkV5aGOObKfoLr1zUgstJYMLB0uWNXTfuKM4EibWUMLMqlCYVzs2R"
"ewIDAQAB";
#elif defined(OS_ANDROID)
// Not used yet
#else
// Not used yet for Android/iOS
static const char kIpfsClientComponentName[] = "";
static const char kIpfsClientComponentId[] = "";
static const char kIpfsClientComponentBase64PublicKey[] = "";
Expand Down
5 changes: 4 additions & 1 deletion components/ipfs/buildflags/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ import("//build/buildflag_header.gni")

buildflag_header("buildflags") {
header = "buildflags.h"
flags = [ "IPFS_ENABLED=$ipfs_enabled" ]
flags = [
"IPFS_ENABLED=$ipfs_enabled",
"IPFS_LOCAL_NODE_ENABLED=$ipfs_local_node_enabled",
]
}
4 changes: 4 additions & 0 deletions components/ipfs/buildflags/buildflags.gni
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare_args() {
ipfs_enabled = !is_ios
}

declare_args() {
ipfs_local_node_enabled = ipfs_enabled && !is_ios
}
31 changes: 19 additions & 12 deletions components/ipfs/ipfs_network_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,21 @@
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "brave/components/ipfs/blob_context_getter_factory.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "content/public/browser/browser_task_traits.h"
#include "net/base/mime_util.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
#include "storage/browser/blob/blob_data_builder.h"
#include "storage/browser/blob/blob_impl.h"
#include "storage/browser/blob/blob_storage_context.h"
#include "third_party/blink/public/mojom/blob/serialized_blob.mojom.h"
#endif

namespace {

struct ImportFileInfo {
ImportFileInfo(base::FilePath full_path,
base::FileEnumerator::FileInfo information) {
path = full_path;
info = information;
}
base::FilePath path;
base::FileEnumerator::FileInfo info;
};

net::NetworkTrafficAnnotationTag GetIpfsNetworkTrafficAnnotationTag() {
return net::DefineNetworkTrafficAnnotation("ipfs_service", R"(
semantics {
Expand All @@ -62,6 +56,17 @@ net::NetworkTrafficAnnotationTag GetIpfsNetworkTrafficAnnotationTag() {
)");
}

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
struct ImportFileInfo {
ImportFileInfo(base::FilePath full_path,
base::FileEnumerator::FileInfo information) {
path = full_path;
info = information;
}
base::FilePath path;
base::FileEnumerator::FileInfo info;
};

bool GetRelativePathComponent(const base::FilePath& parent,
const base::FilePath& child,
base::FilePath::StringType* out) {
Expand Down Expand Up @@ -159,6 +164,7 @@ std::unique_ptr<storage::BlobDataBuilder> BuildBlobWithFolder(

return blob_builder;
}
#endif

} // namespace

Expand All @@ -182,6 +188,7 @@ std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
return url_loader;
}

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
std::unique_ptr<network::ResourceRequest> CreateResourceRequest(
BlobBuilderCallback blob_builder_callback,
const std::string& content_type,
Expand Down Expand Up @@ -327,5 +334,5 @@ void CreateRequestForText(const std::string& text,
content_type, context_factory),
std::move(request_callback));
}

#endif
} // namespace ipfs
13 changes: 9 additions & 4 deletions components/ipfs/ipfs_network_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "base/callback.h"
#include "base/files/file_enumerator.h"
#include "brave/components/ipfs/blob_context_getter_factory.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"

Expand All @@ -34,6 +35,12 @@ class BlobDataBuilder;

namespace ipfs {

std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
const GURL& gurl,
const std::string& method,
std::unique_ptr<network::ResourceRequest> request = nullptr);

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
void AddMultipartHeaderForUploadWithFileName(const std::string& value_name,
const std::string& file_name,
const std::string& absolute_path,
Expand All @@ -46,10 +53,6 @@ int64_t CalculateFileSize(base::FilePath upload_file_path);
using BlobBuilderCallback =
base::OnceCallback<std::unique_ptr<storage::BlobDataBuilder>()>;

std::unique_ptr<network::SimpleURLLoader> CreateURLLoader(
const GURL& gurl,
const std::string& method,
std::unique_ptr<network::ResourceRequest> request = nullptr);

using ResourceRequestGetter =
base::OnceCallback<void(std::unique_ptr<network::ResourceRequest>)>;
Expand All @@ -69,6 +72,8 @@ void CreateRequestForText(const std::string& text,
const std::string& filename,
BlobContextGetterFactory* blob_context_getter_factory,
ResourceRequestGetter request_callback);
#endif

} // namespace ipfs

#endif // BRAVE_COMPONENTS_IPFS_IPFS_NETWORK_UTILS_H_
42 changes: 26 additions & 16 deletions components/ipfs/ipfs_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@
#include "base/task/thread_pool.h"
#include "base/task_runner_util.h"
#include "brave/components/ipfs/blob_context_getter_factory.h"
#include "brave/components/ipfs/import/ipfs_import_worker_base.h"
#include "brave/components/ipfs/import/ipfs_link_import_worker.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_json_parser.h"
#include "brave/components/ipfs/ipfs_network_utils.h"
#include "brave/components/ipfs/ipfs_ports.h"
#include "brave/components/ipfs/ipfs_service_observer.h"
#include "brave/components/ipfs/ipfs_utils.h"
#include "brave/components/ipfs/keys/ipns_keys_manager.h"
#include "brave/components/ipfs/pref_names.h"
#include "brave/components/ipfs/service_sandbox_type.h"
#include "components/grit/brave_components_strings.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/storage_partition.h"
#include "extensions/buildflags/buildflags.h"
#include "net/base/url_util.h"
#include "net/http/http_request_headers.h"
Expand All @@ -45,12 +41,21 @@
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
#include "brave/components/ipfs/import/ipfs_import_worker_base.h"
#include "brave/components/ipfs/import/ipfs_link_import_worker.h"
#include "brave/components/ipfs/keys/ipns_keys_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/storage_partition.h"
#endif

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/extension_registry.h"
#endif

namespace {

#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
// Works similarly to base::AutoReset but checks for access from the wrong
// thread as well as ensuring that the previous value of the re-entrancy guard
// variable was false.
Expand All @@ -67,7 +72,7 @@ class ReentrancyCheck {
private:
bool* const guard_flag_;
};

#endif
// Used to retry request if we got zero peers from ipfs service
// Actual value will be generated randomly in range
// (kMinimalPeersRetryIntervalMs, kPeersRetryRate*kMinimalPeersRetryIntervalMs)
Expand Down Expand Up @@ -117,17 +122,21 @@ IpfsService::IpfsService(
ipfs_client_updater_->AddObserver(this);
OnExecutableReady(ipfs_client_updater_->GetExecutablePath());
}
#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
ipns_keys_manager_ = std::make_unique<IpnsKeysManager>(
blob_context_getter_factory.get(), url_loader_factory.get(),
server_endpoint_);
AddObserver(ipns_keys_manager_.get());
#endif
}

IpfsService::~IpfsService() {
if (ipfs_client_updater_) {
ipfs_client_updater_->RemoveObserver(this);
}
#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
RemoveObserver(ipns_keys_manager_.get());
#endif
Shutdown();
}

Expand Down Expand Up @@ -238,11 +247,12 @@ base::FilePath IpfsService::GetConfigFilePath() const {

void IpfsService::NotifyDaemonLaunched(bool result, int64_t pid) {
bool success = result && pid > 0;
#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
if (success && ipns_keys_manager_) {
ipns_keys_manager_->LoadKeys(base::BindOnce(
&IpfsService::NotifyIpnsKeysLoaded, weak_factory_.GetWeakPtr()));
}

#endif
while (!pending_launch_callbacks_.empty()) {
if (pending_launch_callbacks_.front())
std::move(pending_launch_callbacks_.front()).Run(success);
Expand All @@ -253,12 +263,6 @@ void IpfsService::NotifyDaemonLaunched(bool result, int64_t pid) {
}
}

void IpfsService::NotifyIpnsKeysLoaded(bool result) {
for (auto& observer : observers_) {
observer.OnIpnsKeysLoaded(result);
}
}

void IpfsService::OnIpfsLaunched(bool result, int64_t pid) {
if (result) {
ipfs_pid_ = pid;
Expand All @@ -277,6 +281,12 @@ void IpfsService::Shutdown() {
ipfs_service_.reset();
ipfs_pid_ = -1;
}
#if BUILDFLAG(IPFS_LOCAL_NODE_ENABLED)
void IpfsService::NotifyIpnsKeysLoaded(bool result) {
for (auto& observer : observers_) {
observer.OnIpnsKeysLoaded(result);
}
}

void IpfsService::ImportFileToIpfs(const base::FilePath& path,
const std::string& key,
Expand Down Expand Up @@ -397,7 +407,7 @@ void IpfsService::OnImportFinished(ipfs::ImportCompletedCallback callback,

importers_.erase(key);
}

#endif
void IpfsService::GetConnectedPeers(GetConnectedPeersCallback callback,
int retries) {
if (!IsDaemonLaunched()) {
Expand Down
Loading

0 comments on commit 5a4a600

Please sign in to comment.