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..196f176b8edb06f013ba19438270ab97d745d792 |
--- /dev/null |
+++ b/chrome/browser/notifications/notification_content_settings_provider_android.cc |
@@ -0,0 +1,103 @@ |
+// 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" |
+ |
+#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( |
+ 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()); |
+ DCHECK(primary_url.is_valid()); |
+ 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; |
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
|
+ } else { |
+ DCHECK(!(setting == CONTENT_SETTING_ALLOW && |
+ channel_status == NotificationChannelStatus::BLOCKED)); |
+ DCHECK(!(setting == CONTENT_SETTING_BLOCK && |
+ channel_status == NotificationChannelStatus::ENABLED)); |
+ return true; |
+ } |
+ } |
+ case CONTENT_SETTING_DEFAULT: |
+ Java_NotificationSettingsBridge_deleteChannel(env, jorigin); |
+ return true; |
+ default: |
+ // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. |
+ NOTREACHED(); |
+ return true; |
+ } |
+} |
+ |
+void NotificationContentSettingsProviderAndroid::ClearAllContentSettingsRules( |
+ ContentSettingsType content_type) { |
+ // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete |
+ // all channels. |
+} |
+ |
+void NotificationContentSettingsProviderAndroid::ShutdownOnUIThread() { |
+ RemoveAllObservers(); |
+} |