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 <string> | |
| 8 #include <vector> | |
| 9 | |
| 7 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
| 9 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 10 #include "base/android/scoped_java_ref.h" | 13 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/history/history_types.h" | |
| 15 #include "chrome/browser/history/top_sites.h" | 17 #include "chrome/browser/history/top_sites.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/profiles/profile_android.h" | 19 #include "chrome/browser/profiles/profile_android.h" |
| 18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 20 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 19 #include "chrome/browser/search/suggestions/suggestions_service.h" | 21 #include "chrome/browser/search/suggestions/suggestions_service.h" |
| 20 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 22 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 21 #include "chrome/browser/search/suggestions/suggestions_source.h" | 23 #include "chrome/browser/search/suggestions/suggestions_source.h" |
| 22 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 24 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 23 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/notification_source.h" | 26 #include "content/public/browser/notification_source.h" |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 53 const history::MostVisitedURL& visited = visited_list[i]; | 55 const history::MostVisitedURL& visited = visited_list[i]; |
| 54 | 56 |
| 55 if (visited.url.is_empty()) | 57 if (visited.url.is_empty()) |
| 56 break; // This is the signal that there are no more real visited sites. | 58 break; // This is the signal that there are no more real visited sites. |
| 57 | 59 |
| 58 titles->push_back(visited.title); | 60 titles->push_back(visited.title); |
| 59 urls->push_back(visited.url.spec()); | 61 urls->push_back(visited.url.spec()); |
| 60 } | 62 } |
| 61 } | 63 } |
| 62 | 64 |
| 63 void OnMostVisitedURLsAvailable( | |
| 64 ScopedJavaGlobalRef<jobject>* j_observer, | |
| 65 int num_sites, | |
| 66 const history::MostVisitedURLList& visited_list) { | |
| 67 std::vector<base::string16> titles; | |
| 68 std::vector<std::string> urls; | |
| 69 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); | |
| 70 | |
| 71 JNIEnv* env = AttachCurrentThread(); | |
| 72 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | |
| 73 env, | |
| 74 j_observer->obj(), | |
| 75 ToJavaArrayOfStrings(env, titles).obj(), | |
| 76 ToJavaArrayOfStrings(env, urls).obj()); | |
| 77 } | |
| 78 | |
| 79 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) { | 65 SkBitmap ExtractThumbnail(const base::RefCountedMemory& image_data) { |
| 80 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode( | 66 scoped_ptr<SkBitmap> image(gfx::JPEGCodec::Decode( |
| 81 image_data.front(), | 67 image_data.front(), |
| 82 image_data.size())); | 68 image_data.size())); |
| 83 return image.get() ? *image : SkBitmap(); | 69 return image.get() ? *image : SkBitmap(); |
| 84 } | 70 } |
| 85 | 71 |
| 86 void OnObtainedThumbnail( | 72 void OnObtainedThumbnail( |
| 87 ScopedJavaGlobalRef<jobject>* bitmap, | 73 ScopedJavaGlobalRef<jobject>* bitmap, |
| 88 ScopedJavaGlobalRef<jobject>* j_callback) { | 74 ScopedJavaGlobalRef<jobject>* j_callback) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 BrowserThread::PostTask( | 176 BrowserThread::PostTask( |
| 191 BrowserThread::DB, FROM_HERE, base::Bind( | 177 BrowserThread::DB, FROM_HERE, base::Bind( |
| 192 &GetUrlThumbnailTask, | 178 &GetUrlThumbnailTask, |
| 193 url_string, | 179 url_string, |
| 194 top_sites, base::Owned(j_callback))); | 180 top_sites, base::Owned(j_callback))); |
| 195 } | 181 } |
| 196 | 182 |
| 197 void MostVisitedSites::BlacklistUrl(JNIEnv* env, | 183 void MostVisitedSites::BlacklistUrl(JNIEnv* env, |
| 198 jobject obj, | 184 jobject obj, |
| 199 jstring j_url) { | 185 jstring j_url) { |
| 200 TopSites* top_sites = profile_->GetTopSites(); | 186 std::string url_string = ConvertJavaStringToUTF8(env, j_url); |
| 201 if (!top_sites) | 187 GURL url(url_string); |
|
Mathieu
2014/05/22 21:50:49
I guess you could move this in top_sites->AddBlack
manzagop (departed)
2014/05/23 15:20:02
Done.
| |
| 202 return; | |
| 203 | 188 |
| 204 std::string url_string = ConvertJavaStringToUTF8(env, j_url); | 189 switch (mv_source_) { |
| 205 top_sites->AddBlacklistedURL(GURL(url_string)); | 190 case TOP_SITES: { |
| 191 TopSites* top_sites = profile_->GetTopSites(); | |
| 192 if (top_sites) { | |
| 193 top_sites->AddBlacklistedURL(url); | |
| 194 } | |
| 195 break; | |
| 196 } | |
| 197 | |
| 198 case SUGGESTIONS_SERVICE: { | |
| 199 SuggestionsService* suggestions_service = | |
| 200 SuggestionsServiceFactory::GetForProfile(profile_); | |
| 201 if (suggestions_service) { | |
|
Mathieu
2014/05/22 21:50:49
Aren't we certain at this point that the service e
manzagop (departed)
2014/05/23 15:20:02
If services can't be turned off, then yes. Still,
| |
| 202 suggestions_service->AddBlacklistedURL( | |
| 203 url_string, | |
|
Mathieu
2014/05/22 21:50:49
This should probably take a GURL as well.
manzagop (departed)
2014/05/23 15:20:02
Done.
| |
| 204 base::Bind( | |
| 205 &MostVisitedSites::OnSuggestionsProfileAvailable, | |
| 206 weak_ptr_factory_.GetWeakPtr(), | |
| 207 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | |
| 208 } | |
| 209 break; | |
| 210 } | |
| 211 } | |
| 206 } | 212 } |
| 207 | 213 |
| 208 void MostVisitedSites::Observe(int type, | 214 void MostVisitedSites::Observe(int type, |
| 209 const content::NotificationSource& source, | 215 const content::NotificationSource& source, |
| 210 const content::NotificationDetails& details) { | 216 const content::NotificationDetails& details) { |
| 211 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 217 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| 212 | 218 |
| 213 // Most visited urls changed, query again. | 219 // Most visited urls changed, query again. |
| 214 QueryMostVisitedURLs(); | 220 QueryMostVisitedURLs(); |
| 215 } | 221 } |
| 216 | 222 |
| 217 // static | 223 // static |
| 218 bool MostVisitedSites::Register(JNIEnv* env) { | 224 bool MostVisitedSites::Register(JNIEnv* env) { |
| 219 return RegisterNativesImpl(env); | 225 return RegisterNativesImpl(env); |
| 220 } | 226 } |
| 221 | 227 |
| 222 void MostVisitedSites::QueryMostVisitedURLs() { | 228 void MostVisitedSites::QueryMostVisitedURLs() { |
| 223 SuggestionsServiceFactory* suggestions_service_factory = | |
| 224 SuggestionsServiceFactory::GetInstance(); | |
| 225 SuggestionsService* suggestions_service = | 229 SuggestionsService* suggestions_service = |
| 226 suggestions_service_factory->GetForProfile(profile_); | 230 SuggestionsServiceFactory::GetForProfile(profile_); |
| 227 if (suggestions_service) { | 231 if (suggestions_service) { |
| 228 // Suggestions service is enabled, initiate a query. | 232 // Suggestions service is enabled, initiate a query. |
| 229 suggestions_service->FetchSuggestionsData( | 233 suggestions_service->FetchSuggestionsData( |
| 230 base::Bind( | 234 base::Bind( |
| 231 &MostVisitedSites::OnSuggestionsProfileAvailable, | 235 &MostVisitedSites::OnSuggestionsProfileAvailable, |
| 232 weak_ptr_factory_.GetWeakPtr(), | 236 weak_ptr_factory_.GetWeakPtr(), |
| 233 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 237 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
| 234 } else { | 238 } else { |
| 235 InitiateTopSitesQuery(); | 239 InitiateTopSitesQuery(); |
| 236 } | 240 } |
| 237 } | 241 } |
| 238 | 242 |
| 239 void MostVisitedSites::InitiateTopSitesQuery() { | 243 void MostVisitedSites::InitiateTopSitesQuery() { |
| 240 TopSites* top_sites = profile_->GetTopSites(); | 244 TopSites* top_sites = profile_->GetTopSites(); |
| 241 if (!top_sites) | 245 if (!top_sites) |
| 242 return; | 246 return; |
| 243 | 247 |
| 244 top_sites->GetMostVisitedURLs( | 248 top_sites->GetMostVisitedURLs( |
| 245 base::Bind( | 249 base::Bind( |
| 246 &OnMostVisitedURLsAvailable, | 250 &MostVisitedSites::OnMostVisitedURLsAvailable, |
| 251 weak_ptr_factory_.GetWeakPtr(), | |
| 247 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 252 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
| 248 num_sites_), | 253 num_sites_), |
| 249 false); | 254 false); |
| 250 } | 255 } |
| 251 | 256 |
| 257 void MostVisitedSites::OnMostVisitedURLsAvailable( | |
| 258 ScopedJavaGlobalRef<jobject>* j_observer, | |
| 259 int num_sites, | |
| 260 const history::MostVisitedURLList& visited_list) { | |
| 261 std::vector<base::string16> titles; | |
| 262 std::vector<std::string> urls; | |
| 263 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); | |
| 264 | |
| 265 mv_source_ = TOP_SITES; | |
| 266 | |
| 267 JNIEnv* env = AttachCurrentThread(); | |
| 268 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | |
| 269 env, | |
| 270 j_observer->obj(), | |
| 271 ToJavaArrayOfStrings(env, titles).obj(), | |
| 272 ToJavaArrayOfStrings(env, urls).obj()); | |
| 273 } | |
| 274 | |
| 252 void MostVisitedSites::OnSuggestionsProfileAvailable( | 275 void MostVisitedSites::OnSuggestionsProfileAvailable( |
| 253 ScopedJavaGlobalRef<jobject>* j_observer, | 276 ScopedJavaGlobalRef<jobject>* j_observer, |
| 254 const SuggestionsProfile& suggestions_profile) { | 277 const SuggestionsProfile& suggestions_profile) { |
| 255 size_t size = suggestions_profile.suggestions_size(); | 278 size_t size = suggestions_profile.suggestions_size(); |
| 256 if (size == 0) { | 279 if (size == 0) { |
| 257 // No suggestions data available, initiate Top Sites query. | 280 // No suggestions data available, initiate Top Sites query. |
| 258 InitiateTopSitesQuery(); | 281 InitiateTopSitesQuery(); |
| 259 return; | 282 return; |
| 260 } | 283 } |
| 261 | 284 |
| 262 std::vector<base::string16> titles; | 285 std::vector<base::string16> titles; |
| 263 std::vector<std::string> urls; | 286 std::vector<std::string> urls; |
| 264 for (size_t i = 0; i < size; ++i) { | 287 for (size_t i = 0; i < size; ++i) { |
| 265 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 288 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
| 266 titles.push_back(base::UTF8ToUTF16(suggestion.title())); | 289 titles.push_back(base::UTF8ToUTF16(suggestion.title())); |
| 267 urls.push_back(suggestion.url()); | 290 urls.push_back(suggestion.url()); |
| 268 } | 291 } |
| 269 | 292 |
| 293 mv_source_ = SUGGESTIONS_SERVICE; | |
| 294 | |
| 270 JNIEnv* env = AttachCurrentThread(); | 295 JNIEnv* env = AttachCurrentThread(); |
| 271 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | 296 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
| 272 env, | 297 env, |
| 273 j_observer->obj(), | 298 j_observer->obj(), |
| 274 ToJavaArrayOfStrings(env, titles).obj(), | 299 ToJavaArrayOfStrings(env, titles).obj(), |
| 275 ToJavaArrayOfStrings(env, urls).obj()); | 300 ToJavaArrayOfStrings(env, urls).obj()); |
| 276 } | 301 } |
| 277 | 302 |
| 278 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 303 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
| 279 MostVisitedSites* most_visited_sites = | 304 MostVisitedSites* most_visited_sites = |
| 280 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 305 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
| 281 return reinterpret_cast<intptr_t>(most_visited_sites); | 306 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 282 } | 307 } |
| OLD | NEW |