Skip to content

Commit

Permalink
Disable v2 ext on toggle is turned off. Added the Remove button for d…
Browse files Browse the repository at this point in the history
…isabled extensions.
  • Loading branch information
boocmp committed Dec 6, 2024
1 parent c77e3e1 commit be76e8d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 15 deletions.
3 changes: 3 additions & 0 deletions app/brave_settings_strings.grdp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,9 @@
<message name="IDS_SETTINGS_MANAGE_EXTENSIONS_V2_TOAST_CONFIRM" desc="The label of the error toast button in settings">
Close
</message>
<message name="IDS_SETTINGS_MANAGE_EXTENSIONS_V2_REMOVE_BUTTON" desc="The label of the remove button in settings">
Remove
</message>
<message name="IDS_SETTINGS_MANAGE_EXTENSIONS_V2_NO_SCRIPT_NAME" desc="Manifest V2 extension toggle's label">
Enable NoScript&#x2122;
</message>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ExtensionV2 = {
id: string
name: string
description: string
installed: boolean
enabled: boolean
}

Expand All @@ -23,6 +24,7 @@ export interface BraveDefaultExtensionsBrowserProxy {
isMediaRouterEnabled(): boolean
getExtensionsManifestV2(): Promise<ExtensionV2[]>
enableExtensionManifestV2(id: string, enabled: boolean): Promise<boolean>
removeExtensionManifestV2(id: string): Promise<boolean>
}

export class BraveDefaultExtensionsBrowserProxyImpl implements BraveDefaultExtensionsBrowserProxy {
Expand Down Expand Up @@ -59,7 +61,11 @@ export class BraveDefaultExtensionsBrowserProxyImpl implements BraveDefaultExten
}

enableExtensionManifestV2(id: string, enabled: boolean) {
return sendWithPromise('enableExtensionManifestV2', id, enabled);
return sendWithPromise('enableExtensionManifestV2', id, enabled)
}

removeExtensionManifestV2(id:string) {
return sendWithPromise('removeExtensionManifestV2', id)
}

static getInstance(): BraveDefaultExtensionsBrowserProxy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@

<template is="dom-repeat" items="[[braveV2Extensions_]]">
<settings-toggle-button id="[[item.id]]" class="cr-row" label="[[item.name]]" sub-label="[[item.description]]"
on-settings-boolean-control-change="onExtensionV2EnabledChange_" checked="[[item.enabled]]" pref="[[itemPref_(item.enabled)]]"
no-pref-set disabled="[[installInProgress_]]">
on-settings-boolean-control-change="onExtensionV2EnabledChange_" checked="[[item.enabled]]"
pref="[[itemPref_(item.enabled)]]" no-pref-set disabled="[[installInProgress_]]">
<template is="dom-if" if="[[showRemoveButton_(item)]]" restamp>
<cr-button on-click="removeExtension_" id="[[item.id]]" slot="more-actions">
$i18n{extensionsV2RemoveButtonLabel}
</cr-button>
</template>
</settings-toggle-button>
</template>
<template is="dom-if" if="{{showErrorToast_}}">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class BraveExtensionsV2Subpage extends BraveExtensionsV2SubpageBase {
})
}

removeExtension_(e: any) {
e.stopPropagation()
this.browserProxy_.removeExtensionManifestV2(e.target.id)
}

showRemoveButton_(ext: ExtensionV2): boolean {
return ext.installed && !ext.enabled
}

