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

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
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
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->OnPersistentSchedulerWakeUp();
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 ntp_snippets::metrics::OnSuggestionTargetVisited( 153 ntp_snippets::metrics::OnSuggestionTargetVisited(
177 Category::FromIDValue(j_category_id), 154 Category::FromIDValue(j_category_id),
178 base::TimeDelta::FromMilliseconds(visit_time_ms)); 155 base::TimeDelta::FromMilliseconds(visit_time_ms));
179 } 156 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 &known_suggestion_ids); 246 &known_suggestion_ids);
270 247
271 Category category = Category::FromIDValue(j_category_id); 248 Category category = Category::FromIDValue(j_category_id);
272 content_suggestions_service_->Fetch( 249 content_suggestions_service_->Fetch(
273 category, std::set<std::string>(known_suggestion_ids.begin(), 250 category, std::set<std::string>(known_suggestion_ids.begin(),
274 known_suggestion_ids.end()), 251 known_suggestion_ids.end()),
275 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched, 252 base::Bind(&NTPSnippetsBridge::OnSuggestionsFetched,
276 weak_ptr_factory_.GetWeakPtr(), category)); 253 weak_ptr_factory_.GetWeakPtr(), category));
277 } 254 }
278 255
256 void NTPSnippetsBridge::ReloadSuggestions(
257 JNIEnv* env,
258 const base::android::JavaParamRef<jobject>& obj) {
259 content_suggestions_service_->ReloadSuggestions();
260 }
261
279 void NTPSnippetsBridge::DismissSuggestion( 262 void NTPSnippetsBridge::DismissSuggestion(
280 JNIEnv* env, 263 JNIEnv* env,
281 const JavaParamRef<jobject>& obj, 264 const JavaParamRef<jobject>& obj,
282 const JavaParamRef<jstring>& jurl, 265 const JavaParamRef<jstring>& jurl,
283 jint global_position, 266 jint global_position,
284 jint j_category_id, 267 jint j_category_id,
285 jint category_position, 268 jint category_position,
286 const JavaParamRef<jstring>& id_within_category) { 269 const JavaParamRef<jstring>& id_within_category) {
287 Category category = Category::FromIDValue(j_category_id); 270 Category category = Category::FromIDValue(j_category_id);
288 271
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
481 JNIEnv* env = AttachCurrentThread(); 464 JNIEnv* env = AttachCurrentThread();
482 Java_SnippetsBridge_onMoreSuggestions( 465 Java_SnippetsBridge_onMoreSuggestions(
483 env, observer_, category.id(), 466 env, observer_, category.id(),
484 ToJavaSuggestionList(env, category, suggestions)); 467 ToJavaSuggestionList(env, category, suggestions));
485 } 468 }
486 469
487 // static 470 // static
488 bool NTPSnippetsBridge::Register(JNIEnv* env) { 471 bool NTPSnippetsBridge::Register(JNIEnv* env) {
489 return RegisterNativesImpl(env); 472 return RegisterNativesImpl(env);
490 } 473 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698