Chromium Code Reviews| Index: chrome/browser/notifications/notification_content_settings_provider_android.cc |
| diff --git a/chrome/browser/notifications/notification_content_settings_provider_android.cc b/chrome/browser/notifications/notification_content_settings_provider_android.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6d89be83b86df9e68031b5604815f7f099cc9569 |
| --- /dev/null |
| +++ b/chrome/browser/notifications/notification_content_settings_provider_android.cc |
| @@ -0,0 +1,108 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/notifications/notification_content_settings_provider_android.h" |
|
raymes
2017/05/17 03:13:38
Renaming is really a pain so I hate to suggest thi
awdf
2017/05/17 17:14:41
I was considering changing it to notification_chan
raymes
2017/05/18 00:39:37
So a few quick notes:
1) Keeping the _android suff
|
| + |
| +#include "base/android/jni_android.h" |
| +#include "base/android/jni_string.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string_util.h" |
| +#include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "components/content_settings/core/browser/content_settings_details.h" |
| +#include "components/content_settings/core/browser/content_settings_utils.h" |
| +#include "components/content_settings/core/browser/host_content_settings_map.h" |
| +#include "components/content_settings/core/common/content_settings.h" |
| +#include "components/content_settings/core/common/content_settings_pattern.h" |
| +#include "jni/NotificationSettingsBridge_jni.h" |
| +#include "url/gurl.h" |
| +#include "url/url_constants.h" |
| + |
| +using base::android::AttachCurrentThread; |
| +using base::android::ConvertUTF8ToJavaString; |
| +using base::android::ScopedJavaLocalRef; |
| + |
| +namespace { |
| + |
| +// A Java counterpart will be generated for this enum. |
| +// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications |
| +enum NotificationChannelStatus { ENABLED, BLOCKED, UNAVAILABLE }; |
| + |
| +} // anonymous namespace |
| + |
| +NotificationContentSettingsProviderAndroid:: |
| + NotificationContentSettingsProviderAndroid() |
| + : should_use_channels_( |
| + Java_NotificationSettingsBridge_shouldUseChannelSettings( |
| + AttachCurrentThread())) {} |
| + |
| +NotificationContentSettingsProviderAndroid:: |
| + ~NotificationContentSettingsProviderAndroid() = default; |
| + |
| +std::unique_ptr<content_settings::RuleIterator> |
| +NotificationContentSettingsProviderAndroid::GetRuleIterator( |
|
awdf
2017/05/17 17:14:41
@raymes I'm considering how to implement this and
raymes
2017/05/18 00:39:37
Overall that sounds good. Let's land this CL as it
awdf
2017/05/19 17:59:57
OK. Happy to land this one with just the creating-
|
| + ContentSettingsType content_type, |
| + const content_settings::ResourceIdentifier& resource_identifier, |
| + bool incognito) const { |
| + // TODO(crbug.com/700377) return rule iterator over all channels |
| + return nullptr; |
| +} |
| + |
| +bool NotificationContentSettingsProviderAndroid::SetWebsiteSetting( |
| + const ContentSettingsPattern& primary_pattern, |
| + const ContentSettingsPattern& secondary_pattern, |
| + ContentSettingsType content_type, |
| + const content_settings::ResourceIdentifier& resource_identifier, |
| + base::Value* value) { |
| + if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
| + !should_use_channels_) { |
| + return false; |
| + } |
| + GURL primary_url = GURL(primary_pattern.ToString()); |
| + // Ignore patterns that are not valid urls - we only want to expose |
| + // notification channels for particular origins, not wildcard rules, since |
| + // users should not be able to grant or deny wildcard notification permissions |
| + // (although these may be set by policy). |
| + if (!primary_url.is_valid()) |
| + return false; |
| + JNIEnv* env = AttachCurrentThread(); |
| + const std::string origin = primary_url.spec(); |
| + ScopedJavaLocalRef<jstring> jorigin = ConvertUTF8ToJavaString(env, origin); |
| + ContentSetting setting = content_settings::ValueToContentSetting(value); |
| + switch (setting) { |
| + case CONTENT_SETTING_ALLOW: |
| + case CONTENT_SETTING_BLOCK: { |
| + auto channel_status = static_cast<NotificationChannelStatus>( |
| + Java_NotificationSettingsBridge_getChannelStatus(env, jorigin)); |
| + if (channel_status == NotificationChannelStatus::UNAVAILABLE) { |
| + bool result = Java_NotificationSettingsBridge_createChannel( |
| + env, jorigin, setting == CONTENT_SETTING_ALLOW /* enabled */); |
| + return result; |
| + } else { |
| + DCHECK(!(setting == CONTENT_SETTING_ALLOW && |
| + channel_status == NotificationChannelStatus::BLOCKED)); |
| + DCHECK(!(setting == CONTENT_SETTING_BLOCK && |
| + channel_status == NotificationChannelStatus::ENABLED)); |
|
Peter Beverloo
2017/05/16 16:15:34
These frighten me a bit. Until the mitigations are
raymes
2017/05/17 03:13:38
Won't these help to find the callsites that need t
awdf
2017/05/17 17:14:41
Right now it wouldn't crash because the 'else' is
|
| + return true; |
| + } |
| + } |
| + case CONTENT_SETTING_DEFAULT: |
| + Java_NotificationSettingsBridge_deleteChannel(env, jorigin); |
| + return false; |
|
Peter Beverloo
2017/05/16 16:15:34
Is returning FALSE here the right thing to do? We
raymes
2017/05/17 03:13:38
Good catch, we probably should return true.
awdf
2017/05/17 17:14:41
I think you're right and we should be returning tr
|
| + default: |
| + // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. |
| + NOTREACHED(); |
| + return false; |
|
raymes
2017/05/17 03:13:38
I think it's safer to return true here (and in all
awdf
2017/05/17 17:14:41
shouldn't really make a difference since this is a
|
| + } |
| +} |
| + |
| +void NotificationContentSettingsProviderAndroid::ClearAllContentSettingsRules( |
| + ContentSettingsType content_type) { |
| + // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete |
| + // all channels. |
| +} |
| + |
| +void NotificationContentSettingsProviderAndroid::ShutdownOnUIThread() { |
| + RemoveAllObservers(); |
| +} |