Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/notifications/notification_content_settings_provider_an droid.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/strings/string_util.h" | |
| 11 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | |
| 12 #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_utils.h" | |
| 15 #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_pattern.h" | |
| 18 #include "jni/NotificationSettingsBridge_jni.h" | |
| 19 #include "url/gurl.h" | |
| 20 #include "url/url_constants.h" | |
| 21 | |
| 22 using base::android::AttachCurrentThread; | |
| 23 using base::android::ConvertUTF8ToJavaString; | |
| 24 using base::android::ScopedJavaLocalRef; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // A Java counterpart will be generated for this enum. | |
| 29 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications | |
| 30 enum NotificationChannelStatus { ENABLED, BLOCKED, UNAVAILABLE }; | |
| 31 | |
| 32 } // anonymous namespace | |
| 33 | |
| 34 NotificationContentSettingsProviderAndroid:: | |
| 35 NotificationContentSettingsProviderAndroid() | |
| 36 : should_use_channels_( | |
| 37 Java_NotificationSettingsBridge_shouldUseChannelSettings( | |
| 38 AttachCurrentThread())) {} | |
| 39 | |
| 40 NotificationContentSettingsProviderAndroid:: | |
| 41 ~NotificationContentSettingsProviderAndroid() = default; | |
| 42 | |
| 43 std::unique_ptr<content_settings::RuleIterator> | |
| 44 NotificationContentSettingsProviderAndroid::GetRuleIterator( | |
| 45 ContentSettingsType content_type, | |
| 46 const content_settings::ResourceIdentifier& resource_identifier, | |
| 47 bool incognito) const { | |
| 48 // TODO(crbug.com/700377) return rule iterator over all channels | |
| 49 return nullptr; | |
| 50 } | |
| 51 | |
| 52 bool NotificationContentSettingsProviderAndroid::SetWebsiteSetting( | |
| 53 const ContentSettingsPattern& primary_pattern, | |
| 54 const ContentSettingsPattern& secondary_pattern, | |
| 55 ContentSettingsType content_type, | |
| 56 const content_settings::ResourceIdentifier& resource_identifier, | |
| 57 base::Value* value) { | |
| 58 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || | |
| 59 !should_use_channels_) { | |
| 60 return false; | |
| 61 } | |
| 62 GURL primary_url = GURL(primary_pattern.ToString()); | |
| 63 DCHECK(primary_url.is_valid()); | |
| 64 JNIEnv* env = AttachCurrentThread(); | |
| 65 const std::string origin = primary_url.spec(); | |
| 66 ScopedJavaLocalRef<jstring> jorigin = ConvertUTF8ToJavaString(env, origin); | |
| 67 ContentSetting setting = content_settings::ValueToContentSetting(value); | |
| 68 switch (setting) { | |
| 69 case CONTENT_SETTING_ALLOW: | |
| 70 case CONTENT_SETTING_BLOCK: { | |
| 71 auto channel_status = static_cast<NotificationChannelStatus>( | |
| 72 Java_NotificationSettingsBridge_getChannelStatus(env, jorigin)); | |
| 73 if (channel_status == NotificationChannelStatus::UNAVAILABLE) { | |
| 74 bool result = Java_NotificationSettingsBridge_createChannel( | |
| 75 env, jorigin, setting == CONTENT_SETTING_ALLOW /* enabled */); | |
| 76 return result; | |
|
raymes
2017/05/18 00:39:37
nit: probably we should return true here regardles
awdf
2017/05/19 17:59:57
Turns out we can't create channels as blocked, at
raymes
2017/05/22 00:37:41
That's really unfortunate :( Personally I think we
| |
| 77 } else { | |
| 78 DCHECK(!(setting == CONTENT_SETTING_ALLOW && | |
| 79 channel_status == NotificationChannelStatus::BLOCKED)); | |
| 80 DCHECK(!(setting == CONTENT_SETTING_BLOCK && | |
| 81 channel_status == NotificationChannelStatus::ENABLED)); | |
| 82 return true; | |
| 83 } | |
| 84 } | |
| 85 case CONTENT_SETTING_DEFAULT: | |
| 86 Java_NotificationSettingsBridge_deleteChannel(env, jorigin); | |
| 87 return true; | |
| 88 default: | |
| 89 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. | |
| 90 NOTREACHED(); | |
| 91 return true; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void NotificationContentSettingsProviderAndroid::ClearAllContentSettingsRules( | |
| 96 ContentSettingsType content_type) { | |
| 97 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete | |
| 98 // all channels. | |
| 99 } | |
| 100 | |
| 101 void NotificationContentSettingsProviderAndroid::ShutdownOnUIThread() { | |
| 102 RemoveAllObservers(); | |
| 103 } | |
| OLD | NEW |