Commit dd2fee89 authored by Karandeep Bhatia's avatar Karandeep Bhatia Committed by Commit Bot

DNR: Update setActionCountAsBadgeText schema.

Change it to accept a dictionary instead of individual arguments for
better future compatibility. Also rename it to setExtensionActionOptions
for further flexibility.

Note that this is a breaking change but we'll reach out to the current
users to make sure that they update their extensions.

Skipping presubmit since this seems to be hitting crbug.com/956368.

BUG=1131746

No-Presubmit: True
Change-Id: I6b800985ca2eaff4f52900b984ef5519e3fac266
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2429260Reviewed-by: default avatarDevlin <rdevlin.cronin@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813041}
parent 9d728dd0
......@@ -450,14 +450,15 @@ class DeclarativeNetRequestBrowserTest
void SetActionsAsBadgeText(const ExtensionId& extension_id, bool pref) {
const char* pref_string = pref ? "true" : "false";
static constexpr char kSetActionCountAsBadgeTextScript[] = R"(
chrome.declarativeNetRequest.setActionCountAsBadgeText(%s);
static constexpr char kSetExtensionActionOptionsScript[] = R"(
chrome.declarativeNetRequest.setExtensionActionOptions(
{displayActionCountAsBadgeText: %s});
window.domAutomationController.send("done");
)";
ExecuteScriptInBackgroundPage(
extension_id,
base::StringPrintf(kSetActionCountAsBadgeTextScript, pref_string));
base::StringPrintf(kSetExtensionActionOptionsScript, pref_string));
}
// Navigates frame with name |frame_name| to |url|.
......@@ -3091,8 +3092,9 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
EXPECT_FALSE(action->HasDNRActionCount(first_tab_id));
}
// Test that enabling the setActionCountAsBadgeText preference will update
// all browsers sharing the same browser context.
// Test that enabling the "displayActionCountAsBadgeText" preference using
// setExtensionActionOptions will update all browsers sharing the same browser
// context.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
ActionCountPreferenceMultipleWindows) {
// Load the extension with a background script so scripts can be run from its
......
......@@ -513,16 +513,16 @@ ExtensionActionGetPopupFunction::RunExtensionAction() {
ExtensionFunction::ResponseAction
ExtensionActionGetBadgeTextFunction::RunExtensionAction() {
// Return a placeholder value if the extension has called
// setActionCountAsBadgeText(true) and the badge count shown for this tab is
// the number of actions matched.
// Return a placeholder value if the extension has enabled using
// declarativeNetRequest action count as badge text and the badge count shown
// for this tab is the number of actions matched.
std::string badge_text =
extension_action_->UseDNRActionCountAsBadgeText(tab_id_)
? declarative_net_request::kActionCountPlaceholderBadgeText
: extension_action_->GetExplicitlySetBadgeText(tab_id_);
// TODO(crbug.com/990224): Document this behavior once
// chrome.declarativeNetRequest.setActionCountAsBadgeText is promoted to beta
// chrome.declarativeNetRequest.setExtensionActionOptions is promoted to beta
// from trunk.
return RespondNow(
OneArgument(std::make_unique<base::Value>(std::move(badge_text))));
......
......@@ -61,8 +61,9 @@ class ActionTracker {
const WebRequestInfo& request_info);
// Updates the action count for all tabs for the specified |extension_id|'s
// extension action. Called when chrome.setActionCountAsBadgeText(true) is
// called by an extension.
// extension action. Called when the extension calls setExtensionActionOptions
// to enable setting the action count as badge text.
// TODO(karandeepb): Rename to OnActionCountAsBadgeTextPreferenceEnabled.
void OnPreferenceEnabled(const ExtensionId& extension_id) const;
// Clears the TrackedInfo for the specified |extension_id| for all tabs.
......
......@@ -332,31 +332,35 @@ bool DeclarativeNetRequestGetMatchedRulesFunction::ShouldSkipQuotaLimiting()
return user_gesture() || disable_throttling_for_test_;
}
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::
DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default;
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default;
DeclarativeNetRequestSetExtensionActionOptionsFunction::
DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
DeclarativeNetRequestSetExtensionActionOptionsFunction::
~DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::Run() {
using Params = dnr_api::SetActionCountAsBadgeText::Params;
DeclarativeNetRequestSetExtensionActionOptionsFunction::Run() {
using Params = dnr_api::SetExtensionActionOptions::Params;
base::string16 error;
std::unique_ptr<Params> params(Params::Create(*args_, &error));
EXTENSION_FUNCTION_VALIDATE(params);
EXTENSION_FUNCTION_VALIDATE(error.empty());
bool use_action_count_as_badge_text =
params->options.display_action_count_as_badge_text;
ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context());
if (params->enable == prefs->GetDNRUseActionCountAsBadgeText(extension_id()))
if (use_action_count_as_badge_text ==
prefs->GetDNRUseActionCountAsBadgeText(extension_id()))
return RespondNow(NoArguments());
prefs->SetDNRUseActionCountAsBadgeText(extension_id(), params->enable);
prefs->SetDNRUseActionCountAsBadgeText(extension_id(),
use_action_count_as_badge_text);
// If the preference is switched on, update the extension's badge text with
// the number of actions matched for this extension. Otherwise, clear the
// action count for the extension's icon and show the default badge text if
// set.
if (params->enable) {
if (use_action_count_as_badge_text) {
declarative_net_request::RulesMonitorService* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service);
......
......@@ -105,15 +105,15 @@ class DeclarativeNetRequestGetMatchedRulesFunction : public ExtensionFunction {
static bool disable_throttling_for_test_;
};
class DeclarativeNetRequestSetActionCountAsBadgeTextFunction
class DeclarativeNetRequestSetExtensionActionOptionsFunction
: public ExtensionFunction {
public:
DeclarativeNetRequestSetActionCountAsBadgeTextFunction();
DECLARE_EXTENSION_FUNCTION("declarativeNetRequest.setActionCountAsBadgeText",
DeclarativeNetRequestSetExtensionActionOptionsFunction();
DECLARE_EXTENSION_FUNCTION("declarativeNetRequest.setExtensionActionOptions",
DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT)
protected:
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() override;
~DeclarativeNetRequestSetExtensionActionOptionsFunction() override;
ExtensionFunction::ResponseAction Run() override;
};
......
......@@ -308,7 +308,7 @@ class ExtensionAction {
// Maps tab_id to the number of actions taken based on declarative net request
// rule matches on incoming requests. Overrides the default |badge_text_| for
// this extension if it has called chrome.setActionCountAsBadgeText(true).
// this extension if it has opted into setting the action count as badge text.
std::map<int, int> dnr_action_count_;
// ExtensionIconSet containing paths to bitmaps from which default icon's
......
......@@ -671,7 +671,7 @@ class ExtensionPrefs : public KeyedService {
// Whether the extension with the given |extension_id| is using its ruleset's
// matched action count for the badge text. This is set via the
// setActionCountAsBadgeText API call.
// setExtensionActionOptions API call.
bool GetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id) const;
void SetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id,
bool use_action_count_as_badge_text);
......
......@@ -428,6 +428,12 @@ namespace declarativeNetRequest {
DOMString[]? enableRulesetIds;
};
dictionary ExtensionActionOptions {
// Whether to automatically display the action count for a page as the
// extension's badge text. False by default.
boolean displayActionCountAsBadgeText;
};
callback EmptyCallback = void();
callback GetAllowedPagesCallback = void(DOMString[] result);
callback GetRulesCallback = void(Rule[] rules);
......@@ -500,10 +506,10 @@ namespace declarativeNetRequest {
static void getMatchedRules(optional MatchedRulesFilter filter,
GetMatchedRulesCallback callback);
// Sets whether to automatically badge extension's icon to the matched
// action count for a tab. This preference is persisted across sessions and
// is false by default.
static void setActionCountAsBadgeText(boolean enable);
// Configures how matched actions will be displayed on the extension action.
// This preference is persisted across sessions.
static void setExtensionActionOptions(
ExtensionActionOptions options);
// Checks if the given regular expression will be supported as a
// <code>regexFilter</code> rule condition.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment