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

Side by Side Diff: chrome/browser/notifications/notification_channels_provider_android.cc

Issue 2922473003: [Android] Implement GetRuleIterator for channels provider (Closed)
Patch Set: Replying to comments Created 3 years, 6 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/notifications/notification_channels_provider_android.h" 5 #include "chrome/browser/notifications/notification_channels_provider_android.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "components/content_settings/core/browser/content_settings_details.h" 13 #include "components/content_settings/core/browser/content_settings_details.h"
14 #include "components/content_settings/core/browser/content_settings_rule.h"
14 #include "components/content_settings/core/browser/content_settings_utils.h" 15 #include "components/content_settings/core/browser/content_settings_utils.h"
15 #include "components/content_settings/core/browser/host_content_settings_map.h" 16 #include "components/content_settings/core/browser/host_content_settings_map.h"
16 #include "components/content_settings/core/common/content_settings.h" 17 #include "components/content_settings/core/common/content_settings.h"
17 #include "components/content_settings/core/common/content_settings_pattern.h" 18 #include "components/content_settings/core/common/content_settings_pattern.h"
18 #include "jni/NotificationSettingsBridge_jni.h" 19 #include "jni/NotificationSettingsBridge_jni.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 #include "url/origin.h" 21 #include "url/origin.h"
21 #include "url/url_constants.h" 22 #include "url/url_constants.h"
22 23
23 using base::android::AttachCurrentThread; 24 using base::android::AttachCurrentThread;
(...skipping 25 matching lines...) Expand all
49 return static_cast<NotificationChannelStatus>( 50 return static_cast<NotificationChannelStatus>(
50 Java_NotificationSettingsBridge_getChannelStatus( 51 Java_NotificationSettingsBridge_getChannelStatus(
51 env, ConvertUTF8ToJavaString(env, origin))); 52 env, ConvertUTF8ToJavaString(env, origin)));
52 } 53 }
53 54
54 void DeleteChannel(const std::string& origin) override { 55 void DeleteChannel(const std::string& origin) override {
55 JNIEnv* env = AttachCurrentThread(); 56 JNIEnv* env = AttachCurrentThread();
56 Java_NotificationSettingsBridge_deleteChannel( 57 Java_NotificationSettingsBridge_deleteChannel(
57 env, ConvertUTF8ToJavaString(env, origin)); 58 env, ConvertUTF8ToJavaString(env, origin));
58 } 59 }
60
61 std::vector<NotificationChannel> GetChannels() override {
62 JNIEnv* env = AttachCurrentThread();
63 ScopedJavaLocalRef<jobjectArray> raw_channels =
64 Java_NotificationSettingsBridge_getSiteChannels(env);
65 jsize num_channels = env->GetArrayLength(raw_channels.obj());
66 std::vector<NotificationChannel> channels;
67 for (jsize i = 0; i < num_channels; ++i) {
68 jobject jchannel = env->GetObjectArrayElement(raw_channels.obj(), i);
69 channels.emplace_back(
70 ConvertJavaStringToUTF8(Java_SiteChannel_getOrigin(env, jchannel)),
71 static_cast<NotificationChannelStatus>(
72 Java_SiteChannel_getStatus(env, jchannel)));
73 }
74 return channels;
75 }
76 };
77
78 ContentSetting ChannelStatusToContentSetting(NotificationChannelStatus status) {
79 switch (status) {
80 case NotificationChannelStatus::ENABLED:
81 return CONTENT_SETTING_ALLOW;
82 case NotificationChannelStatus::BLOCKED:
83 return CONTENT_SETTING_BLOCK;
84 case NotificationChannelStatus::UNAVAILABLE:
85 return CONTENT_SETTING_DEFAULT;
raymes 2017/06/04 23:51:21 Arguably UNAVAILABLE doesn't really map to a conte
awdf 2017/06/05 16:29:26 Done.
86 }
87 NOTREACHED();
88 return CONTENT_SETTING_DEFAULT;
89 }
90
91 class ChannelsRuleIterator : public content_settings::RuleIterator {
92 public:
93 explicit ChannelsRuleIterator(std::vector<NotificationChannel> channels)
94 : channels_(std::move(channels)), index_(0) {}
95
96 ~ChannelsRuleIterator() override {}
Peter Beverloo 2017/06/05 12:40:24 micro nit: {} -> = default;
97
98 bool HasNext() const override { return index_ < channels_.size(); }
99
100 content_settings::Rule Next() override {
101 DCHECK(HasNext());
102 DCHECK_NE(channels_[index_].status_,
103 NotificationChannelStatus::UNAVAILABLE);
104 content_settings::Rule rule = content_settings::Rule(
105 ContentSettingsPattern::FromString(channels_[index_].origin_),
106 ContentSettingsPattern(),
raymes 2017/06/04 23:51:21 I think returning ContentSettingsPattern::Wildcard
awdf 2017/06/05 16:29:26 Done.
107 new base::Value(
108 ChannelStatusToContentSetting(channels_[index_].status_)));
109 index_++;
110 return rule;
111 }
112
113 private:
114 std::vector<NotificationChannel> channels_;
115 size_t index_;
59 }; 116 };
Peter Beverloo 2017/06/05 12:40:24 nit: DISALLOW_COPY_AND_ASSIGN
awdf 2017/06/05 16:29:26 Done.
60 117
61 } // anonymous namespace 118 } // anonymous namespace
62 119
63 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() 120 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid()
64 : NotificationChannelsProviderAndroid( 121 : NotificationChannelsProviderAndroid(
65 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} 122 base::MakeUnique<NotificationChannelsBridgeImpl>()) {}
66 123
67 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( 124 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid(
68 std::unique_ptr<NotificationChannelsBridge> bridge) 125 std::unique_ptr<NotificationChannelsBridge> bridge)
69 : bridge_(std::move(bridge)), 126 : bridge_(std::move(bridge)),
70 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} 127 should_use_channels_(bridge_->ShouldUseChannelSettings()) {}
71 128
72 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = 129 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() =
73 default; 130 default;
74 131
75 std::unique_ptr<content_settings::RuleIterator> 132 std::unique_ptr<content_settings::RuleIterator>
76 NotificationChannelsProviderAndroid::GetRuleIterator( 133 NotificationChannelsProviderAndroid::GetRuleIterator(
77 ContentSettingsType content_type, 134 ContentSettingsType content_type,
78 const content_settings::ResourceIdentifier& resource_identifier, 135 const content_settings::ResourceIdentifier& resource_identifier,
79 bool incognito) const { 136 bool incognito) const {
80 // TODO(crbug.com/700377) return rule iterator over all channels 137 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || incognito ||
Peter Beverloo 2017/06/05 12:40:24 nit: please document what this means We don't ena
awdf 2017/06/05 16:29:26 Right. But I have two questions: 1. Since this is
Peter Beverloo 2017/06/05 17:12:50 To be frank: I don't know whether we'd inherit the
awdf 2017/06/05 17:33:36 Re:
raymes 2017/06/06 01:39:04 If incognito is true, this should just return sett
awdf 2017/06/08 15:53:46 Thanks. Added a TODO for now, will discuss with Pe
81 return nullptr; 138 !should_use_channels_) {
139 return nullptr;
140 }
141 std::vector<NotificationChannel> channels = bridge_->GetChannels();
142 return channels.empty()
143 ? nullptr
144 : base::MakeUnique<ChannelsRuleIterator>(std::move(channels));
82 } 145 }
83 146
84 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( 147 bool NotificationChannelsProviderAndroid::SetWebsiteSetting(
85 const ContentSettingsPattern& primary_pattern, 148 const ContentSettingsPattern& primary_pattern,
86 const ContentSettingsPattern& secondary_pattern, 149 const ContentSettingsPattern& secondary_pattern,
87 ContentSettingsType content_type, 150 ContentSettingsType content_type,
88 const content_settings::ResourceIdentifier& resource_identifier, 151 const content_settings::ResourceIdentifier& resource_identifier,
89 base::Value* value) { 152 base::Value* value) {
90 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || 153 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS ||
91 !should_use_channels_) { 154 !should_use_channels_) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 186
124 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( 187 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules(
125 ContentSettingsType content_type) { 188 ContentSettingsType content_type) {
126 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete 189 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete
127 // all channels. 190 // all channels.
128 } 191 }
129 192
130 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { 193 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() {
131 RemoveAllObservers(); 194 RemoveAllObservers();
132 } 195 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698