OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/most_visited_sites.h" | 5 #include "chrome/browser/android/most_visited_sites.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_array.h" | 8 #include "base/android/jni_array.h" |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "base/android/scoped_java_ref.h" | 10 #include "base/android/scoped_java_ref.h" |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | 91 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
92 env, j_callback->obj(), bitmap->obj()); | 92 env, j_callback->obj(), bitmap->obj()); |
93 } | 93 } |
94 | 94 |
95 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, | 95 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, |
96 const GURL& url) { | 96 const GURL& url) { |
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
98 top_sites->AddForcedURL(url, base::Time::Now()); | 98 top_sites->AddForcedURL(url, base::Time::Now()); |
99 } | 99 } |
100 | 100 |
101 void OnSuggestionsThumbnailAvailable( | |
102 ScopedJavaGlobalRef<jobject>* j_callback, | |
103 const GURL& url, | |
104 const SkBitmap* bitmap) { | |
105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
106 JNIEnv* env = AttachCurrentThread(); | |
107 | |
108 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | |
109 new ScopedJavaGlobalRef<jobject>(); | |
110 if (bitmap) { | |
111 j_bitmap_ref->Reset( | |
112 env, | |
113 gfx::ConvertToJavaBitmap(bitmap).obj()); | |
114 } | |
115 | |
116 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | |
117 env, j_callback->obj(), j_bitmap_ref->obj()); | |
118 } | |
119 | |
120 void GetSuggestionsThumbnailOnUIThread( | |
121 const GURL& gurl, | |
Ted C
2014/05/28 00:21:59
+2 for the param lines
Mathieu
2014/05/28 02:48:17
Done.
| |
122 SuggestionsService* suggestions_service, | |
123 ScopedJavaGlobalRef<jobject>* j_callback) { | |
124 suggestions_service->GetPageThumbnail(gurl, | |
125 base::Bind(&OnSuggestionsThumbnailAvailable, | |
126 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback)))); | |
127 } | |
128 | |
101 void GetUrlThumbnailTask( | 129 void GetUrlThumbnailTask( |
102 std::string url_string, | 130 std::string url_string, |
103 scoped_refptr<TopSites> top_sites, | 131 scoped_refptr<TopSites> top_sites, |
132 Profile* profile, | |
104 ScopedJavaGlobalRef<jobject>* j_callback) { | 133 ScopedJavaGlobalRef<jobject>* j_callback) { |
105 JNIEnv* env = AttachCurrentThread(); | 134 JNIEnv* env = AttachCurrentThread(); |
106 | 135 |
107 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | 136 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = |
108 new ScopedJavaGlobalRef<jobject>(); | 137 new ScopedJavaGlobalRef<jobject>(); |
109 | 138 |
110 GURL gurl(url_string); | 139 GURL gurl(url_string); |
111 | 140 |
112 scoped_refptr<base::RefCountedMemory> data; | 141 scoped_refptr<base::RefCountedMemory> data; |
113 if (top_sites->GetPageThumbnail(gurl, false, &data)) { | 142 if (top_sites->GetPageThumbnail(gurl, false, &data)) { |
114 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); | 143 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); |
115 if (!thumbnail_bitmap.empty()) { | 144 if (!thumbnail_bitmap.empty()) { |
116 j_bitmap_ref->Reset( | 145 j_bitmap_ref->Reset( |
117 env, | 146 env, |
118 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); | 147 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); |
119 } | 148 } |
120 } else { | 149 } else { |
121 // A thumbnail is not locally available for |gurl|. Make sure it is put in | 150 // A thumbnail is not locally available for |gurl|. Make sure it is put in |
122 // the list to be fetched at the next visit to this site. | 151 // the list to be fetched at the next visit to this site. |
123 BrowserThread::PostTask( | 152 BrowserThread::PostTask( |
124 BrowserThread::UI, FROM_HERE, | 153 BrowserThread::UI, FROM_HERE, |
125 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); | 154 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); |
155 | |
156 SuggestionsService* service = | |
157 SuggestionsServiceFactory::GetForProfile(profile); | |
Ted C
2014/05/28 00:21:59
BrowserContextKeyedBaseFactory is NonThreadSafe, s
Mathieu
2014/05/28 02:48:17
Ah, thanks. Passing the profile pointer instead sh
Ted C
2014/05/28 17:45:38
I don't think profile is thread safe either. And
| |
158 if (service) { | |
159 BrowserThread::PostTask( | |
160 BrowserThread::UI, FROM_HERE, | |
161 base::Bind(GetSuggestionsThumbnailOnUIThread, gurl, service, | |
162 base::Owned( | |
163 new ScopedJavaGlobalRef<jobject>(*j_callback)))); | |
164 return; | |
165 } | |
126 } | 166 } |
127 | 167 |
128 // Since j_callback is owned by this callback, when the callback falls out of | 168 // Since j_callback is owned by this callback, when the callback falls out of |
129 // scope it will be deleted. We need to pass ownership to the next callback. | 169 // scope it will be deleted. We need to pass ownership to the next callback. |
130 ScopedJavaGlobalRef<jobject>* j_callback_pass = | 170 ScopedJavaGlobalRef<jobject>* j_callback_pass = |
131 new ScopedJavaGlobalRef<jobject>(*j_callback); | 171 new ScopedJavaGlobalRef<jobject>(*j_callback); |
132 BrowserThread::PostTask( | 172 BrowserThread::PostTask( |
133 BrowserThread::UI, FROM_HERE, | 173 BrowserThread::UI, FROM_HERE, |
134 base::Bind( | 174 base::Bind( |
135 &OnObtainedThumbnail, | 175 &OnObtainedThumbnail, |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 ScopedJavaGlobalRef<jobject>* j_callback = | 224 ScopedJavaGlobalRef<jobject>* j_callback = |
185 new ScopedJavaGlobalRef<jobject>(); | 225 new ScopedJavaGlobalRef<jobject>(); |
186 j_callback->Reset(env, j_callback_obj); | 226 j_callback->Reset(env, j_callback_obj); |
187 | 227 |
188 std::string url_string = ConvertJavaStringToUTF8(env, url); | 228 std::string url_string = ConvertJavaStringToUTF8(env, url); |
189 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 229 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); |
190 BrowserThread::PostTask( | 230 BrowserThread::PostTask( |
191 BrowserThread::DB, FROM_HERE, base::Bind( | 231 BrowserThread::DB, FROM_HERE, base::Bind( |
192 &GetUrlThumbnailTask, | 232 &GetUrlThumbnailTask, |
193 url_string, | 233 url_string, |
194 top_sites, base::Owned(j_callback))); | 234 top_sites, profile_, base::Owned(j_callback))); |
195 } | 235 } |
196 | 236 |
197 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 237 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
198 jobject obj, | 238 jobject obj, |
199 jstring j_url) { | 239 jstring j_url) { |
200 TopSites* top_sites = profile_->GetTopSites(); | 240 TopSites* top_sites = profile_->GetTopSites(); |
201 if (!top_sites) | 241 if (!top_sites) |
202 return; | 242 return; |
203 | 243 |
204 std::string url_string = ConvertJavaStringToUTF8(env, j_url); | 244 std::string url_string = ConvertJavaStringToUTF8(env, j_url); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
273 j_observer->obj(), | 313 j_observer->obj(), |
274 ToJavaArrayOfStrings(env, titles).obj(), | 314 ToJavaArrayOfStrings(env, titles).obj(), |
275 ToJavaArrayOfStrings(env, urls).obj()); | 315 ToJavaArrayOfStrings(env, urls).obj()); |
276 } | 316 } |
277 | 317 |
278 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 318 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
279 MostVisitedSites* most_visited_sites = | 319 MostVisitedSites* most_visited_sites = |
280 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 320 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
281 return reinterpret_cast<intptr_t>(most_visited_sites); | 321 return reinterpret_cast<intptr_t>(most_visited_sites); |
282 } | 322 } |
OLD | NEW |