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/url_constants.h" | |
21 | |
22 using base::android::AttachCurrentThread; | |
23 using base::android::ConvertUTF8ToJavaString; | |
24 using base::android::ScopedJavaLocalRef; | |
25 | |
26 namespace { | |
raymes
2017/05/24 00:59:50
nit: newline after this
awdf
2017/06/01 15:11:21
Done.
| |
27 class NotificationChannelsBridgeImpl | |
28 : public NotificationChannelsProviderAndroid::NotificationChannelsBridge { | |
29 public: | |
30 NotificationChannelsBridgeImpl() = default; | |
31 ~NotificationChannelsBridgeImpl() override = default; | |
32 | |
33 bool ShouldUseChannelSettings() override { | |
34 return Java_NotificationSettingsBridge_shouldUseChannelSettings( | |
35 AttachCurrentThread()); | |
36 } | |
37 | |
38 void CreateChannel(const std::string& origin, bool enabled) { | |
39 JNIEnv* env = AttachCurrentThread(); | |
40 Java_NotificationSettingsBridge_createChannel( | |
41 env, ConvertUTF8ToJavaString(env, origin), enabled); | |
42 } | |
43 | |
44 NotificationChannelStatus GetChannelStatus(const std::string& origin) { | |
45 JNIEnv* env = AttachCurrentThread(); | |
46 return static_cast<NotificationChannelStatus>( | |
47 Java_NotificationSettingsBridge_getChannelStatus( | |
48 env, ConvertUTF8ToJavaString(env, origin))); | |
49 } | |
50 | |
51 void DeleteChannel(const std::string& origin) { | |
52 JNIEnv* env = AttachCurrentThread(); | |
53 Java_NotificationSettingsBridge_deleteChannel( | |
54 env, ConvertUTF8ToJavaString(env, origin)); | |
55 } | |
56 }; | |
57 | |
58 } // anonymous namespace | |
59 | |
60 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid() | |
61 : NotificationChannelsProviderAndroid( | |
62 base::MakeUnique<NotificationChannelsBridgeImpl>()) {} | |
63 | |
64 NotificationChannelsProviderAndroid::NotificationChannelsProviderAndroid( | |
65 std::unique_ptr<NotificationChannelsBridge> bridge) | |
66 : bridge_(std::move(bridge)), | |
67 should_use_channels_(bridge_->ShouldUseChannelSettings()) {} | |
68 | |
69 NotificationChannelsProviderAndroid::~NotificationChannelsProviderAndroid() = | |
70 default; | |
71 | |
72 std::unique_ptr<content_settings::RuleIterator> | |
73 NotificationChannelsProviderAndroid::GetRuleIterator( | |
74 ContentSettingsType content_type, | |
75 const content_settings::ResourceIdentifier& resource_identifier, | |
76 bool incognito) const { | |
77 // TODO(crbug.com/700377) return rule iterator over all channels | |
78 return nullptr; | |
79 } | |
80 | |
81 bool NotificationChannelsProviderAndroid::SetWebsiteSetting( | |
82 const ContentSettingsPattern& primary_pattern, | |
83 const ContentSettingsPattern& secondary_pattern, | |
84 ContentSettingsType content_type, | |
85 const content_settings::ResourceIdentifier& resource_identifier, | |
86 base::Value* value) { | |
87 if (content_type != CONTENT_SETTINGS_TYPE_NOTIFICATIONS || | |
88 !should_use_channels_) { | |
89 return false; | |
90 } | |
91 GURL primary_url = GURL(primary_pattern.ToString()); | |
92 DCHECK(primary_url.is_valid()); | |
93 const std::string origin = primary_url.spec(); | |
raymes
2017/05/24 00:59:51
One thing I didn't consider earlier:
Some valid no
awdf
2017/06/01 15:11:21
Done.
| |
94 ContentSetting setting = content_settings::ValueToContentSetting(value); | |
95 switch (setting) { | |
96 case CONTENT_SETTING_ALLOW: | |
97 case CONTENT_SETTING_BLOCK: { | |
98 auto channel_status = bridge_->GetChannelStatus(origin); | |
99 if (channel_status == NotificationChannelStatus::UNAVAILABLE) { | |
100 bridge_->CreateChannel(origin, | |
101 setting == CONTENT_SETTING_ALLOW /* enabled */); | |
102 } else { | |
103 DCHECK(!(setting == CONTENT_SETTING_ALLOW && | |
104 channel_status == NotificationChannelStatus::BLOCKED)); | |
105 DCHECK(!(setting == CONTENT_SETTING_BLOCK && | |
106 channel_status == NotificationChannelStatus::ENABLED)); | |
107 } | |
108 break; | |
109 } | |
110 case CONTENT_SETTING_DEFAULT: | |
111 bridge_->DeleteChannel(origin); | |
112 break; | |
113 default: | |
114 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT. | |
115 NOTREACHED(); | |
116 break; | |
117 } | |
118 return true; | |
119 } | |
120 | |
121 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules( | |
122 ContentSettingsType content_type) { | |
123 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete | |
124 // all channels. | |
125 } | |
126 | |
127 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() { | |
128 RemoveAllObservers(); | |
129 } | |
OLD | NEW |