Skip to content

Commit

Permalink
Merge pull request #6966 from brave/ipfs_admin_policy
Browse files Browse the repository at this point in the history
Add IPFSEnabled policy to control IPFS feature
  • Loading branch information
yrliou authored Oct 30, 2020
2 parents 96edc12 + 830a2df commit f2f77a1
Show file tree
Hide file tree
Showing 27 changed files with 374 additions and 118 deletions.
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ source_set("browser_process") {
"//brave/browser/infobars",
"//brave/components/ipfs",
"//brave/components/services/ipfs/public/mojom",
"//components/user_prefs",
"//extensions/browser",
"//extensions/common",
]
Expand Down
5 changes: 4 additions & 1 deletion browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,11 @@ BraveBrowserProcessImpl::ipfs_client_updater() {
if (ipfs_client_updater_)
return ipfs_client_updater_.get();

base::FilePath user_data_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);

ipfs_client_updater_ = ipfs::BraveIpfsClientUpdaterFactory(
brave_component_updater_delegate());
brave_component_updater_delegate(), user_data_dir);
return ipfs_client_updater_.get();
}
#endif // BUILDFLAG(IPFS_ENABLED)
20 changes: 7 additions & 13 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <vector>

#include "base/bind.h"
#include "base/feature_list.h"
#include "base/json/json_reader.h"
#include "base/rand_util.h"
#include "base/task/post_task.h"
Expand All @@ -31,7 +30,6 @@
#include "brave/components/brave_wallet/buildflags/buildflags.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ipfs/features.h"
#include "brave/components/speedreader/buildflags.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "brave/grit/brave_generated_resources.h"
Expand Down Expand Up @@ -160,11 +158,9 @@ void BraveContentBrowserClient::BrowserURLHandlerCreated(
&webtorrent::HandleTorrentURLReverseRewrite);
#endif
#if BUILDFLAG(IPFS_ENABLED)
if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
handler->AddHandlerPair(
&ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite,
&ipfs::ContentBrowserClientHelper::HandleIPFSURLReverseRewrite);
}
handler->AddHandlerPair(
&ipfs::ContentBrowserClientHelper::HandleIPFSURLRewrite,
&ipfs::ContentBrowserClientHelper::HandleIPFSURLReverseRewrite);
#endif
handler->AddHandlerPair(&HandleURLRewrite, &HandleURLReverseOverrideRewrite);
ChromeContentBrowserClient::BrowserURLHandlerCreated(handler);
Expand Down Expand Up @@ -197,8 +193,7 @@ bool BraveContentBrowserClient::HandleExternalProtocol(
}
#endif
#if BUILDFLAG(IPFS_ENABLED)
if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature) &&
ipfs::ContentBrowserClientHelper::IsIPFSProtocol(url)) {
if (ipfs::ContentBrowserClientHelper::IsIPFSProtocol(url)) {
ipfs::ContentBrowserClientHelper::HandleIPFSProtocol(url,
std::move(web_contents_getter),
page_transition, has_user_gesture,
Expand Down Expand Up @@ -499,10 +494,9 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(

#if BUILDFLAG(IPFS_ENABLED)
std::unique_ptr<content::NavigationThrottle> ipfs_navigation_throttle =
ipfs::IpfsNavigationThrottle::MaybeCreateThrottleFor(handle,
ipfs::IpfsServiceFactory::GetForContext(context),
brave::IsRegularProfile(context),
g_brave_browser_process->GetApplicationLocale());
ipfs::IpfsNavigationThrottle::MaybeCreateThrottleFor(handle,
ipfs::IpfsServiceFactory::GetForContext(context),
g_brave_browser_process->GetApplicationLocale());
if (ipfs_navigation_throttle)
throttles.push_back(std::move(ipfs_navigation_throttle));
#endif
Expand Down
8 changes: 8 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/components/brave_referrals/buildflags/buildflags.h"
#include "brave/components/brave_shields/browser/ad_block_service.h"
#include "brave/components/brave_shields/browser/brave_shields_p3a.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/p3a/brave_p3a_service.h"
#include "brave/components/p3a/buildflags.h"
Expand All @@ -31,6 +32,10 @@
#include "brave/components/tor/tor_profile_service.h"
#endif

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/components/ipfs/ipfs_service.h"
#endif

#if !defined(OS_ANDROID)
#include "brave/browser/p3a/p3a_core_metrics.h"
#endif // !defined(OS_ANDROID)
Expand Down Expand Up @@ -60,6 +65,9 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
registry->SetDefaultPrefValue(
metrics::prefs::kMetricsReportingEnabled,
base::Value(GetDefaultPrefValueForMetricsReporting()));
#if BUILDFLAG(IPFS_ENABLED)
ipfs::IpfsService::RegisterLocalStatePrefs(registry);
#endif

#if BUILDFLAG(BRAVE_P3A_ENABLED)
brave::BraveP3AService::RegisterPrefs(registry,
Expand Down
16 changes: 10 additions & 6 deletions browser/extensions/api/ipfs_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ ipfs::IpfsService* GetIpfsService(content::BrowserContext* context) {
return ipfs::IpfsServiceFactory::GetInstance()->GetForContext(context);
}

bool IsIpfsEnabled(content::BrowserContext* context) {
return ipfs::IpfsServiceFactory::IsIpfsEnabled(context);
}

base::Value MakeSelectValue(const base::string16& name,
ipfs::IPFSResolveMethodTypes value) {
base::Value item(base::Value::Type::DICTIONARY);
Expand Down Expand Up @@ -61,13 +65,13 @@ ExtensionFunction::ResponseAction IpfsGetResolveMethodListFunction::Run() {
}

ExtensionFunction::ResponseAction IpfsGetIPFSEnabledFunction::Run() {
bool enabled = base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature);
bool enabled = IsIpfsEnabled(browser_context());
return RespondNow(OneArgument(std::make_unique<base::Value>(enabled)));
}

ExtensionFunction::ResponseAction IpfsGetResolveMethodTypeFunction::Run() {
std::string value = "invalid";
if (base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
if (IsIpfsEnabled(browser_context())) {
switch (GetIpfsService(browser_context())->GetIPFSResolveMethodType()) {
case ipfs::IPFSResolveMethodTypes::IPFS_ASK:
value = "ask";
Expand All @@ -87,7 +91,7 @@ ExtensionFunction::ResponseAction IpfsGetResolveMethodTypeFunction::Run() {
}

ExtensionFunction::ResponseAction IpfsLaunchFunction::Run() {
if (!base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
if (!IsIpfsEnabled(browser_context())) {
return RespondNow(Error("IPFS not enabled"));
}
GetIpfsService(browser_context())
Expand All @@ -100,7 +104,7 @@ void IpfsLaunchFunction::OnLaunch(bool launched) {
}

ExtensionFunction::ResponseAction IpfsShutdownFunction::Run() {
if (!base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
if (!IsIpfsEnabled(browser_context())) {
return RespondNow(Error("IPFS not enabled"));
}
GetIpfsService(browser_context())
Expand All @@ -113,7 +117,7 @@ void IpfsShutdownFunction::OnShutdown(bool shutdown) {
}

ExtensionFunction::ResponseAction IpfsGetConfigFunction::Run() {
if (!base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
if (!IsIpfsEnabled(browser_context())) {
return RespondNow(Error("IPFS not enabled"));
}
GetIpfsService(browser_context())
Expand All @@ -128,7 +132,7 @@ void IpfsGetConfigFunction::OnGetConfig(bool success,
}

ExtensionFunction::ResponseAction IpfsGetExecutableAvailableFunction::Run() {
if (!base::FeatureList::IsEnabled(ipfs::features::kIpfsFeature)) {
if (!IsIpfsEnabled(browser_context())) {
return RespondNow(Error("IPFS not enabled"));
}
bool avail = GetIpfsService(browser_context())->IsIPFSExecutableAvailable();
Expand Down
22 changes: 19 additions & 3 deletions browser/extensions/brave_extension_management.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "brave/browser/extensions/brave_extension_provider.h"
#include "brave/common/pref_names.h"
#include "brave/browser/tor/tor_profile_service_factory.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "brave/components/tor/pref_names.h"
#include "chrome/browser/extensions/external_policy_loader.h"
Expand All @@ -25,6 +26,11 @@
#include "brave/components/tor/brave_tor_client_updater.h"
#endif

#if BUILDFLAG(IPFS_ENABLED)
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/components/ipfs/brave_ipfs_client_updater.h"
#endif

namespace extensions {

BraveExtensionManagement::BraveExtensionManagement(Profile* profile)
Expand All @@ -39,9 +45,7 @@ BraveExtensionManagement::BraveExtensionManagement(Profile* profile)
tor::prefs::kTorDisabled,
base::BindRepeating(&BraveExtensionManagement::OnTorDisabledChanged,
base::Unretained(this)));
// BrowserPolicyConnector enforce policy earlier than this constructor so we
// have to manully cleanup tor executable when tor is disabled by gpo
OnTorDisabledChanged();
Cleanup();
}

BraveExtensionManagement::~BraveExtensionManagement() {
Expand Down Expand Up @@ -70,4 +74,16 @@ void BraveExtensionManagement::OnTorDisabledChanged() {
#endif
}

void BraveExtensionManagement::Cleanup() {
// BrowserPolicyConnector enforce policy earlier than this constructor so we
// have to manully cleanup tor executable when tor is disabled by gpo
OnTorDisabledChanged();

#if BUILDFLAG(IPFS_ENABLED)
// Remove ipfs executable if it is disabled by GPO.
if (ipfs::IpfsServiceFactory::IsIpfsDisabledByPolicy())
g_brave_browser_process->ipfs_client_updater()->Cleanup();
#endif
}

} // namespace extensions
1 change: 1 addition & 0 deletions browser/extensions/brave_extension_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BraveExtensionManagement : public ExtensionManagement,
UnloadedExtensionReason reason) override;

void OnTorDisabledChanged();
void Cleanup();

PrefChangeRegistrar local_state_pref_change_registrar_;

Expand Down
15 changes: 3 additions & 12 deletions browser/ipfs/content_browser_client_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include <utility>

#include "base/task/post_task.h"
#include "brave/browser/ipfs/ipfs_service_factory.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/url_constants.h"
#include "brave/components/ipfs/ipfs_constants.h"
#include "brave/components/ipfs/ipfs_service.h"
#include "brave/components/ipfs/pref_names.h"
#include "brave/components/ipfs/translate_ipfs_uri.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
Expand All @@ -27,15 +27,6 @@

namespace {

bool IsIPFSDisabled(content::BrowserContext* browser_context) {
auto* prefs = user_prefs::UserPrefs::Get(browser_context);
auto resolve_method = static_cast<ipfs::IPFSResolveMethodTypes>(
prefs->GetInteger(kIPFSResolveMethod));
return resolve_method == ipfs::IPFSResolveMethodTypes::IPFS_DISABLED ||
!ipfs::IpfsService::IsIpfsEnabled(
browser_context, brave::IsRegularProfile(browser_context));
}

bool IsIPFSLocalGateway(content::BrowserContext* browser_context) {
auto* prefs = user_prefs::UserPrefs::Get(browser_context);
auto resolve_method = static_cast<ipfs::IPFSResolveMethodTypes>(
Expand All @@ -51,7 +42,7 @@ namespace ipfs {
bool ContentBrowserClientHelper::HandleIPFSURLRewrite(
GURL* url,
content::BrowserContext* browser_context) {
if (!IsIPFSDisabled(browser_context) &&
if (!IpfsServiceFactory::IsIpfsResolveMethodDisabled(browser_context) &&
// When it's not the local gateway we don't want to show a ipfs:// URL.
// We instead will translate the URL later in LoadOrLaunchIPFSURL.
IsIPFSLocalGateway(browser_context) &&
Expand All @@ -76,7 +67,7 @@ bool ContentBrowserClientHelper::ShouldNavigateIPFSURI(
content::BrowserContext* browser_context) {
*new_url = url;
bool is_ipfs_scheme = url.SchemeIs(kIPFSScheme) || url.SchemeIs(kIPNSScheme);
return !IsIPFSDisabled(browser_context) &&
return !IpfsServiceFactory::IsIpfsResolveMethodDisabled(browser_context) &&
(!is_ipfs_scheme ||
TranslateIPFSURI(url, new_url, IsIPFSLocalGateway(browser_context)));
}
Expand Down
Loading

0 comments on commit f2f77a1

Please sign in to comment.