| 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 "components/ntp_tiles/most_visited_sites.h" | 5 #include "components/ntp_tiles/most_visited_sites.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #if defined(OS_ANDROID) | |
| 13 #include <jni.h> | |
| 14 #endif | |
| 15 | |
| 16 #include "base/callback.h" | 12 #include "base/callback.h" |
| 17 #include "base/command_line.h" | |
| 18 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
| 19 #include "base/metrics/field_trial.h" | |
| 20 #include "base/strings/string_util.h" | |
| 21 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 22 #include "components/history/core/browser/top_sites.h" | 15 #include "components/history/core/browser/top_sites.h" |
| 23 #include "components/ntp_tiles/constants.h" | 16 #include "components/ntp_tiles/constants.h" |
| 17 #include "components/ntp_tiles/field_trial.h" |
| 24 #include "components/ntp_tiles/icon_cacher.h" | 18 #include "components/ntp_tiles/icon_cacher.h" |
| 25 #include "components/ntp_tiles/metrics.h" | 19 #include "components/ntp_tiles/metrics.h" |
| 26 #include "components/ntp_tiles/pref_names.h" | 20 #include "components/ntp_tiles/pref_names.h" |
| 27 #include "components/ntp_tiles/switches.h" | 21 #include "components/ntp_tiles/switches.h" |
| 28 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
| 29 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 30 | 24 |
| 31 #if defined(OS_ANDROID) | |
| 32 #include "base/android/jni_android.h" | |
| 33 #include "jni/MostVisitedSites_jni.h" | |
| 34 #endif | |
| 35 | |
| 36 using history::TopSites; | 25 using history::TopSites; |
| 37 using suggestions::ChromeSuggestion; | 26 using suggestions::ChromeSuggestion; |
| 38 using suggestions::SuggestionsProfile; | 27 using suggestions::SuggestionsProfile; |
| 39 using suggestions::SuggestionsService; | 28 using suggestions::SuggestionsService; |
| 40 | 29 |
| 41 namespace ntp_tiles { | 30 namespace ntp_tiles { |
| 42 | 31 |
| 43 namespace { | 32 namespace { |
| 44 | 33 |
| 45 const base::Feature kDisplaySuggestionsServiceTiles{ | 34 const base::Feature kDisplaySuggestionsServiceTiles{ |
| 46 "DisplaySuggestionsServiceTiles", base::FEATURE_ENABLED_BY_DEFAULT}; | 35 "DisplaySuggestionsServiceTiles", base::FEATURE_ENABLED_BY_DEFAULT}; |
| 47 | 36 |
| 48 bool ShouldShowPopularSites() { | |
| 49 // Note: It's important to query the field trial state first, to ensure that | |
| 50 // UMA reports the correct group. | |
| 51 const std::string group_name = | |
| 52 base::FieldTrialList::FindFullName(kPopularSitesFieldTrialName); | |
| 53 | |
| 54 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 55 if (cmd_line->HasSwitch(switches::kDisableNTPPopularSites)) | |
| 56 return false; | |
| 57 | |
| 58 if (cmd_line->HasSwitch(switches::kEnableNTPPopularSites)) | |
| 59 return true; | |
| 60 | |
| 61 #if defined(OS_ANDROID) | |
| 62 if (Java_MostVisitedSites_isPopularSitesForceEnabled( | |
| 63 base::android::AttachCurrentThread())) { | |
| 64 return true; | |
| 65 } | |
| 66 #endif | |
| 67 | |
| 68 return base::StartsWith(group_name, "Enabled", | |
| 69 base::CompareCase::INSENSITIVE_ASCII); | |
| 70 } | |
| 71 | |
| 72 // Determine whether we need any tiles from PopularSites to fill up a grid of | 37 // Determine whether we need any tiles from PopularSites to fill up a grid of |
| 73 // |num_tiles| tiles. | 38 // |num_tiles| tiles. |
| 74 bool NeedPopularSites(const PrefService* prefs, int num_tiles) { | 39 bool NeedPopularSites(const PrefService* prefs, int num_tiles) { |
| 75 return prefs->GetInteger(prefs::kNumPersonalTiles) < num_tiles; | 40 return prefs->GetInteger(prefs::kNumPersonalTiles) < num_tiles; |
| 76 } | 41 } |
| 77 | 42 |
| 78 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { | 43 bool AreURLsEquivalent(const GURL& url1, const GURL& url2) { |
| 79 return url1.host_piece() == url2.host_piece() && | 44 return url1.host_piece() == url2.host_piece() && |
| 80 url1.path_piece() == url2.path_piece(); | 45 url1.path_piece() == url2.path_piece(); |
| 81 } | 46 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 if (supervisor_) | 79 if (supervisor_) |
| 115 supervisor_->SetObserver(nullptr); | 80 supervisor_->SetObserver(nullptr); |
| 116 } | 81 } |
| 117 | 82 |
| 118 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, | 83 void MostVisitedSites::SetMostVisitedURLsObserver(Observer* observer, |
| 119 int num_sites) { | 84 int num_sites) { |
| 120 DCHECK(observer); | 85 DCHECK(observer); |
| 121 observer_ = observer; | 86 observer_ = observer; |
| 122 num_sites_ = num_sites; | 87 num_sites_ = num_sites; |
| 123 | 88 |
| 124 if (popular_sites_ && ShouldShowPopularSites() && | 89 // The order for this condition is important, ShouldShowPopularSite() should |
| 125 NeedPopularSites(prefs_, num_sites_)) { | 90 // always be called last to keep metrics as relevant as possible. |
| 91 if (popular_sites_ && NeedPopularSites(prefs_, num_sites_) && |
| 92 ShouldShowPopularSites()) { |
| 126 popular_sites_->StartFetch( | 93 popular_sites_->StartFetch( |
| 127 false, base::Bind(&MostVisitedSites::OnPopularSitesAvailable, | 94 false, base::Bind(&MostVisitedSites::OnPopularSitesAvailable, |
| 128 base::Unretained(this))); | 95 base::Unretained(this))); |
| 129 } else { | 96 } else { |
| 130 waiting_for_popular_sites_ = false; | 97 waiting_for_popular_sites_ = false; |
| 131 } | 98 } |
| 132 | 99 |
| 133 if (top_sites_) { | 100 if (top_sites_) { |
| 134 // TopSites updates itself after a delay. To ensure up-to-date results, | 101 // TopSites updates itself after a delay. To ensure up-to-date results, |
| 135 // force an update now. | 102 // force an update now. |
| (...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 | 402 |
| 436 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, | 403 void MostVisitedSites::TopSitesChanged(TopSites* top_sites, |
| 437 ChangeReason change_reason) { | 404 ChangeReason change_reason) { |
| 438 if (mv_source_ == NTPTileSource::TOP_SITES) { | 405 if (mv_source_ == NTPTileSource::TOP_SITES) { |
| 439 // The displayed tiles are invalidated. | 406 // The displayed tiles are invalidated. |
| 440 InitiateTopSitesQuery(); | 407 InitiateTopSitesQuery(); |
| 441 } | 408 } |
| 442 } | 409 } |
| 443 | 410 |
| 444 } // namespace ntp_tiles | 411 } // namespace ntp_tiles |
| OLD | NEW |