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 ...@@ -450,14 +450,15 @@ class DeclarativeNetRequestBrowserTest
void SetActionsAsBadgeText(const ExtensionId& extension_id, bool pref) { void SetActionsAsBadgeText(const ExtensionId& extension_id, bool pref) {
const char* pref_string = pref ? "true" : "false"; const char* pref_string = pref ? "true" : "false";
static constexpr char kSetActionCountAsBadgeTextScript[] = R"( static constexpr char kSetExtensionActionOptionsScript[] = R"(
chrome.declarativeNetRequest.setActionCountAsBadgeText(%s); chrome.declarativeNetRequest.setExtensionActionOptions(
{displayActionCountAsBadgeText: %s});
window.domAutomationController.send("done"); window.domAutomationController.send("done");
)"; )";
ExecuteScriptInBackgroundPage( ExecuteScriptInBackgroundPage(
extension_id, extension_id,
base::StringPrintf(kSetActionCountAsBadgeTextScript, pref_string)); base::StringPrintf(kSetExtensionActionOptionsScript, pref_string));
} }
// Navigates frame with name |frame_name| to |url|. // Navigates frame with name |frame_name| to |url|.
...@@ -3091,8 +3092,9 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, ...@@ -3091,8 +3092,9 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
EXPECT_FALSE(action->HasDNRActionCount(first_tab_id)); EXPECT_FALSE(action->HasDNRActionCount(first_tab_id));
} }
// Test that enabling the setActionCountAsBadgeText preference will update // Test that enabling the "displayActionCountAsBadgeText" preference using
// all browsers sharing the same browser context. // setExtensionActionOptions will update all browsers sharing the same browser
// context.
IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
ActionCountPreferenceMultipleWindows) { ActionCountPreferenceMultipleWindows) {
// Load the extension with a background script so scripts can be run from its // Load the extension with a background script so scripts can be run from its
......
...@@ -513,16 +513,16 @@ ExtensionActionGetPopupFunction::RunExtensionAction() { ...@@ -513,16 +513,16 @@ ExtensionActionGetPopupFunction::RunExtensionAction() {
ExtensionFunction::ResponseAction ExtensionFunction::ResponseAction
ExtensionActionGetBadgeTextFunction::RunExtensionAction() { ExtensionActionGetBadgeTextFunction::RunExtensionAction() {
// Return a placeholder value if the extension has called // Return a placeholder value if the extension has enabled using
// setActionCountAsBadgeText(true) and the badge count shown for this tab is // declarativeNetRequest action count as badge text and the badge count shown
// the number of actions matched. // for this tab is the number of actions matched.
std::string badge_text = std::string badge_text =
extension_action_->UseDNRActionCountAsBadgeText(tab_id_) extension_action_->UseDNRActionCountAsBadgeText(tab_id_)
? declarative_net_request::kActionCountPlaceholderBadgeText ? declarative_net_request::kActionCountPlaceholderBadgeText
: extension_action_->GetExplicitlySetBadgeText(tab_id_); : extension_action_->GetExplicitlySetBadgeText(tab_id_);
// TODO(crbug.com/990224): Document this behavior once // TODO(crbug.com/990224): Document this behavior once
// chrome.declarativeNetRequest.setActionCountAsBadgeText is promoted to beta // chrome.declarativeNetRequest.setExtensionActionOptions is promoted to beta
// from trunk. // from trunk.
return RespondNow( return RespondNow(
OneArgument(std::make_unique<base::Value>(std::move(badge_text)))); OneArgument(std::make_unique<base::Value>(std::move(badge_text))));
......
...@@ -61,8 +61,9 @@ class ActionTracker { ...@@ -61,8 +61,9 @@ class ActionTracker {
const WebRequestInfo& request_info); const WebRequestInfo& request_info);
// Updates the action count for all tabs for the specified |extension_id|'s // Updates the action count for all tabs for the specified |extension_id|'s
// extension action. Called when chrome.setActionCountAsBadgeText(true) is // extension action. Called when the extension calls setExtensionActionOptions
// called by an extension. // to enable setting the action count as badge text.
// TODO(karandeepb): Rename to OnActionCountAsBadgeTextPreferenceEnabled.
void OnPreferenceEnabled(const ExtensionId& extension_id) const; void OnPreferenceEnabled(const ExtensionId& extension_id) const;
// Clears the TrackedInfo for the specified |extension_id| for all tabs. // Clears the TrackedInfo for the specified |extension_id| for all tabs.
......
...@@ -332,31 +332,35 @@ bool DeclarativeNetRequestGetMatchedRulesFunction::ShouldSkipQuotaLimiting() ...@@ -332,31 +332,35 @@ bool DeclarativeNetRequestGetMatchedRulesFunction::ShouldSkipQuotaLimiting()
return user_gesture() || disable_throttling_for_test_; return user_gesture() || disable_throttling_for_test_;
} }
DeclarativeNetRequestSetActionCountAsBadgeTextFunction:: DeclarativeNetRequestSetExtensionActionOptionsFunction::
DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default; DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
DeclarativeNetRequestSetActionCountAsBadgeTextFunction:: DeclarativeNetRequestSetExtensionActionOptionsFunction::
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default; ~DeclarativeNetRequestSetExtensionActionOptionsFunction() = default;
ExtensionFunction::ResponseAction ExtensionFunction::ResponseAction
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::Run() { DeclarativeNetRequestSetExtensionActionOptionsFunction::Run() {
using Params = dnr_api::SetActionCountAsBadgeText::Params; using Params = dnr_api::SetExtensionActionOptions::Params;
base::string16 error; base::string16 error;
std::unique_ptr<Params> params(Params::Create(*args_, &error)); std::unique_ptr<Params> params(Params::Create(*args_, &error));
EXTENSION_FUNCTION_VALIDATE(params); EXTENSION_FUNCTION_VALIDATE(params);
EXTENSION_FUNCTION_VALIDATE(error.empty()); 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()); 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()); 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 // If the preference is switched on, update the extension's badge text with
// the number of actions matched for this extension. Otherwise, clear the // 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 // action count for the extension's icon and show the default badge text if
// set. // set.
if (params->enable) { if (use_action_count_as_badge_text) {
declarative_net_request::RulesMonitorService* rules_monitor_service = declarative_net_request::RulesMonitorService* rules_monitor_service =
declarative_net_request::RulesMonitorService::Get(browser_context()); declarative_net_request::RulesMonitorService::Get(browser_context());
DCHECK(rules_monitor_service); DCHECK(rules_monitor_service);
......
...@@ -105,15 +105,15 @@ class DeclarativeNetRequestGetMatchedRulesFunction : public ExtensionFunction { ...@@ -105,15 +105,15 @@ class DeclarativeNetRequestGetMatchedRulesFunction : public ExtensionFunction {
static bool disable_throttling_for_test_; static bool disable_throttling_for_test_;
}; };
class DeclarativeNetRequestSetActionCountAsBadgeTextFunction class DeclarativeNetRequestSetExtensionActionOptionsFunction
: public ExtensionFunction { : public ExtensionFunction {
public: public:
DeclarativeNetRequestSetActionCountAsBadgeTextFunction(); DeclarativeNetRequestSetExtensionActionOptionsFunction();
DECLARE_EXTENSION_FUNCTION("declarativeNetRequest.setActionCountAsBadgeText", DECLARE_EXTENSION_FUNCTION("declarativeNetRequest.setExtensionActionOptions",
DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT) DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT)
protected: protected:
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() override; ~DeclarativeNetRequestSetExtensionActionOptionsFunction() override;
ExtensionFunction::ResponseAction Run() override; ExtensionFunction::ResponseAction Run() override;
}; };
......
...@@ -308,7 +308,7 @@ class ExtensionAction { ...@@ -308,7 +308,7 @@ class ExtensionAction {
// Maps tab_id to the number of actions taken based on declarative net request // 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 // 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_; std::map<int, int> dnr_action_count_;
// ExtensionIconSet containing paths to bitmaps from which default icon's // ExtensionIconSet containing paths to bitmaps from which default icon's
......
...@@ -671,7 +671,7 @@ class ExtensionPrefs : public KeyedService { ...@@ -671,7 +671,7 @@ class ExtensionPrefs : public KeyedService {
// Whether the extension with the given |extension_id| is using its ruleset's // 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 // 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; bool GetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id) const;
void SetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id, void SetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id,
bool use_action_count_as_badge_text); bool use_action_count_as_badge_text);
......
...@@ -428,6 +428,12 @@ namespace declarativeNetRequest { ...@@ -428,6 +428,12 @@ namespace declarativeNetRequest {
DOMString[]? enableRulesetIds; 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 EmptyCallback = void();
callback GetAllowedPagesCallback = void(DOMString[] result); callback GetAllowedPagesCallback = void(DOMString[] result);
callback GetRulesCallback = void(Rule[] rules); callback GetRulesCallback = void(Rule[] rules);
...@@ -500,10 +506,10 @@ namespace declarativeNetRequest { ...@@ -500,10 +506,10 @@ namespace declarativeNetRequest {
static void getMatchedRules(optional MatchedRulesFilter filter, static void getMatchedRules(optional MatchedRulesFilter filter,
GetMatchedRulesCallback callback); GetMatchedRulesCallback callback);
// Sets whether to automatically badge extension's icon to the matched // Configures how matched actions will be displayed on the extension action.
// action count for a tab. This preference is persisted across sessions and // This preference is persisted across sessions.
// is false by default. static void setExtensionActionOptions(
static void setActionCountAsBadgeText(boolean enable); ExtensionActionOptions options);
// Checks if the given regular expression will be supported as a // Checks if the given regular expression will be supported as a
// <code>regexFilter</code> rule condition. // <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