Skip to content

Commit

Permalink
Change BraveWaybackMahcineDelegate interface
Browse files Browse the repository at this point in the history
Infobar view is created via delegate.
  • Loading branch information
simonhong committed Dec 18, 2019
1 parent d0c0cd8 commit 84400ed
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 65 deletions.
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ browser/net/ @iefremov
browser/widevine @simonhong

# Wayback machine
browser/ui/views/infobars @simonhong
browser/ui/views/infobars/brave_wayback_machine_* @simonhong
components/brave_wayback_machine @simonhong

# Licensing of third-party components
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include "brave/browser/widevine/widevine_utils.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
#include "brave/components/brave_wayback_machine/pref_names.h"
#endif

using extensions::FeatureSwitch;

namespace brave {
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "components/gcm_driver/gcm_channel_status_syncer.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
#include "brave/components/brave_wayback_machine/pref_names.h"
#endif

using BraveProfilePrefsBrowserTest = InProcessBrowserTest;

// Check download prompt preference is set to true by default.
Expand Down
7 changes: 5 additions & 2 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import("//brave/browser/tor/buildflags/buildflags.gni")
import("//brave/components/brave_rewards/browser/buildflags/buildflags.gni")
import("//brave/components/brave_sync/buildflags/buildflags.gni")
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni")
import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
import("//brave/components/brave_webtorrent/browser/buildflags/buildflags.gni")
import("//build/config/features.gni")
import("//components/gcm_driver/config.gni")
Expand Down Expand Up @@ -61,13 +62,15 @@ source_set("extensions") {
deps = [
"//base",
"//brave/app:brave_generated_resources_grit",
"//brave/browser/tor:buildflags",
"//brave/browser/webcompat_reporter",
"//brave/common",
"//brave/common:pref_names",
"//brave/common/extensions/api",
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_extension:generated_resources",
"//brave/components/brave_extension:static_resources",
"//brave/components/brave_wayback_machine:buildflags",
"//chrome/browser/extensions",
"//components/gcm_driver:gcm_driver",
"//components/gcm_driver:gcm_buildflags",
Expand Down Expand Up @@ -115,9 +118,9 @@ source_set("extensions") {
"brave_webtorrent_navigation_throttle.h",
]
}
if (enable_tor) {
if (enable_brave_wayback_machine) {
deps += [
"//brave/browser/tor:buildflags",
"//brave/components/brave_wayback_machine",
]
}
}
Expand Down
8 changes: 7 additions & 1 deletion browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "brave/browser/extensions/api/settings_private/brave_prefs_util.h"

#include "brave/common/pref_names.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "chrome/browser/extensions/api/settings_private/prefs_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/extensions/api/settings_private.h"
Expand All @@ -16,6 +17,10 @@
#include "components/gcm_driver/gcm_channel_status_syncer.h"
#endif

#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
#include "brave/components/brave_wayback_machine/pref_names.h"
#endif

namespace extensions {

namespace settings_api = api::settings_private;
Expand Down Expand Up @@ -90,9 +95,10 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
// WebTorrent pref
(*s_brave_whitelist)[kWebTorrentEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
// Wayback Machine pref
#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
(*s_brave_whitelist)[kBraveWaybackMachineEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
#endif
// Hangouts pref
(*s_brave_whitelist)[kHangoutsEnabled] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
34 changes: 14 additions & 20 deletions browser/infobars/brave_wayback_machine_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,35 @@

#include "brave/browser/infobars/brave_wayback_machine_delegate_impl.h"

#include <memory>

#include "base/command_line.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/brave_switches.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h"
#include "brave/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h"
#include "components/infobars/core/infobar.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"

// static
void BraveWaybackMachineDelegateImpl::AttachTabHelperIfNeeded(
content::WebContents* web_contents) {
auto* context = web_contents->GetBrowserContext();
if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableBraveWaybackMachineExtension) &&
!brave::IsTorProfile(context)) {
switches::kDisableBraveWaybackMachineExtension)) {
BraveWaybackMachineTabHelper::CreateForWebContents(web_contents);
auto* tab_helper =
BraveWaybackMachineTabHelper::FromWebContents(web_contents);
tab_helper->SetInfoBarManager(
InfoBarService::FromWebContents(web_contents));
tab_helper->set_delegate(std::make_unique<BraveWaybackMachineDelegateImpl>(
Profile::FromBrowserContext(context)));
tab_helper->set_delegate(
std::make_unique<BraveWaybackMachineDelegateImpl>());
}
}

BraveWaybackMachineDelegateImpl::BraveWaybackMachineDelegateImpl(
Profile* profile)
: profile_(profile) {
}

BraveWaybackMachineDelegateImpl::BraveWaybackMachineDelegateImpl() = default;
BraveWaybackMachineDelegateImpl::~BraveWaybackMachineDelegateImpl() = default;

bool BraveWaybackMachineDelegateImpl::IsWaybackMachineEnabled() const {
return profile_->GetPrefs()->GetBoolean(kBraveWaybackMachineEnabled);

void BraveWaybackMachineDelegateImpl::CreateInfoBar(
content::WebContents* web_contents) {
InfoBarService::FromWebContents(web_contents)->AddInfoBar(
CreateInfoBarView(
std::make_unique<BraveWaybackMachineInfoBarDelegate>(),
web_contents),
true);
}
16 changes: 8 additions & 8 deletions browser/infobars/brave_wayback_machine_delegate_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@

#include "brave/components/brave_wayback_machine/brave_wayback_machine_delegate.h"

class Profile;

namespace content {
class WebContents;
} // namespace content

namespace infobars {
class InfoBar;
} // namespace infobars

class BraveWaybackMachineDelegateImpl : public BraveWaybackMachineDelegate {
public:
static void AttachTabHelperIfNeeded(content::WebContents* web_contents);

explicit BraveWaybackMachineDelegateImpl(Profile* profile);
BraveWaybackMachineDelegateImpl();
~BraveWaybackMachineDelegateImpl() override;

BraveWaybackMachineDelegateImpl(
Expand All @@ -30,13 +32,11 @@ class BraveWaybackMachineDelegateImpl : public BraveWaybackMachineDelegate {

private:
// BraveWaybackMachineDelegate overrides:
bool IsWaybackMachineEnabled() const override;
void CreateInfoBar(content::WebContents* web_contents) override;

std::unique_ptr<infobars::InfoBar> CreateInfoBar(
std::unique_ptr<infobars::InfoBar> CreateInfoBarView(
std::unique_ptr<BraveWaybackMachineInfoBarDelegate> delegate,
content::WebContents* contents) override;

Profile* profile_;
content::WebContents* contents);
};

#endif // BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// static
std::unique_ptr<infobars::InfoBar>
BraveWaybackMachineDelegateImpl::CreateInfoBar(
BraveWaybackMachineDelegateImpl::CreateInfoBarView(
std::unique_ptr<BraveWaybackMachineInfoBarDelegate> delegate,
content::WebContents* contents) {
return std::make_unique<BraveWaybackMachineInfoBarView>(std::move(delegate),
Expand Down
1 change: 0 additions & 1 deletion common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ const char kMigratedMuonProfile[] = "brave.muon.migrated_profile";
const char kBravePaymentsPinnedItemCount[] =
"brave.muon.import_pinned_item_count";
const char kWebTorrentEnabled[] = "brave.webtorrent_enabled";
const char kBraveWaybackMachineEnabled[] = "brave.wayback_machine_enabled";
const char kHangoutsEnabled[] = "brave.hangouts_enabled";
const char kHideBraveRewardsButton[] = "brave.hide_brave_rewards_button";
const char kIPFSCompanionEnabled[] = "brave.ipfs_companion_enabled";
Expand Down
1 change: 0 additions & 1 deletion common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ extern const char kLinkedInEmbedControlType[];
extern const char kMigratedMuonProfile[];
extern const char kBravePaymentsPinnedItemCount[];
extern const char kWebTorrentEnabled[];
extern const char kBraveWaybackMachineEnabled[];
extern const char kHangoutsEnabled[];
extern const char kHideBraveRewardsButton[];
extern const char kIPFSCompanionEnabled[];
Expand Down
3 changes: 3 additions & 0 deletions components/brave_wayback_machine/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ source_set("brave_wayback_machine") {
"brave_wayback_machine_infobar_delegate.h",
"brave_wayback_machine_tab_helper.cc",
"brave_wayback_machine_tab_helper.h",
"pref_names.cc",
"pref_names.h",
"wayback_machine_url_fetcher.cc",
"wayback_machine_url_fetcher.h",
]
Expand All @@ -23,6 +25,7 @@ source_set("brave_wayback_machine") {
"//base",
"//components/infobars/core",
"//components/prefs",
"//components/user_prefs",
"//content/public/browser",
"//net",
"//services/network/public/cpp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@
#ifndef BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_
#define BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_

#include <memory>

namespace content {
class WebContents;
} // namespace content

namespace infobars {
class InfoBar;
} // namespace infobars

class BraveWaybackMachineInfoBarDelegate;

class BraveWaybackMachineDelegate {
public:
virtual ~BraveWaybackMachineDelegate() = default;

virtual bool IsWaybackMachineEnabled() const = 0;
virtual std::unique_ptr<infobars::InfoBar> CreateInfoBar(
std::unique_ptr<BraveWaybackMachineInfoBarDelegate> delegate,
content::WebContents* contents) = 0;
virtual void CreateInfoBar(content::WebContents* web_contents) = 0;
};

#endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
#include "base/task/post_task.h"
#include "base/task/task_traits.h"
#include "brave/components/brave_wayback_machine/brave_wayback_machine_delegate.h"
#include "brave/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h"
#include "components/infobars/core/infobar.h"
#include "components/infobars/core/infobar_manager.h"
#include "brave/components/brave_wayback_machine/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "net/http/http_response_headers.h"
Expand All @@ -25,15 +26,11 @@ BraveWaybackMachineTabHelper::BraveWaybackMachineTabHelper(
content::WebContents* contents)
: WebContentsObserver(contents),
weak_factory_(this) {
pref_service_ = user_prefs::UserPrefs::Get(contents->GetBrowserContext());
}

BraveWaybackMachineTabHelper::~BraveWaybackMachineTabHelper() = default;

void BraveWaybackMachineTabHelper::SetInfoBarManager(
infobars::InfoBarManager* manager) {
DCHECK(manager);
infobar_manager_ = manager;
}
void BraveWaybackMachineTabHelper::set_delegate(
std::unique_ptr<BraveWaybackMachineDelegate> delegate) {
DCHECK(delegate);
Expand All @@ -43,7 +40,7 @@ void BraveWaybackMachineTabHelper::set_delegate(
void BraveWaybackMachineTabHelper::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
DCHECK(delegate_);
if (!delegate_->IsWaybackMachineEnabled())
if (!IsWaybackMachineEnabled())
return;

if (!navigation_handle->IsInMainFrame() ||
Expand All @@ -65,12 +62,12 @@ void BraveWaybackMachineTabHelper::DidFinishNavigation(
}

void BraveWaybackMachineTabHelper::CreateInfoBar() {
DCHECK(infobar_manager_);
infobar_manager_->AddInfoBar(
delegate_->CreateInfoBar(
std::make_unique<BraveWaybackMachineInfoBarDelegate>(),
web_contents()),
true);
DCHECK(delegate_);
delegate_->CreateInfoBar(web_contents());
}

bool BraveWaybackMachineTabHelper::IsWaybackMachineEnabled() const {
return pref_service_->GetBoolean(kBraveWaybackMachineEnabled);
}

bool BraveWaybackMachineTabHelper::ShouldAttachWaybackMachineInfoBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace infobars {
class InfoBarManager;
} // namespace infobars

class BraveWaybackMachineDelegate;
class PrefService;

class BraveWaybackMachineTabHelper
: public content::WebContentsObserver,
Expand All @@ -29,7 +26,6 @@ class BraveWaybackMachineTabHelper
BraveWaybackMachineTabHelper& operator=(
const BraveWaybackMachineTabHelper&) = delete;

void SetInfoBarManager(infobars::InfoBarManager* manager);
void set_delegate(std::unique_ptr<BraveWaybackMachineDelegate> delegate);

WEB_CONTENTS_USER_DATA_KEY_DECL();
Expand All @@ -41,11 +37,12 @@ class BraveWaybackMachineTabHelper
content::NavigationHandle* navigation_handle) override;

void CreateInfoBar();
bool IsWaybackMachineEnabled() const;

// virtual for test.
virtual bool ShouldAttachWaybackMachineInfoBar(int response_code) const;

infobars::InfoBarManager* infobar_manager_ = nullptr;
PrefService* pref_service_ = nullptr;
std::unique_ptr<BraveWaybackMachineDelegate> delegate_;

base::WeakPtrFactory<BraveWaybackMachineTabHelper> weak_factory_;
Expand Down
8 changes: 8 additions & 0 deletions components/brave_wayback_machine/pref_names.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* 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/components/brave_wayback_machine/pref_names.h"

const char kBraveWaybackMachineEnabled[] = "brave.wayback_machine_enabled";
11 changes: 11 additions & 0 deletions components/brave_wayback_machine/pref_names.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* 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_COMPONENTS_BRAVE_WAYBACK_MACHINE_PREF_NAMES_H_
#define BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_PREF_NAMES_H_

extern const char kBraveWaybackMachineEnabled[];

#endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_PREF_NAMES_H_

0 comments on commit 84400ed

Please sign in to comment.