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

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

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Rebase Created 4 years 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/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 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 env, java_suggestion, 87 env, java_suggestion,
88 ConvertUTF8ToJavaString( 88 ConvertUTF8ToJavaString(
89 env, suggestion.recent_tab_suggestion_extra()->tab_id), 89 env, suggestion.recent_tab_suggestion_extra()->tab_id),
90 suggestion.recent_tab_suggestion_extra()->offline_page_id); 90 suggestion.recent_tab_suggestion_extra()->offline_page_id);
91 } 91 }
92 } 92 }
93 93
94 return result; 94 return result;
95 } 95 }
96 96
97 ntp_snippets::RemoteSuggestionsProvider* GetRemoteSuggestionsProvider() { 97 ntp_snippets::PersistentScheduler::Listener* GetPersistentSchedulerListener() {
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->persistent_scheduler_listener();
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 // Initiates a background fetch for remote suggestions.
118 const JavaParamRef<jclass>& caller) { 118 static void PersistentSchedulerListenerOnFetchDue(
119 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider = 119 JNIEnv* env,
120 GetRemoteSuggestionsProvider(); 120 const JavaParamRef<jclass>& caller) {
121 ntp_snippets::PersistentScheduler::Listener* listener =
122 GetPersistentSchedulerListener();
121 // Can be null if the feature has been disabled but the scheduler has not been 123 // Can be null if the feature has been disabled but the scheduler has not been
122 // unregistered yet. The next start should unregister it. 124 // unregistered yet. The next start should unregister it.
123 if (!remote_suggestions_provider) { 125 if (!listener) {
124 return; 126 return;
125 } 127 }
126 remote_suggestions_provider->FetchSnippetsForAllCategories();
127 }
128 128
129 static void FetchRemoteSuggestionsInTheBackground( 129 listener->OnFetchDue();
130 JNIEnv* env,
131 const JavaParamRef<jclass>& caller) {
132 ntp_snippets::RemoteSuggestionsProvider* remote_suggestions_provider =
133 GetRemoteSuggestionsProvider();
134 // 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 if (!remote_suggestions_provider) {
137 return;
138 }
139 remote_suggestions_provider->FetchSnippetsInTheBackground();
140 } 130 }
141 131
142 // Reschedules the fetching of snippets. If tasks are already scheduled, they 132 // Reschedules the fetching of snippets. If tasks are already scheduled, they
143 // will be rescheduled anyway, so all running intervals will be reset. 133 // will be rescheduled anyway, so all running intervals will be reset.
144 static void RescheduleFetching(JNIEnv* env, 134 static void PersistentSchedulerListenerRescheduleFetching(
145 const JavaParamRef<jclass>& caller) { 135 JNIEnv* env,
146 Profile* profile = ProfileManager::GetLastUsedProfile(); 136 const JavaParamRef<jclass>& caller) {
147 // Temporary check while investigating crbug.com/647920. 137 ntp_snippets::PersistentScheduler::Listener* listener =
148 CHECK(profile); 138 GetPersistentSchedulerListener();
149 139 // Can be null if the feature has been disabled but the scheduler has not been
150 ntp_snippets::ContentSuggestionsService* content_suggestions_service = 140 // unregistered yet. The next start should unregister it.
151 ContentSuggestionsServiceFactory::GetForProfile(profile); 141 if (!listener) {
152
153 // Can maybe be null in some cases? (Incognito profile?) crbug.com/647920
154 if (!content_suggestions_service) {
155 return; 142 return;
156 } 143 }
157 144
158 ntp_snippets::RemoteSuggestionsProvider* service = 145 listener->RescheduleFetching();
159 content_suggestions_service->ntp_snippets_service();
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 } 146 }
169 147
170 static void OnSuggestionTargetVisited(JNIEnv* env, 148 static void OnSuggestionTargetVisited(JNIEnv* env,
171 const JavaParamRef<jclass>& caller, 149 const JavaParamRef<jclass>& caller,
172 jint j_category_id, 150 jint j_category_id,
173 jlong visit_time_ms) { 151 jlong visit_time_ms) {
174 Profile* profile = ProfileManager::GetLastUsedProfile(); 152 Profile* profile = ProfileManager::GetLastUsedProfile();
175 ntp_snippets::ContentSuggestionsService* content_suggestions_service = 153 ntp_snippets::ContentSuggestionsService* content_suggestions_service =
176 ContentSuggestionsServiceFactory::GetForProfile(profile); 154 ContentSuggestionsServiceFactory::GetForProfile(profile);
177 ntp_snippets::metrics::OnSuggestionTargetVisited( 155 ntp_snippets::metrics::OnSuggestionTargetVisited(
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 &known_suggestion_ids); 249 &known_suggestion_ids);
272 250
273 Category category = CategoryFromIDValue(j_category_id); 251 Category category = CategoryFromIDValue(j_category_id);
274 content_suggestions_service_->Fetch( 252 content_suggestions_service_->Fetch(
275 category, std::set<std::string>(known_suggestion_ids.begin(), 253 category, std::set<std::string>(known_suggestion_ids.begin(),
276 known_suggestion_ids.end()), 254 known_suggestion_ids.end()),
277 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, 255 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched,
278 weak_ptr_factory_.GetWeakPtr(), category)); 256 weak_ptr_factory_.GetWeakPtr(), category));
279 } 257 }
280 258
259 void NTPSnippetsBridge::ReloadSuggestions(
260 JNIEnv* env,
261 const base::android::JavaParamRef<jobject>& obj) {
262 content_suggestions_service_->ReloadSuggestions();
263 }
264
281 void NTPSnippetsBridge::DismissSuggestion( 265 void NTPSnippetsBridge::DismissSuggestion(
282 JNIEnv* env, 266 JNIEnv* env,
283 const JavaParamRef<jobject>& obj, 267 const JavaParamRef<jobject>& obj,
284 const JavaParamRef<jstring>& jurl, 268 const JavaParamRef<jstring>& jurl,
285 jint global_position, 269 jint global_position,
286 jint j_category_id, 270 jint j_category_id,
287 jint category_position, 271 jint category_position,
288 const JavaParamRef<jstring>& id_within_category) { 272 const JavaParamRef<jstring>& id_within_category) {
289 Category category = CategoryFromIDValue(j_category_id); 273 Category category = CategoryFromIDValue(j_category_id);
290 274
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 } 460 }
477 461
478 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) { 462 Category NTPSnippetsBridge::CategoryFromIDValue(jint id) {
479 return content_suggestions_service_->category_factory()->FromIDValue(id); 463 return content_suggestions_service_->category_factory()->FromIDValue(id);
480 } 464 }
481 465
482 // static 466 // static
483 bool NTPSnippetsBridge::Register(JNIEnv* env) { 467 bool NTPSnippetsBridge::Register(JNIEnv* env) {
484 return RegisterNativesImpl(env); 468 return RegisterNativesImpl(env);
485 } 469 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698