Skip to content

Commit

Permalink
Merge pull request #3698 from brave/rewards-delay-extension
Browse files Browse the repository at this point in the history
Rewards Extension: delay loading until enabled
  • Loading branch information
petemill authored Oct 24, 2019
2 parents e4f2651 + 221129c commit 87e70e8
Show file tree
Hide file tree
Showing 14 changed files with 424 additions and 96 deletions.
1 change: 1 addition & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ source_set("extensions") {

if (brave_rewards_enabled) {
deps += [
"//brave/components/brave_rewards/browser",
"//brave/components/brave_rewards/resources/extension:extension_generated_resources",
"//brave/components/brave_rewards/resources/extension:static_resources",
]
Expand Down
43 changes: 36 additions & 7 deletions browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <string>

#include "base/bind.h"
#include "base/command_line.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/component_updater/brave_component_installer.h"
Expand All @@ -15,13 +16,16 @@
#include "brave/common/pref_names.h"
#include "brave/components/brave_extension/grit/brave_extension.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_rewards/resources/extension/grit/brave_rewards_extension_resources.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/components_ui.h"
#include "chrome/common/pref_names.h"
#include "components/grit/brave_components_resources.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
Expand All @@ -33,7 +37,14 @@ BraveComponentLoader::BraveComponentLoader(
Profile* profile)
: ComponentLoader(extension_service, profile),
profile_(profile),
profile_prefs_(profile->GetPrefs()) {}
profile_prefs_(profile->GetPrefs()) {
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
pref_change_registrar_.Init(profile_prefs_);
pref_change_registrar_.Add(brave_rewards::prefs::kBraveRewardsEnabled,
base::Bind(&BraveComponentLoader::HandleRewardsEnabledStatus,
base::Unretained(this)));
#endif
}

BraveComponentLoader::~BraveComponentLoader() {
}
Expand Down Expand Up @@ -89,12 +100,8 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
}

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
if (!command_line.HasSwitch(switches::kDisableBraveRewardsExtension)) {
base::FilePath brave_rewards_path(FILE_PATH_LITERAL(""));
brave_rewards_path =
brave_rewards_path.Append(FILE_PATH_LITERAL("brave_rewards"));
Add(IDR_BRAVE_REWARDS, brave_rewards_path);
}
// Enable rewards extension if already opted-in
HandleRewardsEnabledStatus();
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
Expand All @@ -106,6 +113,28 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
#endif
}

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
void BraveComponentLoader::AddRewardsExtension() {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (!command_line.HasSwitch(switches::kDisableBraveRewardsExtension) &&
!Exists(brave_rewards_extension_id)) {
base::FilePath brave_rewards_path(FILE_PATH_LITERAL(""));
brave_rewards_path =
brave_rewards_path.Append(FILE_PATH_LITERAL("brave_rewards"));
Add(IDR_BRAVE_REWARDS, brave_rewards_path);
}
}

