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