Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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<Channel> 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<Channel> channels; | |
| 67 for (jsize i = 0; i < num_channels; ++i) { | |
| 68 jobject jchannel = env->GetObjectArrayElement(raw_channels.obj(), i); | |
| 69 Channel channel = {.origin = ConvertJavaStringToUTF8( | |
| 70 Java_SiteChannel_getOrigin(env, jchannel)), | |
| 71 .status = static_cast<NotificationChannelStatus>( | |
| 72 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).
| |
| 73 channels.push_back(channel); | |
| 74 } | |
| 75 return channels; | |
| 76 } | |
| 59 }; | 77 }; |
| 60 | 78 |
| 79 class ListIterator : public content_settings::RuleIterator { | |
| 80 public: | |
| 81 explicit ListIterator(const std::list<content_settings::Rule>& rules) | |
| 82 : rules_(rules) {} | |
| 83 | |
| 84 ~ListIterator() override {} | |
| 85 | |
| 86 bool HasNext() const override { return !rules_.empty(); } | |
| 87 | |
| 88 content_settings::Rule Next() override { | |
| 89 // |front()| returns a reference but we're going to discard the object | |
| 90 // referred to; force copying here. | |
| 91 content_settings::Rule rule = rules_.front(); | |
| 92 rules_.pop_front(); | |
| 93 return rule; | |
| 94 } | |
| 95 | |
| 96 private: | |
| 97 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.
| |
| 98 }; | |
| 99 | |
| 100 ContentSetting ChannelStatusToContentSetting(NotificationChannelStatus status) { | |
| 101 switch (status) { | |
| 102 case NotificationChannelStatus::ENABLED: | |
| 103 return CONTENT_SETTING_ALLOW; | |
| 104 case NotificationChannelStatus::BLOCKED: | |
| 105 return CONTENT_SETTING_BLOCK; | |
| 106 case NotificationChannelStatus::UNAVAILABLE: | |
| 107 return CONTENT_SETTING_DEFAULT; | |
| 108 } | |
| 109 NOTREACHED(); | |
| 110 return CONTENT_SETTING_DEFAULT; | |
| 111 } | |
| 112 | |
| 61 } // anonymous namespace | 113 } // anonymous namespace |
| 62 | 114 |
| 63 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() | 115 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() |
| 64 : NotificationChannelsProviderAndroid( | 116 : NotificationChannelsProviderAndroid( |
| 65 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} | 117 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} |
| 66 | 118 |
| 67 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( | 119 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( |
| 68 std::unique_ptr<NotificationChannelsBridge> bridge) | 120 std::unique_ptr<NotificationChannelsBridge> bridge) |
| 69 : bridge_(std::move(bridge)), | 121 : bridge_(std::move(bridge)), |
| 70 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} | 122 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} |
| 71 | 123 |
| 72 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = | 124 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = |
| 73 default; | 125 default; |
| 74 | 126 |
| 75 std::unique_ptr<content_settings::RuleIterator> | 127 std::unique_ptr<content_settings::RuleIterator> |
| 76 NotificationChannelsProviderAndroid::GetRuleIterator( | 128 NotificationChannelsProviderAndroid::GetRuleIterator( |
| 77 ContentSettingsType content_type, | 129 ContentSettingsType content_type, |
| 78 const content_settings::ResourceIdentifier& resource_identifier, | 130 const content_settings::ResourceIdentifier& resource_identifier, |
| 79 bool incognito) const { | 131 bool incognito) const { |
| 80 // TODO(crbug.com/700377) return rule iterator over all channels | 132 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || incognito || |
| 81 return nullptr; | 133 !should_use_channels_) { |
| 134 return nullptr; | |
| 135 } | |
| 136 std::vector<Channel> channels = bridge_->GetChannels(); | |
| 137 if (channels.empty()) { | |
| 138 return nullptr; | |
| 139 } | |
| 140 std::list<content_settings::Rule> rules; | |
| 141 for (const auto& channel : channels) { | |
| 142 DCHECK(channel.status != NotificationChannelStatus::UNAVAILABLE); | |
|
Peter Beverloo
2017/06/02 15:14:26
nit: DCHECK_NE
awdf
2017/06/02 16:59:48
Done.
| |
| 143 rules.push_back(content_settings::Rule( | |
| 144 ContentSettingsPattern::FromString(channel.origin), | |
| 145 ContentSettingsPattern(), | |
| 146 new base::Value(ChannelStatusToContentSetting(channel.status)))); | |
| 147 } | |
| 148 return std::unique_ptr<content_settings::RuleIterator>( | |
| 149 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.
| |
| 82 } | 150 } |
| 83 | 151 |
| 84 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( | 152 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( |
| 85 const ContentSettingsPattern& primary_pattern, | 153 const ContentSettingsPattern& primary_pattern, |
| 86 const ContentSettingsPattern& secondary_pattern, | 154 const ContentSettingsPattern& secondary_pattern, |
| 87 ContentSettingsType content_type, | 155 ContentSettingsType content_type, |
| 88 const content_settings::ResourceIdentifier& resource_identifier, | 156 const content_settings::ResourceIdentifier& resource_identifier, |
| 89 base::Value* value) { | 157 base::Value* value) { |
| 90 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || | 158 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
| 91 !should_use_channels_) { | 159 !should_use_channels_) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 123 | 191 |
| 124 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( | 192 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( |
| 125 ContentSettingsType content_type) { | 193 ContentSettingsType content_type) { |
| 126 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete | 194 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete |
| 127 // all channels. | 195 // all channels. |
| 128 } | 196 } |
| 129 | 197 |
| 130 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { | 198 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { |
| 131 RemoveAllObservers(); | 199 RemoveAllObservers(); |
| 132 } | 200 } |
| OLD | NEW |