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

Side by Side Diff: chrome/browser/android/ntp/content_suggestions_notification_helper.cc

Issue 2639553002: Track active notifications in Java (Closed)
Patch Set: FindBugs 2 Created 3 years, 11 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
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/metrics/histogram_macros.h"
12 #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" 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" 14 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/profiles/profile_manager.h" 15 #include "chrome/browser/profiles/profile_manager.h"
17 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
18 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
19 #include "components/variations/variations_associated_data.h" 18 #include "components/variations/variations_associated_data.h"
20 #include "jni/ContentSuggestionsNotificationHelper_jni.h" 19 #include "jni/ContentSuggestionsNotificationHelper_jni.h"
21 #include "ui/gfx/android/java_bitmap.h" 20 #include "ui/gfx/android/java_bitmap.h"
22 #include "ui/gfx/image/image.h" 21 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia.h" 22 #include "ui/gfx/image/image_skia.h"
24 23
25 using base::android::JavaParamRef; 24 using base::android::JavaParamRef;
26 25
27 namespace ntp_snippets { 26 namespace ntp_snippets {
28 27
29 namespace { 28 namespace {
30 29
31 bool IsDisabledForProfile(Profile* profile) { 30 bool IsDisabledForProfile(Profile* profile) {
32 PrefService* prefs = profile->GetPrefs(); 31 PrefService* prefs = profile->GetPrefs();
33 int current = 32 int current =
34 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); 33 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName);
35 int limit = variations::GetVariationParamByFeatureAsInt( 34 int limit = variations::GetVariationParamByFeatureAsInt(
36 kContentSuggestionsNotificationsFeature, 35 kContentSuggestionsNotificationsFeature,
37 kContentSuggestionsNotificationsIgnoredLimitParam, 36 kContentSuggestionsNotificationsIgnoredLimitParam,
38 kContentSuggestionsNotificationsIgnoredDefaultLimit); 37 kContentSuggestionsNotificationsIgnoredDefaultLimit);
39 return current >= limit; 38 return current >= limit;
40 } 39 }
41 40
42 } // namespace 41 } // namespace
43 42
44 void ContentSuggestionsNotificationHelper::SendNotification( 43 bool ContentSuggestionsNotificationHelper::SendNotification(
44 const ContentSuggestion::ID& id,
45 const GURL& url, 45 const GURL& url,
46 const base::string16& title, 46 const base::string16& title,
47 const base::string16& text, 47 const base::string16& text,
48 const gfx::Image& image, 48 const gfx::Image& image,
49 base::Time timeout_at) { 49 base::Time timeout_at) {
50 JNIEnv* env = base::android::AttachCurrentThread(); 50 JNIEnv* env = base::android::AttachCurrentThread();
51 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap(); 51 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap();
52 if (skimage.empty()) 52 if (skimage.empty())
53 return; 53 return false;
54 54
55 jint timeout_at_millis = timeout_at.ToJavaTime(); 55 jint timeout_at_millis = timeout_at.ToJavaTime();
56 if (timeout_at == base::Time::Max()) { 56 if (timeout_at == base::Time::Max()) {
57 timeout_at_millis = std::numeric_limits<jint>::max(); 57 timeout_at_millis = std::numeric_limits<jint>::max();
58 } 58 }
59 59
60 Java_ContentSuggestionsNotificationHelper_showNotification( 60 if (Java_ContentSuggestionsNotificationHelper_showNotification(
61 env, base::android::ConvertUTF8ToJavaString(env, url.spec()), 61 env, id.category().id(),
62 base::android::ConvertUTF16ToJavaString(env, title), 62 base::android::ConvertUTF8ToJavaString(env, id.id_within_category()),
63 base::android::ConvertUTF16ToJavaString(env, text), 63 base::android::ConvertUTF8ToJavaString(env, url.spec()),
64 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis); 64 base::android::ConvertUTF16ToJavaString(env, title),
65 base::android::ConvertUTF16ToJavaString(env, text),
66 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis)) {
67 DVLOG(1) << "Displayed notification for " << id;
68 return true;
69 } else {
70 DVLOG(1) << "Suppressed notification for " << url.spec();
71 return false;
72 }
65 } 73 }
66 74
67 void ContentSuggestionsNotificationHelper::HideAllNotifications() { 75 void ContentSuggestionsNotificationHelper::HideNotification(
76 const ContentSuggestion::ID& id,
77 ContentSuggestionsNotificationAction why) {
68 JNIEnv* env = base::android::AttachCurrentThread(); 78 JNIEnv* env = base::android::AttachCurrentThread();
69 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env); 79 Java_ContentSuggestionsNotificationHelper_hideNotification(
80 env, id.category().id(),
81 base::android::ConvertUTF8ToJavaString(env, id.id_within_category()),
82 static_cast<int>(why));
83 }
84
85 void ContentSuggestionsNotificationHelper::HideAllNotifications(
86 ContentSuggestionsNotificationAction why) {
87 JNIEnv* env = base::android::AttachCurrentThread();
88 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env, why);
70 } 89 }
71 90
72 void ContentSuggestionsNotificationHelper::FlushCachedMetrics() { 91 void ContentSuggestionsNotificationHelper::FlushCachedMetrics() {
73 JNIEnv* env = base::android::AttachCurrentThread(); 92 JNIEnv* env = base::android::AttachCurrentThread();
74 Java_ContentSuggestionsNotificationHelper_flushCachedMetrics(env); 93 Java_ContentSuggestionsNotificationHelper_flushCachedMetrics(env);
75 } 94 }
76 95
77 bool ContentSuggestionsNotificationHelper::IsDisabledForProfile( 96 bool ContentSuggestionsNotificationHelper::IsDisabledForProfile(
78 Profile* profile) { 97 Profile* profile) {
79 return ntp_snippets::IsDisabledForProfile(profile); 98 return ntp_snippets::IsDisabledForProfile(profile);
80 } 99 }
81 100
82 // static 101 // static
83 bool ContentSuggestionsNotificationHelper::Register(JNIEnv* env) { 102 bool ContentSuggestionsNotificationHelper::Register(JNIEnv* env) {
84 return RegisterNativesImpl(env); 103 return RegisterNativesImpl(env);
85 } 104 }
86 105
87 static void ReceiveFlushedMetrics(JNIEnv* env, 106 static void ReceiveFlushedMetrics(JNIEnv* env,
88 const JavaParamRef<jclass>& class_object, 107 const JavaParamRef<jclass>& class_object,
89 jint tap_count, 108 jint tap_count,
90 jint dismissal_count, 109 jint dismissal_count,
91 jint hide_deadline_count, 110 jint hide_deadline_count,
111 jint hide_expiry_count,
112 jint hide_frontmost_count,
113 jint hide_disabled_count,
114 jint hide_shutdown_count,
92 jint consecutive_ignored) { 115 jint consecutive_ignored) {
93 DVLOG(1) << "Flushing metrics: tap_count=" << tap_count 116 DVLOG(1) << "Flushing metrics: tap_count=" << tap_count
94 << "; dismissal_count=" << dismissal_count 117 << "; dismissal_count=" << dismissal_count
95 << "; hide_deadline_count=" << hide_deadline_count 118 << "; hide_deadline_count=" << hide_deadline_count
119 << "; hide_expiry_count=" << hide_expiry_count
120 << "; hide_frontmost_count=" << hide_frontmost_count
121 << "; hide_disabled_count=" << hide_disabled_count
122 << "; hide_shutdown_count=" << hide_shutdown_count
96 << "; consecutive_ignored=" << consecutive_ignored; 123 << "; consecutive_ignored=" << consecutive_ignored;
97 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); 124 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
98 PrefService* prefs = profile->GetPrefs(); 125 PrefService* prefs = profile->GetPrefs();
99 126
100 for (int i = 0; i < tap_count; ++i) { 127 for (int i = 0; i < tap_count; ++i) {
101 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_TAP); 128 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_TAP);
102 } 129 }
103 for (int i = 0; i < dismissal_count; ++i) { 130 for (int i = 0; i < dismissal_count; ++i) {
104 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_DISMISSAL); 131 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_DISMISSAL);
105 } 132 }
106 for (int i = 0; i < hide_deadline_count; ++i) { 133 for (int i = 0; i < hide_deadline_count; ++i) {
107 RecordContentSuggestionsNotificationAction( 134 RecordContentSuggestionsNotificationAction(
108 CONTENT_SUGGESTIONS_HIDE_DEADLINE); 135 CONTENT_SUGGESTIONS_HIDE_DEADLINE);
109 } 136 }
137 for (int i = 0; i < hide_expiry_count; ++i) {
138 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_HIDE_EXPIRY);
139 }
140 for (int i = 0; i < hide_frontmost_count; ++i) {
141 RecordContentSuggestionsNotificationAction(
142 CONTENT_SUGGESTIONS_HIDE_FRONTMOST);
143 }
144 for (int i = 0; i < hide_disabled_count; ++i) {
145 RecordContentSuggestionsNotificationAction(
146 CONTENT_SUGGESTIONS_HIDE_DISABLED);
147 }
148 for (int i = 0; i < hide_shutdown_count; ++i) {
149 RecordContentSuggestionsNotificationAction(
150 CONTENT_SUGGESTIONS_HIDE_SHUTDOWN);
151 }
110 152
111 const bool was_disabled = IsDisabledForProfile(profile); 153 const bool was_disabled = IsDisabledForProfile(profile);
112 if (tap_count == 0) { 154 if (tap_count == 0) {
113 // There were no taps, consecutive_ignored has not been reset and continues 155 // 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 156 // 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. 157 // with the number of ignored notifications since that point.
116 consecutive_ignored += 158 consecutive_ignored +=
117 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName); 159 prefs->GetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName);
118 } 160 }
119 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName, 161 prefs->SetInteger(prefs::kContentSuggestionsConsecutiveIgnoredPrefName,
120 consecutive_ignored); 162 consecutive_ignored);
121 const bool is_disabled = IsDisabledForProfile(profile); 163 const bool is_disabled = IsDisabledForProfile(profile);
122 if (!was_disabled && is_disabled) { 164 if (!was_disabled && is_disabled) {
123 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); 165 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT);
124 } 166 }
125 } 167 }
126 168
127 } // namespace ntp_snippets 169 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698