Skip to content

Commit

Permalink
Merge pull request #2328 from brave/install_ipfs_as_extension
Browse files Browse the repository at this point in the history
Install ipfs as a normal extension
  • Loading branch information
yrliou committed Apr 24, 2019
2 parents 5758539 + 95a04f8 commit 5aacdbc
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 69 deletions.
2 changes: 1 addition & 1 deletion app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
Uses Hangouts component to enable screen sharing and other features in the browser.
</message>
<message name="IDS_SETTINGS_IPFS_COMPANION_ENABLED_DESC" desc="The description for IPFS companion switch in settings">
Uses IPFS companion component to support IPFS in the browser.
Uses IPFS companion extension to support IPFS in the browser.
</message>
<message name="IDS_SETTINGS_MANAGE_EXTENSIONS_LABEL" desc="The label of manage extensions link in settings">
Manage extensions
Expand Down
3 changes: 3 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ source_set("extensions") {

deps = [
"//base",
"//brave/common:pref_names",
"//brave/common/extensions/api",
"//brave/components/brave_extension:static_resources",
"//brave/components/brave_extension:generated_resources",
Expand All @@ -53,7 +54,9 @@ source_set("extensions") {
"//brave/components/brave_webtorrent:generated_resources",
"//brave/components/brave_webtorrent:static_resources",
"//chrome/browser",
"//components/prefs",
"//content/public/browser",
"//extensions/browser",
"//extensions/common",
]
}
8 changes: 0 additions & 8 deletions browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ void BraveComponentLoader::OnComponentRegistered(std::string extension_id) {
// to patch for friend access.
demand_updater.OnDemandUpdate(g_browser_process->component_updater(),
extension_id);
if (testing_callbacks_) {
testing_callbacks_->OnComponentRegistered(extension_id);
}
}

void BraveComponentLoader::OnComponentReady(std::string extension_id,
Expand Down Expand Up @@ -127,11 +124,6 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
brave_webtorrent_path.Append(FILE_PATH_LITERAL("brave_webtorrent"));
Add(IDR_BRAVE_WEBTORRENT, brave_webtorrent_path);
}

if (profile_prefs_->GetBoolean(kIPFSCompanionEnabled)) {
AddExtension(ipfs_companion_extension_id, ipfs_companion_extension_name,
ipfs_companion_extension_public_key);
}
}

void BraveComponentLoader::ObserveOpenPdfExternallySetting() {
Expand Down
1 change: 0 additions & 1 deletion browser/extensions/brave_component_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class BraveComponentLoader : public ComponentLoader {
WILL_REMOVE,
};
virtual void OnPdfExtensionAction(PdfExtensionAction action) = 0;
virtual void OnComponentRegistered(std::string extension_id) = 0;
};

void set_testing_callbacks(TestingCallbacks* testing_callbacks);
Expand Down
34 changes: 0 additions & 34 deletions browser/extensions/brave_component_loader_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::AnyNumber;
using extensions::BraveComponentLoader;

class BraveComponentLoaderTest : public extensions::ExtensionFunctionalTest,
Expand All @@ -35,8 +32,6 @@ class BraveComponentLoaderTest : public extensions::ExtensionFunctionalTest,
BraveComponentLoader* loader =
static_cast<BraveComponentLoader*>(service->component_loader());
loader->set_testing_callbacks(this);
// Do this again so OnComponentRegistered callback will be called.
loader->AddDefaultComponentExtensions(false);
}

// BraveComponentLoader::TestingCallbacks
Expand All @@ -45,8 +40,6 @@ class BraveComponentLoaderTest : public extensions::ExtensionFunctionalTest,
pdf_extension_action_ = action;
}

MOCK_METHOD1(OnComponentRegistered, void(std::string));

TestingCallbacks::PdfExtensionAction pdf_extension_action() {
return pdf_extension_action_;
}
Expand All @@ -55,33 +48,6 @@ class BraveComponentLoaderTest : public extensions::ExtensionFunctionalTest,
TestingCallbacks::PdfExtensionAction pdf_extension_action_;
};

class BraveIPFSExtensionTest: public BraveComponentLoaderTest {
public:
BraveIPFSExtensionTest() {}
~BraveIPFSExtensionTest() override = default;
};

IN_PROC_BROWSER_TEST_F(BraveIPFSExtensionTest, DisabledByDefault) {
ASSERT_FALSE(
profile()->GetPrefs()->GetBoolean(kIPFSCompanionEnabled));
EXPECT_CALL(*this, OnComponentRegistered(_)).Times(AnyNumber());
EXPECT_CALL(*this,
OnComponentRegistered(ipfs_companion_extension_id)).Times(0);
}

