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_content_settings_provider_an droid.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
| |
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/url_constants.h" | |
21 | |
22 using base::android::AttachCurrentThread; | |
23 using base::android::ConvertUTF8ToJavaString; | |
24 using base::android::ScopedJavaLocalRef; | |
25 | |
26 namespace { | |
27 | |
28 // A Java counterpart will be generated for this enum. | |
29 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser.notifications | |
30 enum NotificationChannelStatus { ENABLED, BLOCKED, UNAVAILABLE }; | |
31 | |
32 } // anonymous namespace | |
33 | |
34 NotificationContentSettingsProviderAndroid:: | |
35 NotificationContentSettingsProviderAndroid() | |
36 : should_use_channels_( | |
37 Java_NotificationSettingsBridge_shouldUseChannelSettings( | |
38 AttachCurrentThread())) {} | |
39 | |
40 NotificationContentSettingsProviderAndroid:: | |
41 ~NotificationContentSettingsProviderAndroid() = default; | |
42 | |
43 std::unique_ptr<content_settings::RuleIterator> | |
44 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-
| |
45 ContentSettingsType content_type, | |
46 const content_settings::ResourceIdentifier& resource_identifier, | |
47 bool incognito) const { | |
48 // TODO(crbug.com/700377) return rule iterator over all channels | |
49 return nullptr; | |
50 } | |
51 | |
52 bool NotificationContentSettingsProviderAndroid::SetWebsiteSetting( | |
53 const ContentSettingsPattern& primary_pattern, | |
54 const ContentSettingsPattern& secondary_pattern, | |
55 ContentSettingsType content_type, | |
56 const content_settings::ResourceIdentifier& resource_identifier, | |
57 base::Value* value) { | |
58 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || | |
59 !should_use_channels_) { | |
60 return false; | |
61 } | |
62 GURL primary_url = GURL(primary_pattern.ToString()); | |
63 // Ignore patterns that are not valid urls - we only want to expose | |
64 // notification channels for particular origins, not wildcard rules, since | |
65 // users should not be able to grant or deny wildcard notification permissions | |
66 // (although these may be set by policy). | |
67 if (!primary_url.is_valid()) | |
68 return false; | |
69 JNIEnv* env = AttachCurrentThread(); | |
70 const std::string origin = primary_url.spec(); | |
71 ScopedJavaLocalRef<jstring> jorigin = ConvertUTF8ToJavaString(env, origin); | |
72 ContentSetting setting = content_settings::ValueToContentSetting(value); | |
73 switch (setting) { | |
74 case CONTENT_SETTING_ALLOW: | |
75 case CONTENT_SETTING_BLOCK: { | |
76 auto channel_status = static_cast<NotificationChannelStatus>( | |
77 Java_NotificationSettingsBridge_getChannelStatus(env, jorigin)); | |
78 if (channel_status == NotificationChannelStatus::UNAVAILABLE) { | |
79 bool result = Java_NotificationSettingsBridge_createChannel( | |
80 env, jorigin, setting == CONTENT_SETTING_ALLOW /* enabled */); | |
81 return result; | |
82 } else { | |
83 DCHECK(!(setting == CONTENT_SETTING_ALLOW && | |
84 channel_status == NotificationChannelStatus::BLOCKED)); | |
85 DCHECK(!(setting == CONTENT_SETTING_BLOCK && | |
86 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
| |
87 return true; | |
88 } | |
89 } | |
90 case CONTENT_SETTING_DEFAULT: | |
91 Java_NotificationSettingsBridge_deleteChannel(env, jorigin); | |
92 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
| |
93 default: | |
94 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. | |
95 NOTREACHED(); | |
96 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
| |
97 } | |
98 } | |
99 | |
100 void NotificationContentSettingsProviderAndroid::ClearAllContentSettingsRules( | |
101 ContentSettingsType content_type) { | |
102 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete | |
103 // all channels. | |
104 } | |
105 | |
106 void NotificationContentSettingsProviderAndroid::ShutdownOnUIThread() { | |
107 RemoveAllObservers(); | |
108 } | |
OLD | NEW |