Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: chrome/browser/notifications/notification_channels_provider_android.cc

Issue 2886433002: [Android] Adding content settings provider for notification channels (Closed)
Patch Set: Use Origin.Serialize instead of GURL.spec() Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 if (primary_pattern.MatchesAllHosts()) {
95 // This provider only handles settings for specific origins.
Peter Beverloo 2017/06/05 15:11:31 nit: I'd place the comment above the conditional a
awdf 2017/06/05 17:06:56 Done.
96 return false;
97 }
98 GURL primary_url = GURL(primary_pattern.ToString());
99 url::Origin origin = url::Origin(GURL(primary_pattern.ToString()));
100 DCHECK(!origin.unique());
101 const std::string origin_string = origin.Serialize();
102 ContentSetting setting = content_settings::ValueToContentSetting(value);
103 switch (setting) {
104 case CONTENT_SETTING_ALLOW:
105 case CONTENT_SETTING_BLOCK: {
106 auto channel_status = bridge_->GetChannelStatus(origin_string);
107 if (channel_status == NotificationChannelStatus::UNAVAILABLE) {
108 bridge_->CreateChannel(origin_string,
109 setting == CONTENT_SETTING_ALLOW /* enabled */);
110 } else {
111 DCHECK(!(setting == CONTENT_SETTING_ALLOW &&
112 channel_status == NotificationChannelStatus::BLOCKED));
113 DCHECK(!(setting == CONTENT_SETTING_BLOCK &&
114 channel_status == NotificationChannelStatus::ENABLED));
115 }
116 break;
117 }
118 case CONTENT_SETTING_DEFAULT:
119 bridge_->DeleteChannel(origin_string);
120 break;
121 default:
122 // We rely on notification settings being one of ALLOW/BLOCK/DEFAULT.
123 NOTREACHED();
124 break;
125 }
126 return true;
127 }
128
129 void NotificationChannelsProviderAndroid::ClearAllContentSettingsRules(
130 ContentSettingsType content_type) {
131 // TODO(crbug.com/700377): If |content_type| == NOTIFICATIONS, delete
132 // all channels.
133 }
134
135 void NotificationChannelsProviderAndroid::ShutdownOnUIThread() {
136 RemoveAllObservers();
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698