Commit 67a5a9ef authored by Kelvin Jiang's avatar Kelvin Jiang Committed by Commit Bot

[DNR] Add setActionCountAsBadgeText API call

Add an API call "setActionCountAsBadgeText" to toggle an extension's
preference for setting the # of actions matched by its ruleset. This CL
only adds the call itself and preference. The functionality will be
implemented in a follow up.

Skipping presubmit as the idl parser fails weirdly as seen
in crbug.com/956368

NOPRESUBMIT=true

Bug: 973211
Change-Id: I9e98cb4ac13fba3d18012f716753e613507b2747
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1642275
Commit-Queue: Kelvin Jiang <kelvinjiang@chromium.org>
Reviewed-by: default avatarKaran Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#668588}
parent d7e4cc0b
...@@ -573,6 +573,7 @@ source_set("unit_tests") { ...@@ -573,6 +573,7 @@ source_set("unit_tests") {
"api/declarative/deduping_factory_unittest.cc", "api/declarative/deduping_factory_unittest.cc",
"api/declarative/rules_registry_unittest.cc", "api/declarative/rules_registry_unittest.cc",
"api/declarative_net_request/composite_matcher_unittest.cc", "api/declarative_net_request/composite_matcher_unittest.cc",
"api/declarative_net_request/declarative_net_request_api_unittest.cc",
"api/declarative_net_request/file_sequence_helper_unittest.cc", "api/declarative_net_request/file_sequence_helper_unittest.cc",
"api/declarative_net_request/flat_ruleset_indexer_unittest.cc", "api/declarative_net_request/flat_ruleset_indexer_unittest.cc",
"api/declarative_net_request/indexed_rule_unittest.cc", "api/declarative_net_request/indexed_rule_unittest.cc",
......
...@@ -337,4 +337,23 @@ DeclarativeNetRequestGetMatchedRulesFunction::Run() { ...@@ -337,4 +337,23 @@ DeclarativeNetRequestGetMatchedRulesFunction::Run() {
return RespondNow(NoArguments()); return RespondNow(NoArguments());
} }
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::
DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default;
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() = default;
ExtensionFunction::ResponseAction
DeclarativeNetRequestSetActionCountAsBadgeTextFunction::Run() {
using Params = dnr_api::SetActionCountAsBadgeText::Params;
base::string16 error;
std::unique_ptr<Params> params(Params::Create(*args_, &error));
EXTENSION_FUNCTION_VALIDATE(params);
EXTENSION_FUNCTION_VALIDATE(error.empty());
ExtensionPrefs* prefs = ExtensionPrefs::Get(browser_context());
prefs->SetDNRUseActionCountAsBadgeText(extension_id(), params->enable);
return RespondNow(NoArguments());
}
} // namespace extensions } // namespace extensions
...@@ -197,6 +197,23 @@ class DeclarativeNetRequestGetMatchedRulesFunction ...@@ -197,6 +197,23 @@ class DeclarativeNetRequestGetMatchedRulesFunction
DISALLOW_COPY_AND_ASSIGN(DeclarativeNetRequestGetMatchedRulesFunction); DISALLOW_COPY_AND_ASSIGN(DeclarativeNetRequestGetMatchedRulesFunction);
}; };
class DeclarativeNetRequestSetActionCountAsBadgeTextFunction
: public UIThreadExtensionFunction {
public:
DeclarativeNetRequestSetActionCountAsBadgeTextFunction();
DECLARE_EXTENSION_FUNCTION("declarativeNetRequest.setActionCountAsBadgeText",
DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT)
protected:
~DeclarativeNetRequestSetActionCountAsBadgeTextFunction() override;
ExtensionFunction::ResponseAction Run() override;
private:
DISALLOW_COPY_AND_ASSIGN(
DeclarativeNetRequestSetActionCountAsBadgeTextFunction);
};
} // namespace extensions } // namespace extensions
#endif // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_DECLARATIVE_NET_REQUEST_API_H_ #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_NET_REQUEST_DECLARATIVE_NET_REQUEST_API_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/declarative_net_request/declarative_net_request_api.h"
#include "base/memory/ref_counted.h"
#include "components/version_info/version_info.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/api_unittest.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/common/extension.h"
#include "extensions/common/features/feature_channel.h"
namespace extensions {
namespace {
using DeclarativeNetRequestApiUnittest = ApiUnitTest;
TEST_F(DeclarativeNetRequestApiUnittest, SetActionCountAsBadgeText) {
// Set the current channel to trunk.
ScopedCurrentChannel scoped_channel(version_info::Channel::UNKNOWN);
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context());
// Preference should be false by default.
EXPECT_FALSE(
extension_prefs->GetDNRUseActionCountAsBadgeText(extension()->id()));
auto function = base::MakeRefCounted<
DeclarativeNetRequestSetActionCountAsBadgeTextFunction>();
function->set_extension(extension());
api_test_utils::RunFunction(function.get(), "[true]", browser_context());
extension_prefs = ExtensionPrefs::Get(browser_context());
EXPECT_TRUE(
extension_prefs->GetDNRUseActionCountAsBadgeText(extension()->id()));
}
} // namespace
} // namespace extensions
...@@ -1404,6 +1404,7 @@ enum HistogramValue { ...@@ -1404,6 +1404,7 @@ enum HistogramValue {
LOGINSCREENUI_SHOW = 1341, LOGINSCREENUI_SHOW = 1341,
LOGINSCREENUI_CLOSE = 1342, LOGINSCREENUI_CLOSE = 1342,
DECLARATIVENETREQUEST_GETMATCHEDRULES = 1343, DECLARATIVENETREQUEST_GETMATCHEDRULES = 1343,
DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT = 1344,
// Last entry: Add new entries above, then run: // Last entry: Add new entries above, then run:
// python tools/metrics/histograms/update_extension_histograms.py // python tools/metrics/histograms/update_extension_histograms.py
ENUM_BOUNDARY ENUM_BOUNDARY
......
...@@ -177,7 +177,7 @@ constexpr const char kPrefGeometryCache[] = "geometry_cache"; ...@@ -177,7 +177,7 @@ constexpr const char kPrefGeometryCache[] = "geometry_cache";
// A preference that indicates when an extension is last launched. // A preference that indicates when an extension is last launched.
constexpr const char kPrefLastLaunchTime[] = "last_launch_time"; constexpr const char kPrefLastLaunchTime[] = "last_launch_time";
// Am installation parameter bundled with an extension. // An installation parameter bundled with an extension.
constexpr const char kPrefInstallParam[] = "install_parameter"; constexpr const char kPrefInstallParam[] = "install_parameter";
// A list of installed ids and a signature. // A list of installed ids and a signature.
...@@ -204,6 +204,11 @@ constexpr const char kPrefDNRAllowedPages[] = "dnr_whitelisted_pages"; ...@@ -204,6 +204,11 @@ constexpr const char kPrefDNRAllowedPages[] = "dnr_whitelisted_pages";
constexpr const char kPrefDNRDynamicRulesetChecksum[] = constexpr const char kPrefDNRDynamicRulesetChecksum[] =
"dnr_dynamic_ruleset_checksum"; "dnr_dynamic_ruleset_checksum";
// A boolean preference that indicates whether the extension's icon should be
// automatically badged to the matched action count for a tab. False by default.
constexpr const char kPrefDNRUseActionCountAsBadgeText[] =
"dnr_use_action_count_as_badge_text";
// Provider of write access to a dictionary storing extension prefs. // Provider of write access to a dictionary storing extension prefs.
class ScopedExtensionPrefUpdate : public prefs::ScopedDictionaryPrefUpdate { class ScopedExtensionPrefUpdate : public prefs::ScopedDictionaryPrefUpdate {
public: public:
...@@ -1817,6 +1822,20 @@ URLPatternSet ExtensionPrefs::GetDNRAllowedPages( ...@@ -1817,6 +1822,20 @@ URLPatternSet ExtensionPrefs::GetDNRAllowedPages(
return result; return result;
} }
bool ExtensionPrefs::GetDNRUseActionCountAsBadgeText(
const ExtensionId& extension_id) const {
return ReadPrefAsBooleanAndReturn(extension_id,
kPrefDNRUseActionCountAsBadgeText);
}
void ExtensionPrefs::SetDNRUseActionCountAsBadgeText(
const ExtensionId& extension_id,
bool use_action_count_as_badge_text) {
UpdateExtensionPref(
extension_id, kPrefDNRUseActionCountAsBadgeText,
std::make_unique<base::Value>(use_action_count_as_badge_text));
}
// static // static
void ExtensionPrefs::SetRunAlertsInFirstRunForTest() { void ExtensionPrefs::SetRunAlertsInFirstRunForTest() {
g_run_alerts_in_first_run_for_testing = true; g_run_alerts_in_first_run_for_testing = true;
......
...@@ -578,9 +578,9 @@ class ExtensionPrefs : public KeyedService { ...@@ -578,9 +578,9 @@ class ExtensionPrefs : public KeyedService {
int GetCorruptedDisableCount() const; int GetCorruptedDisableCount() const;
void IncrementCorruptedDisableCount(); void IncrementCorruptedDisableCount();
// Whether the extension with the given |id| needs to be synced. This is set // Whether the extension with the given |extension_id| needs to be synced.
// when the state (such as enabled/disabled or allowed in incognito) is // This is set when the state (such as enabled/disabled or allowed in
// changed before Sync is ready. // incognito) is changed before Sync is ready.
bool NeedsSync(const std::string& extension_id) const; bool NeedsSync(const std::string& extension_id) const;
void SetNeedsSync(const std::string& extension_id, bool needs_sync); void SetNeedsSync(const std::string& extension_id, bool needs_sync);
...@@ -607,6 +607,13 @@ class ExtensionPrefs : public KeyedService { ...@@ -607,6 +607,13 @@ class ExtensionPrefs : public KeyedService {
// Returns the set of allowed pages for the given |extension_id|. // Returns the set of allowed pages for the given |extension_id|.
URLPatternSet GetDNRAllowedPages(const ExtensionId& extension_id) const; URLPatternSet GetDNRAllowedPages(const ExtensionId& extension_id) const;
// 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.
bool GetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id) const;
void SetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id,
bool use_action_count_as_badge_text);
// When called before the ExtensionService is created, alerts that are // When called before the ExtensionService is created, alerts that are
// normally suppressed in first run will still trigger. // normally suppressed in first run will still trigger.
static void SetRunAlertsInFirstRunForTest(); static void SetRunAlertsInFirstRunForTest();
......
...@@ -161,6 +161,11 @@ ...@@ -161,6 +161,11 @@
"contexts": ["blessed_extension"], "contexts": ["blessed_extension"],
"channel": "trunk" "channel": "trunk"
}, },
"declarativeNetRequest.setActionCountAsBadgeText": {
"dependencies": ["permission:declarativeNetRequest"],
"contexts": ["blessed_extension"],
"channel": "trunk"
},
"declarativeWebRequest": { "declarativeWebRequest": {
"dependencies": ["permission:declarativeWebRequest"], "dependencies": ["permission:declarativeWebRequest"],
"contexts": ["blessed_extension"] "contexts": ["blessed_extension"]
......
...@@ -270,6 +270,14 @@ namespace declarativeNetRequest { ...@@ -270,6 +270,14 @@ namespace declarativeNetRequest {
[nodoc] [nodoc]
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
// action count for a tab. This preference is persisted across sessions and
// is false by default.
// TODO(crbug.com/973211): Add documentation once implementation is
// complete.
[nodoc]
static void setActionCountAsBadgeText(boolean enable);
}; };
interface Properties { interface Properties {
......
...@@ -19705,6 +19705,7 @@ Called by update_net_error_codes.py.--> ...@@ -19705,6 +19705,7 @@ Called by update_net_error_codes.py.-->
<int value="1341" label="LOGINSCREENUI_SHOW"/> <int value="1341" label="LOGINSCREENUI_SHOW"/>
<int value="1342" label="LOGINSCREENUI_CLOSE"/> <int value="1342" label="LOGINSCREENUI_CLOSE"/>
<int value="1343" label="DECLARATIVENETREQUEST_GETMATCHEDRULES"/> <int value="1343" label="DECLARATIVENETREQUEST_GETMATCHEDRULES"/>
<int value="1344" label="DECLARATIVENETREQUEST_SETACTIONCOUNTASBADGETEXT"/>
</enum> </enum>
<enum name="ExtensionIconState"> <enum name="ExtensionIconState">
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