void BraveComponentLoader::HandleRewardsEnabledStatus() {
const bool is_rewards_enabled = profile_prefs_->GetBoolean(
brave_rewards::prefs::kBraveRewardsEnabled);
if (is_rewards_enabled) {
AddRewardsExtension();
}
}
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
void BraveComponentLoader::AddEthereumRemoteClientExtension() {
if (profile_prefs_->GetBoolean(kBraveWalletEnabled)) {
Expand Down
18 changes: 17 additions & 1 deletion browser/extensions/brave_component_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,19 @@
#include <string>

#include "base/files/file_path.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"
#include "chrome/browser/extensions/component_loader.h"
#include "components/prefs/pref_change_registrar.h"

class PrefService;
class Profile;

namespace brave_rewards {
class RewardsService;
}


namespace extensions {

// For registering, loading, and unloading component extensions.
Expand All @@ -29,6 +38,9 @@ class BraveComponentLoader : public ComponentLoader {
void AddDefaultComponentExtensions(bool skip_session_components) override;
void OnComponentRegistered(std::string extension_id);

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
void AddRewardsExtension();
#endif
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
void AddEthereumRemoteClientExtension();
#endif
Expand All @@ -48,9 +60,13 @@ class BraveComponentLoader : public ComponentLoader {
void AddHangoutServicesExtension() override;
#endif // BUILDFLAG(ENABLE_HANGOUT_SERVICES_EXTENSION)

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
void HandleRewardsEnabledStatus();
#endif

Profile* profile_;
PrefService* profile_prefs_;
PrefChangeRegistrar registrar_;
PrefChangeRegistrar pref_change_registrar_;
std::string ethereum_remote_client_manifest_;
base::FilePath ethereum_remote_client_install_dir_;

Expand Down
9 changes: 8 additions & 1 deletion browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ source_set("ui") {
deps += [ "//brave/app:brave_generated_resources_grit" ]
}

if (enable_extensions) {
if (enable_extensions && toolkit_views) {
sources += [
"brave_actions/brave_action_icon_with_badge_image_source.cc",
"brave_actions/brave_action_icon_with_badge_image_source.h",
Expand Down Expand Up @@ -215,6 +215,13 @@ source_set("ui") {
"webui/settings/brave_default_extensions_handler.h",
]

if (brave_rewards_enabled) {
sources += [
"views/brave_actions/brave_rewards_action_stub_view.cc",
"views/brave_actions/brave_rewards_action_stub_view.h",
]
}

deps += [
"//brave/browser/resources/extensions:resources",
"//brave/components/brave_extension:generated_resources",
Expand Down
126 changes: 91 additions & 35 deletions browser/ui/views/brave_actions/brave_actions_container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/one_shot_event.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/browser/ui/brave_actions/brave_action_view_controller.h"
#include "brave/browser/ui/brave_actions/constants.h"
#include "brave/browser/ui/views/brave_actions/brave_action_view.h"
#include "brave/browser/ui/views/brave_actions/brave_rewards_action_stub_view.h"
#include "brave/browser/ui/views/rounded_separator.h"
#include "brave/common/brave_switches.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_rewards/common/pref_names.h"
#include "chrome/browser/extensions/extension_action_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/layout_constants.h"
Expand Down Expand Up @@ -47,6 +54,7 @@ void BraveActionsContainer::BraveActionInfo::Reset() {
BraveActionsContainer::BraveActionsContainer(Browser* browser, Profile* profile)
: views::View(),
browser_(browser),
extension_system_(extensions::ExtensionSystem::Get(profile)),
extension_action_api_(extensions::ExtensionActionAPI::Get(profile)),
extension_registry_(extensions::ExtensionRegistry::Get(profile)),
extension_action_manager_(
Expand All @@ -55,7 +63,7 @@ BraveActionsContainer::BraveActionsContainer(Browser* browser, Profile* profile)
extension_action_observer_(this),
weak_ptr_factory_(this) {
// Handle when the extension system is ready
extensions::ExtensionSystem::Get(profile)->ready().Post(
extension_system_->ready().Post(
FROM_HERE, base::Bind(&BraveActionsContainer::OnExtensionSystemReady,
weak_ptr_factory_.GetWeakPtr()));
}
Expand Down Expand Up @@ -118,20 +126,26 @@ bool BraveActionsContainer::ShouldAddAction(const std::string& id) const {
}

bool BraveActionsContainer::ShouldAddBraveRewardsAction() const {
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kDisableBraveRewardsExtension)) {
return false;
}
const PrefService* prefs = browser_->profile()->GetPrefs();
return prefs->GetBoolean(brave_rewards::prefs::kBraveRewardsEnabled) ||
!prefs->GetBoolean(kHideBraveRewardsButton);
}

void BraveActionsContainer::AddAction(const extensions::Extension* extension,
int pos) {
void BraveActionsContainer::AddAction(const extensions::Extension* extension) {
DCHECK(extension);
if (!ShouldAddAction(extension->id()))
return;
VLOG(1) << "AddAction (" << extension->id() << "), was already loaded: "
<< static_cast<bool>(actions_[extension->id()].view_);
if (!actions_[extension->id()].view_) {
if (!actions_[extension->id()].view_controller_) {
const auto& id = extension->id();
// Remove existing stub view, if present
actions_[id].Reset();
// Create a ExtensionActionViewController for the extension
// Passing |nullptr| instead of ToolbarActionsBar since we
// do not require that logic.
Expand All @@ -144,30 +158,59 @@ void BraveActionsContainer::AddAction(const extensions::Extension* extension,
// The button view
actions_[id].view_ = std::make_unique<BraveActionView>(
actions_[id].view_controller_.get(), this);
// Add extension view after separator view
// `AddChildView` should be called first, so that changes that modify
// layout (e.g. preferred size) are forwarded to its parent
if (actions_[id].position_ != ACTION_ANY_POSITION) {
DCHECK_GT(actions_[id].position_, 0);
AddChildViewAt(actions_[id].view_.get(), actions_[id].position_);
} else {
AddChildView(actions_[id].view_.get());
AttachAction(actions_[id]);
// Handle if we are in a continuing pressed state for this extension.
if (is_rewards_pressed_ && id == brave_rewards_extension_id) {
is_rewards_pressed_ = false;
actions_[id].view_controller_->ExecuteAction(true);
}
// we control destruction
actions_[id].view_->set_owned_by_client();
// Sets overall size of button but not image graphic. We set a large width
// in order to give space for the bubble.
actions_[id].view_->SetPreferredSize(gfx::Size(34, 24));
Update();
}
}

void BraveActionsContainer::AddAction(const std::string& id, int pos) {
void BraveActionsContainer::AddActionStubForRewards() {
const std::string id = brave_rewards_extension_id;
if (!ShouldAddAction(id)) {
return;
}
if (actions_[id].view_) {
return;
}
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
actions_[id].view_ = std::make_unique<BraveRewardsActionStubView>(this);
AttachAction(actions_[id]);
#endif
}

void BraveActionsContainer::AttachAction(BraveActionInfo &action) {
// Add extension view after separator view
// `AddChildView` should be called first, so that changes that modify
// layout (e.g. preferred size) are forwarded to its parent
if (action.position_ != ACTION_ANY_POSITION) {
DCHECK_GT(action.position_, 0);
AddChildViewAt(action.view_.get(), action.position_);
} else {
AddChildView(action.view_.get());
}
// we control destruction
action.view_->set_owned_by_client();
Update();
}

void BraveActionsContainer::AddAction(const std::string& id) {
DCHECK(extension_registry_);
const extensions::Extension* extension =
extension_registry_->enabled_extensions().GetByID(id);
if (extension)
AddAction(extension, pos);
if (extension) {
AddAction(extension);
return;
}
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
if (id == brave_rewards_extension_id) {
AddActionStubForRewards();
return;
}
#endif
LOG(ERROR) << "Extension not found for Brave Action: " << id;
}

void BraveActionsContainer::RemoveAction(const std::string& id) {
Expand Down Expand Up @@ -240,10 +283,9 @@ views::LabelButton* BraveActionsContainer::GetOverflowReferenceView() const {

// ToolbarActionView::Delegate members
gfx::Size BraveActionsContainer::GetToolbarActionSize() {
// Shields icon should be square, and full-height
gfx::Rect rect(gfx::Size(height(), height()));
rect.Inset(-GetLayoutInsets(LOCATION_BAR_ICON_INTERIOR_PADDING));
return rect.size();
// Width > Height to give space for a large bubble (especially for shields).
// TODO(petemill): Generate based on toolbar size.
return gfx::Size(34, 24);
}

void BraveActionsContainer::WriteDragDataForView(View* sender,
Expand All @@ -264,20 +306,34 @@ bool BraveActionsContainer::CanStartDragForView(View* sender,
}
// end ToolbarActionView::Delegate members

#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
// BraveRewardsActionStubView::Delegate members
void BraveActionsContainer::OnRewardsStubButtonClicked() {
// Keep button state visually pressed until new extension button
// takes over.
actions_[brave_rewards_extension_id].view_->SetState(
views::Button::STATE_PRESSED);
extensions::ExtensionService* service =
extension_system_->extension_service();
if (service) {
is_rewards_pressed_ = true;
extensions::ComponentLoader* loader = service->component_loader();
static_cast<extensions::BraveComponentLoader*>(loader)->
AddRewardsExtension();
}
}
// end BraveRewardsActionStubView::Delegate members
#endif

void BraveActionsContainer::OnExtensionSystemReady() {
// observe changes in extension system
extension_registry_observer_.Add(extension_registry_);
extension_action_observer_.Add(extension_action_api_);
// Check if brave extension already loaded
const extensions::Extension* extension =
extension_registry_->enabled_extensions().GetByID(brave_extension_id);
if (extension)
AddAction(extension);
// Check if brave rewards extension already loaded
extension = extension_registry_->enabled_extensions().GetByID(
brave_rewards_extension_id);
if (extension)
AddAction(extension);
// Check if extensions already loaded
AddAction(brave_extension_id);
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
AddAction(brave_rewards_extension_id);
#endif
}

// ExtensionRegistry::Observer
Expand Down
Loading

0 comments on commit 87e70e8

Please sign in to comment.