| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 env, java_suggestion, | 90 env, java_suggestion, |
| 90 ConvertUTF8ToJavaString( | 91 ConvertUTF8ToJavaString( |
| 91 env, suggestion.recent_tab_suggestion_extra()->tab_id), | 92 env, suggestion.recent_tab_suggestion_extra()->tab_id), |
| 92 suggestion.recent_tab_suggestion_extra()->offline_page_id); | 93 suggestion.recent_tab_suggestion_extra()->offline_page_id); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 96 return result; | 97 return result; |
| 97 } | 98 } |
| 98 | 99 |
| 99 ntp_snippets::RemoteSuggestionsProvider* GetRemoteSuggestionsProvider() { | 100 ntp_snippets::RemoteSuggestionsScheduler* GetRemoteSuggestionsScheduler() { |
| 100 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 101 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 101 ContentSuggestionsServiceFactory::GetForProfile( | 102 ContentSuggestionsServiceFactory::GetForProfile( |
| 102 ProfileManager::GetLastUsedProfile()); | 103 ProfileManager::GetLastUsedProfile()); |
| 103 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | 104 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
| 104 if (!content_suggestions_service) { | 105 if (!content_suggestions_service) { |
| 105 return nullptr; | 106 return nullptr; |
| 106 } | 107 } |
| 107 return content_suggestions_service->ntp_snippets_service(); | 108 return content_suggestions_service->remote_suggestions_scheduler(); |
| 108 } | 109 } |
| 109 | 110 |
| 110 } // namespace | 111 } // namespace |
| 111 | 112 |
| 112 static jlong Init(JNIEnv* env, | 113 static jlong Init(JNIEnv* env, |
| 113 const JavaParamRef<jobject>& obj, | 114 const JavaParamRef<jobject>& obj, |
| 114 const JavaParamRef<jobject>& j_profile) { | 115 const JavaParamRef<jobject>& j_profile) { |
| 115 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 116 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
| 116 return reinterpret_cast<intptr_t>(snippets_bridge); | 117 return reinterpret_cast<intptr_t>(snippets_bridge); |
| 117 } | 118 } |
| 118 | 119 |
| 119 static void FetchRemoteSuggestions(JNIEnv* env, | 120 // Initiates a background fetch for remote suggestions. |
| 120 const JavaParamRef<jclass>& caller) { | 121 static void RemoteSuggestionsSchedulerOnFetchDue( |
| 121 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | 122 JNIEnv* env, |
| 122 GetRemoteSuggestionsProvider(); | 123 const JavaParamRef<jclass>& caller) { |
| 123 // Can be null if the feature has been disabled but the scheduler has not been | 124 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 124 // unregistered yet. The next start should unregister it. | 125 GetRemoteSuggestionsScheduler(); |
| 125 if (!remote_suggestions_provider) { | 126 if (!scheduler) { |
| 126 return; | 127 return; |
| 127 } | 128 } |
| 128 remote_suggestions_provider->FetchSnippetsForAllCategories(); | |
| 129 } | |
| 130 | 129 |
| 131 static void FetchRemoteSuggestionsInTheBackground( | 130 scheduler->OnFetchDue(); |
| 132 JNIEnv* env, | |
| 133 const JavaParamRef<jclass>& caller) { | |
| 134 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | |
| 135 GetRemoteSuggestionsProvider(); | |
| 136 // Can be null if the feature has been disabled but the scheduler has not been | |
| 137 // unregistered yet. The next start should unregister it. | |
| 138 if (!remote_suggestions_provider) { | |
| 139 return; | |
| 140 } | |
| 141 remote_suggestions_provider->FetchSnippetsInTheBackground(); | |
| 142 } | 131 } |
| 143 | 132 |
| 144 // Reschedules the fetching of snippets. If tasks are already scheduled, they | 133 // Reschedules the fetching of snippets. If tasks are already scheduled, they |
| 145 // will be rescheduled anyway, so all running intervals will be reset. | 134 // will be rescheduled anyway, so all running intervals will be reset. |
| 146 static void RescheduleFetching(JNIEnv* env, | 135 static void RemoteSuggestionsSchedulerRescheduleFetching( |
| 147 const JavaParamRef<jclass>& caller) { | 136 JNIEnv* env, |
| 148 Profile* profile = ProfileManager::GetLastUsedProfile(); | 137 const JavaParamRef<jclass>& caller) { |
| 149 // Temporary check while investigating crbug.com/647920. | 138 ntp_snippets::RemoteSuggestionsScheduler* scheduler = |
| 150 CHECK(profile); | 139 GetRemoteSuggestionsScheduler(); |
| 151 | 140 // Can be null if the feature has been disabled but the scheduler has not been |
| 152 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 141 // unregistered yet. The next start should unregister it. |
| 153 ContentSuggestionsServiceFactory::GetForProfile(profile); | 142 if (!scheduler) { |
| 154 | |
| 155 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | |
| 156 if (!content_suggestions_service) { | |
| 157 return; | 143 return; |
| 158 } | 144 } |
| 159 | 145 |
| 160 ntp_snippets::RemoteSuggestionsProvider* service = | 146 scheduler->RescheduleFetching(); |
| 161 content_suggestions_service->ntp_snippets_service(); | |
| 162 | |
| 163 // Can be null if the feature has been disabled but the scheduler has not been | |
| 164 // unregistered yet. The next start should unregister it. | |
| 165 if (!service) { | |
| 166 return; | |
| 167 } | |
| 168 | |
| 169 service->RescheduleFetching(/*force=*/true); | |
| 170 } | 147 } |
| 171 | 148 |
| 172 static void OnSuggestionTargetVisited(JNIEnv* env, | 149 static void OnSuggestionTargetVisited(JNIEnv* env, |
| 173 const JavaParamRef<jclass>& caller, | 150 const JavaParamRef<jclass>& caller, |
| 174 jint j_category_id, | 151 jint j_category_id, |
| 175 jlong visit_time_ms) { | 152 jlong visit_time_ms) { |
| 176 Profile* profile = ProfileManager::GetLastUsedProfile(); | 153 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 177 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 154 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 178 ContentSuggestionsServiceFactory::GetForProfile(profile); | 155 ContentSuggestionsServiceFactory::GetForProfile(profile); |
| 179 ntp_snippets::metrics::OnSuggestionTargetVisited( | 156 ntp_snippets::metrics::OnSuggestionTargetVisited( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 &known_suggestion_ids); | 250 &known_suggestion_ids); |
| 274 | 251 |
| 275 Category category = CategoryFromIDValue(j_category_id); | 252 Category category = CategoryFromIDValue(j_category_id); |
| 276 content_suggestions_service_->Fetch( | 253 content_suggestions_service_->Fetch( |
| 277 category, std::set<std::string>(known_suggestion_ids.begin(), | 254 category, std::set<std::string>(known_suggestion_ids.begin(), |
| 278 known_suggestion_ids.end()), | 255 known_suggestion_ids.end()), |
| 279 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, | 256 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, |
| 280 weak_ptr_factory_.GetWeakPtr(), category)); | 257 weak_ptr_factory_.GetWeakPtr(), category)); |
| 281 } | 258 } |
| 282 | 259 |
| 260 void NTPSnippetsBridge::ReloadSuggestions( |
| 261 JNIEnv* env, |
| 262 const base::android::JavaParamRef<jobject>& obj) { |
| 263 content_suggestions_service_->ReloadSuggestions(); |
| 264 } |
| 265 |
| 283 void NTPSnippetsBridge::DismissSuggestion( | 266 void NTPSnippetsBridge::DismissSuggestion( |
| 284 JNIEnv* env, | 267 JNIEnv* env, |
| 285 const JavaParamRef<jobject>& obj, | 268 const JavaParamRef<jobject>& obj, |
| 286 const JavaParamRef<jstring>& jurl, | 269 const JavaParamRef<jstring>& jurl, |
| 287 jint global_position, | 270 jint global_position, |
| 288 jint j_category_id, | 271 jint j_category_id, |
| 289 jint category_position, | 272 jint category_position, |
| 290 const JavaParamRef<jstring>& id_within_category) { | 273 const JavaParamRef<jstring>& id_within_category) { |
| 291 Category category = CategoryFromIDValue(j_category_id); | 274 Category category = CategoryFromIDValue(j_category_id); |
| 292 | 275 |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 } | 467 } |
| 485 | 468 |
| 486 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { | 469 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { |
| 487 return content_suggestions_service_->category_factory()->FromIDValue(id); | 470 return content_suggestions_service_->category_factory()->FromIDValue(id); |
| 488 } | 471 } |
| 489 | 472 |
| 490 // static | 473 // static |
| 491 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 474 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 492 return RegisterNativesImpl(env); | 475 return RegisterNativesImpl(env); |
| 493 } | 476 } |
| OLD | NEW |