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/macros.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 12 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
13 #include "components/content_settings/core/browser/content_settings_details.h" | 14 #include "components/content_settings/core/browser/content_settings_details.h" |
| 15 #include "components/content_settings/core/browser/content_settings_rule.h" |
14 #include "components/content_settings/core/browser/content_settings_utils.h" | 16 #include "components/content_settings/core/browser/content_settings_utils.h" |
15 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
16 #include "components/content_settings/core/common/content_settings.h" | 18 #include "components/content_settings/core/common/content_settings.h" |
17 #include "components/content_settings/core/common/content_settings_pattern.h" | 19 #include "components/content_settings/core/common/content_settings_pattern.h" |
18 #include "jni/NotificationSettingsBridge_jni.h" | 20 #include "jni/NotificationSettingsBridge_jni.h" |
19 #include "url/gurl.h" | 21 #include "url/gurl.h" |
20 #include "url/origin.h" | 22 #include "url/origin.h" |
21 #include "url/url_constants.h" | 23 #include "url/url_constants.h" |
22 | 24 |
23 using base::android::AttachCurrentThread; | 25 using base::android::AttachCurrentThread; |
(...skipping 25 matching lines...) Expand all Loading... |
49 return static_cast<NotificationChannelStatus>( | 51 return static_cast<NotificationChannelStatus>( |
50 Java_NotificationSettingsBridge_getChannelStatus( | 52 Java_NotificationSettingsBridge_getChannelStatus( |
51 env, ConvertUTF8ToJavaString(env, origin))); | 53 env, ConvertUTF8ToJavaString(env, origin))); |
52 } | 54 } |
53 | 55 |
54 void DeleteChannel(const std::string& origin) override { | 56 void DeleteChannel(const std::string& origin) override { |
55 JNIEnv* env = AttachCurrentThread(); | 57 JNIEnv* env = AttachCurrentThread(); |
56 Java_NotificationSettingsBridge_deleteChannel( | 58 Java_NotificationSettingsBridge_deleteChannel( |
57 env, ConvertUTF8ToJavaString(env, origin)); | 59 env, ConvertUTF8ToJavaString(env, origin)); |
58 } | 60 } |
| 61 |
| 62 std::vector<NotificationChannel> GetChannels() override { |
| 63 JNIEnv* env = AttachCurrentThread(); |
| 64 ScopedJavaLocalRef<jobjectArray> raw_channels = |
| 65 Java_NotificationSettingsBridge_getSiteChannels(env); |
| 66 jsize num_channels = env->GetArrayLength(raw_channels.obj()); |
| 67 std::vector<NotificationChannel> channels; |
| 68 for (jsize i = 0; i < num_channels; ++i) { |
| 69 jobject jchannel = env->GetObjectArrayElement(raw_channels.obj(), i); |
| 70 channels.emplace_back( |
| 71 ConvertJavaStringToUTF8(Java_SiteChannel_getOrigin(env, jchannel)), |
| 72 static_cast<NotificationChannelStatus>( |
| 73 Java_SiteChannel_getStatus(env, jchannel))); |
| 74 } |
| 75 return channels; |
| 76 } |
| 77 }; |
| 78 |
| 79 ContentSetting ChannelStatusToContentSetting(NotificationChannelStatus status) { |
| 80 switch (status) { |
| 81 case NotificationChannelStatus::ENABLED: |
| 82 return CONTENT_SETTING_ALLOW; |
| 83 case NotificationChannelStatus::BLOCKED: |
| 84 return CONTENT_SETTING_BLOCK; |
| 85 case NotificationChannelStatus::UNAVAILABLE: |
| 86 NOTREACHED(); |
| 87 } |
| 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 = 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::Wildcard(), |
| 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_; |
| 116 DISALLOW_COPY_AND_ASSIGN(ChannelsRuleIterator); |
59 }; | 117 }; |
60 | 118 |
61 } // anonymous namespace | 119 } // anonymous namespace |
62 | 120 |
63 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() | 121 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() |
64 : NotificationChannelsProviderAndroid( | 122 : NotificationChannelsProviderAndroid( |
65 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} | 123 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} |
66 | 124 |
67 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( | 125 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( |
68 std::unique_ptr<NotificationChannelsBridge> bridge) | 126 std::unique_ptr<NotificationChannelsBridge> bridge) |
69 : bridge_(std::move(bridge)), | 127 : bridge_(std::move(bridge)), |
70 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} | 128 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} |
71 | 129 |
72 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = | 130 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = |
73 default; | 131 default; |
74 | 132 |
75 std::unique_ptr<content_settings::RuleIterator> | 133 std::unique_ptr<content_settings::RuleIterator> |
76 NotificationChannelsProviderAndroid::GetRuleIterator( | 134 NotificationChannelsProviderAndroid::GetRuleIterator( |
77 ContentSettingsType content_type, | 135 ContentSettingsType content_type, |
78 const content_settings::ResourceIdentifier& resource_identifier, | 136 const content_settings::ResourceIdentifier& resource_identifier, |
79 bool incognito) const { | 137 bool incognito) const { |
80 // TODO(crbug.com/700377) return rule iterator over all channels | 138 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || incognito || |
81 return nullptr; | 139 !should_use_channels_) { |
| 140 return nullptr; |
| 141 } |
| 142 std::vector<NotificationChannel> channels = bridge_->GetChannels(); |
| 143 return channels.empty() |
| 144 ? nullptr |
| 145 : base::MakeUnique<ChannelsRuleIterator>(std::move(channels)); |
82 } | 146 } |
83 | 147 |
84 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( | 148 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( |
85 const ContentSettingsPattern& primary_pattern, | 149 const ContentSettingsPattern& primary_pattern, |
86 const ContentSettingsPattern& secondary_pattern, | 150 const ContentSettingsPattern& secondary_pattern, |
87 ContentSettingsType content_type, | 151 ContentSettingsType content_type, |
88 const content_settings::ResourceIdentifier& resource_identifier, | 152 const content_settings::ResourceIdentifier& resource_identifier, |
89 base::Value* value) { | 153 base::Value* value) { |
90 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || | 154 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
91 !should_use_channels_) { | 155 !should_use_channels_) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 // all channels. | 191 // all channels. |
128 } | 192 } |
129 | 193 |
130 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { | 194 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { |
131 RemoveAllObservers(); | 195 RemoveAllObservers(); |
132 } | 196 } |
133 | 197 |
134 void NotificationChannelsProviderAndroid::CreateChannelIfRequired( | 198 void NotificationChannelsProviderAndroid::CreateChannelIfRequired( |
135 const std::string& origin_string, | 199 const std::string& origin_string, |
136 NotificationChannelStatus new_channel_status) { | 200 NotificationChannelStatus new_channel_status) { |
| 201 // TODO(awdf): Maybe check cached incognito status here to make sure |
| 202 // channels are never created in incognito mode. |
137 auto old_channel_status = bridge_->GetChannelStatus(origin_string); | 203 auto old_channel_status = bridge_->GetChannelStatus(origin_string); |
138 if (old_channel_status == NotificationChannelStatus::UNAVAILABLE) { | 204 if (old_channel_status == NotificationChannelStatus::UNAVAILABLE) { |
139 bridge_->CreateChannel( | 205 bridge_->CreateChannel( |
140 origin_string, | 206 origin_string, |
141 new_channel_status == NotificationChannelStatus::ENABLED); | 207 new_channel_status == NotificationChannelStatus::ENABLED); |
142 } else { | 208 } else { |
143 // TODO(awdf): Maybe remove this DCHECK - channel status could change any | 209 // TODO(awdf): Maybe remove this DCHECK - channel status could change any |
144 // time so this may be vulnerable to a race condition. | 210 // time so this may be vulnerable to a race condition. |
145 DCHECK(old_channel_status == new_channel_status); | 211 DCHECK(old_channel_status == new_channel_status); |
146 } | 212 } |
147 } | 213 } |
OLD | NEW |