| 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 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 } | 95 } |
| 96 | 96 |
| 97 ntp_snippets::RemoteSuggestionsProvider* GetRemoteSuggestionsProvider() { | 97 ntp_snippets::RemoteSuggestionsProvider* GetRemoteSuggestionsProvider() { |
| 98 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 98 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 99 ContentSuggestionsServiceFactory::GetForProfile( | 99 ContentSuggestionsServiceFactory::GetForProfile( |
| 100 ProfileManager::GetLastUsedProfile()); | 100 ProfileManager::GetLastUsedProfile()); |
| 101 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | 101 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 |
| 102 if (!content_suggestions_service) { | 102 if (!content_suggestions_service) { |
| 103 return nullptr; | 103 return nullptr; |
| 104 } | 104 } |
| 105 return content_suggestions_service->ntp_snippets_service(); | 105 return content_suggestions_service->remote_suggestions_provider(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 | 109 |
| 110 static jlong Init(JNIEnv* env, | 110 static jlong Init(JNIEnv* env, |
| 111 const JavaParamRef<jobject>& obj, | 111 const JavaParamRef<jobject>& obj, |
| 112 const JavaParamRef<jobject>& j_profile) { | 112 const JavaParamRef<jobject>& j_profile) { |
| 113 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); | 113 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); |
| 114 return reinterpret_cast<intptr_t>(snippets_bridge); | 114 return reinterpret_cast<intptr_t>(snippets_bridge); |
| 115 } | 115 } |
| 116 | 116 |
| 117 static void FetchRemoteSuggestions(JNIEnv* env, | 117 static void FetchRemoteSuggestions(JNIEnv* env, |
| 118 const JavaParamRef<jclass>& caller) { | 118 const JavaParamRef<jclass>& caller) { |
| 119 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | 119 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = |
| 120 GetRemoteSuggestionsProvider(); | 120 GetRemoteSuggestionsProvider(); |
| 121 // Can be null if the feature has been disabled but the scheduler has not been | 121 // Can be null if the feature has been disabled but the scheduler has not been |
| 122 // unregistered yet. The next start should unregister it. | 122 // unregistered yet. The next start should unregister it. |
| 123 if (!remote_suggestions_provider) { | 123 if (!remote_suggestions_provider) { |
| 124 return; | 124 return; |
| 125 } | 125 } |
| 126 |
| 126 remote_suggestions_provider->FetchSnippetsForAllCategories(); | 127 remote_suggestions_provider->FetchSnippetsForAllCategories(); |
| 127 } | 128 } |
| 128 | 129 |
| 129 static void FetchRemoteSuggestionsInTheBackground( | 130 static void FetchRemoteSuggestionsInTheBackground( |
| 130 JNIEnv* env, | 131 JNIEnv* env, |
| 131 const JavaParamRef<jclass>& caller) { | 132 const JavaParamRef<jclass>& caller) { |
| 132 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = | 133 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = |
| 133 GetRemoteSuggestionsProvider(); | 134 GetRemoteSuggestionsProvider(); |
| 134 // Can be null if the feature has been disabled but the scheduler has not been | 135 // Can be null if the feature has been disabled but the scheduler has not been |
| 135 // unregistered yet. The next start should unregister it. | 136 // unregistered yet. The next start should unregister it. |
| 136 if (!remote_suggestions_provider) { | 137 if (!remote_suggestions_provider) { |
| 137 return; | 138 return; |
| 138 } | 139 } |
| 139 remote_suggestions_provider->FetchSnippetsInTheBackground(); | 140 |
| 141 remote_suggestions_provider->scheduler()->PerformHardUpdate(); |
| 140 } | 142 } |
| 141 | 143 |
| 142 // Reschedules the fetching of snippets. If tasks are already scheduled, they | 144 // Reschedules the fetching of snippets. If tasks are already scheduled, they |
| 143 // will be rescheduled anyway, so all running intervals will be reset. | 145 // will be rescheduled anyway, so all running intervals will be reset. |
| 144 static void RescheduleFetching(JNIEnv* env, | 146 static void RescheduleFetching(JNIEnv* env, |
| 145 const JavaParamRef<jclass>& caller) { | 147 const JavaParamRef<jclass>& caller) { |
| 146 Profile* profile = ProfileManager::GetLastUsedProfile(); | 148 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = |
| 147 // Temporary check while investigating crbug.com/647920. | 149 GetRemoteSuggestionsProvider(); |
| 148 CHECK(profile); | 150 // Can be null if the feature has been disabled but the scheduler has not been |
| 149 | 151 // unregistered yet. The next start should unregister it. |
| 150 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 152 if (!remote_suggestions_provider) { |
| 151 ContentSuggestionsServiceFactory::GetForProfile(profile); | |
| 152 | |
| 153 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920 | |
| 154 if (!content_suggestions_service) { | |
| 155 return; | 153 return; |
| 156 } | 154 } |
| 157 | 155 |
| 158 ntp_snippets::RemoteSuggestionsProvider* service = | 156 remote_suggestions_provider->scheduler()->Unschedule(); |
| 159 content_suggestions_service->ntp_snippets_service(); | 157 remote_suggestions_provider->scheduler()->Schedule(); |
| 160 | |
| 161 // Can be null if the feature has been disabled but the scheduler has not been | |
| 162 // unregistered yet. The next start should unregister it. | |
| 163 if (!service) { | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 service->RescheduleFetching(/*force=*/true); | |
| 168 } | 158 } |
| 169 | 159 |
| 170 static void OnSuggestionTargetVisited(JNIEnv* env, | 160 static void OnSuggestionTargetVisited(JNIEnv* env, |
| 171 const JavaParamRef<jclass>& caller, | 161 const JavaParamRef<jclass>& caller, |
| 172 jint j_category_id, | 162 jint j_category_id, |
| 173 jlong visit_time_ms) { | 163 jlong visit_time_ms) { |
| 174 Profile* profile = ProfileManager::GetLastUsedProfile(); | 164 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 175 ntp_snippets::ContentSuggestionsService* content_suggestions_service = | 165 ntp_snippets::ContentSuggestionsService* content_suggestions_service = |
| 176 ContentSuggestionsServiceFactory::GetForProfile(profile); | 166 ContentSuggestionsServiceFactory::GetForProfile(profile); |
| 177 ntp_snippets::metrics::OnSuggestionTargetVisited( | 167 ntp_snippets::metrics::OnSuggestionTargetVisited( |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 } | 466 } |
| 477 | 467 |
| 478 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { | 468 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { |
| 479 return content_suggestions_service_->category_factory()->FromIDValue(id); | 469 return content_suggestions_service_->category_factory()->FromIDValue(id); |
| 480 } | 470 } |
| 481 | 471 |
| 482 // static | 472 // static |
| 483 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 473 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 484 return RegisterNativesImpl(env); | 474 return RegisterNativesImpl(env); |
| 485 } | 475 } |
| OLD | NEW |