| 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 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const JavaParamRef<jclass>& caller) { | 34 const JavaParamRef<jclass>& caller) { |
| 35 Profile* profile = ProfileManager::GetLastUsedProfile(); | 35 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 36 NTPSnippetsServiceFactory::GetForProfile(profile)->FetchSnippets(); | 36 NTPSnippetsServiceFactory::GetForProfile(profile)->FetchSnippets(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, | 39 NTPSnippetsBridge::NTPSnippetsBridge(JNIEnv* env, |
| 40 const JavaParamRef<jobject>& j_profile) | 40 const JavaParamRef<jobject>& j_profile) |
| 41 : snippet_service_observer_(this) { | 41 : snippet_service_observer_(this) { |
| 42 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); | 42 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 43 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile); | 43 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetForProfile(profile); |
| 44 snippet_service_observer_.Add(ntp_snippets_service_); | |
| 45 } | 44 } |
| 46 | 45 |
| 47 void NTPSnippetsBridge::SetObserver(JNIEnv* env, | 46 void NTPSnippetsBridge::SetObserver(JNIEnv* env, |
| 48 const JavaParamRef<jobject>& obj, | 47 const JavaParamRef<jobject>& obj, |
| 49 const JavaParamRef<jobject>& j_observer) { | 48 const JavaParamRef<jobject>& j_observer) { |
| 50 observer_.Reset(env, j_observer); | 49 observer_.Reset(env, j_observer); |
| 51 NTPSnippetsServiceLoaded(); | 50 // This will call NTPSnippetsServiceLoaded. |
| 51 snippet_service_observer_.Add(ntp_snippets_service_); |
| 52 } | 52 } |
| 53 | 53 |
| 54 NTPSnippetsBridge::~NTPSnippetsBridge() {} | 54 NTPSnippetsBridge::~NTPSnippetsBridge() {} |
| 55 | 55 |
| 56 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 56 void NTPSnippetsBridge::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 57 delete this; | 57 delete this; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env, | 60 void NTPSnippetsBridge::DiscardSnippet(JNIEnv* env, |
| 61 const JavaParamRef<jobject>& obj, | 61 const JavaParamRef<jobject>& obj, |
| 62 const JavaParamRef<jstring>& url) { | 62 const JavaParamRef<jstring>& url) { |
| 63 ntp_snippets_service_->DiscardSnippet( | 63 ntp_snippets_service_->DiscardSnippet( |
| 64 GURL(ConvertJavaStringToUTF8(env, url))); | 64 GURL(ConvertJavaStringToUTF8(env, url))); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void NTPSnippetsBridge::NTPSnippetsServiceLoaded() { | 67 void NTPSnippetsBridge::NTPSnippetsServiceLoaded() { |
| 68 if (observer_.is_null()) | 68 DCHECK(!observer_.is_null()); |
| 69 return; | |
| 70 | 69 |
| 71 std::vector<std::string> titles; | 70 std::vector<std::string> titles; |
| 72 std::vector<std::string> urls; | 71 std::vector<std::string> urls; |
| 73 std::vector<std::string> thumbnail_urls; | 72 std::vector<std::string> thumbnail_urls; |
| 74 std::vector<std::string> snippets; | 73 std::vector<std::string> snippets; |
| 75 std::vector<int64_t> timestamps; | 74 std::vector<int64_t> timestamps; |
| 76 for (const ntp_snippets::NTPSnippet& snippet : *ntp_snippets_service_) { | 75 for (const ntp_snippets::NTPSnippet& snippet : *ntp_snippets_service_) { |
| 77 titles.push_back(snippet.title()); | 76 titles.push_back(snippet.title()); |
| 78 urls.push_back(snippet.url().spec()); | 77 urls.push_back(snippet.url().spec()); |
| 79 thumbnail_urls.push_back(snippet.salient_image_url().spec()); | 78 thumbnail_urls.push_back(snippet.salient_image_url().spec()); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 92 | 91 |
| 93 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { | 92 void NTPSnippetsBridge::NTPSnippetsServiceShutdown() { |
| 94 observer_.Reset(); | 93 observer_.Reset(); |
| 95 snippet_service_observer_.Remove(ntp_snippets_service_); | 94 snippet_service_observer_.Remove(ntp_snippets_service_); |
| 96 } | 95 } |
| 97 | 96 |
| 98 // static | 97 // static |
| 99 bool NTPSnippetsBridge::Register(JNIEnv* env) { | 98 bool NTPSnippetsBridge::Register(JNIEnv* env) { |
| 100 return RegisterNativesImpl(env); | 99 return RegisterNativesImpl(env); |
| 101 } | 100 } |
| OLD | NEW |