Chromium Code Reviews| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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()) { | 406 if (bridge_.is_null()) { |
|
Bernhard Bauer
2017/01/04 17:27:34
Hm, we now know that |bridge_| is non-null, right?
Michael van Ouwerkerk
2017/01/05 11:35:40
I think it could be, as it is reset in ContentSugg
Bernhard Bauer
2017/01/05 11:57:47
But in that case we shouldn't get called back anym
Michael van Ouwerkerk
2017/01/05 12:15:55
Done.
| |
| 409 return; | 407 return; |
| 410 } | 408 } |
| 411 | 409 |
| 412 JNIEnv* env = base::android::AttachCurrentThread(); | 410 JNIEnv* env = base::android::AttachCurrentThread(); |
| 413 Java_SnippetsBridge_onNewSuggestions(env, observer_, | 411 Java_SnippetsBridge_onNewSuggestions(env, bridge_, |
| 414 static_cast<int>(category.id())); | 412 static_cast<int>(category.id())); |
| 415 } | 413 } |
| 416 | 414 |
| 417 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, | 415 void NTPSnippetsBridge::OnCategoryStatusChanged(Category category, |
| 418 CategoryStatus new_status) { | 416 CategoryStatus new_status) { |
| 419 if (observer_.is_null()) { | 417 if (bridge_.is_null()) { |
| 420 return; | 418 return; |
| 421 } | 419 } |
| 422 | 420 |
| 423 JNIEnv* env = base::android::AttachCurrentThread(); | 421 JNIEnv* env = base::android::AttachCurrentThread(); |
| 424 Java_SnippetsBridge_onCategoryStatusChanged(env, observer_, | 422 Java_SnippetsBridge_onCategoryStatusChanged(env, bridge_, |
| 425 static_cast<int>(category.id()), | 423 static_cast<int>(category.id()), |
| 426 static_cast<int>(new_status)); | 424 static_cast<int>(new_status)); |
| 427 } | 425 } |
| 428 | 426 |
| 429 void NTPSnippetsBridge::OnSuggestionInvalidated( | 427 void NTPSnippetsBridge::OnSuggestionInvalidated( |
| 430 const ContentSuggestion::ID& suggestion_id) { | 428 const ContentSuggestion::ID& suggestion_id) { |
| 431 if (observer_.is_null()) { | 429 if (bridge_.is_null()) { |
| 432 return; | 430 return; |
| 433 } | 431 } |
| 434 | 432 |
| 435 JNIEnv* env = base::android::AttachCurrentThread(); | 433 JNIEnv* env = base::android::AttachCurrentThread(); |
| 436 Java_SnippetsBridge_onSuggestionInvalidated( | 434 Java_SnippetsBridge_onSuggestionInvalidated( |
| 437 env, observer_.obj(), static_cast<int>(suggestion_id.category().id()), | 435 env, bridge_.obj(), static_cast<int>(suggestion_id.category().id()), |
| 438 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); | 436 ConvertUTF8ToJavaString(env, suggestion_id.id_within_category()).obj()); |
| 439 } | 437 } |
| 440 | 438 |
| 441 void NTPSnippetsBridge::OnFullRefreshRequired() { | 439 void NTPSnippetsBridge::OnFullRefreshRequired() { |
| 442 if (observer_.is_null()) { | 440 if (bridge_.is_null()) { |
| 443 return; | 441 return; |
| 444 } | 442 } |
| 445 | 443 |
| 446 JNIEnv* env = base::android::AttachCurrentThread(); | 444 JNIEnv* env = base::android::AttachCurrentThread(); |
| 447 Java_SnippetsBridge_onFullRefreshRequired(env, observer_.obj()); | 445 Java_SnippetsBridge_onFullRefreshRequired(env, bridge_.obj()); |
| 448 } | 446 } |
| 449 | 447 |
| 450 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { | 448 void NTPSnippetsBridge::ContentSuggestionsServiceShutdown() { |
| 451 observer_.Reset(); | 449 bridge_.Reset(); |
| 452 content_suggestions_service_observer_.Remove(content_suggestions_service_); | 450 content_suggestions_service_observer_.Remove(content_suggestions_service_); |
| 453 } | 451 } |
| 454 | 452 |
| 455 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, | 453 void NTPSnippetsBridge::OnImageFetched(ScopedJavaGlobalRef<jobject> callback, |
| 456 const gfx::Image& image) { | 454 const gfx::Image& image) { |
| 457 ScopedJavaLocalRef<jobject> j_bitmap; | 455 ScopedJavaLocalRef<jobject> j_bitmap; |
| 458 if (!image.IsEmpty()) { | 456 if (!image.IsEmpty()) { |
| 459 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); | 457 j_bitmap = gfx::ConvertToJavaBitmap(image.ToSkBitmap()); |
| 460 } | 458 } |
| 461 base::android::RunCallbackAndroid(callback, j_bitmap); | 459 base::android::RunCallbackAndroid(callback, j_bitmap); |
| 462 } | 460 } |
| 463 | 461 |
| 464 void NTPSnippetsBridge::OnSuggestionsFetched( | 462 void NTPSnippetsBridge::OnSuggestionsFetched( |
| 465 Category category, | 463 Category category, |
| 466 ntp_snippets::Status status, | 464 ntp_snippets::Status status, |
| 467 std::vector<ContentSuggestion> suggestions) { | 465 std::vector<ContentSuggestion> suggestions) { |
| 468 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. | 466 // TODO(fhorschig, dgn): Allow refetch or show notification acc. to status. |
| 469 JNIEnv* env = AttachCurrentThread(); | 467 JNIEnv* env = AttachCurrentThread(); |
| 470 Java_SnippetsBridge_onMoreSuggestions( | 468 Java_SnippetsBridge_onMoreSuggestions( |
| 471 env, observer_, category.id(), | 469 env, bridge_, category.id(), |
| 472 ToJavaSuggestionList(env, category, suggestions)); | 470 ToJavaSuggestionList(env, category, suggestions)); |
| 473 } | 471 } |
| 474 | 472 |
| 475 // static | 473 // static |
| 476 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 474 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 477 return RegisterNativesImpl(env); | 475 return RegisterNativesImpl(env); |
| 478 } | 476 } |
| OLD | NEW |