| 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 <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 14 #include "base/callback.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 16 #include "chrome/browser/chrome_notification_types.h" | 17 #include "chrome/browser/chrome_notification_types.h" |
| 17 #include "chrome/browser/history/top_sites.h" | 18 #include "chrome/browser/history/top_sites.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/profiles/profile_android.h" | 20 #include "chrome/browser/profiles/profile_android.h" |
| 20 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 21 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 21 #include "chrome/browser/search/suggestions/suggestions_service.h" | 22 #include "chrome/browser/search/suggestions/suggestions_service.h" |
| 22 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 23 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 23 #include "chrome/browser/search/suggestions/suggestions_source.h" | 24 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | 78 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 78 env, j_callback->obj(), bitmap->obj()); | 79 env, j_callback->obj(), bitmap->obj()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, | 82 void AddForcedURLOnUIThread(scoped_refptr<history::TopSites> top_sites, |
| 82 const GURL& url) { | 83 const GURL& url) { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 84 top_sites->AddForcedURL(url, base::Time::Now()); | 85 top_sites->AddForcedURL(url, base::Time::Now()); |
| 85 } | 86 } |
| 86 | 87 |
| 88 void OnSuggestionsThumbnailAvailable( |
| 89 ScopedJavaGlobalRef<jobject>* j_callback, |
| 90 const GURL& url, |
| 91 const SkBitmap* bitmap) { |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 93 JNIEnv* env = AttachCurrentThread(); |
| 94 |
| 95 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = |
| 96 new ScopedJavaGlobalRef<jobject>(); |
| 97 if (bitmap) { |
| 98 j_bitmap_ref->Reset( |
| 99 env, |
| 100 gfx::ConvertToJavaBitmap(bitmap).obj()); |
| 101 } |
| 102 |
| 103 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 104 env, j_callback->obj(), j_bitmap_ref->obj()); |
| 105 } |
| 106 |
| 107 // Runs on the DB thread. |
| 87 void GetUrlThumbnailTask( | 108 void GetUrlThumbnailTask( |
| 88 std::string url_string, | 109 std::string url_string, |
| 89 scoped_refptr<TopSites> top_sites, | 110 scoped_refptr<TopSites> top_sites, |
| 90 ScopedJavaGlobalRef<jobject>* j_callback) { | 111 ScopedJavaGlobalRef<jobject>* j_callback, |
| 112 base::Closure lookup_failed_ui_callback) { |
| 91 JNIEnv* env = AttachCurrentThread(); | 113 JNIEnv* env = AttachCurrentThread(); |
| 92 | 114 |
| 93 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = | 115 ScopedJavaGlobalRef<jobject>* j_bitmap_ref = |
| 94 new ScopedJavaGlobalRef<jobject>(); | 116 new ScopedJavaGlobalRef<jobject>(); |
| 95 | 117 |
| 96 GURL gurl(url_string); | 118 GURL gurl(url_string); |
| 97 | 119 |
| 98 scoped_refptr<base::RefCountedMemory> data; | 120 scoped_refptr<base::RefCountedMemory> data; |
| 99 if (top_sites->GetPageThumbnail(gurl, false, &data)) { | 121 if (top_sites->GetPageThumbnail(gurl, false, &data)) { |
| 100 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); | 122 SkBitmap thumbnail_bitmap = ExtractThumbnail(*data.get()); |
| 101 if (!thumbnail_bitmap.empty()) { | 123 if (!thumbnail_bitmap.empty()) { |
| 102 j_bitmap_ref->Reset( | 124 j_bitmap_ref->Reset( |
| 103 env, | 125 env, |
| 104 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); | 126 gfx::ConvertToJavaBitmap(&thumbnail_bitmap).obj()); |
| 105 } | 127 } |
| 106 } else { | 128 } else { |
| 107 // A thumbnail is not locally available for |gurl|. Make sure it is put in | 129 // A thumbnail is not locally available for |gurl|. Make sure it is put in |
| 108 // the list to be fetched at the next visit to this site. | 130 // the list to be fetched at the next visit to this site. |
| 109 BrowserThread::PostTask( | 131 BrowserThread::PostTask( |
| 110 BrowserThread::UI, FROM_HERE, | 132 BrowserThread::UI, FROM_HERE, |
| 111 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); | 133 base::Bind(AddForcedURLOnUIThread, top_sites, gurl)); |
| 134 |
| 135 // If appropriate, return on the UI thread to execute the proper callback. |
| 136 if (!lookup_failed_ui_callback.is_null()) { |
| 137 BrowserThread::PostTask( |
| 138 BrowserThread::UI, FROM_HERE, lookup_failed_ui_callback); |
| 139 return; |
| 140 } |
| 112 } | 141 } |
| 113 | 142 |
| 114 // Since j_callback is owned by this callback, when the callback falls out of | 143 // Since j_callback is owned by this callback, when the callback falls out of |
| 115 // scope it will be deleted. We need to pass ownership to the next callback. | 144 // scope it will be deleted. We need to pass ownership to the next callback. |
| 116 ScopedJavaGlobalRef<jobject>* j_callback_pass = | 145 ScopedJavaGlobalRef<jobject>* j_callback_pass = |
| 117 new ScopedJavaGlobalRef<jobject>(*j_callback); | 146 new ScopedJavaGlobalRef<jobject>(*j_callback); |
| 118 BrowserThread::PostTask( | 147 BrowserThread::PostTask( |
| 119 BrowserThread::UI, FROM_HERE, | 148 BrowserThread::UI, FROM_HERE, |
| 120 base::Bind( | 149 base::Bind( |
| 121 &OnObtainedThumbnail, | 150 &OnObtainedThumbnail, |
| 122 base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); | 151 base::Owned(j_bitmap_ref), base::Owned(j_callback_pass))); |
| 123 } | 152 } |
| 124 | 153 |
| 154 void GetSuggestionsThumbnailOnUIThread( |
| 155 SuggestionsService* suggestions_service, |
| 156 const std::string& url_string, |
| 157 ScopedJavaGlobalRef<jobject>* j_callback) { |
| 158 suggestions_service->GetPageThumbnail( |
| 159 GURL(url_string), |
| 160 base::Bind(&OnSuggestionsThumbnailAvailable, |
| 161 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback)))); |
| 162 } |
| 163 |
| 125 } // namespace | 164 } // namespace |
| 126 | 165 |
| 127 MostVisitedSites::MostVisitedSites(Profile* profile) | 166 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 128 : profile_(profile), num_sites_(0), weak_ptr_factory_(this) { | 167 : profile_(profile), num_sites_(0), weak_ptr_factory_(this) { |
| 129 // Register the debugging page for the Suggestions Service and the thumbnails | 168 // Register the debugging page for the Suggestions Service and the thumbnails |
| 130 // debugging page. | 169 // debugging page. |
| 131 content::URLDataSource::Add(profile_, | 170 content::URLDataSource::Add(profile_, |
| 132 new suggestions::SuggestionsSource(profile_)); | 171 new suggestions::SuggestionsSource(profile_)); |
| 133 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 172 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
| 134 } | 173 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 // force an update now. | 194 // force an update now. |
| 156 top_sites->SyncWithHistory(); | 195 top_sites->SyncWithHistory(); |
| 157 | 196 |
| 158 // Register for notification when TopSites changes so that we can update | 197 // Register for notification when TopSites changes so that we can update |
| 159 // ourself. | 198 // ourself. |
| 160 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 199 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 161 content::Source<history::TopSites>(top_sites)); | 200 content::Source<history::TopSites>(top_sites)); |
| 162 } | 201 } |
| 163 } | 202 } |
| 164 | 203 |
| 165 // May be called from any thread | 204 // Called from the UI Thread. |
| 166 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, | 205 void MostVisitedSites::GetURLThumbnail(JNIEnv* env, |
| 167 jobject obj, | 206 jobject obj, |
| 168 jstring url, | 207 jstring url, |
| 169 jobject j_callback_obj) { | 208 jobject j_callback_obj) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 170 ScopedJavaGlobalRef<jobject>* j_callback = | 210 ScopedJavaGlobalRef<jobject>* j_callback = |
| 171 new ScopedJavaGlobalRef<jobject>(); | 211 new ScopedJavaGlobalRef<jobject>(); |
| 172 j_callback->Reset(env, j_callback_obj); | 212 j_callback->Reset(env, j_callback_obj); |
| 173 | 213 |
| 174 std::string url_string = ConvertJavaStringToUTF8(env, url); | 214 std::string url_string = ConvertJavaStringToUTF8(env, url); |
| 175 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); | 215 scoped_refptr<TopSites> top_sites(profile_->GetTopSites()); |
| 216 |
| 217 // If the Suggestions service is enabled, create a callback to fetch a |
| 218 // server thumbnail from it, in case the local thumbnail is not found. |
| 219 SuggestionsService* suggestions_service = |
| 220 SuggestionsServiceFactory::GetForProfile(profile_); |
| 221 base::Closure lookup_failed_callback = suggestions_service ? |
| 222 base::Bind(&GetSuggestionsThumbnailOnUIThread, |
| 223 suggestions_service, url_string, |
| 224 base::Owned(new ScopedJavaGlobalRef<jobject>(*j_callback))) : |
| 225 base::Closure(); |
| 176 BrowserThread::PostTask( | 226 BrowserThread::PostTask( |
| 177 BrowserThread::DB, FROM_HERE, base::Bind( | 227 BrowserThread::DB, FROM_HERE, |
| 178 &GetUrlThumbnailTask, | 228 base::Bind( |
| 179 url_string, | 229 &GetUrlThumbnailTask, url_string, top_sites, |
| 180 top_sites, base::Owned(j_callback))); | 230 base::Owned(j_callback), lookup_failed_callback)); |
| 181 } | 231 } |
| 182 | 232 |
| 183 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 233 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
| 184 jobject obj, | 234 jobject obj, |
| 185 jstring j_url) { | 235 jstring j_url) { |
| 186 std::string url = ConvertJavaStringToUTF8(env, j_url); | 236 std::string url = ConvertJavaStringToUTF8(env, j_url); |
| 187 | 237 |
| 188 switch (mv_source_) { | 238 switch (mv_source_) { |
| 189 case TOP_SITES: { | 239 case TOP_SITES: { |
| 190 TopSites* top_sites = profile_->GetTopSites(); | 240 TopSites* top_sites = profile_->GetTopSites(); |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 j_observer->obj(), | 347 j_observer->obj(), |
| 298 ToJavaArrayOfStrings(env, titles).obj(), | 348 ToJavaArrayOfStrings(env, titles).obj(), |
| 299 ToJavaArrayOfStrings(env, urls).obj()); | 349 ToJavaArrayOfStrings(env, urls).obj()); |
| 300 } | 350 } |
| 301 | 351 |
| 302 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 352 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 303 MostVisitedSites* most_visited_sites = | 353 MostVisitedSites* most_visited_sites = |
| 304 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 354 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 305 return reinterpret_cast<intptr_t>(most_visited_sites); | 355 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 306 } | 356 } |
| OLD | NEW |