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) override { |
| 41 JNIEnv* env = AttachCurrentThread(); |
| 42 Java_NotificationSettingsBridge_createChannel( |
| 43 env, ConvertUTF8ToJavaString(env, origin), enabled); |
| 44 } |
| 45 |
| 46 NotificationChannelStatus GetChannelStatus( |
| 47 const std::string& origin) override { |
| 48 JNIEnv* env = AttachCurrentThread(); |
| 49 return static_cast<NotificationChannelStatus>( |
| 50 Java_NotificationSettingsBridge_getChannelStatus( |
| 51 env, ConvertUTF8ToJavaString(env, origin))); |
| 52 } |
| 53 |
| 54 void DeleteChannel(const std::string& origin) override { |
| 55 JNIEnv* env = AttachCurrentThread(); |
| 56 Java_NotificationSettingsBridge_deleteChannel( |
| 57 env, ConvertUTF8ToJavaString(env, origin)); |
| 58 } |
| 59 }; |
| 60 |
| 61 } // anonymous namespace |
| 62 |
| 63 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() |
| 64 : NotificationChannelsProviderAndroid( |
| 65 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} |
| 66 |
| 67 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( |
| 68 std::unique_ptr<NotificationChannelsBridge> bridge) |
| 69 : bridge_(std::move(bridge)), |
| 70 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} |
| 71 |
| 72 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = |
| 73 default; |
| 74 |
| 75 std::unique_ptr<content_settings::RuleIterator> |
| 76 NotificationChannelsProviderAndroid::GetRuleIterator( |
| 77 ContentSettingsType content_type, |
| 78 const content_settings::ResourceIdentifier& resource_identifier, |
| 79 bool incognito) const { |
| 80 // TODO(crbug.com/700377) return rule iterator over all channels |
| 81 return nullptr; |
| 82 } |
| 83 |
| 84 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( |
| 85 const ContentSettingsPattern& primary_pattern, |
| 86 const ContentSettingsPattern& secondary_pattern, |
| 87 ContentSettingsType content_type, |
| 88 const content_settings::ResourceIdentifier& resource_identifier, |
| 89 base::Value* value) { |
| 90 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
| 91 !should_use_channels_) { |
| 92 return false; |
| 93 } |
| 94 // This provider only handles settings for specific origins. |
| 95 if (primary_pattern == ContentSettingsPattern::Wildcard() && |
| 96 secondary_pattern == ContentSettingsPattern::Wildcard() && |
| 97 resource_identifier.empty()) { |
| 98 return false; |
| 99 } |
| 100 url::Origin origin = url::Origin(GURL(primary_pattern.ToString())); |
| 101 DCHECK(!origin.unique()); |
| 102 const std::string origin_string = origin.Serialize(); |
| 103 ContentSetting setting = content_settings::ValueToContentSetting(value); |
| 104 switch (setting) { |
| 105 case CONTENT_SETTING_ALLOW: |
| 106 CreateChannelIfRequired(origin_string, |
| 107 NotificationChannelStatus::ENABLED); |
| 108 break; |
| 109 case CONTENT_SETTING_BLOCK: |
| 110 CreateChannelIfRequired(origin_string, |
| 111 NotificationChannelStatus::BLOCKED); |
| 112 break; |
| 113 case CONTENT_SETTING_DEFAULT: |
| 114 bridge_->DeleteChannel(origin_string); |
| 115 break; |
| 116 default: |
| 117 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. |
| 118 NOTREACHED(); |
| 119 break; |
| 120 } |
| 121 return true; |
| 122 } |
| 123 |
| 124 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( |
| 125 ContentSettingsType content_type) { |
| 126 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete |
| 127 // all channels. |
| 128 } |
| 129 |
| 130 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { |
| 131 RemoveAllObservers(); |
| 132 } |
| 133 |
| 134 void NotificationChannelsProviderAndroid::CreateChannelIfRequired( |
| 135 const std::string& origin_string, |
| 136 NotificationChannelStatus new_channel_status) { |
| 137 auto old_channel_status = bridge_->GetChannelStatus(origin_string); |
| 138 if (old_channel_status == NotificationChannelStatus::UNAVAILABLE) { |
| 139 bridge_->CreateChannel( |
| 140 origin_string, |
| 141 new_channel_status == NotificationChannelStatus::ENABLED); |
| 142 } else { |
| 143 // TODO(awdf): Maybe remove this DCHECK - channel status could change any |
| 144 // time so this may be vulnerable to a race condition. |
| 145 DCHECK(old_channel_status == new_channel_status); |
| 146 } |
| 147 } |
OLD | NEW |