Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2694)

Unified Diff: chrome/browser/notifications/notification_channels_provider_android.cc

Issue 2922473003: [Android] Implement GetRuleIterator for channels provider (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/notifications/notification_channels_provider_android.cc
diff --git a/chrome/browser/notifications/notification_channels_provider_android.cc b/chrome/browser/notifications/notification_channels_provider_android.cc
index f865fb126b9e95ab59de07953c75cbdbf6426046..d95fee623d90456a6642cf272b1799e571e736a8 100644
--- a/chrome/browser/notifications/notification_channels_provider_android.cc
+++ b/chrome/browser/notifications/notification_channels_provider_android.cc
@@ -11,6 +11,7 @@
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/content_settings_details.h"
+#include "components/content_settings/core/browser/content_settings_rule.h"
#include "components/content_settings/core/browser/content_settings_utils.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
@@ -56,8 +57,59 @@ class NotificationChannelsBridgeImpl
Java_NotificationSettingsBridge_deleteChannel(
env, ConvertUTF8ToJavaString(env, origin));
}
+
+ std::vector<Channel> GetChannels() override {
+ JNIEnv* env = AttachCurrentThread();
+ ScopedJavaLocalRef<jobjectArray> raw_channels =
+ Java_NotificationSettingsBridge_getSiteChannels(env);
+ jsize num_channels = env->GetArrayLength(raw_channels.obj());
+ std::vector<Channel> channels;
+ for (jsize i = 0; i < num_channels; ++i) {
+ jobject jchannel = env->GetObjectArrayElement(raw_channels.obj(), i);
+ Channel channel = {.origin = ConvertJavaStringToUTF8(
+ Java_SiteChannel_getOrigin(env, jchannel)),
+ .status = static_cast<NotificationChannelStatus>(
+ Java_SiteChannel_getStatus(env, jchannel))};
Peter Beverloo 2017/06/02 15:14:26 Channel channel; channel.origin = ConvertJ.... cha
awdf 2017/06/02 16:59:48 Done (with a constructor).
+ channels.push_back(channel);
+ }
+ return channels;
+ }
};
+class ListIterator : public content_settings::RuleIterator {
+ public:
+ explicit ListIterator(const std::list<content_settings::Rule>& rules)
+ : rules_(rules) {}
+
+ ~ListIterator() override {}
+
+ bool HasNext() const override { return !rules_.empty(); }
+
+ content_settings::Rule Next() override {
+ // |front()| returns a reference but we're going to discard the object
+ // referred to; force copying here.
+ content_settings::Rule rule = rules_.front();
+ rules_.pop_front();
+ return rule;
+ }
+
+ private:
+ std::list<content_settings::Rule> rules_;
Peter Beverloo 2017/06/02 15:14:26 Since an instance of the Rule object will be retur
awdf 2017/06/02 16:59:48 Done.
+};
+
+ContentSetting ChannelStatusToContentSetting(NotificationChannelStatus status) {
+ switch (status) {
+ case NotificationChannelStatus::ENABLED:
+ return CONTENT_SETTING_ALLOW;
+ case NotificationChannelStatus::BLOCKED:
+ return CONTENT_SETTING_BLOCK;
+ case NotificationChannelStatus::UNAVAILABLE:
+ return CONTENT_SETTING_DEFAULT;
+ }
+ NOTREACHED();
+ return CONTENT_SETTING_DEFAULT;
+}
+
} // anonymous namespace
NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid()
@@ -77,8 +129,24 @@ NotificationChannelsProviderAndroid::GetRuleIterator(
ContentSettingsType content_type,
const content_settings::ResourceIdentifier& resource_identifier,
bool incognito) const {
- // TODO(crbug.com/700377) return rule iterator over all channels
- return nullptr;
+ if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || incognito ||
+ !should_use_channels_) {
+ return nullptr;
+ }
+ std::vector<Channel> channels = bridge_->GetChannels();
+ if (channels.empty()) {
+ return nullptr;
+ }
+ std::list<content_settings::Rule> rules;
+ for (const auto& channel : channels) {
+ DCHECK(channel.status != NotificationChannelStatus::UNAVAILABLE);
Peter Beverloo 2017/06/02 15:14:26 nit: DCHECK_NE
awdf 2017/06/02 16:59:48 Done.
+ rules.push_back(content_settings::Rule(
+ ContentSettingsPattern::FromString(channel.origin),
+ ContentSettingsPattern(),
+ new base::Value(ChannelStatusToContentSetting(channel.status))));
+ }
+ return std::unique_ptr<content_settings::RuleIterator>(
+ new ListIterator(rules));
Peter Beverloo 2017/06/02 15:14:26 nit (the compiler can figure out typing): return
awdf 2017/06/02 16:59:48 Done.
}
bool NotificationChannelsProviderAndroid::SetWebsiteSetting(

Powered by Google App Engine
This is Rietveld 408576698