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_channels_provider_android.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/origin.h" |
| 21 #include "url/url_constants.h" |
| 22 |
| 23 using base::android::AttachCurrentThread; |
| 24 using base::android::ConvertUTF8ToJavaString; |
| 25 using base::android::ScopedJavaLocalRef; |
| 26 |
| 27 namespace { |
| 28 |
| 29 class NotificationChannelsBridgeImpl |
| 30 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { |
| 31 public: |
| 32 NotificationChannelsBridgeImpl() = default; |
| 33 ~NotificationChannelsBridgeImpl() override = default; |
| 34 |
| 35 bool ShouldUseChannelSettings() override { |
| 36 return Java_NotificationSettingsBridge_shouldUseChannelSettings( |
| 37 AttachCurrentThread()); |
| 38 } |
| 39 |
| 40 void CreateChannel(const std::string& origin, bool enabled) { |
| 41 JNIEnv* env = AttachCurrentThread(); |
| 42 Java_NotificationSettingsBridge_createChannel( |
| 43 env, ConvertUTF8ToJavaString(env, origin), enabled); |
| 44 } |
| 45 |
| 46 NotificationChannelStatus GetChannelStatus(const std::string& origin) { |
| 47 JNIEnv* env = AttachCurrentThread(); |
| 48 return static_cast<NotificationChannelStatus>( |
| 49 Java_NotificationSettingsBridge_getChannelStatus( |
| 50 env, ConvertUTF8ToJavaString(env, origin))); |
| 51 } |
| 52 |
| 53 void DeleteChannel(const std::string& origin) { |
| 54 JNIEnv* env = AttachCurrentThread(); |
| 55 Java_NotificationSettingsBridge_deleteChannel( |
| 56 env, ConvertUTF8ToJavaString(env, origin)); |
| 57 } |
| 58 }; |
| 59 |
| 60 } // anonymous namespace |
| 61 |
| 62 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() |
| 63 : NotificationChannelsProviderAndroid( |
| 64 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} |
| 65 |
| 66 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( |
| 67 std::unique_ptr<NotificationChannelsBridge> bridge) |
| 68 : bridge_(std::move(bridge)), |
| 69 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} |
| 70 |
| 71 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = |
| 72 default; |
| 73 |
| 74 std::unique_ptr<content_settings::RuleIterator> |
| 75 NotificationChannelsProviderAndroid::GetRuleIterator( |
| 76 ContentSettingsType content_type, |
| 77 const content_settings::ResourceIdentifier& resource_identifier, |
| 78 bool incognito) const { |
| 79 // TODO(crbug.com/700377) return rule iterator over all channels |
| 80 return nullptr; |
| 81 } |
| 82 |
| 83 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( |
| 84 const ContentSettingsPattern& primary_pattern, |
| 85 const ContentSettingsPattern& secondary_pattern, |
| 86 ContentSettingsType content_type, |
| 87 const content_settings::ResourceIdentifier& resource_identifier, |
| 88 base::Value* value) { |
| 89 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
| 90 !should_use_channels_) { |
| 91 return false; |
| 92 } |
| 93 GURL primary_url = GURL(primary_pattern.ToString()); |
| 94 DCHECK(!url::Origin(GURL(primary_pattern.ToString())).unique()); |
| 95 const std::string origin = primary_url.spec(); |
| 96 ContentSetting setting = content_settings::ValueToContentSetting(value); |
| 97 switch (setting) { |
| 98 case CONTENT_SETTING_ALLOW: |
| 99 case CONTENT_SETTING_BLOCK: { |
| 100 auto channel_status = bridge_->GetChannelStatus(origin); |
| 101 if (channel_status == NotificationChannelStatus::UNAVAILABLE) { |
| 102 bridge_->CreateChannel(origin, |
| 103 setting == CONTENT_SETTING_ALLOW /* enabled */); |
| 104 } else { |
| 105 DCHECK(!(setting == CONTENT_SETTING_ALLOW && |
| 106 channel_status == NotificationChannelStatus::BLOCKED)); |
| 107 DCHECK(!(setting == CONTENT_SETTING_BLOCK && |
| 108 channel_status == NotificationChannelStatus::ENABLED)); |
| 109 } |
| 110 break; |
| 111 } |
| 112 case CONTENT_SETTING_DEFAULT: |
| 113 bridge_->DeleteChannel(origin); |
| 114 break; |
| 115 default: |
| 116 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. |
| 117 NOTREACHED(); |
| 118 break; |
| 119 } |
| 120 return true; |
| 121 } |
| 122 |
| 123 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( |
| 124 ContentSettingsType content_type) { |
| 125 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete |
| 126 // all channels. |
| 127 } |
| 128 |
| 129 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { |
| 130 RemoveAllObservers(); |
| 131 } |
OLD | NEW |