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

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

Issue 2608333004: Clarify that NTPSnippetsBridge holds a Java SnippetsBridge. (Closed)
Patch Set: Add null checks for the Java observer. Created 3 years, 11 months 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
« no previous file with comments | « chrome/browser/android/ntp/ntp_snippets_bridge.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // 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
104 if (!content_suggestions_service) { 104 if (!content_suggestions_service) {
105 return nullptr; 105 return nullptr;
106 } 106 }
107 return content_suggestions_service->remote_suggestions_scheduler(); 107 return content_suggestions_service->remote_suggestions_scheduler();
108 } 108 }
109 109
110 } // namespace 110 } // namespace
111 111
112 static jlong Init(JNIEnv* env, 112 static jlong Init(JNIEnv* env,
113 const JavaParamRef<jobject>& obj, 113 const JavaParamRef<jobject>& j_bridge,
114 const JavaParamRef<jobject>& j_profile) { 114 const JavaParamRef<jobject>& j_profile) {
115 NTPSnippetsBridge* snippets_bridge = new NTPSnippetsBridge(env, j_profile); 115 NTPSnippetsBridge* snippets_bridge =
116 new NTPSnippetsBridge(env, j_bridge, j_profile);
116 return reinterpret_cast<intptr_t>(snippets_bridge); 117 return reinterpret_cast<intptr_t>(snippets_bridge);
117 } 118 }
118 119
119 // Initiates a background fetch for remote suggestions. 120 // Initiates a background fetch for remote suggestions.
120 static void RemoteSuggestionsSchedulerOnFetchDue( 121 static void RemoteSuggestionsSchedulerOnFetchDue(
121 JNIEnv* env, 122 JNIEnv* env,
122 const JavaParamRef<jclass>& caller) { 123 const JavaParamRef<jclass>& caller) {
123 ntp_snippets::RemoteSuggestionsScheduler* scheduler = 124 ntp_snippets::RemoteSuggestionsScheduler* scheduler =
124 GetRemoteSuggestionsScheduler(); 125 GetRemoteSuggestionsScheduler();
125 if (!scheduler) { 126 if (!scheduler) {
(...skipping 22 matching lines...) Expand all
148 static void OnSuggestionTargetVisited(JNIEnv* env, 149 static void OnSuggestionTargetVisited(JNIEnv* env,
149 const JavaParamRef<jclass>& caller, 150 const JavaParamRef<jclass>& caller,
150 jint j_category_id, 151 jint j_category_id,
151 jlong visit_time_ms) { 152 jlong visit_time_ms) {
152 ntp_snippets::metrics::OnSuggestionTargetVisited( 153 ntp_snippets::metrics::OnSuggestionTargetVisited(
153 Category::FromIDValue(j_category_id), 154 Category::FromIDValue(j_category_id),
154 base::TimeDelta::FromMilliseconds(visit_time_ms)); 155 base::TimeDelta::FromMilliseconds(visit_time_ms));
155 } 156 }
156 157
157 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, 158 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env,
159 const JavaParamRef<jobject>& j_bridge,
158 const JavaParamRef<jobject>& j_profile) 160 const JavaParamRef<jobject>& j_profile)
159 : content_suggestions_service_observer_(this), weak_ptr_factory_(this) { 161 : content_suggestions_service_observer_(this),
162 bridge_(env, j_bridge),
163 weak_ptr_factory_(this) {
160 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); 164 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
161 content_suggestions_service_ = 165 content_suggestions_service_ =
162 ContentSuggestionsServiceFactory::GetForProfile(profile); 166 ContentSuggestionsServiceFactory::GetForProfile(profile);
163 history_service_ = 167 history_service_ =
164 HistoryServiceFactory::GetForProfile(profile, 168 HistoryServiceFactory::GetForProfile(profile,
165 ServiceAccessType::EXPLICIT_ACCESS); 169 ServiceAccessType::EXPLICIT_ACCESS);
166 content_suggestions_service_observer_.Add(content_suggestions_service_); 170 content_suggestions_service_observer_.Add(content_suggestions_service_);
167 } 171 }
168 172
169 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { 173 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) {
170 delete this; 174 delete this;
171 } 175 }
172 176
173 void NTPSnippetsBridge::SetObserver(JNIEnv* env,
174 const JavaParamRef<jobject>& obj,
175 const JavaParamRef<jobject>& j_observer) {
176 observer_.Reset(env, j_observer);
177 }
178
179 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories( 177 ScopedJavaLocalRef<jintArray> NTPSnippetsBridge::GetCategories(
180 JNIEnv* env, 178 JNIEnv* env,
181 const base::android::JavaParamRef<jobject>& obj) { 179 const base::android::JavaParamRef<jobject>& obj) {
182 std::vector<int> category_ids; 180 std::vector<int> category_ids;
183 for (Category category : content_suggestions_service_->GetCategories()) { 181 for (Category category : content_suggestions_service_->GetCategories()) {
184 category_ids.push_back(category.id()); 182 category_ids.push_back(category.id());
185 } 183 }
186 return ToJavaIntArray(env, category_ids); 184 return ToJavaIntArray(env, category_ids);
187 } 185 }
188 186
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 396
399 void NTPSnippetsBridge::OnNTPInitialized( 397 void NTPSnippetsBridge::OnNTPInitialized(
400 JNIEnv* env, 398 JNIEnv* env,
401 const base::android::JavaParamRef<jobject>& obj) { 399 const base::android::JavaParamRef<jobject>& obj) {
402 content_suggestions_service_->remote_suggestions_scheduler()->OnNTPOpened(); 400 content_suggestions_service_->remote_suggestions_scheduler()->OnNTPOpened();
403 } 401 }
404 402
405 NTPSnippetsBridge::~NTPSnippetsBridge() {} 403 NTPSnippetsBridge::~NTPSnippetsBridge() {}
406 404
407 void NTPSnippetsBridge::OnNewSuggestions(Category category) { 405 void NTPSnippetsBridge::OnNewSuggestions(Category category) {
408 if (observer_.is_null()) {
409 return;
410 }
411
412 JNIEnv* env = base::android::AttachCurrentThread(); 406 JNIEnv* env = base::android::AttachCurrentThread();
413 Java_SnippetsBridge_onNewSuggestions(env, observer_, 407 Java_SnippetsBridge_onNewSuggestions(env, bridge_,
414 static_cast<int>(category.id())); 408 static_cast<int>(category.id()));
415 } 409 }
416 410
417 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, 411 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category,
418 CategoryStatus new_status) { 412 CategoryStatus new_status) {
419 if (observer_.is_null()) {
420 return;
421 }
422
423 JNIEnv* env = base::android::AttachCurrentThread(); 413 JNIEnv* env = base::android::AttachCurrentThread();
424 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_, 414 Java_SnippetsBridge_onCategoryStatusChanged(env, bridge_,
425 static_cast<int>(category.id()), 415 static_cast<int>(category.id()),
426 static_cast<int>(new_status)); 416 static_cast<int>(new_status));
427 } 417 }
428 418
429 void NTPSnippetsBridge::OnSuggestionInvalidated( 419 void NTPSnippetsBridge::OnSuggestionInvalidated(
430 const ContentSuggestion::ID& suggestion_id) { 420 const ContentSuggestion::ID& suggestion_id) {
431 if (observer_.is_null()) {
432 return;
433 }
434
435 JNIEnv* env = base::android::AttachCurrentThread(); 421 JNIEnv* env = base::android::AttachCurrentThread();
436 Java_SnippetsBridge_onSuggestionInvalidated( 422 Java_SnippetsBridge_onSuggestionInvalidated(
437 env, observer_.obj(), static_cast<int>(suggestion_id.category().id()), 423 env, bridge_.obj(), static_cast<int>(suggestion_id.category().id()),
438 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); 424 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj());
439 } 425 }
440 426
441 void NTPSnippetsBridge::OnFullRefreshRequired() { 427 void NTPSnippetsBridge::OnFullRefreshRequired() {
442 if (observer_.is_null()) {
443 return;
444 }
445
446 JNIEnv* env = base::android::AttachCurrentThread(); 428 JNIEnv* env = base::android::AttachCurrentThread();
447 Java_SnippetsBridge_onFullRefreshRequired(env, observer_.obj()); 429 Java_SnippetsBridge_onFullRefreshRequired(env, bridge_.obj());
448 } 430 }
449 431
450 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { 432 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() {
451 observer_.Reset(); 433 bridge_.Reset();
452 content_suggestions_service_observer_.Remove(content_suggestions_service_); 434 content_suggestions_service_observer_.Remove(content_suggestions_service_);
453 } 435 }
454 436
455 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, 437 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback,
456 const gfx::Image& image) { 438 const gfx::Image& image) {
457 ScopedJavaLocalRef<jobject> j_bitmap; 439 ScopedJavaLocalRef<jobject> j_bitmap;
458 if (!image.IsEmpty()) { 440 if (!image.IsEmpty()) {
459 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); 441 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap());
460 } 442 }
461 base::android::RunCallbackAndroid(callback, j_bitmap); 443 base::android::RunCallbackAndroid(callback, j_bitmap);
462 } 444 }
463 445
464 void NTPSnippetsBridge::OnSuggestionsFetched( 446 void NTPSnippetsBridge::OnSuggestionsFetched(
465 Category category, 447 Category category,
466 ntp_snippets::Status status, 448 ntp_snippets::Status status,
467 std::vector<ContentSuggestion> suggestions) { 449 std::vector<ContentSuggestion> suggestions) {
468 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. 450 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status.
469 JNIEnv* env = AttachCurrentThread(); 451 JNIEnv* env = AttachCurrentThread();
470 Java_SnippetsBridge_onMoreSuggestions( 452 Java_SnippetsBridge_onMoreSuggestions(
471 env, observer_, category.id(), 453 env, bridge_, category.id(),
472 ToJavaSuggestionList(env, category, suggestions)); 454 ToJavaSuggestionList(env, category, suggestions));
473 } 455 }
474 456
475 // static 457 // static
476 bool NTPSnippetsBridge::Register(JNIEnv* env) { 458 bool NTPSnippetsBridge::Register(JNIEnv* env) {
477 return RegisterNativesImpl(env); 459 return RegisterNativesImpl(env);
478 } 460 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/ntp_snippets_bridge.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698