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/ntp/most_visited_sites.h" | 5 #include "chrome/browser/android/ntp/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 155 } |
| 156 // The whole grid is already filled with personal suggestions, no point in | 156 // The whole grid is already filled with personal suggestions, no point in |
| 157 // bothering with popular ones. | 157 // bothering with popular ones. |
| 158 return false; | 158 return false; |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { | 161 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { |
| 162 return url1.host() == url2.host() && url1.path() == url2.path(); | 162 return url1.host() == url2.host() && url1.path() == url2.path(); |
| 163 } | 163 } |
| 164 | 164 |
| 165 class JavaObserverBridge : public MostVisitedSitesObserver { | 165 void CallJavaWithBitmap( |
| 166 public: | 166 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, |
| 167 JavaObserverBridge(JNIEnv* env, const JavaParamRef<jobject>& obj) | 167 bool is_local_thumbnail, |
| 168 : observer_(env, obj) {} | 168 const SkBitmap* bitmap) { |
| 169 | 169 JNIEnv* env = AttachCurrentThread(); |
| 170 void OnMostVisitedURLsAvailable( | 170 ScopedJavaLocalRef<jobject> j_bitmap; |
| 171 const std::vector<base::string16>& titles, | 171 if (bitmap) |
| 172 const std::vector<std::string>& urls, | 172 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); |
| 173 const std::vector<std::string>& whitelist_icon_paths) override { | 173 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( |
| 174 JNIEnv* env = AttachCurrentThread(); | 174 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); |
| 175 DCHECK_EQ(titles.size(), urls.size()); | 175 } |
| 176 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | |
| 177 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | |
| 178 ToJavaArrayOfStrings(env, urls).obj(), | |
| 179 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj()); | |
| 180 } | |
| 181 | |
| 182 void OnPopularURLsAvailable( | |
| 183 const std::vector<std::string>& urls, | |
| 184 const std::vector<std::string>& favicon_urls, | |
| 185 const std::vector<std::string>& large_icon_urls) override { | |
| 186 JNIEnv* env = AttachCurrentThread(); | |
| 187 Java_MostVisitedURLsObserver_onPopularURLsAvailable( | |
| 188 env, observer_.obj(), ToJavaArrayOfStrings(env, urls).obj(), | |
| 189 ToJavaArrayOfStrings(env, favicon_urls).obj(), | |
| 190 ToJavaArrayOfStrings(env, large_icon_urls).obj()); | |
| 191 } | |
| 192 | |
| 193 private: | |
| 194 ScopedJavaGlobalRef<jobject> observer_; | |
| 195 | |
| 196 DISALLOW_COPY_AND_ASSIGN(JavaObserverBridge); | |
| 197 }; | |
| 198 | 176 |
| 199 } // namespace | 177 } // namespace |
| 200 | 178 |
| 201 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} | 179 MostVisitedSites::Suggestion::Suggestion() : provider_index(-1) {} |
| 202 | 180 |
| 203 MostVisitedSites::Suggestion::~Suggestion() {} | 181 MostVisitedSites::Suggestion::~Suggestion() {} |
| 204 | 182 |
| 205 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { | 183 std::string MostVisitedSites::Suggestion::GetSourceHistogramName() const { |
| 206 switch (source) { | 184 switch (source) { |
| 207 case MostVisitedSites::TOP_SITES: | 185 case MostVisitedSites::TOP_SITES: |
| 208 return kHistogramClientName; | 186 return kHistogramClientName; |
| 209 case MostVisitedSites::POPULAR: | 187 case MostVisitedSites::POPULAR: |
| 210 return kHistogramPopularName; | 188 return kHistogramPopularName; |
| 211 case MostVisitedSites::WHITELIST: | 189 case MostVisitedSites::WHITELIST: |
| 212 return kHistogramWhitelistName; | 190 return kHistogramWhitelistName; |
| 213 case MostVisitedSites::SUGGESTIONS_SERVICE: | 191 case MostVisitedSites::SUGGESTIONS_SERVICE: |
| 214 return provider_index >= 0 | 192 return provider_index >= 0 |
| 215 ? base::StringPrintf(kHistogramServerFormat, provider_index) | 193 ? base::StringPrintf(kHistogramServerFormat, provider_index) |
| 216 : kHistogramServerName; | 194 : kHistogramServerName; |
| 217 } | 195 } |
| 218 NOTREACHED(); | 196 NOTREACHED(); |
| 219 return std::string(); | 197 return std::string(); |
| 220 } | 198 } |
| 221 | 199 |
| 222 MostVisitedSites::MostVisitedSites(Profile* profile) | 200 MostVisitedSites::MostVisitedSites(Profile* profile) |
| 223 : profile_(profile), num_sites_(0), received_most_visited_sites_(false), | 201 : profile_(profile), observer_(nullptr), num_sites_(0), |
| 224 received_popular_sites_(false), recorded_uma_(false), | 202 received_most_visited_sites_(false), received_popular_sites_(false), |
| 225 scoped_observer_(this), weak_ptr_factory_(this) { | 203 recorded_uma_(false), scoped_observer_(this), weak_ptr_factory_(this) { |
| 226 // Register the debugging page for the Suggestions Service and the thumbnails | 204 // Register the debugging page for the Suggestions Service and the thumbnails |
| 227 // debugging page. | 205 // debugging page. |
| 228 content::URLDataSource::Add(profile_, | 206 content::URLDataSource::Add(profile_, |
| 229 new suggestions::SuggestionsSource(profile_)); | 207 new suggestions::SuggestionsSource(profile_)); |
| 230 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); | 208 content::URLDataSource::Add(profile_, new ThumbnailListSource(profile_)); |
| 231 | 209 |
| 232 SupervisedUserService* supervised_user_service = | 210 SupervisedUserService* supervised_user_service = |
| 233 SupervisedUserServiceFactory::GetForProfile(profile_); | 211 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 234 supervised_user_service->AddObserver(this); | 212 supervised_user_service->AddObserver(this); |
| 235 } | 213 } |
| 236 | 214 |
| 237 MostVisitedSites::~MostVisitedSites() { | 215 MostVisitedSites::~MostVisitedSites() { |
| 238 SupervisedUserService* supervised_user_service = | 216 SupervisedUserService* supervised_user_service = |
| 239 SupervisedUserServiceFactory::GetForProfile(profile_); | 217 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 240 supervised_user_service->RemoveObserver(this); | 218 supervised_user_service->RemoveObserver(this); |
| 241 } | 219 } |
| 242 | 220 |
| 243 void MostVisitedSites::Destroy(JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
| 244 delete this; | |
| 245 } | |
| 246 | |
| 247 void MostVisitedSites::SetMostVisitedURLsObserver( | 221 void MostVisitedSites::SetMostVisitedURLsObserver( |
| 248 JNIEnv* env, | 222 MostVisitedSitesObserver* observer, int num_sites) { |
| 249 const JavaParamRef<jobject>& obj, | 223 observer_ = observer; |
|
Marc Treib
2016/04/19 08:14:47
DCHECK that observer_ wasn't set before?
(Eventual
sfiera
2016/04/19 12:15:13
Done.
I looked at the standard observer stuff in
Marc Treib
2016/04/19 13:20:54
Yup, the "num_pages" is weird, and we should get r
| |
| 250 const JavaParamRef<jobject>& j_observer, | |
| 251 jint num_sites) { | |
| 252 SetMostVisitedURLsObserver( | |
| 253 std::unique_ptr<MostVisitedSitesObserver>( | |
| 254 new JavaObserverBridge(env, j_observer)), | |
| 255 num_sites); | |
| 256 } | |
| 257 | |
| 258 void MostVisitedSites::SetMostVisitedURLsObserver( | |
| 259 std::unique_ptr<MostVisitedSitesObserver> observer, int num_sites) { | |
| 260 observer_ = std::move(observer); | |
| 261 num_sites_ = num_sites; | 224 num_sites_ = num_sites; |
| 262 | 225 |
| 263 if (ShouldShowPopularSites() && | 226 if (ShouldShowPopularSites() && |
| 264 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { | 227 NeedPopularSites(profile_->GetPrefs(), num_sites_)) { |
| 265 popular_sites_.reset(new PopularSites( | 228 popular_sites_.reset(new PopularSites( |
| 266 profile_, | 229 profile_, |
| 267 GetPopularSitesCountry(), | 230 GetPopularSitesCountry(), |
| 268 GetPopularSitesVersion(), | 231 GetPopularSitesVersion(), |
| 269 false, | 232 false, |
| 270 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 233 base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 291 base::Unretained(this))); | 254 base::Unretained(this))); |
| 292 | 255 |
| 293 // Immediately get the current suggestions from the cache. If the cache is | 256 // Immediately get the current suggestions from the cache. If the cache is |
| 294 // empty, this will fall back to TopSites. | 257 // empty, this will fall back to TopSites. |
| 295 OnSuggestionsProfileAvailable( | 258 OnSuggestionsProfileAvailable( |
| 296 suggestions_service->GetSuggestionsDataFromCache()); | 259 suggestions_service->GetSuggestionsDataFromCache()); |
| 297 // Also start a request for fresh suggestions. | 260 // Also start a request for fresh suggestions. |
| 298 suggestions_service->FetchSuggestionsData(); | 261 suggestions_service->FetchSuggestionsData(); |
| 299 } | 262 } |
| 300 | 263 |
| 301 static void CallJavaWithBitmap( | |
| 302 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
| 303 bool is_local_thumbnail, | |
| 304 const SkBitmap* bitmap); | |
| 305 | |
| 306 void MostVisitedSites::GetURLThumbnail( | |
| 307 JNIEnv* env, | |
| 308 const JavaParamRef<jobject>& obj, | |
| 309 const JavaParamRef<jstring>& j_url, | |
| 310 const JavaParamRef<jobject>& j_callback_obj) { | |
| 311 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | |
| 312 new ScopedJavaGlobalRef<jobject>(env, j_callback_obj)); | |
| 313 auto callback = base::Bind(&CallJavaWithBitmap, base::Passed(&j_callback)); | |
| 314 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
| 315 GetURLThumbnail(url, callback); | |
| 316 } | |
| 317 | |
| 318 void MostVisitedSites::GetURLThumbnail( | 264 void MostVisitedSites::GetURLThumbnail( |
| 319 const GURL& url, | 265 const GURL& url, |
| 320 const ThumbnailCallback& callback) { | 266 const ThumbnailCallback& callback) { |
| 321 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 267 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 322 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); | 268 scoped_refptr<TopSites> top_sites(TopSitesFactory::GetForProfile(profile_)); |
| 323 | 269 |
| 324 BrowserThread::PostTaskAndReplyWithResult( | 270 BrowserThread::PostTaskAndReplyWithResult( |
| 325 BrowserThread::DB, FROM_HERE, | 271 BrowserThread::DB, FROM_HERE, |
| 326 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), | 272 base::Bind(&MaybeFetchLocalThumbnail, url, top_sites), |
| 327 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, | 273 base::Bind(&MostVisitedSites::OnLocalThumbnailFetched, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 366 | 312 |
| 367 void MostVisitedSites::OnObtainedThumbnail( | 313 void MostVisitedSites::OnObtainedThumbnail( |
| 368 bool is_local_thumbnail, | 314 bool is_local_thumbnail, |
| 369 const ThumbnailCallback& callback, | 315 const ThumbnailCallback& callback, |
| 370 const GURL& url, | 316 const GURL& url, |
| 371 const SkBitmap* bitmap) { | 317 const SkBitmap* bitmap) { |
| 372 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 318 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 373 callback.Run(is_local_thumbnail, bitmap); | 319 callback.Run(is_local_thumbnail, bitmap); |
| 374 } | 320 } |
| 375 | 321 |
| 376 static void CallJavaWithBitmap( | |
| 377 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback, | |
| 378 bool is_local_thumbnail, | |
| 379 const SkBitmap* bitmap) { | |
| 380 JNIEnv* env = AttachCurrentThread(); | |
| 381 ScopedJavaLocalRef<jobject> j_bitmap; | |
| 382 if (bitmap) | |
| 383 j_bitmap = gfx::ConvertToJavaBitmap(bitmap); | |
| 384 Java_ThumbnailCallback_onMostVisitedURLsThumbnailAvailable( | |
| 385 env, j_callback->obj(), j_bitmap.obj(), is_local_thumbnail); | |
| 386 } | |
| 387 | |
| 388 void MostVisitedSites::AddOrRemoveBlacklistedUrl( | |
| 389 JNIEnv* env, | |
| 390 const JavaParamRef<jobject>& obj, | |
| 391 const JavaParamRef<jstring>& j_url, | |
| 392 jboolean add_url) { | |
| 393 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
| 394 AddOrRemoveBlacklistedUrl(url, add_url); | |
| 395 } | |
| 396 | |
| 397 void MostVisitedSites::AddOrRemoveBlacklistedUrl( | 322 void MostVisitedSites::AddOrRemoveBlacklistedUrl( |
| 398 const GURL& url, bool add_url) { | 323 const GURL& url, bool add_url) { |
| 399 // Always blacklist in the local TopSites. | 324 // Always blacklist in the local TopSites. |
| 400 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); | 325 scoped_refptr<TopSites> top_sites = TopSitesFactory::GetForProfile(profile_); |
| 401 if (top_sites) { | 326 if (top_sites) { |
| 402 if (add_url) | 327 if (add_url) |
| 403 top_sites->AddBlacklistedURL(url); | 328 top_sites->AddBlacklistedURL(url); |
| 404 else | 329 else |
| 405 top_sites->RemoveBlacklistedURL(url); | 330 top_sites->RemoveBlacklistedURL(url); |
| 406 } | 331 } |
| 407 | 332 |
| 408 // Only blacklist in the server-side suggestions service if it's active. | 333 // Only blacklist in the server-side suggestions service if it's active. |
| 409 if (mv_source_ == SUGGESTIONS_SERVICE) { | 334 if (mv_source_ == SUGGESTIONS_SERVICE) { |
| 410 SuggestionsService* suggestions_service = | 335 SuggestionsService* suggestions_service = |
| 411 SuggestionsServiceFactory::GetForProfile(profile_); | 336 SuggestionsServiceFactory::GetForProfile(profile_); |
| 412 if (add_url) | 337 if (add_url) |
| 413 suggestions_service->BlacklistURL(url); | 338 suggestions_service->BlacklistURL(url); |
| 414 else | 339 else |
| 415 suggestions_service->UndoBlacklistURL(url); | 340 suggestions_service->UndoBlacklistURL(url); |
| 416 } | 341 } |
| 417 } | 342 } |
| 418 | 343 |
| 419 void MostVisitedSites::RecordTileTypeMetrics( | 344 void MostVisitedSites::RecordTileTypeMetrics( |
| 420 JNIEnv* env, | |
| 421 const JavaParamRef<jobject>& obj, | |
| 422 const JavaParamRef<jintArray>& jtile_types) { | |
| 423 std::vector<int> tile_types; | |
| 424 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); | |
| 425 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); | |
| 426 RecordTileTypeMetrics(tile_types); | |
| 427 } | |
| 428 | |
| 429 void MostVisitedSites::RecordTileTypeMetrics( | |
| 430 const std::vector<int>& tile_types) { | 345 const std::vector<int>& tile_types) { |
| 346 DCHECK_EQ(current_suggestions_.size(), tile_types.size()); | |
| 431 int counts_per_type[NUM_TILE_TYPES] = {0}; | 347 int counts_per_type[NUM_TILE_TYPES] = {0}; |
| 432 for (size_t i = 0; i < tile_types.size(); ++i) { | 348 for (size_t i = 0; i < tile_types.size(); ++i) { |
| 433 int tile_type = tile_types[i]; | 349 int tile_type = tile_types[i]; |
| 434 ++counts_per_type[tile_type]; | 350 ++counts_per_type[tile_type]; |
| 435 std::string histogram = base::StringPrintf( | 351 std::string histogram = base::StringPrintf( |
| 436 "NewTabPage.TileType.%s", | 352 "NewTabPage.TileType.%s", |
| 437 current_suggestions_[i]->GetSourceHistogramName().c_str()); | 353 current_suggestions_[i]->GetSourceHistogramName().c_str()); |
| 438 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 354 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
| 439 } | 355 } |
| 440 | 356 |
| 441 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", | 357 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsReal", |
| 442 counts_per_type[ICON_REAL]); | 358 counts_per_type[ICON_REAL]); |
| 443 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", | 359 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsColor", |
| 444 counts_per_type[ICON_COLOR]); | 360 counts_per_type[ICON_COLOR]); |
| 445 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", | 361 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.IconsGray", |
| 446 counts_per_type[ICON_DEFAULT]); | 362 counts_per_type[ICON_DEFAULT]); |
| 447 } | 363 } |
| 448 | 364 |
| 449 void MostVisitedSites::RecordOpenedMostVisitedItem( | |
| 450 JNIEnv* env, | |
| 451 const JavaParamRef<jobject>& obj, | |
| 452 jint index, | |
| 453 jint tile_type) { | |
| 454 RecordOpenedMostVisitedItem(index, tile_type); | |
| 455 } | |
| 456 | |
| 457 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { | 365 void MostVisitedSites::RecordOpenedMostVisitedItem(int index, int tile_type) { |
| 458 DCHECK_GE(index, 0); | 366 DCHECK_GE(index, 0); |
| 459 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); | 367 DCHECK_LT(index, static_cast<int>(current_suggestions_.size())); |
| 460 std::string histogram = base::StringPrintf( | 368 std::string histogram = base::StringPrintf( |
| 461 "NewTabPage.MostVisited.%s", | 369 "NewTabPage.MostVisited.%s", |
| 462 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 370 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| 463 LogHistogramEvent(histogram, index, num_sites_); | 371 LogHistogramEvent(histogram, index, num_sites_); |
| 464 | 372 |
| 465 histogram = base::StringPrintf( | 373 histogram = base::StringPrintf( |
| 466 "NewTabPage.TileTypeClicked.%s", | 374 "NewTabPage.TileTypeClicked.%s", |
| 467 current_suggestions_[index]->GetSourceHistogramName().c_str()); | 375 current_suggestions_[index]->GetSourceHistogramName().c_str()); |
| 468 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); | 376 LogHistogramEvent(histogram, tile_type, NUM_TILE_TYPES); |
| 469 } | 377 } |
| 470 | 378 |
| 471 void MostVisitedSites::OnURLFilterChanged() { | 379 void MostVisitedSites::OnURLFilterChanged() { |
| 472 QueryMostVisitedURLs(); | 380 QueryMostVisitedURLs(); |
| 473 } | 381 } |
| 474 | 382 |
| 475 // static | 383 // static |
| 476 bool MostVisitedSites::Register(JNIEnv* env) { | |
| 477 return RegisterNativesImpl(env); | |
| 478 } | |
| 479 | |
| 480 // static | |
| 481 void MostVisitedSites::RegisterProfilePrefs( | 384 void MostVisitedSites::RegisterProfilePrefs( |
| 482 user_prefs::PrefRegistrySyncable* registry) { | 385 user_prefs::PrefRegistrySyncable* registry) { |
| 483 registry->RegisterListPref(prefs::kNTPSuggestionsURL); | 386 registry->RegisterListPref(prefs::kNTPSuggestionsURL); |
| 484 registry->RegisterListPref(prefs::kNTPSuggestionsIsPersonal); | 387 registry->RegisterListPref(prefs::kNTPSuggestionsIsPersonal); |
| 485 } | 388 } |
| 486 | 389 |
| 487 void MostVisitedSites::QueryMostVisitedURLs() { | 390 void MostVisitedSites::QueryMostVisitedURLs() { |
| 488 SuggestionsService* suggestions_service = | 391 SuggestionsService* suggestions_service = |
| 489 SuggestionsServiceFactory::GetForProfile(profile_); | 392 SuggestionsServiceFactory::GetForProfile(profile_); |
| 490 if (suggestions_service->FetchSuggestionsData()) { | 393 if (suggestions_service->FetchSuggestionsData()) { |
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 924 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} | 827 void MostVisitedSites::TopSitesLoaded(TopSites* top_sites) {} |
| 925 | 828 |
| 926 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 829 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 927 ChangeReason change_reason) { | 830 ChangeReason change_reason) { |
| 928 if (mv_source_ == TOP_SITES) { | 831 if (mv_source_ == TOP_SITES) { |
| 929 // The displayed suggestions are invalidated. | 832 // The displayed suggestions are invalidated. |
| 930 InitiateTopSitesQuery(); | 833 InitiateTopSitesQuery(); |
| 931 } | 834 } |
| 932 } | 835 } |
| 933 | 836 |
| 837 // static | |
| 838 bool MostVisitedSitesJavaBridge::Register(JNIEnv* env) { | |
| 839 return RegisterNativesImpl(env); | |
| 840 } | |
| 841 | |
| 842 MostVisitedSitesJavaBridge::MostVisitedSitesJavaBridge(Profile* profile) | |
| 843 : mv_(profile) { } | |
| 844 | |
| 845 MostVisitedSitesJavaBridge::~MostVisitedSitesJavaBridge() { } | |
| 846 | |
| 847 void MostVisitedSitesJavaBridge::Destroy( | |
| 848 JNIEnv* env, const JavaParamRef<jobject>& obj) { | |
| 849 delete this; | |
| 850 } | |
| 851 | |
| 852 void MostVisitedSitesJavaBridge::SetMostVisitedURLsObserver( | |
| 853 JNIEnv* env, | |
| 854 const JavaParamRef<jobject>& obj, | |
| 855 const JavaParamRef<jobject>& j_observer, | |
| 856 jint num_sites) { | |
| 857 observer_.reset(new Observer(env, j_observer)); | |
| 858 mv_.SetMostVisitedURLsObserver(observer_.get(), num_sites); | |
| 859 } | |
| 860 | |
| 861 void MostVisitedSitesJavaBridge::GetURLThumbnail( | |
| 862 JNIEnv* env, | |
| 863 const JavaParamRef<jobject>& obj, | |
| 864 const JavaParamRef<jstring>& j_url, | |
| 865 const JavaParamRef<jobject>& j_callback_obj) { | |
| 866 std::unique_ptr<ScopedJavaGlobalRef<jobject>> j_callback( | |
| 867 new ScopedJavaGlobalRef<jobject>(env, j_callback_obj)); | |
| 868 auto callback = base::Bind(&CallJavaWithBitmap, base::Passed(&j_callback)); | |
| 869 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
|
Marc Treib
2016/04/19 08:14:47
nit: I'd inline both of these below.
sfiera
2016/04/19 12:15:13
That pushes the line over 80 chars. If it's going
Marc Treib
2016/04/19 13:20:54
I tend to prefer "one" (broken) line - I guess I'v
| |
| 870 mv_.GetURLThumbnail(url, callback); | |
| 871 } | |
| 872 | |
| 873 void MostVisitedSitesJavaBridge::AddOrRemoveBlacklistedUrl( | |
| 874 JNIEnv* env, | |
| 875 const JavaParamRef<jobject>& obj, | |
| 876 const JavaParamRef<jstring>& j_url, | |
| 877 jboolean add_url) { | |
| 878 GURL url(ConvertJavaStringToUTF8(env, j_url)); | |
|
Marc Treib
2016/04/19 08:14:47
Also here
sfiera
2016/04/19 12:15:13
Also here
| |
| 879 mv_.AddOrRemoveBlacklistedUrl(url, add_url); | |
| 880 } | |
| 881 | |
| 882 void MostVisitedSitesJavaBridge::RecordTileTypeMetrics( | |
| 883 JNIEnv* env, | |
| 884 const JavaParamRef<jobject>& obj, | |
| 885 const JavaParamRef<jintArray>& jtile_types) { | |
| 886 std::vector<int> tile_types; | |
| 887 base::android::JavaIntArrayToIntVector(env, jtile_types, &tile_types); | |
| 888 mv_.RecordTileTypeMetrics(tile_types); | |
| 889 } | |
| 890 | |
| 891 void MostVisitedSitesJavaBridge::RecordOpenedMostVisitedItem( | |
| 892 JNIEnv* env, | |
| 893 const JavaParamRef<jobject>& obj, | |
| 894 jint index, | |
| 895 jint tile_type) { | |
| 896 mv_.RecordOpenedMostVisitedItem(index, tile_type); | |
| 897 } | |
| 898 | |
| 899 MostVisitedSitesJavaBridge::Observer::Observer( | |
| 900 JNIEnv* env, const JavaParamRef<jobject>& obj) | |
| 901 : observer_(env, obj) {} | |
| 902 | |
| 903 void MostVisitedSitesJavaBridge::Observer::OnMostVisitedURLsAvailable( | |
| 904 const std::vector<base::string16>& titles, | |
| 905 const std::vector<std::string>& urls, | |
| 906 const std::vector<std::string>& whitelist_icon_paths) { | |
| 907 JNIEnv* env = AttachCurrentThread(); | |
| 908 DCHECK_EQ(titles.size(), urls.size()); | |
|
Marc Treib
2016/04/19 08:14:47
Not your doing, but it would be good to add anothe
sfiera
2016/04/19 12:15:13
Sure. (I'm also going to count this as a vote for
| |
| 909 Java_MostVisitedURLsObserver_onMostVisitedURLsAvailable( | |
| 910 env, observer_.obj(), ToJavaArrayOfStrings(env, titles).obj(), | |
| 911 ToJavaArrayOfStrings(env, urls).obj(), | |
| 912 ToJavaArrayOfStrings(env, whitelist_icon_paths).obj()); | |
| 913 } | |
| 914 | |
| 915 void MostVisitedSitesJavaBridge::Observer::OnPopularURLsAvailable( | |
| 916 const std::vector<std::string>& urls, | |
| 917 const std::vector<std::string>& favicon_urls, | |
| 918 const std::vector<std::string>& large_icon_urls) { | |
| 919 JNIEnv* env = AttachCurrentThread(); | |
| 920 Java_MostVisitedURLsObserver_onPopularURLsAvailable( | |
| 921 env, observer_.obj(), ToJavaArrayOfStrings(env, urls).obj(), | |
| 922 ToJavaArrayOfStrings(env, favicon_urls).obj(), | |
| 923 ToJavaArrayOfStrings(env, large_icon_urls).obj()); | |
| 924 } | |
| 925 | |
| 934 static jlong Init(JNIEnv* env, | 926 static jlong Init(JNIEnv* env, |
| 935 const JavaParamRef<jobject>& obj, | 927 const JavaParamRef<jobject>& obj, |
| 936 const JavaParamRef<jobject>& jprofile) { | 928 const JavaParamRef<jobject>& jprofile) { |
| 937 MostVisitedSites* most_visited_sites = | 929 MostVisitedSitesJavaBridge* most_visited_sites = |
| 938 new MostVisitedSites(ProfileAndroid::FromProfileAndroid(jprofile)); | 930 new MostVisitedSitesJavaBridge( |
| 931 ProfileAndroid::FromProfileAndroid(jprofile)); | |
| 939 return reinterpret_cast<intptr_t>(most_visited_sites); | 932 return reinterpret_cast<intptr_t>(most_visited_sites); |
| 940 } | 933 } |
| OLD | NEW |