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 void ContentSuggestionsNotificationHelper::FlushCachedMetrics() { |
| 73 JNIEnv* env = base::android::AttachCurrentThread(); |
| 74 Java_ContentSuggestionsNotificationHelper_flushCachedMetrics(env); |
| 75 } |
| 76 |
| 77 bool ContentSuggestionsNotificationHelper::IsDisabledForProfile( |
| 78 Profile* profile) { |
| 79 return ntp_snippets::IsDisabledForProfile(profile); |
| 80 } |
| 81 |
| 82 // static |
| 83 bool ContentSuggestionsNotificationHelper::Register(JNIEnv* env) { |
| 84 return RegisterNativesImpl(env); |
| 85 } |
| 86 |
| 87 static void ReceiveFlushedMetrics(JNIEnv* env, |
| 88 const JavaParamRef<jclass>& class_object, |
| 89 jint tap_count, |
| 90 jint dismissal_count, |
| 91 jint hide_deadline_count, |
| 92 jint consecutive_ignored) { |
| 93 DVLOG(1) << "Flushing metrics: tap_count=" << tap_count |
| 94 << "; dismissal_count=" << dismissal_count |
| 95 << "; hide_deadline_count=" << hide_deadline_count |
| 96 << "; consecutive_ignored=" << consecutive_ignored; |
| 97 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); |
| 98 PrefService* prefs = profile->GetPrefs(); |
| 99 |
| 100 for (int i = 0; i < tap_count; ++i) { |
| 101 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_TAP); |
| 102 } |
| 103 for (int i = 0; i < dismissal_count; ++i) { |
| 104 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_DISMISSAL); |
| 105 } |
| 106 for (int i = 0; i < hide_deadline_count; ++i) { |
| 107 RecordContentSuggestionsNotificationAction( |
| 108 CONTENT_SUGGESTIONS_HIDE_DEADLINE); |
| 109 } |
| 110 |
| 111 const bool was_disabled = IsDisabledForProfile(profile); |
| 112 if (tap_count == 0) { |
| 113 // There were no taps, consecutive_ignored has not been reset and continues |
| 114 // from where it left off. If there was a tap, then Java has provided us |
| 115 // with the number of ignored notifications since that point. |
| 116 consecutive_ignored += |
| 117 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); |
| 118 } |
| 119 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName, |
| 120 consecutive_ignored); |
| 121 const bool is_disabled = IsDisabledForProfile(profile); |
| 122 if (!was_disabled && is_disabled) { |
| 123 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); |
| 124 } |
| 125 } |
| 126 |
53 } // namespace ntp_snippets | 127 } // namespace ntp_snippets |
OLD | NEW |