Index: chrome/browser/notifications/android_settings_provider.cc |
diff --git a/chrome/browser/notifications/android_settings_provider.cc b/chrome/browser/notifications/android_settings_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f20d08f1ee429271664c85e16157548748b3fe93 |
--- /dev/null |
+++ b/chrome/browser/notifications/android_settings_provider.cc |
@@ -0,0 +1,97 @@ |
+// 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/android_settings_provider.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 |
+ |
+namespace content_settings { |
+ |
+AndroidSettingsProvider::AndroidSettingsProvider() |
+ : should_use_channels_( |
+ Java_NotificationSettingsBridge_shouldUseChannelSettings( |
+ AttachCurrentThread())) {} |
+ |
+AndroidSettingsProvider::~AndroidSettingsProvider() {} |
Peter Beverloo
2017/05/15 15:10:08
micro nit: {} -> = default;
awdf
2017/05/16 15:17:35
Done.
|
+ |
+std::unique_ptr<RuleIterator> AndroidSettingsProvider::GetRuleIterator( |
+ ContentSettingsType content_type, |
+ const ResourceIdentifier& resource_identifier, |
+ bool incognito) const { |
+ // TODO(crbug.com/700377) return rule iterator over all channels |
+ return nullptr; |
+} |
+ |
+bool AndroidSettingsProvider::SetWebsiteSetting( |
+ const ContentSettingsPattern& primary_pattern, |
+ const ContentSettingsPattern& secondary_pattern, |
+ ContentSettingsType content_type, |
+ const ResourceIdentifier& resource_identifier, |
+ base::Value* value) { |
+ if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || |
raymes
2017/05/16 01:08:18
Should we just DCHECK that it's only notifications
awdf
2017/05/16 15:17:35
But won't this get all types of content settings u
raymes
2017/05/17 03:13:38
Ah good point, you're right. Sorry for the misguid
|
+ !should_use_channels_) |
+ return false; |
Peter Beverloo
2017/05/15 15:10:08
micro nit: {} (multi-line statement)
awdf
2017/05/16 15:17:35
Done.
|
+ GURL primary_url = GURL(primary_pattern.ToString()); |
+ if (!primary_url.is_valid()) |
Peter Beverloo
2017/05/15 15:10:08
This warrants a comment
raymes
2017/05/16 01:08:18
Would this be better as DCHECK too?
awdf
2017/05/16 15:17:35
Done.
awdf
2017/05/16 15:17:35
hmmm good question.
I agree it might be useful t
raymes
2017/05/17 03:13:38
I guess my thought is that if there are callers pa
awdf
2017/05/17 17:14:40
Okay I have added the DCHECK here and in HCSM on A
|
+ return false; |
+ JNIEnv* env = AttachCurrentThread(); |
+ const std::string origin = primary_url.spec(); |
+ ScopedJavaLocalRef<jstring> jorigin = ConvertUTF8ToJavaString(env, origin); |
+ ContentSetting setting = 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; |
Peter Beverloo
2017/05/15 15:10:08
When would creating a channel fail?
awdf
2017/05/16 15:17:35
Well right now it does because I haven't implement
|
+ } |
raymes
2017/05/16 01:08:18
What if there was already a channel created but th
awdf
2017/05/16 15:17:35
Done.
|
+ return true; |
+ } |
+ case CONTENT_SETTING_ASK: |
raymes
2017/05/16 01:08:18
This should never be passed in so I think we can r
awdf
2017/05/16 15:17:35
Done.
|
+ case CONTENT_SETTING_DEFAULT: |
+ Java_NotificationSettingsBridge_deleteChannel(env, jorigin); |
+ return false; |
+ default: |
+ return false; |
raymes
2017/05/16 01:08:17
I'd suggest DCHECKing here too
awdf
2017/05/16 15:17:35
again I wonder about dchecking this earlier on, if
|
+ } |
+} |
+ |
+void AndroidSettingsProvider::ClearAllContentSettingsRules( |
+ ContentSettingsType content_type) { |
+ // TODO(crbug.com/700377) delete all channels |
Peter Beverloo
2017/05/15 15:10:08
nit: if |content_type| == NOTIFICATIONS
raymes
2017/05/16 01:08:17
DCHECK here too?
awdf
2017/05/16 15:17:35
again, I don't think I can?
awdf
2017/05/16 15:17:35
Done.
|
+} |
+ |
+void AndroidSettingsProvider::ShutdownOnUIThread() { |
+ RemoveAllObservers(); |
+} |
+ |
+} // namespace content_settings |