itemPref_(enabled: boolean) {
return {
key: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ bool ClickExtensionToggle(content::WebContents* web_contents) {
.value.is_none();
}

bool ClickExtensionRemove(content::WebContents* web_contents) {
return EvalJs(web_contents,
"window.testing.extensionsV2Subpage.getElementById('"
"doojmbjmlfjjnbmnoijecmcbfeoakpjm').querySelector('#"
"doojmbjmlfjjnbmnoijecmcbfeoakpjm').click()")
.value.is_none();
}

bool IsExtensionToggled(content::WebContents* web_contents) {
return EvalJs(web_contents,
"window.testing.extensionsV2Subpage.getElementById('"
Expand Down Expand Up @@ -86,6 +94,12 @@ class BraveExtensionsManifestV2BrowserTest : public InProcessBrowserTest {
}
}

bool IsExtensionEnabled() {
return extensions::ExtensionRegistry::Get(browser()->profile())
->enabled_extensions()
.Contains(kExtensionId);
}

bool IsExtensionInstalled() {
return extensions::ExtensionRegistry::Get(browser()->profile())
->GetInstalledExtension(kExtensionId);
Expand Down Expand Up @@ -147,9 +161,16 @@ IN_PROC_BROWSER_TEST_F(BraveExtensionsManifestV2BrowserTest,
ClickExtensionToggle(web_contents);
WaitExtensionToggled(true);
EXPECT_TRUE(IsExtensionInstalled());
EXPECT_TRUE(IsExtensionEnabled());

// disabled from settings -> uninstalled.
// disabled from settings.
ClickExtensionToggle(web_contents);
WaitExtensionToggled(false);
EXPECT_TRUE(IsExtensionInstalled());
EXPECT_FALSE(IsExtensionEnabled());

// remove from settings.
ClickExtensionRemove(web_contents);
EXPECT_FALSE(IsExtensionInstalled());
EXPECT_FALSE(IsExtensionEnabled());
}
49 changes: 40 additions & 9 deletions browser/ui/webui/settings/brave_extensions_manifest_v2_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ struct ExtensionManifestV2 {
std::string id;
std::u16string name;
std::u16string description;
bool installed = false;
bool enabled = false;

base::Value ToValue() const {
base::Value::Dict v;
v.Set("id", id);
v.Set("name", name);
v.Set("description", description);
v.Set("installed", installed);
v.Set("enabled", enabled);
return base::Value(std::move(v));
}
Expand Down Expand Up @@ -123,6 +125,11 @@ void BraveExtensionsManifestV2Handler::RegisterMessages() {
base::BindRepeating(
&BraveExtensionsManifestV2Handler::EnableExtensionManifestV2,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"removeExtensionManifestV2",
base::BindRepeating(
&BraveExtensionsManifestV2Handler::RemoveExtensionManifestV2,
base::Unretained(this)));

observation_.Observe(GetExtensionRegistry());
}
Expand All @@ -131,36 +138,36 @@ void BraveExtensionsManifestV2Handler::OnExtensionLoaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension) {
DCHECK(extension);
NotifyExtensionManifestV2Changed(browser_context, extension->id(), true);
NotifyExtensionManifestV2Changed(browser_context, extension->id());
}

void BraveExtensionsManifestV2Handler::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionReason reason) {
DCHECK(extension);
NotifyExtensionManifestV2Changed(browser_context, extension->id(), false);
NotifyExtensionManifestV2Changed(browser_context, extension->id());
}

void BraveExtensionsManifestV2Handler::OnExtensionInstalled(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
bool is_update) {
OnExtensionLoaded(browser_context, extension);
DCHECK(extension);
NotifyExtensionManifestV2Changed(browser_context, extension->id());
}

void BraveExtensionsManifestV2Handler::OnExtensionUninstalled(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UninstallReason reason) {
OnExtensionUnloaded(browser_context, extension,
extensions::UnloadedExtensionReason::UNINSTALL);
DCHECK(extension);
NotifyExtensionManifestV2Changed(browser_context, extension->id());
}

void BraveExtensionsManifestV2Handler::NotifyExtensionManifestV2Changed(
content::BrowserContext* browser_context,
const std::string& id,
bool enabled) {
const std::string& id) {
if (!IsJavascriptAllowed() ||
browser_context != web_ui()->GetWebContents()->GetBrowserContext()) {
return;
Expand All @@ -169,7 +176,6 @@ void BraveExtensionsManifestV2Handler::NotifyExtensionManifestV2Changed(
if (fnd == extensions_.end()) {
return;
}
fnd->enabled = enabled;
FireWebUIListener("brave-extension-manifest-v2-changed");
}

Expand Down Expand Up @@ -216,10 +222,33 @@ void BraveExtensionsManifestV2Handler::EnableExtensionManifestV2(
}
} else {
installer_.reset();
extension_service->DisableExtension(
id, extensions::disable_reason::DISABLE_USER_ACTION);
ResolveJavascriptCallback(args[0], base::Value(true));
}
}

void BraveExtensionsManifestV2Handler::RemoveExtensionManifestV2(
const base::Value::List& args) {
CHECK_EQ(args.size(), 2U);
const std::string id = args[1].GetString();

const bool installed =
GetExtensionRegistry()->GetInstalledExtension(id) != nullptr;

installer_.reset();

if (installed) {
auto* profile = Profile::FromBrowserContext(
web_ui()->GetWebContents()->GetBrowserContext());
auto* extension_service =
extensions::ExtensionSystem::Get(profile)->extension_service();
extension_service->UninstallExtension(
id, extensions::UNINSTALL_REASON_INTERNAL_MANAGEMENT, nullptr);
ResolveJavascriptCallback(args[0], base::Value(true));
}

AllowJavascript();
ResolveJavascriptCallback(args[0], base::Value(true));
}

void BraveExtensionsManifestV2Handler::GetExtensionsManifestV2(
Expand All @@ -229,6 +258,8 @@ void BraveExtensionsManifestV2Handler::GetExtensionsManifestV2(

base::Value::List result;
for (auto& e : extensions_) {
e.installed =
GetExtensionRegistry()->GetInstalledExtension(e.id) != nullptr;
e.enabled = GetExtensionRegistry()->enabled_extensions().Contains(e.id);
result.Append(e.ToValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ class BraveExtensionsManifestV2Handler

void NotifyExtensionManifestV2Changed(
content::BrowserContext* browser_context,
const std::string& id,
bool enabled);
const std::string& id);

extensions::ExtensionRegistry* GetExtensionRegistry();

void GetExtensionsManifestV2(const base::Value::List& args);
void EnableExtensionManifestV2(const base::Value::List& args);
void RemoveExtensionManifestV2(const base::Value::List& args);
void OnExtensionManifestV2Installed(
base::Value js_callback,
bool success,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source,
IDS_SETTINGS_MANAGE_EXTENSIONS_V2_SUBLABEL},
{"extensionsV2ToastConfirmButtonLabel",
IDS_SETTINGS_MANAGE_EXTENSIONS_V2_TOAST_CONFIRM},
{"extensionsV2RemoveButtonLabel",
IDS_SETTINGS_MANAGE_EXTENSIONS_V2_REMOVE_BUTTON},
{"manageExtensionsLabel", IDS_SETTINGS_MANAGE_EXTENSIONS_LABEL},
{"keyboardShortcuts", IDS_EXTENSIONS_SIDEBAR_KEYBOARD_SHORTCUTS},
{"getMoreExtensionsLabel", IDS_BRAVE_SETTINGS_GET_MORE_EXTENSIONS_LABEL},
Expand Down

0 comments on commit be76e8d

Please sign in to comment.