Chromium Code Reviews| 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/ntp_snippets/ntp_snippets_prefs.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/profiles/profile_manager.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) { | |
| 20 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 21 Java_ContentSuggestionsNotificationHelper_openUrl( | |
| 22 env, base::android::ConvertUTF8ToJavaString(env, url.spec())); | |
| 23 } | |
| 24 | |
| 25 void ContentSuggestionsNotificationHelper::SendNotification( | 29 void ContentSuggestionsNotificationHelper::SendNotification( |
| 26 const GURL& url, | 30 const GURL& url, |
| 27 const base::string16& title, | 31 const base::string16& title, |
| 28 const base::string16& text, | 32 const base::string16& text, |
| 29 const gfx::Image& image, | 33 const gfx::Image& image, |
| 30 base::Time timeout_at) { | 34 base::Time timeout_at) { |
| 31 JNIEnv* env = base::android::AttachCurrentThread(); | 35 JNIEnv* env = base::android::AttachCurrentThread(); |
| 32 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap(); | 36 SkBitmap skimage = image.AsImageSkia().GetRepresentation(1.0f).sk_bitmap(); |
| 33 if (skimage.empty()) | 37 if (skimage.empty()) |
| 34 return; | 38 return; |
| 35 | 39 |
| 36 jint timeout_at_millis = timeout_at.ToJavaTime(); | 40 jint timeout_at_millis = timeout_at.ToJavaTime(); |
| 37 if (timeout_at == base::Time::Max()) { | 41 if (timeout_at == base::Time::Max()) { |
| 38 timeout_at_millis = std::numeric_limits<jint>::max(); | 42 timeout_at_millis = std::numeric_limits<jint>::max(); |
| 39 } | 43 } |
| 40 | 44 |
| 41 Java_ContentSuggestionsNotificationHelper_showNotification( | 45 Java_ContentSuggestionsNotificationHelper_showNotification( |
| 42 env, base::android::ConvertUTF8ToJavaString(env, url.spec()), | 46 env, base::android::ConvertUTF8ToJavaString(env, url.spec()), |
| 43 base::android::ConvertUTF16ToJavaString(env, title), | 47 base::android::ConvertUTF16ToJavaString(env, title), |
| 44 base::android::ConvertUTF16ToJavaString(env, text), | 48 base::android::ConvertUTF16ToJavaString(env, text), |
| 45 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis); | 49 gfx::ConvertToJavaBitmap(&skimage), timeout_at_millis); |
| 46 } | 50 } |
| 47 | 51 |
| 48 void ContentSuggestionsNotificationHelper::HideAllNotifications() { | 52 void ContentSuggestionsNotificationHelper::HideAllNotifications() { |
| 49 JNIEnv* env = base::android::AttachCurrentThread(); | 53 JNIEnv* env = base::android::AttachCurrentThread(); |
| 50 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env); | 54 Java_ContentSuggestionsNotificationHelper_hideAllNotifications(env); |
| 51 } | 55 } |
| 52 | 56 |
| 57 bool ContentSuggestionsNotificationHelper::IsDisabledForProfile( | |
| 58 Profile* profile) { | |
| 59 auto prefs = profile->GetPrefs(); | |
|
Bernhard Bauer
2017/01/11 15:58:24
Use PrefService* in the other places as well?
sfiera
2017/01/12 11:26:23
Done.
| |
| 60 int current = | |
| 61 prefs->GetInteger(kContentSuggestionsConsecutiveIgnoredPrefName); | |
| 62 int limit = variations::GetVariationParamByFeatureAsInt( | |
| 63 kContentSuggestionsNotificationsFeature, | |
| 64 kContentSuggestionsNotificationsIgnoredLimitParam, | |
| 65 kContentSuggestionsNotificationsIgnoredDefaultLimit); | |
| 66 return current >= limit; | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 bool ContentSuggestionsNotificationHelper::Register(JNIEnv* env) { | |
| 71 return RegisterNativesImpl(env); | |
| 72 } | |
| 73 | |
| 53 } // namespace ntp_snippets | 74 } // namespace ntp_snippets |
| 75 | |
| 76 static bool IsDisabledForProfile(Profile* profile) { | |
|
Bernhard Bauer
2017/01/11 15:58:24
Is this just a convenience method to get rid of "n
sfiera
2017/01/12 11:26:23
"using ContentSuggestionsNotificationHelper::IsDis
| |
| 77 return ntp_snippets::ContentSuggestionsNotificationHelper:: | |
| 78 IsDisabledForProfile(profile); | |
| 79 } | |
| 80 | |
| 81 static void NotificationTapped(JNIEnv* env, const JavaParamRef<jclass>&) { | |
|
Bernhard Bauer
2017/01/11 15:58:24
Add a commented out variable name for the class?
sfiera
2017/01/12 11:26:23
Done.
| |
| 82 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
| 83 PrefService* prefs = profile->GetPrefs(); | |
| 84 prefs->SetInteger(kContentSuggestionsConsecutiveIgnoredPrefName, 0); | |
| 85 RecordContentSuggestionsNotificationAction(CONTENT_SUGGESTIONS_TAP); | |
| 86 } | |
| 87 | |
| 88 static void NotificationDismissed(JNIEnv* env, const JavaParamRef<jclass>&) { | |
| 89 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
| 90 auto prefs = profile->GetPrefs(); | |
| 91 const bool was_disabled = IsDisabledForProfile(profile); | |
| 92 int ignored = | |
| 93 prefs->GetInteger(kContentSuggestionsConsecutiveIgnoredPrefName); | |
| 94 prefs->SetInteger(kContentSuggestionsConsecutiveIgnoredPrefName, ignored + 1); | |
| 95 RecordContentSuggestionsNotificationAction( | |
| 96 CONTENT_SUGGESTIONS_DISMISSAL); | |
| 97 const bool is_disabled = IsDisabledForProfile(profile); | |
| 98 if (!was_disabled && is_disabled) { | |
| 99 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 static void NotificationExpired(JNIEnv* env, const JavaParamRef<jclass>&) { | |
|
Alexei Svitkine (slow)
2017/01/11 16:45:59
You can use @JNINamespace("ntp_snippets") on the J
sfiera
2017/01/12 11:26:23
That's great, thanks.
| |
| 104 Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile(); | |
| 105 auto prefs = profile->GetPrefs(); | |
| 106 const bool was_disabled = IsDisabledForProfile(profile); | |
| 107 int ignored = | |
| 108 prefs->GetInteger(kContentSuggestionsConsecutiveIgnoredPrefName); | |
| 109 prefs->SetInteger(kContentSuggestionsConsecutiveIgnoredPrefName, ignored + 1); | |
| 110 RecordContentSuggestionsNotificationAction( | |
| 111 CONTENT_SUGGESTIONS_HIDE_DEADLINE); | |
| 112 const bool is_disabled = IsDisabledForProfile(profile); | |
| 113 if (!was_disabled && is_disabled) { | |
| 114 RecordContentSuggestionsNotificationOptOut(CONTENT_SUGGESTIONS_IMPLICIT); | |
| 115 } | |
| 116 } | |
| OLD | NEW |