Chromium Code Reviews| 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" |
| 11 #include "base/callback.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/history/history_types.h" | 15 #include "chrome/browser/history/history_types.h" |
| 15 #include "chrome/browser/history/top_sites.h" | 16 #include "chrome/browser/history/top_sites.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_android.h" | 18 #include "chrome/browser/profiles/profile_android.h" |
| 18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 19 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 19 #include "chrome/browser/search/suggestions/suggestions_service.h" | 20 #include "chrome/browser/search/suggestions/suggestions_service.h" |
| 20 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 21 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | 92 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 92 env, j_callback->obj(), bitmap->obj()); | 93 env, j_callback->obj(), bitmap->obj()); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, | 96 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, |
| 96 const GURL& url) { | 97 const GURL& url) { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 top_sites->AddForcedURL(url, base::Time::Now()); | 99 top_sites->AddForcedURL(url, base::Time::Now()); |
| 99 } | 100 } |
| 100 | 101 |
| 102 void OnSuggestionsThumbnailAvailable( | |
| 103 ScopedJavaGlobalRef<jobject>* j_callback, | |
| 104 const GURL& url, | |
| 105 const SkBitmap* bitmap) { | |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 107 JNIEnv* env = AttachCurrentThread(); | |
| 108 | |
| 109 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | |
| 110 new ScopedJavaGlobalRef<jobject>(); | |
| 111 if (bitmap) { | |
| 112 j_bitmap_ref->Reset( | |
| 113 env, | |
| 114 gfx::ConvertToJavaBitmap(bitmap).obj()); | |
| 115 } | |
| 116 | |
| 117 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | |
| 118 env, j_callback->obj(), j_bitmap_ref->obj()); | |
| 119 } | |
| 120 | |
| 121 // Runs on the DB thread. | |
| 101 void GetUrlThumbnailTask( | 122 void GetUrlThumbnailTask( |
| 102 std::string url_string, | 123 std::string url_string, |
| 103 scoped_refptr<TopSites> top_sites, | 124 scoped_refptr<TopSites> top_sites, |
| 104 ScopedJavaGlobalRef<jobject>* j_callback) { | 125 ScopedJavaGlobalRef<jobject>* j_callback, |
| 126 base::Closure lookup_failed_ui_callback) { | |
| 105 JNIEnv* env = AttachCurrentThread(); | 127 JNIEnv* env = AttachCurrentThread(); |
| 106 | 128 |
| 107 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | 129 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = |
| 108 new ScopedJavaGlobalRef<jobject>(); | 130 new ScopedJavaGlobalRef<jobject>(); |
| 109 | 131 |
| 110 GURL gurl(url_string); | 132 GURL gurl(url_string); |
| 111 | 133 |
| 112 scoped_refptr<base::RefCountedMemory> data; | 134 scoped_refptr<base::RefCountedMemory> data; |
| 113 if (top_sites->GetPageThumbnail(gurl, false, &data)) { | 135 if (top_sites->GetPageThumbnail(gurl, false, &data)) { |
| 114 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); | 136 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); |
| 115 if (!thumbnail_bitmap.empty()) { | 137 if (!thumbnail_bitmap.empty()) { |
| 116 j_bitmap_ref->Reset( | 138 j_bitmap_ref->Reset( |
| 117 env, | 139 env, |
| 118 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); | 140 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); |
| 119 } | 141 } |
| 120 } else { | 142 } else { |
| 121 // A thumbnail is not locally available for |gurl|. Make sure it is put in | 143 // 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. | 144 // the list to be fetched at the next visit to this site. |
| 123 BrowserThread::PostTask( | 145 BrowserThread::PostTask( |
| 124 BrowserThread::UI, FROM_HERE, | 146 BrowserThread::UI, FROM_HERE, |
| 125 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); | 147 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); |
| 148 | |
| 149 // If appropriate, return on the UI thread to execute the proper callback. | |
| 150 if (!lookup_failed_ui_callback.is_null()) { | |
| 151 BrowserThread::PostTask( | |
| 152 BrowserThread::UI, FROM_HERE, lookup_failed_ui_callback); | |
| 153 return; | |
| 154 } | |
| 126 } | 155 } |
| 127 | 156 |
| 128 // Since j_callback is owned by this callback, when the callback falls out of | 157 // 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. | 158 // scope it will be deleted. We need to pass ownership to the next callback. |
| 130 ScopedJavaGlobalRef<jobject>* j_callback_pass = | 159 ScopedJavaGlobalRef<jobject>* j_callback_pass = |
| 131 new ScopedJavaGlobalRef<jobject>(*j_callback); | 160 new ScopedJavaGlobalRef<jobject>(*j_callback); |
| 132 BrowserThread::PostTask( | 161 BrowserThread::PostTask( |
| 133 BrowserThread::UI, FROM_HERE, | 162 BrowserThread::UI, FROM_HERE, |
| 134 base::Bind( | 163 base::Bind( |
| 135 &OnObtainedThumbnail, | 164 &OnObtainedThumbnail, |
| 136 base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); | 165 base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); |
| 137 } | 166 } |
| 138 | 167 |
| 168 void GetSuggestionsThumbnailOnUIThread( | |
| 169 SuggestionsService* suggestions_service, | |
| 170 const std::string& url_string, | |
| 171 ScopedJavaGlobalRef<jobject>* j_callback) { | |
| 172 suggestions_service->GetPageThumbnail( | |
| 173 GURL(url_string), | |
| 174 base::Bind(&OnSuggestionsThumbnailAvailable, | |
| 175 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback)))); | |
| 176 } | |
| 177 | |
| 139 } // namespace | 178 } // namespace |
| 140 | 179 |
| 141 MostVisitedSites::MostVisitedSites(Profile* profile) | 180 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 142 : profile_(profile), num_sites_(0), weak_ptr_factory_(this) { | 181 : profile_(profile), num_sites_(0), weak_ptr_factory_(this) { |
| 143 // Register the debugging page for the Suggestions Service and the thumbnails | 182 // Register the debugging page for the Suggestions Service and the thumbnails |
| 144 // debugging page. | 183 // debugging page. |
| 145 content::URLDataSource::Add(profile_, | 184 content::URLDataSource::Add(profile_, |
| 146 new suggestions::SuggestionsSource(profile_)); | 185 new suggestions::SuggestionsSource(profile_)); |
| 147 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 186 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
| 148 } | 187 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 169 // force an update now. | 208 // force an update now. |
| 170 top_sites->SyncWithHistory(); | 209 top_sites->SyncWithHistory(); |
| 171 | 210 |
| 172 // Register for notification when TopSites changes so that we can update | 211 // Register for notification when TopSites changes so that we can update |
| 173 // ourself. | 212 // ourself. |
| 174 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 213 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 175 content::Source<history::TopSites>(top_sites)); | 214 content::Source<history::TopSites>(top_sites)); |
| 176 } | 215 } |
| 177 } | 216 } |
| 178 | 217 |
| 179 // May be called from any thread | 218 // Called from the UI Thread. |
|
Ted C
2014/06/02 22:36:26
Should we DCHECK we are on the UI thread here now?
Mathieu
2014/06/03 15:01:55
Done.
| |
| 180 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 219 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
| 181 jobject obj, | 220 jobject obj, |
| 182 jstring url, | 221 jstring url, |
| 183 jobject j_callback_obj) { | 222 jobject j_callback_obj) { |
| 184 ScopedJavaGlobalRef<jobject>* j_callback = | 223 ScopedJavaGlobalRef<jobject>* j_callback = |
| 185 new ScopedJavaGlobalRef<jobject>(); | 224 new ScopedJavaGlobalRef<jobject>(); |
| 186 j_callback->Reset(env, j_callback_obj); | 225 j_callback->Reset(env, j_callback_obj); |
| 187 | 226 |
| 188 std::string url_string = ConvertJavaStringToUTF8(env, url); | 227 std::string url_string = ConvertJavaStringToUTF8(env, url); |
| 189 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 228 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); |
| 229 | |
| 230 // If the Suggestions service is enabled, create a callback to fetch a | |
| 231 // server thumbnail from it, in case the local thumbnail is not found. | |
| 232 SuggestionsService* suggestions_service = | |
| 233 SuggestionsServiceFactory::GetForProfile(profile_); | |
| 234 base::Closure lookup_failed_callback = suggestions_service ? | |
| 235 base::Bind(&GetSuggestionsThumbnailOnUIThread, | |
|
Ted C
2014/06/02 22:36:26
Out of curiosity, would it be better to batch all
Mathieu
2014/06/03 15:01:55
My next CL is going to be implementing local cachi
| |
| 236 suggestions_service, url_string, | |
| 237 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : | |
| 238 base::Closure(); | |
| 190 BrowserThread::PostTask( | 239 BrowserThread::PostTask( |
| 191 BrowserThread::DB, FROM_HERE, base::Bind( | 240 BrowserThread::DB, FROM_HERE, |
| 192 &GetUrlThumbnailTask, | 241 base::Bind( |
| 193 url_string, | 242 &GetUrlThumbnailTask, url_string, top_sites, |
| 194 top_sites, base::Owned(j_callback))); | 243 base::Owned(j_callback), lookup_failed_callback)); |
| 195 } | 244 } |
| 196 | 245 |
| 197 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 246 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
| 198 jobject obj, | 247 jobject obj, |
| 199 jstring j_url) { | 248 jstring j_url) { |
| 200 TopSites* top_sites = profile_->GetTopSites(); | 249 TopSites* top_sites = profile_->GetTopSites(); |
| 201 if (!top_sites) | 250 if (!top_sites) |
| 202 return; | 251 return; |
| 203 | 252 |
| 204 std::string url_string = ConvertJavaStringToUTF8(env, j_url); | 253 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(), | 322 j_observer->obj(), |
| 274 ToJavaArrayOfStrings(env, titles).obj(), | 323 ToJavaArrayOfStrings(env, titles).obj(), |
| 275 ToJavaArrayOfStrings(env, urls).obj()); | 324 ToJavaArrayOfStrings(env, urls).obj()); |
| 276 } | 325 } |
| 277 | 326 |
| 278 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 327 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 279 MostVisitedSites* most_visited_sites = | 328 MostVisitedSites* most_visited_sites = |
| 280 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 329 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 281 return reinterpret_cast<intptr_t>(most_visited_sites); | 330 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 282 } | 331 } |
| OLD | NEW |