OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h" | 5 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/metrics/histogram_macros.h" | |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/ntp_snippets/ntp_snippets_features.h" | |
14 #include "chrome/browser/ntp_snippets/ntp_snippets_metrics.h" | |
15 #include "chrome/browser/profiles/profile.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | |
17 #include "chrome/common/pref_names.h" | |
18 #include "components/prefs/pref_service.h" | |
19 #include "components/variations/variations_associated_data.h" | |
12 #include "jni/ContentSuggestionsNotificationHelper_jni.h" | 20 #include "jni/ContentSuggestionsNotificationHelper_jni.h" |
13 #include "ui/gfx/android/java_bitmap.h" | 21 #include "ui/gfx/android/java_bitmap.h" |
14 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
15 #include "ui/gfx/image/image_skia.h" | 23 #include "ui/gfx/image/image_skia.h" |
16 | 24 |
25 using base::android::JavaParamRef; | |
26 | |
17 namespace ntp_snippets { | 27 namespace ntp_snippets { |
18 | 28 |
19 void ContentSuggestionsNotificationHelper::OpenURL(const GURL& url) { | 29 namespace { |
20 JNIEnv* env = base::android::AttachCurrentThread(); | 30 |
21 Java_ContentSuggestionsNotificationHelper_openUrl( | 31 bool IsDisabledForProfile(Profile* profile) { |
22 env, base::android::ConvertUTF8ToJavaString(env, url.spec())); | 32 PrefService* prefs = profile->GetPrefs(); |
33 int current = | |
34 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); | |
35 int limit = variations::GetVariationParamByFeatureAsInt( | |
36 kContentSuggestionsNotificationsFeature, | |
37 kContentSuggestionsNotificationsIgnoredLimitParam, | |
38 kContentSuggestionsNotificationsIgnoredDefaultLimit); | |
39 return current >= limit; | |
23 } | 40 } |
24 | 41 |
42 } // namespace | |
43 | |
25 void ContentSuggestionsNotificationHelper::SendNotification( | 44 void ContentSuggestionsNotificationHelper::SendNotification( |
26 const GURL& url, | 45 const GURL& url, |
27 const base::string16& title, | 46 const base::string16& title, |
28 const base::string16& text, | 47 const base::string16& text, |
29 const gfx::Image& image, | 48 const gfx::Image& image, |
30 base::Time timeout_at) { | 49 base::Time timeout_at) { |
31 JNIEnv* env = base::android::AttachCurrentThread(); | 50 JNIEnv* env = base::android::AttachCurrentThread(); |
32 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap(); | 51 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap(); |
33 if (skimage.empty()) | 52 if (skimage.empty()) |
34 return; | 53 return; |
35 | 54 |
36 jint timeout_at_millis = timeout_at.ToJavaTime(); | 55 jint timeout_at_millis = timeout_at.ToJavaTime(); |
37 if (timeout_at == base::Time::Max()) { | 56 if (timeout_at == base::Time::Max()) { |
38 timeout_at_millis = std::numeric_limits<jint>::max(); | 57 timeout_at_millis = std::numeric_limits<jint>::max(); |
39 } | 58 } |
40 | 59 |
41 Java_ContentSuggestionsNotificationHelper_showNotification( | 60 Java_ContentSuggestionsNotificationHelper_showNotification( |
42 env, base::android::ConvertUTF8ToJavaString(env, url.spec()), | 61 env, base::android::ConvertUTF8ToJavaString(env, url.spec()), |
43 base::android::ConvertUTF16ToJavaString(env, title), | 62 base::android::ConvertUTF16ToJavaString(env, title), |
44 base::android::ConvertUTF16ToJavaString(env, text), | 63 base::android::ConvertUTF16ToJavaString(env, text), |
45 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis); | 64 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis); |
46 } | 65 } |
47 | 66 |
48 void ContentSuggestionsNotificationHelper::HideAllNotifications() { | 67 void ContentSuggestionsNotificationHelper::HideAllNotifications() { |
49 JNIEnv* env = base::android::AttachCurrentThread(); | 68 JNIEnv* env = base::android::AttachCurrentThread(); |
50 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env); | 69 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env); |
51 } | 70 } |
52 | 71 |
72 bool ContentSuggestionsNotificationHelper::IsDisabledForProfile( | |
73 Profile* profile) { | |
74 return ntp_snippets::IsDisabledForProfile(profile); | |
75 } | |
76 | |
77 // static | |
78 bool ContentSuggestionsNotificationHelper::Register(JNIEnv* env) { | |
79 return RegisterNativesImpl(env); | |
80 } | |
81 | |
82 static void NotificationTapped(JNIEnv* env, | |
83 const JavaParamRef<jclass> /* class_object */&) { | |
84 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
85 PrefService* prefs = profile->GetPrefs(); | |
86 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName, 0); | |
87 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_TAP); | |
88 } | |
89 | |
90 static void NotificationDismissed( | |
91 JNIEnv* env, | |
92 const JavaParamRef<jclass> /* class_object */&) { | |
Bernhard Bauer
2017/01/12 12:13:52
Put the ampersand before the variable name?
| |
93 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
94 PrefService* prefs = profile->GetPrefs(); | |
95 const bool was_disabled = IsDisabledForProfile(profile); | |
96 int ignored = | |
97 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); | |
98 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName, | |
99 ignored + 1); | |
100 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_DISMISSAL); | |
101 const bool is_disabled = IsDisabledForProfile(profile); | |
102 if (!was_disabled && is_disabled) { | |
103 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); | |
104 } | |
105 } | |
106 | |
107 static void NotificationExpired( | |
108 JNIEnv* env, | |
109 const JavaParamRef<jclass>& /* class_object */) { | |
110 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
111 PrefService* prefs = profile->GetPrefs(); | |
112 const bool was_disabled = IsDisabledForProfile(profile); | |
113 int ignored = | |
114 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); | |
115 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName, | |
116 ignored + 1); | |
117 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_HIDE_DEADLINE); | |
118 const bool is_disabled = IsDisabledForProfile(profile); | |
119 if (!was_disabled && is_disabled) { | |
120 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); | |
121 } | |
122 } | |
123 | |
53 } // namespace ntp_snippets | 124 } // namespace ntp_snippets |
OLD | NEW |