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 = ConvertJavaStringToUTF8(env, j_url); |
201 if (!top_sites) | |
202 return; | |
203 | 187 |
204 std::string url_string = ConvertJavaStringToUTF8(env, j_url); | 188 switch (mv_source_) { |
205 top_sites->AddBlacklistedURL(GURL(url_string)); | 189 case TOP_SITES: { |
| 190 TopSites* top_sites = profile_->GetTopSites(); |
| 191 DCHECK(top_sites); |
| 192 top_sites->AddBlacklistedURL(GURL(url)); |
| 193 break; |
| 194 } |
| 195 |
| 196 case SUGGESTIONS_SERVICE: { |
| 197 SuggestionsService* suggestions_service = |
| 198 SuggestionsServiceFactory::GetForProfile(profile_); |
| 199 DCHECK(suggestions_service); |
| 200 suggestions_service->BlacklistURL( |
| 201 GURL(url), |
| 202 base::Bind( |
| 203 &MostVisitedSites::OnSuggestionsProfileAvailable, |
| 204 weak_ptr_factory_.GetWeakPtr(), |
| 205 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
| 206 break; |
| 207 } |
| 208 } |
206 } | 209 } |
207 | 210 |
208 void MostVisitedSites::Observe(int type, | 211 void MostVisitedSites::Observe(int type, |
209 const content::NotificationSource& source, | 212 const content::NotificationSource& source, |
210 const content::NotificationDetails& details) { | 213 const content::NotificationDetails& details) { |
211 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 214 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
212 | 215 |
213 // Most visited urls changed, query again. | 216 if (mv_source_ == TOP_SITES) { |
214 QueryMostVisitedURLs(); | 217 // The displayed suggestions are invalidated. |
| 218 QueryMostVisitedURLs(); |
| 219 } |
215 } | 220 } |
216 | 221 |
217 // static | 222 // static |
218 bool MostVisitedSites::Register(JNIEnv* env) { | 223 bool MostVisitedSites::Register(JNIEnv* env) { |
219 return RegisterNativesImpl(env); | 224 return RegisterNativesImpl(env); |
220 } | 225 } |
221 | 226 |
222 void MostVisitedSites::QueryMostVisitedURLs() { | 227 void MostVisitedSites::QueryMostVisitedURLs() { |
223 SuggestionsServiceFactory* suggestions_service_factory = | |
224 SuggestionsServiceFactory::GetInstance(); | |
225 SuggestionsService* suggestions_service = | 228 SuggestionsService* suggestions_service = |
226 suggestions_service_factory->GetForProfile(profile_); | 229 SuggestionsServiceFactory::GetForProfile(profile_); |
227 if (suggestions_service) { | 230 if (suggestions_service) { |
228 // Suggestions service is enabled, initiate a query. | 231 // Suggestions service is enabled, initiate a query. |
229 suggestions_service->FetchSuggestionsData( | 232 suggestions_service->FetchSuggestionsData( |
230 base::Bind( | 233 base::Bind( |
231 &MostVisitedSites::OnSuggestionsProfileAvailable, | 234 &MostVisitedSites::OnSuggestionsProfileAvailable, |
232 weak_ptr_factory_.GetWeakPtr(), | 235 weak_ptr_factory_.GetWeakPtr(), |
233 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); | 236 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)))); |
234 } else { | 237 } else { |
235 InitiateTopSitesQuery(); | 238 InitiateTopSitesQuery(); |
236 } | 239 } |
237 } | 240 } |
238 | 241 |
239 void MostVisitedSites::InitiateTopSitesQuery() { | 242 void MostVisitedSites::InitiateTopSitesQuery() { |
240 TopSites* top_sites = profile_->GetTopSites(); | 243 TopSites* top_sites = profile_->GetTopSites(); |
241 if (!top_sites) | 244 if (!top_sites) |
242 return; | 245 return; |
243 | 246 |
244 top_sites->GetMostVisitedURLs( | 247 top_sites->GetMostVisitedURLs( |
245 base::Bind( | 248 base::Bind( |
246 &OnMostVisitedURLsAvailable, | 249 &MostVisitedSites::OnMostVisitedURLsAvailable, |
| 250 weak_ptr_factory_.GetWeakPtr(), |
247 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), | 251 base::Owned(new ScopedJavaGlobalRef<jobject>(observer_)), |
248 num_sites_), | 252 num_sites_), |
249 false); | 253 false); |
250 } | 254 } |
251 | 255 |
| 256 void MostVisitedSites::OnMostVisitedURLsAvailable( |
| 257 ScopedJavaGlobalRef<jobject>* j_observer, |
| 258 int num_sites, |
| 259 const history::MostVisitedURLList& visited_list) { |
| 260 std::vector<base::string16> titles; |
| 261 std::vector<std::string> urls; |
| 262 ExtractMostVisitedTitlesAndURLs(visited_list, &titles, &urls, num_sites); |
| 263 |
| 264 mv_source_ = TOP_SITES; |
| 265 |
| 266 JNIEnv* env = AttachCurrentThread(); |
| 267 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
| 268 env, |
| 269 j_observer->obj(), |
| 270 ToJavaArrayOfStrings(env, titles).obj(), |
| 271 ToJavaArrayOfStrings(env, urls).obj()); |
| 272 } |
| 273 |
252 void MostVisitedSites::OnSuggestionsProfileAvailable( | 274 void MostVisitedSites::OnSuggestionsProfileAvailable( |
253 ScopedJavaGlobalRef<jobject>* j_observer, | 275 ScopedJavaGlobalRef<jobject>* j_observer, |
254 const SuggestionsProfile& suggestions_profile) { | 276 const SuggestionsProfile& suggestions_profile) { |
255 size_t size = suggestions_profile.suggestions_size(); | 277 size_t size = suggestions_profile.suggestions_size(); |
256 if (size == 0) { | 278 if (size == 0) { |
257 // No suggestions data available, initiate Top Sites query. | 279 // No suggestions data available, initiate Top Sites query. |
258 InitiateTopSitesQuery(); | 280 InitiateTopSitesQuery(); |
259 return; | 281 return; |
260 } | 282 } |
261 | 283 |
262 std::vector<base::string16> titles; | 284 std::vector<base::string16> titles; |
263 std::vector<std::string> urls; | 285 std::vector<std::string> urls; |
264 for (size_t i = 0; i < size; ++i) { | 286 for (size_t i = 0; i < size; ++i) { |
265 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); | 287 const ChromeSuggestion& suggestion = suggestions_profile.suggestions(i); |
266 titles.push_back(base::UTF8ToUTF16(suggestion.title())); | 288 titles.push_back(base::UTF8ToUTF16(suggestion.title())); |
267 urls.push_back(suggestion.url()); | 289 urls.push_back(suggestion.url()); |
268 } | 290 } |
269 | 291 |
| 292 mv_source_ = SUGGESTIONS_SERVICE; |
| 293 |
270 JNIEnv* env = AttachCurrentThread(); | 294 JNIEnv* env = AttachCurrentThread(); |
271 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | 295 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( |
272 env, | 296 env, |
273 j_observer->obj(), | 297 j_observer->obj(), |
274 ToJavaArrayOfStrings(env, titles).obj(), | 298 ToJavaArrayOfStrings(env, titles).obj(), |
275 ToJavaArrayOfStrings(env, urls).obj()); | 299 ToJavaArrayOfStrings(env, urls).obj()); |
276 } | 300 } |
277 | 301 |
278 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { | 302 static jlong Init(JNIEnv* env, jobject obj, jobject jprofile) { |
279 MostVisitedSites* most_visited_sites = | 303 MostVisitedSites* most_visited_sites = |
280 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 304 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); |
281 return reinterpret_cast<intptr_t>(most_visited_sites); | 305 return reinterpret_cast<intptr_t>(most_visited_sites); |
282 } | 306 } |
OLD | NEW |