| 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/ntp_snippets_bridge.h" | 5 #include "chrome/browser/android/ntp/ntp_snippets_bridge.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/android/callback_android.h" | 11 #include "base/android/callback_android.h" |
| 12 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 13 #include "base/android/jni_array.h" | 13 #include "base/android/jni_array.h" |
| 14 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
| 18 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 18 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/profiles/profile_android.h" | 20 #include "chrome/browser/profiles/profile_android.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "components/history/core/browser/history_service.h" | 22 #include "components/history/core/browser/history_service.h" |
| 23 #include "components/ntp_snippets/content_suggestion.h" | 23 #include "components/ntp_snippets/content_suggestion.h" |
| 24 #include "components/ntp_snippets/content_suggestions_metrics.h" | 24 #include "components/ntp_snippets/content_suggestions_metrics.h" |
| 25 #include "components/ntp_snippets/pref_names.h" | 25 #include "components/ntp_snippets/pref_names.h" |
| 26 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" | 26 #include "components/ntp_snippets/remote/remote_suggestions_provider.h" |
| 27 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" |
| 27 #include "components/prefs/pref_service.h" | 28 #include "components/prefs/pref_service.h" |
| 28 #include "jni/SnippetsBridge_jni.h" | 29 #include "jni/SnippetsBridge_jni.h" |
| 29 #include "ui/base/window_open_disposition.h" | 30 #include "ui/base/window_open_disposition.h" |
| 30 #include "ui/gfx/android/java_bitmap.h" | 31 #include "ui/gfx/android/java_bitmap.h" |
| 31 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
| 32 | 33 |
| 33 using base::android::AttachCurrentThread; | 34 using base::android::AttachCurrentThread; |
| 34 using base::android::ConvertJavaStringToUTF8; | 35 using base::android::ConvertJavaStringToUTF8; |
| 35 using base::android::ConvertUTF8ToJavaString; | 36 using base::android::ConvertUTF8ToJavaString; |
| 36 using base::android::ConvertUTF16ToJavaString; | 37 using base::android::ConvertUTF16ToJavaString; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 env, java_suggestion, | 89 env, java_suggestion, |
| 89 ConvertUTF8ToJavaString( | 90 ConvertUTF8ToJavaString( |
| 90 env, suggestion.recent_tab_suggestion_extra()->tab_id), | 91 env, suggestion.recent_tab_suggestion_extra()->tab_id), |
| 91 suggestion.recent_tab_suggestion_extra()->offline_page_id); | 92 suggestion.recent_tab_suggestion_extra()->offline_page_id); |
| 92 } | 93 } |
| 93 } | 94 } |
| 94 | 95 |
| 95 return result; | 96 return result; |
| 96 } | 97 } |
| 97 | 98 |
| 98 ntp_snippets::RemoteSuggestionsProvider* GetRemoteSuggestionsProvider() { | 99 ntp_snippets::RemoteSuggestionsScheduler* GetRemoteSuggestionsScheduler() { |
| 99 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 100 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 100 ContentSuggestionsServiceFactory::GetForProfile( | 101 ContentSuggestionsServiceFactory::GetForProfile( |
| 101 ProfileManager::GetLastUsedProfile()); | 102 ProfileManager::GetLastUsedProfile()); |
| 102 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | 103 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
| 103 if (!content_suggestions_service) { | 104 if (!content_suggestions_service) { |
| 104 return nullptr; | 105 return nullptr; |
| 105 } | 106 } |
| 106 return content_suggestions_service->ntp_snippets_service(); | 107 return content_suggestions_service->remote_suggestions_scheduler(); |
| 107 } | 108 } |
| 108 | 109 |
| 109 } // namespace | 110 } // namespace |
| 110 | 111 |
| 111 static jlong Init(JNIEnv* env, | 112 static jlong Init(JNIEnv* env, |
| 112 const JavaParamRef<jobject>& obj, | 113 const JavaParamRef<jobject>& obj, |
| 113 const JavaParamRef<jobject>& j_profile) { | 114 const JavaParamRef<jobject>& j_profile) { |
| 114 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 115 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
| 115 return reinterpret_cast<intptr_t>(snippets_bridge); | 116 return reinterpret_cast<intptr_t>(snippets_bridge); |
| 116 } | 117 } |
| 117 | 118 |
| 118 static void FetchRemoteSuggestions(JNIEnv* env, | 119 // Initiates a background fetch for remote suggestions. |
| 119 const JavaParamRef<jclass>& caller) { | 120 static void RemoteSuggestionsSchedulerOnFetchDue( |
| 120 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | 121 JNIEnv* env, |
| 121 GetRemoteSuggestionsProvider(); | 122 const JavaParamRef<jclass>& caller) { |
| 122 // Can be null if the feature has been disabled but the scheduler has not been | 123 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 123 // unregistered yet. The next start should unregister it. | 124 GetRemoteSuggestionsScheduler(); |
| 124 if (!remote_suggestions_provider) { | 125 if (!scheduler) { |
| 125 return; | 126 return; |
| 126 } | 127 } |
| 127 remote_suggestions_provider->FetchSnippetsForAllCategories(); | |
| 128 } | |
| 129 | 128 |
| 130 static void FetchRemoteSuggestionsInTheBackground( | 129 scheduler->OnPersistentSchedulerWakeUp(); |
| 131 JNIEnv* env, | |
| 132 const JavaParamRef<jclass>& caller) { | |
| 133 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | |
| 134 GetRemoteSuggestionsProvider(); | |
| 135 // Can be null if the feature has been disabled but the scheduler has not been | |
| 136 // unregistered yet. The next start should unregister it. | |
| 137 if (!remote_suggestions_provider) { | |
| 138 return; | |
| 139 } | |
| 140 remote_suggestions_provider->FetchSnippetsInTheBackground(); | |
| 141 } | 130 } |
| 142 | 131 |
| 143 // Reschedules the fetching of snippets. If tasks are already scheduled, they | 132 // Reschedules the fetching of snippets. If tasks are already scheduled, they |
| 144 // will be rescheduled anyway, so all running intervals will be reset. | 133 // will be rescheduled anyway, so all running intervals will be reset. |
| 145 static void RescheduleFetching(JNIEnv* env, | 134 static void RemoteSuggestionsSchedulerRescheduleFetching( |
| 146 const JavaParamRef<jclass>& caller) { | 135 JNIEnv* env, |
| 147 Profile* profile = ProfileManager::GetLastUsedProfile(); | 136 const JavaParamRef<jclass>& caller) { |
| 148 // Temporary check while investigating crbug.com/647920. | 137 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 149 CHECK(profile); | 138 GetRemoteSuggestionsScheduler(); |
| 150 | 139 // Can be null if the feature has been disabled but the scheduler has not been |
| 151 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 140 // unregistered yet. The next start should unregister it. |
| 152 ContentSuggestionsServiceFactory::GetForProfile(profile); | 141 if (!scheduler) { |
| 153 | |
| 154 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | |
| 155 if (!content_suggestions_service) { | |
| 156 return; | 142 return; |
| 157 } | 143 } |
| 158 | 144 |
| 159 ntp_snippets::RemoteSuggestionsProvider* service = | 145 scheduler->RescheduleFetching(); |
| 160 content_suggestions_service->ntp_snippets_service(); | |
| 161 | |
| 162 // Can be null if the feature has been disabled but the scheduler has not been | |
| 163 // unregistered yet. The next start should unregister it. | |
| 164 if (!service) { | |
| 165 return; | |
| 166 } | |
| 167 | |
| 168 service->RescheduleFetching(/*force=*/true); | |
| 169 } | 146 } |
| 170 | 147 |
| 171 static void OnSuggestionTargetVisited(JNIEnv* env, | 148 static void OnSuggestionTargetVisited(JNIEnv* env, |
| 172 const JavaParamRef<jclass>& caller, | 149 const JavaParamRef<jclass>& caller, |
| 173 jint j_category_id, | 150 jint j_category_id, |
| 174 jlong visit_time_ms) { | 151 jlong visit_time_ms) { |
| 175 ntp_snippets::metrics::OnSuggestionTargetVisited( | 152 ntp_snippets::metrics::OnSuggestionTargetVisited( |
| 176 Category::FromIDValue(j_category_id), | 153 Category::FromIDValue(j_category_id), |
| 177 base::TimeDelta::FromMilliseconds(visit_time_ms)); | 154 base::TimeDelta::FromMilliseconds(visit_time_ms)); |
| 178 } | 155 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 &known_suggestion_ids); | 245 &known_suggestion_ids); |
| 269 | 246 |
| 270 Category category = Category::FromIDValue(j_category_id); | 247 Category category = Category::FromIDValue(j_category_id); |
| 271 content_suggestions_service_->Fetch( | 248 content_suggestions_service_->Fetch( |
| 272 category, std::set<std::string>(known_suggestion_ids.begin(), | 249 category, std::set<std::string>(known_suggestion_ids.begin(), |
| 273 known_suggestion_ids.end()), | 250 known_suggestion_ids.end()), |
| 274 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, | 251 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, |
| 275 weak_ptr_factory_.GetWeakPtr(), category)); | 252 weak_ptr_factory_.GetWeakPtr(), category)); |
| 276 } | 253 } |
| 277 | 254 |
| 255 void NTPSnippetsBridge::ReloadSuggestions( |
| 256 JNIEnv* env, |
| 257 const base::android::JavaParamRef<jobject>& obj) { |
| 258 content_suggestions_service_->ReloadSuggestions(); |
| 259 } |
| 260 |
| 278 void NTPSnippetsBridge::DismissSuggestion( | 261 void NTPSnippetsBridge::DismissSuggestion( |
| 279 JNIEnv* env, | 262 JNIEnv* env, |
| 280 const JavaParamRef<jobject>& obj, | 263 const JavaParamRef<jobject>& obj, |
| 281 const JavaParamRef<jstring>& jurl, | 264 const JavaParamRef<jstring>& jurl, |
| 282 jint global_position, | 265 jint global_position, |
| 283 jint j_category_id, | 266 jint j_category_id, |
| 284 jint category_position, | 267 jint category_position, |
| 285 const JavaParamRef<jstring>& id_within_category) { | 268 const JavaParamRef<jstring>& id_within_category) { |
| 286 Category category = Category::FromIDValue(j_category_id); | 269 Category category = Category::FromIDValue(j_category_id); |
| 287 | 270 |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 JNIEnv* env = AttachCurrentThread(); | 463 JNIEnv* env = AttachCurrentThread(); |
| 481 Java_SnippetsBridge_onMoreSuggestions( | 464 Java_SnippetsBridge_onMoreSuggestions( |
| 482 env, observer_, category.id(), | 465 env, observer_, category.id(), |
| 483 ToJavaSuggestionList(env, category, suggestions)); | 466 ToJavaSuggestionList(env, category, suggestions)); |
| 484 } | 467 } |
| 485 | 468 |
| 486 // static | 469 // static |
| 487 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 470 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 488 return RegisterNativesImpl(env); | 471 return RegisterNativesImpl(env); |
| 489 } | 472 } |
| OLD | NEW |