IN_PROC_BROWSER_TEST_F(BraveIPFSExtensionTest,
PRE_IPFSCompanionEnabledDoesRegisterComponent) {
profile()->GetPrefs()->SetBoolean(kIPFSCompanionEnabled, true);
}

IN_PROC_BROWSER_TEST_F(BraveIPFSExtensionTest,
IPFSCompanionEnabledDoesRegisterComponent) {
ASSERT_TRUE(
profile()->GetPrefs()->GetBoolean(kIPFSCompanionEnabled));
EXPECT_CALL(*this, OnComponentRegistered(_)).Times(AnyNumber());
EXPECT_CALL(*this, OnComponentRegistered(ipfs_companion_extension_id));
}

class BravePDFExtensionTest : public BraveComponentLoaderTest {
public:
BravePDFExtensionTest() {}
Expand Down
30 changes: 28 additions & 2 deletions browser/extensions/brave_extension_management.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 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/. */

#include "brave/browser/extensions/brave_extension_management.h"

#include <memory>

#include "base/command_line.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/common/brave_switches.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "brave/browser/extensions/brave_extension_provider.h"
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "chrome/browser/extensions/external_policy_loader.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_urls.h"

namespace extensions {

BraveExtensionManagement::BraveExtensionManagement(Profile* profile)
: ExtensionManagement(profile) {
: ExtensionManagement(profile),
extension_registry_observer_(this) {
extension_registry_observer_.Add(ExtensionRegistry::Get(
static_cast<content::BrowserContext*>(profile)));
providers_.push_back(
std::make_unique<BraveExtensionProvider>());
RegisterBraveExtensions();
Expand All @@ -32,4 +43,19 @@ void BraveExtensionManagement::RegisterBraveExtensions() {
g_brave_browser_process->tor_client_updater()->Register();
}

void BraveExtensionManagement::OnExtensionLoaded(
content::BrowserContext* browser_context,
const Extension* extension) {
if (extension->id() == ipfs_companion_extension_id)
pref_service_->SetBoolean(kIPFSCompanionEnabled, true);
}

void BraveExtensionManagement::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) {
if (extension->id() == ipfs_companion_extension_id)
pref_service_->SetBoolean(kIPFSCompanionEnabled, false);
}

} // namespace extensions
31 changes: 25 additions & 6 deletions browser/extensions/brave_extension_management.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 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/. */

#ifndef BRAVE_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
#define BRAVE_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
#ifndef BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSION_MANAGEMENT_H_
#define BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSION_MANAGEMENT_H_

#include "base/scoped_observer.h"
#include "chrome/browser/extensions/extension_management.h"
#include "extensions/browser/extension_registry_observer.h"

namespace extensions {

class BraveExtensionManagement : public ExtensionManagement {
class ExtensionRegistry;

class BraveExtensionManagement : public ExtensionManagement,
public ExtensionRegistryObserver {
public:
BraveExtensionManagement(Profile* profile);
explicit BraveExtensionManagement(Profile* profile);
~BraveExtensionManagement() override;

private:
void RegisterBraveExtensions();

// ExtensionRegistryObserver implementation.
void OnExtensionLoaded(
content::BrowserContext* browser_context,
const Extension* extension) override;
void OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) override;

ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_;

DISALLOW_COPY_AND_ASSIGN(BraveExtensionManagement);
};

} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_EXTENSION_MANAGEMENT_H_
11 changes: 7 additions & 4 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ source_set("ui") {
"//brave/app/theme:brave_unscaled_resources",
"//brave/app/theme:brave_theme_resources",
"//brave/app/vector_icons:vector_icons",
"//brave/browser/devtools",
"//brave/browser/tor",
"//brave/browser/resources/md_extensions:resources",
"//brave/browser/resources/settings:resources",
"//brave/common",
"//brave/components/brave_adblock_ui:generated_resources",
"//brave/components/brave_extension:static_resources",
Expand All @@ -127,14 +131,13 @@ source_set("ui") {
"//brave/components/brave_sync:static_resources",
"//brave/components/brave_sync:generated_resources",
"//brave/components/brave_welcome_ui:generated_resources",
"//brave/browser/devtools",
"//brave/browser/tor",
"//brave/browser/resources/md_extensions:resources",
"//brave/browser/resources/settings:resources",
"//chrome/app:command_ids",
"//chrome/browser/extensions",
"//chrome/common",
"//components/prefs",
"//content/public/browser",
"//content/public/common",
"//extensions/browser",
"//skia",
"//ui/accessibility",
"//ui/base",
Expand Down
47 changes: 38 additions & 9 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@
#include "base/values.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "chrome/browser/extensions/component_loader.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/webstore_install_with_prompt.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"

BraveDefaultExtensionsHandler::BraveDefaultExtensionsHandler()
: weak_ptr_factory_(this) {
}

BraveDefaultExtensionsHandler::~BraveDefaultExtensionsHandler() {
}

void BraveDefaultExtensionsHandler::RegisterMessages() {
profile_ = Profile::FromWebUI(web_ui());
web_ui()->RegisterMessageCallback(
Expand Down Expand Up @@ -83,6 +94,24 @@ void BraveDefaultExtensionsHandler::SetHangoutsEnabled(
}
}

bool BraveDefaultExtensionsHandler::IsExtensionInstalled(
const std::string& extension_id) const {
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(
static_cast<content::BrowserContext*>(profile_));
return registry && registry->GetInstalledExtension(extension_id);
}

void BraveDefaultExtensionsHandler::OnInstallResult(
const std::string& pref_name,
bool success, const std::string& error,
extensions::webstore_install::Result result) {
if (result != extensions::webstore_install::Result::SUCCESS &&
result != extensions::webstore_install::Result::LAUNCH_IN_PROGRESS) {
profile_->GetPrefs()->SetBoolean(pref_name, false);
}
}

void BraveDefaultExtensionsHandler::SetIPFSCompanionEnabled(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);
Expand All @@ -91,19 +120,19 @@ void BraveDefaultExtensionsHandler::SetIPFSCompanionEnabled(
args->GetBoolean(0, &enabled);

extensions::ExtensionService* service =
extensions::ExtensionSystem::Get(profile_)->extension_service();

extensions::ExtensionSystem::Get(profile_)->extension_service();
if (enabled) {
extensions::ComponentLoader* loader = service->component_loader();
if (!loader->Exists(ipfs_companion_extension_id)) {
static_cast<extensions::BraveComponentLoader*>(loader)->
AddExtension(ipfs_companion_extension_id,
ipfs_companion_extension_name,
ipfs_companion_extension_public_key);
if (!IsExtensionInstalled(ipfs_companion_extension_id)) {
scoped_refptr<extensions::WebstoreInstallWithPrompt> installer =
new extensions::WebstoreInstallWithPrompt(
ipfs_companion_extension_id, profile_,
base::BindOnce(&BraveDefaultExtensionsHandler::OnInstallResult,
weak_ptr_factory_.GetWeakPtr(), kIPFSCompanionEnabled));
installer->BeginInstall();
}
service->EnableExtension(ipfs_companion_extension_id);
} else {
service->DisableExtension(ipfs_companion_extension_id,
extensions::disable_reason::DisableReason::DISABLE_BLOCKED_BY_POLICY);
extensions::disable_reason::DisableReason::DISABLE_USER_ACTION);
}
}
13 changes: 11 additions & 2 deletions browser/ui/webui/settings/brave_default_extensions_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
#ifndef BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_DEFAULT_EXTENSIONS_HANDLER_H_
#define BRAVE_BROWSER_UI_WEBUI_SETTINGS_BRAVE_DEFAULT_EXTENSIONS_HANDLER_H_

#include <string>
#include "base/memory/weak_ptr.h"
#include "chrome/browser/ui/webui/settings/settings_page_ui_handler.h"
#include "chrome/common/extensions/webstore_install_result.h"

class Profile;

class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler {
public:
BraveDefaultExtensionsHandler() = default;
~BraveDefaultExtensionsHandler() override = default;
BraveDefaultExtensionsHandler();
~BraveDefaultExtensionsHandler() override;

private:
// SettingsPageUIHandler overrides:
Expand All @@ -25,7 +28,13 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler {
void SetHangoutsEnabled(const base::ListValue* args);
void SetIPFSCompanionEnabled(const base::ListValue* args);

bool IsExtensionInstalled(const std::string& extension_id) const;
void OnInstallResult(const std::string& pref_name,
bool success, const std::string& error,
extensions::webstore_install::Result result);

Profile* profile_ = nullptr;
base::WeakPtrFactory<BraveDefaultExtensionsHandler> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(BraveDefaultExtensionsHandler);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace extensions {
pdfjs_extension_id,
brave_rewards_extension_id,
brave_sync_extension_id,
brave_webtorrent_extension_id,
ipfs_companion_extension_id
brave_webtorrent_extension_id
};

for (size_t i = 0; i < base::size(kAllowed); ++i) {
Expand Down

0 comments on commit 5aacdbc

Please sign in to comment.