Skip to content

Commit

Permalink
Updates to shields panel v2
Browse files Browse the repository at this point in the history
- Added favicon observer
- Added web component button
  • Loading branch information
taher authored and taher committed Mar 14, 2022
1 parent af27007 commit d21fef2
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 95 deletions.
42 changes: 41 additions & 1 deletion browser/ui/brave_shields_data_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/favicon/content/content_favicon_driver.h"
#include "content/public/browser/navigation_handle.h"
#include "net/base/url_util.h"

using net::AppendQueryParameter;

namespace brave_shields {

Expand All @@ -22,7 +26,10 @@ BraveShieldsDataController::~BraveShieldsDataController() = default;
BraveShieldsDataController::BraveShieldsDataController(
content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<BraveShieldsDataController>(*web_contents) {}
content::WebContentsUserData<BraveShieldsDataController>(*web_contents) {
favicon::ContentFaviconDriver::FromWebContents(web_contents)
->AddObserver(this);
}

void BraveShieldsDataController::DidFinishNavigation(
content::NavigationHandle* navigation_handle) {
Expand All @@ -32,6 +39,23 @@ void BraveShieldsDataController::DidFinishNavigation(
}
}

void BraveShieldsDataController::WebContentsDestroyed() {
favicon::ContentFaviconDriver::FromWebContents(web_contents())
->RemoveObserver(this);
}

void BraveShieldsDataController::OnFaviconUpdated(
favicon::FaviconDriver* favicon_driver,
NotificationIconType notification_icon_type,
const GURL& icon_url,
bool icon_url_changed,
const gfx::Image& image) {
if (icon_url_changed) {
for (Observer& obs : observer_list_)
obs.OnFaviconUpdated();
}
}

void BraveShieldsDataController::ReloadWebContents() {
web_contents()->GetController().Reload(content::ReloadType::NORMAL, true);
}
Expand Down Expand Up @@ -112,6 +136,22 @@ GURL BraveShieldsDataController::GetCurrentSiteURL() {
return web_contents()->GetLastCommittedURL();
}

GURL BraveShieldsDataController::GetFaviconURL(bool refresh) {
auto url = GURL("chrome://favicon2/");
url = AppendQueryParameter(url, "size", "16");
url = AppendQueryParameter(url, "scale_factor", "2x");
url = AppendQueryParameter(url, "show_fallback_monogram", "");
url = AppendQueryParameter(url, "page_url",
GetCurrentSiteURL().GetWithoutFilename().spec());

if (refresh) {
url = AppendQueryParameter(url, "v",
std::to_string(base::Time::Now().ToJsTime()));
}

return url;
}

AdBlockMode BraveShieldsDataController::GetAdBlockMode() {
auto* map = HostContentSettingsMapFactory::GetForProfile(
web_contents()->GetBrowserContext());
Expand Down
15 changes: 14 additions & 1 deletion browser/ui/brave_shields_data_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "base/observer_list.h"
#include "brave/components/brave_shields/common/brave_shields_panel.mojom.h"
#include "components/favicon/core/favicon_driver_observer.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
Expand All @@ -26,7 +27,8 @@ namespace brave_shields {
// Per-tab class to manage Shields panel data
class BraveShieldsDataController
: public content::WebContentsObserver,
public content::WebContentsUserData<BraveShieldsDataController> {
public content::WebContentsUserData<BraveShieldsDataController>,
public favicon::FaviconDriverObserver {
public:
BraveShieldsDataController(const BraveShieldsDataController&) = delete;
BraveShieldsDataController& operator=(const BraveShieldsDataController&) =
Expand All @@ -36,6 +38,7 @@ class BraveShieldsDataController
class Observer : public base::CheckedObserver {
public:
virtual void OnResourcesChanged() = 0;
virtual void OnFaviconUpdated() {}
};

void HandleItemBlocked(const std::string& block_type,
Expand All @@ -49,6 +52,7 @@ class BraveShieldsDataController
bool GetBraveShieldsEnabled();
void SetBraveShieldsEnabled(bool is_enabled);
GURL GetCurrentSiteURL();
GURL GetFaviconURL(bool refresh);

AdBlockMode GetAdBlockMode();
FingerprintMode GetFingerprintMode();
Expand All @@ -70,8 +74,17 @@ class BraveShieldsDataController

explicit BraveShieldsDataController(content::WebContents* web_contents);

// content::WebContentsObserver
void DidFinishNavigation(
content::NavigationHandle* navigation_handle) override;
void WebContentsDestroyed() override;

// favicon::FaviconDriverObserver
void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver,
NotificationIconType notification_icon_type,
const GURL& icon_url,
bool icon_url_changed,
const gfx::Image& image) override;

void ReloadWebContents();

Expand Down
3 changes: 3 additions & 0 deletions browser/ui/brave_shields_data_controller_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "chrome/test/base/testing_profile_manager.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/favicon/content/content_favicon_driver.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
Expand Down Expand Up @@ -49,6 +50,8 @@ class BraveShieldsDataControllerTest : public testing::Test {

test_web_contents_ =
content::WebContentsTester::CreateTestWebContents(profile_, nullptr);
favicon::ContentFaviconDriver::CreateForWebContents(
test_web_contents_.get(), nullptr);
BraveShieldsDataController::CreateForWebContents(test_web_contents_.get());
}

Expand Down
27 changes: 25 additions & 2 deletions browser/ui/views/brave_actions/brave_shields_action_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
#include <utility>

#include "brave/browser/ui/brave_actions/brave_action_icon_with_badge_image_source.h"
#include "brave/common/pref_names.h"
#include "brave/common/webui_url_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h"
#include "components/grit/brave_components_resources.h"
#include "components/grit/brave_components_strings.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/url_constants.h"
#include "extensions/common/constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
Expand Down Expand Up @@ -104,8 +107,8 @@ BraveShieldsActionView::GetImageSource() {
auto* shields_data_controller =
brave_shields::BraveShieldsDataController::FromWebContents(
web_contents);
int count = shields_data_controller->GetTotalBlockedCount();

int count = shields_data_controller->GetTotalBlockedCount();
if (count > 0) {
badge_text = count > 99 ? "99+" : std::to_string(count);
}
Expand All @@ -116,10 +119,18 @@ BraveShieldsActionView::GetImageSource() {
badge = std::make_unique<IconWithBadgeImageSource::Badge>(
badge_text, SK_ColorWHITE, kBadgeBg);
}

SetTooltipText(l10n_util::GetStringUTF16(IDS_BRAVE_SHIELDS));

if (is_enabled) {
SetTooltipText(
l10n_util::GetStringFUTF16Int(IDS_BRAVE_SHIELDS_ICON_TOOLTIP, count));
}
}

image_source->SetIcon(gfx::Image(GetIconImage(is_enabled)));
if (is_enabled)

if (is_enabled && profile_->GetPrefs()->GetBoolean(kShieldsStatsBadgeVisible))
image_source->SetBadge(std::move(badge));

return image_source;
Expand All @@ -145,6 +156,11 @@ void BraveShieldsActionView::UpdateIconState() {
}

void BraveShieldsActionView::ButtonPressed() {
auto* web_content = tab_strip_model_->GetActiveWebContents();
if (web_content && SchemeIsLocal(web_content->GetLastCommittedURL())) {
return; // Do not show bubble if it's a local scheme
}

if (!webui_bubble_manager_) {
webui_bubble_manager_ =
std::make_unique<WebUIBubbleManagerT<ShieldsPanelUI>>(
Expand All @@ -159,6 +175,13 @@ void BraveShieldsActionView::ButtonPressed() {
webui_bubble_manager_->ShowBubble();
}

bool BraveShieldsActionView::SchemeIsLocal(GURL url) {
return url.SchemeIs(url::kAboutScheme) || url.SchemeIs(url::kBlobScheme) ||
url.SchemeIs(url::kDataScheme) ||
url.SchemeIs(url::kFileSystemScheme) ||
url.SchemeIs(content::kChromeUIScheme);
}

std::unique_ptr<views::LabelButtonBorder>
BraveShieldsActionView::CreateDefaultBorder() const {
std::unique_ptr<views::LabelButtonBorder> border =
Expand Down
1 change: 1 addition & 0 deletions browser/ui/views/brave_actions/brave_shields_action_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BraveShieldsActionView

private:
void ButtonPressed();
bool SchemeIsLocal(GURL url);
void UpdateIconState();
gfx::ImageSkia GetIconImage(bool is_enabled);
std::unique_ptr<IconWithBadgeImageSource> GetImageSource();
Expand Down
27 changes: 27 additions & 0 deletions browser/ui/webui/brave_shields/shields_panel_data_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <utility>

#include "brave/browser/ui/webui/brave_shields/shields_panel_data_handler.h"
#include "brave/browser/webcompat_reporter/webcompat_reporter_dialog.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
Expand Down Expand Up @@ -107,6 +108,13 @@ void ShieldsPanelDataHandler::SetBraveShieldsEnabled(bool is_enabled) {
shields_data_ctrlr->SetBraveShieldsEnabled(is_enabled);
}

void ShieldsPanelDataHandler::OpenWebCompatWindow() {
auto* shields_data_ctrlr = GetActiveShieldsDataController();
DCHECK(shields_data_ctrlr);

OpenWebcompatReporterDialog(shields_data_ctrlr->web_contents());
}

BraveShieldsDataController*
ShieldsPanelDataHandler::GetActiveShieldsDataController() {
auto* profile = Profile::FromWebUI(webui_controller_->web_ui());
Expand Down Expand Up @@ -139,6 +147,12 @@ void ShieldsPanelDataHandler::UpdateSiteBlockInfo() {
site_block_info_.is_shields_enabled =
shields_data_ctrlr->GetBraveShieldsEnabled();

// This method gets called from various callsites. Constantly updating favicon
// url will replace the hashed version too. So, we update this once only
if (site_block_info_.favicon_url.is_empty()) {
site_block_info_.favicon_url = shields_data_ctrlr->GetFaviconURL(false);
}

// Notify remote that data changed
if (ui_handler_remote_) {
ui_handler_remote_.get()->OnSiteBlockInfoChanged(site_block_info_.Clone());
Expand All @@ -149,6 +163,19 @@ void ShieldsPanelDataHandler::OnResourcesChanged() {
UpdateSiteBlockInfo();
}

void ShieldsPanelDataHandler::OnFaviconUpdated() {
auto* shields_data_ctrlr = GetActiveShieldsDataController();
if (!shields_data_ctrlr)
return;

site_block_info_.favicon_url = shields_data_ctrlr->GetFaviconURL(true);

// Notify remote that favicon changed
if (ui_handler_remote_) {
ui_handler_remote_.get()->OnSiteBlockInfoChanged(site_block_info_.Clone());
}
}

void ShieldsPanelDataHandler::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
Expand Down
6 changes: 5 additions & 1 deletion browser/ui/webui/brave_shields/shields_panel_data_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MojoBubbleWebUIController;
using brave_shields::BraveShieldsDataController;
using brave_shields::mojom::SiteBlockInfo;
using brave_shields::mojom::SiteSettings;
using favicon::FaviconDriver;

class ShieldsPanelDataHandler : public brave_shields::mojom::DataHandler,
public BraveShieldsDataController::Observer,
Expand All @@ -45,13 +46,16 @@ class ShieldsPanelDataHandler : public brave_shields::mojom::DataHandler,
void SetIsNoScriptsEnabled(bool is_enabled) override;
void SetHTTPSEverywhereEnabled(bool is_enabled) override;
void SetBraveShieldsEnabled(bool is_enabled) override;
void OpenWebCompatWindow() override;

private:
BraveShieldsDataController* GetActiveShieldsDataController();
void UpdateSiteBlockInfo();

// brave_shields::BraveShieldsDataController
// BraveShieldsDataController::Observer
void OnResourcesChanged() override;
void OnFaviconUpdated() override;

// TabStripModelObserver
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
Expand Down
10 changes: 0 additions & 10 deletions browser/ui/webui/brave_shields/shields_panel_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,3 @@ void ShieldsPanelHandler::CloseUI() {
embedder->CloseUI();
}
}

void ShieldsPanelHandler::OpenURL(const GURL& url) {
Browser* browser = chrome::FindBrowserWithWebContents(
webui_controller_->web_ui()->GetWebContents());

if (!browser)
return;

chrome::AddTabAt(browser, url, -1, true);
}
1 change: 0 additions & 1 deletion browser/ui/webui/brave_shields/shields_panel_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class ShieldsPanelHandler : public brave_shields::mojom::PanelHandler {
// brave_shields::mojom::PanelHandler:
void ShowUI() override;
void CloseUI() override;
void OpenURL(const GURL& url) override;

private:
mojo::Receiver<brave_shields::mojom::PanelHandler> receiver_;
Expand Down
3 changes: 2 additions & 1 deletion common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"chrome://read-later.top-chrome/*",
"chrome://tab-strip/*",
"chrome://wallet-panel.top-chrome/*",
"chrome://wallet/*"
"chrome://wallet/*",
"chrome://brave-shields.top-chrome/*"
]
}, {
"channel": "stable",
Expand Down
4 changes: 2 additions & 2 deletions components/brave_shields/common/brave_shields_panel.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interface PanelHandler {

// Notify the backend that the dialog should be closed.
CloseUI();

OpenURL(url.mojom.Url url);
};

// WebUI-side handler for requests from the browser.
Expand All @@ -40,12 +38,14 @@ interface DataHandler {
SetIsNoScriptsEnabled(bool is_enabled);
SetHTTPSEverywhereEnabled(bool is_enabled);
SetBraveShieldsEnabled(bool is_enabled);
OpenWebCompatWindow();
};

struct SiteBlockInfo {
string host;
int32 total_blocked_resources;
bool is_shields_enabled;
url.mojom.Url favicon_url;
array<url.mojom.Url> ads_list;
array<url.mojom.Url> http_redirects_list;
array<url.mojom.Url> js_list;
Expand Down
Loading

0 comments on commit d21fef2

Please sign in to comment.