Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Side by Side Diff: chrome/browser/android/popular_sites.cc

Issue 1840033002: Move popular_sites.* files to c/b/a/ntp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: synced Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/android/popular_sites.h ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/popular_sites.h"
6
7 #include <stddef.h>
8
9 #include "base/bind.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_reader.h"
14 #include "base/path_service.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/task_runner_util.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/net/file_downloader.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/search_engines/template_url_service_factory.h"
24 #include "chrome/common/chrome_paths.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "components/google/core/browser/google_util.h"
27 #include "components/pref_registry/pref_registry_syncable.h"
28 #include "components/prefs/pref_service.h"
29 #include "components/search_engines/search_engine_type.h"
30 #include "components/search_engines/template_url_prepopulate_data.h"
31 #include "components/search_engines/template_url_service.h"
32 #include "components/variations/service/variations_service.h"
33 #include "content/public/browser/browser_thread.h"
34
35 using content::BrowserThread;
36
37 namespace {
38
39 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s";
40 const char kPopularSitesServerFilenameFormat[] = "suggested_sites_%s_%s.json";
41 const char kPopularSitesDefaultCountryCode[] = "DEFAULT";
42 const char kPopularSitesDefaultVersion[] = "3";
43 const char kPopularSitesLocalFilename[] = "suggested_sites.json";
44 const int kPopularSitesRedownloadIntervalHours = 24;
45
46 const char kPopularSitesLastDownloadPref[] = "popular_sites_last_download";
47
48 // Extract the country from the default search engine if the default search
49 // engine is Google.
50 std::string GetDefaultSearchEngineCountryCode(Profile* profile) {
51 DCHECK(profile);
52
53 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
54 if (!cmd_line->HasSwitch(switches::kEnableNTPSearchEngineCountryDetection))
55 return std::string();
56
57 const TemplateURLService* template_url_service =
58 TemplateURLServiceFactory::GetForProfile(profile);
59 DCHECK(template_url_service);
60
61 const TemplateURL* default_provider =
62 template_url_service->GetDefaultSearchProvider();
63 // It's possible to not have a default provider in the case that the default
64 // search engine is defined by policy.
65 if (default_provider) {
66 bool is_google_search_engine =
67 TemplateURLPrepopulateData::GetEngineType(
68 *default_provider, template_url_service->search_terms_data()) ==
69 SearchEngineType::SEARCH_ENGINE_GOOGLE;
70
71 if (is_google_search_engine) {
72 GURL search_url = default_provider->GenerateSearchURL(
73 template_url_service->search_terms_data());
74 return google_util::GetGoogleCountryCode(search_url);
75 }
76 }
77
78 return std::string();
79 }
80
81 // Get the country that the experiment is running under
82 std::string GetVariationsServiceCountry() {
83 DCHECK(g_browser_process);
84 variations::VariationsService* variations_service =
85 g_browser_process->variations_service();
86 if (variations_service)
87 return variations_service->GetStoredPermanentCountry();
88 return std::string();
89 }
90
91 // Find out the country code of the user by using the Google country code if
92 // Google is the default search engine set. If Google is not the default search
93 // engine use the country provided by VariationsService. Fallback to a default
94 // if we can't make an educated guess.
95 std::string GetCountryCode(Profile* profile) {
96 std::string country_code = GetDefaultSearchEngineCountryCode(profile);
97
98 if (country_code.empty())
99 country_code = GetVariationsServiceCountry();
100
101 if (country_code.empty())
102 country_code = kPopularSitesDefaultCountryCode;
103
104 return base::ToUpperASCII(country_code);
105 }
106
107 std::string GetPopularSitesServerFilename(
108 Profile* profile,
109 const std::string& override_country,
110 const std::string& override_version) {
111 std::string country =
112 !override_country.empty() ? override_country : GetCountryCode(profile);
113 std::string version = !override_version.empty() ? override_version
114 : kPopularSitesDefaultVersion;
115 return base::StringPrintf(kPopularSitesServerFilenameFormat,
116 country.c_str(), version.c_str());
117 }
118
119 GURL GetPopularSitesURL(Profile* profile,
120 const std::string& override_country,
121 const std::string& override_version) {
122 return GURL(base::StringPrintf(
123 kPopularSitesURLFormat,
124 GetPopularSitesServerFilename(profile, override_country, override_version)
125 .c_str()));
126 }
127
128 GURL GetPopularSitesFallbackURL(Profile* profile) {
129 return GetPopularSitesURL(profile, kPopularSitesDefaultCountryCode,
130 kPopularSitesDefaultVersion);
131 }
132
133 base::FilePath GetPopularSitesPath() {
134 base::FilePath dir;
135 PathService::Get(chrome::DIR_USER_DATA, &dir);
136 return dir.AppendASCII(kPopularSitesLocalFilename);
137 }
138
139 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile(
140 const base::FilePath& path) {
141 std::string json;
142 if (!base::ReadFileToString(path, &json)) {
143 DLOG(WARNING) << "Failed reading file";
144 return nullptr;
145 }
146
147 scoped_ptr<base::Value> value =
148 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS);
149 base::ListValue* list;
150 if (!value || !value->GetAsList(&list)) {
151 DLOG(WARNING) << "Failed parsing json";
152 return nullptr;
153 }
154
155 scoped_ptr<std::vector<PopularSites::Site>> sites(
156 new std::vector<PopularSites::Site>);
157 for (size_t i = 0; i < list->GetSize(); i++) {
158 base::DictionaryValue* item;
159 if (!list->GetDictionary(i, &item))
160 continue;
161 base::string16 title;
162 std::string url;
163 if (!item->GetString("title", &title) || !item->GetString("url", &url))
164 continue;
165 std::string favicon_url;
166 item->GetString("favicon_url", &favicon_url);
167 std::string thumbnail_url;
168 item->GetString("thumbnail_url", &thumbnail_url);
169 std::string large_icon_url;
170 item->GetString("large_icon_url", &large_icon_url);
171
172 sites->push_back(PopularSites::Site(title, GURL(url), GURL(favicon_url),
173 GURL(large_icon_url),
174 GURL(thumbnail_url)));
175 }
176
177 return sites;
178 }
179
180 } // namespace
181
182 PopularSites::Site::Site(const base::string16& title,
183 const GURL& url,
184 const GURL& favicon_url,
185 const GURL& large_icon_url,
186 const GURL& thumbnail_url)
187 : title(title),
188 url(url),
189 favicon_url(favicon_url),
190 large_icon_url(large_icon_url),
191 thumbnail_url(thumbnail_url) {}
192
193 PopularSites::Site::~Site() {}
194
195 PopularSites::PopularSites(Profile* profile,
196 const std::string& override_country,
197 const std::string& override_version,
198 bool force_download,
199 const FinishedCallback& callback)
200 : PopularSites(profile,
201 GetPopularSitesURL(profile, override_country,
202 override_version),
203 force_download,
204 callback) {}
205
206 PopularSites::PopularSites(Profile* profile,
207 const GURL& url,
208 const FinishedCallback& callback)
209 : PopularSites(profile, url, true, callback) {}
210
211 PopularSites::~PopularSites() {}
212
213 // static
214 void PopularSites::RegisterProfilePrefs(
215 user_prefs::PrefRegistrySyncable* user_prefs) {
216 user_prefs->RegisterInt64Pref(kPopularSitesLastDownloadPref, 0);
217 }
218
219 PopularSites::PopularSites(Profile* profile,
220 const GURL& url,
221 bool force_download,
222 const FinishedCallback& callback)
223 : callback_(callback),
224 popular_sites_local_path_(GetPopularSitesPath()),
225 profile_(profile),
226 weak_ptr_factory_(this) {
227 const base::Time last_download_time = base::Time::FromInternalValue(
228 profile_->GetPrefs()->GetInt64(kPopularSitesLastDownloadPref));
229 const base::TimeDelta time_since_last_download =
230 base::Time::Now() - last_download_time;
231 const base::TimeDelta redownload_interval =
232 base::TimeDelta::FromHours(kPopularSitesRedownloadIntervalHours);
233 const bool download_time_is_future = base::Time::Now() < last_download_time;
234
235 const bool should_redownload_if_exists =
236 force_download || download_time_is_future ||
237 (time_since_last_download > redownload_interval);
238
239 FetchPopularSites(url, should_redownload_if_exists, false /* is_fallback */);
240 }
241
242 void PopularSites::FetchPopularSites(const GURL& url,
243 bool force_download,
244 bool is_fallback) {
245 downloader_.reset(
246 new FileDownloader(url, popular_sites_local_path_, force_download,
247 profile_->GetRequestContext(),
248 base::Bind(&PopularSites::OnDownloadDone,
249 base::Unretained(this), is_fallback)));
250 }
251
252 void PopularSites::OnDownloadDone(bool is_fallback, bool success) {
253 downloader_.reset();
254 if (success) {
255 profile_->GetPrefs()->SetInt64(kPopularSitesLastDownloadPref,
256 base::Time::Now().ToInternalValue());
257 ParseSiteList(popular_sites_local_path_);
258 } else if (!is_fallback) {
259 DLOG(WARNING) << "Download country site list failed";
260 // It is fine to force the download as Fallback is only triggered after a
261 // failed download.
262 FetchPopularSites(GetPopularSitesFallbackURL(profile_),
263 true /* force_download */, true /* is_fallback */);
264 } else {
265 DLOG(WARNING) << "Download fallback site list failed";
266 callback_.Run(false);
267 }
268 }
269
270 void PopularSites::ParseSiteList(const base::FilePath& path) {
271 base::PostTaskAndReplyWithResult(
272 BrowserThread::GetBlockingPool()
273 ->GetTaskRunnerWithShutdownBehavior(
274 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
275 .get(),
276 FROM_HERE, base::Bind(&ReadAndParseJsonFile, path),
277 base::Bind(&PopularSites::OnJsonParsed, weak_ptr_factory_.GetWeakPtr()));
278 }
279
280 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) {
281 if (sites)
282 sites_.swap(*sites);
283 else
284 sites_.clear();
285 callback_.Run(!!sites);
286 }
OLDNEW
« no previous file with comments | « chrome/browser/android/popular_sites.h ('k') | chrome/browser/prefs/browser_prefs.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698