| 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/popular_sites.h" | 5 #include "chrome/browser/android/popular_sites.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "components/search_engines/search_engine_type.h" | 21 #include "components/search_engines/search_engine_type.h" |
| 22 #include "components/search_engines/template_url_prepopulate_data.h" | 22 #include "components/search_engines/template_url_prepopulate_data.h" |
| 23 #include "components/search_engines/template_url_service.h" | 23 #include "components/search_engines/template_url_service.h" |
| 24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 | 27 |
| 28 namespace { | 28 namespace { |
| 29 | 29 |
| 30 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; | 30 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; |
| 31 const char kPopularSitesServerFilenameFormat[] = "suggested_sites_%s_2.json"; | 31 const char kPopularSitesServerFilenameFormat[] = "suggested_sites_%s_%s.json"; |
| 32 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 32 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 33 const char kPopularSitesDefaultVersion[] = "2"; |
| 33 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; | 34 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| 34 | 35 |
| 35 | 36 |
| 36 // Find out the country code of the user by using the Google country code if | 37 // Find out the country code of the user by using the Google country code if |
| 37 // Google is the default search engine set. Fallback to a default if we can't | 38 // Google is the default search engine set. Fallback to a default if we can't |
| 38 // make an educated guess. | 39 // make an educated guess. |
| 39 std::string GetCountryCode(Profile* profile) { | 40 std::string GetCountryCode(Profile* profile) { |
| 40 DCHECK(profile); | 41 DCHECK(profile); |
| 41 | 42 |
| 42 const TemplateURLService* template_url_service = | 43 const TemplateURLService* template_url_service = |
| 43 TemplateURLServiceFactory::GetForProfile(profile); | 44 TemplateURLServiceFactory::GetForProfile(profile); |
| 44 DCHECK(template_url_service); | 45 DCHECK(template_url_service); |
| 45 | 46 |
| 46 const TemplateURL* default_provider = | 47 const TemplateURL* default_provider = |
| 47 template_url_service->GetDefaultSearchProvider(); | 48 template_url_service->GetDefaultSearchProvider(); |
| 48 // It's possible to not have a default provider in the case that the default | 49 // It's possible to not have a default provider in the case that the default |
| 49 // search engine is defined by policy. | 50 // search engine is defined by policy. |
| 50 if (!default_provider) | 51 if (!default_provider) |
| 51 return kPopularSitesDefaultCountryCode; | 52 return kPopularSitesDefaultCountryCode; |
| 52 | 53 |
| 53 bool is_google_search_engine = TemplateURLPrepopulateData::GetEngineType( | 54 bool is_google_search_engine = TemplateURLPrepopulateData::GetEngineType( |
| 54 *default_provider, template_url_service->search_terms_data()) == | 55 *default_provider, template_url_service->search_terms_data()) == |
| 55 SearchEngineType::SEARCH_ENGINE_GOOGLE; | 56 SearchEngineType::SEARCH_ENGINE_GOOGLE; |
| 56 | 57 |
| 57 if (!is_google_search_engine) | 58 if (!is_google_search_engine) |
| 58 return kPopularSitesDefaultCountryCode; | 59 return kPopularSitesDefaultCountryCode; |
| 59 | 60 |
| 60 GURL search_url = default_provider->GenerateSearchURL( | 61 GURL search_url = default_provider->GenerateSearchURL( |
| 61 template_url_service->search_terms_data()); | 62 template_url_service->search_terms_data()); |
| 62 | 63 |
| 63 std::string country_code = | 64 std::string country_code = |
| 64 base::ToUpperASCII(google_util::GetGoogleCountryCode(search_url)); | 65 base::ToUpperASCII(google_util::GetGoogleCountryCode(search_url)); |
| 65 | 66 |
| 66 return country_code; | 67 return country_code; |
| 67 } | 68 } |
| 68 | 69 |
| 69 std::string GetPopularSitesServerFilename(Profile* profile, | 70 std::string GetPopularSitesServerFilename( |
| 70 const std::string& filename) { | 71 Profile* profile, |
| 71 if (!filename.empty()) | 72 const std::string& override_country, |
| 72 return filename; | 73 const std::string& override_version, |
| 74 const std::string& override_filename) { |
| 75 if (!override_filename.empty()) |
| 76 return override_filename; |
| 73 | 77 |
| 78 std::string country = !override_country.empty() ? override_country |
| 79 : GetCountryCode(profile); |
| 80 std::string version = !override_version.empty() ? override_version |
| 81 : kPopularSitesDefaultVersion; |
| 74 return base::StringPrintf(kPopularSitesServerFilenameFormat, | 82 return base::StringPrintf(kPopularSitesServerFilenameFormat, |
| 75 GetCountryCode(profile).c_str()); | 83 country.c_str(), version.c_str()); |
| 76 } | 84 } |
| 77 | 85 |
| 78 GURL GetPopularSitesURL(Profile* profile, const std::string& filename) { | 86 GURL GetPopularSitesURL(Profile* profile, |
| 87 const std::string& override_country, |
| 88 const std::string& override_version, |
| 89 const std::string& override_filename) { |
| 79 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 90 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 80 GetPopularSitesServerFilename(profile, filename).c_str())); | 91 GetPopularSitesServerFilename(profile, |
| 92 override_country, |
| 93 override_version, |
| 94 override_filename).c_str())); |
| 81 } | 95 } |
| 82 | 96 |
| 83 base::FilePath GetPopularSitesPath() { | 97 base::FilePath GetPopularSitesPath() { |
| 84 base::FilePath dir; | 98 base::FilePath dir; |
| 85 PathService::Get(chrome::DIR_USER_DATA, &dir); | 99 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 86 return dir.AppendASCII(kPopularSitesLocalFilename); | 100 return dir.AppendASCII(kPopularSitesLocalFilename); |
| 87 } | 101 } |
| 88 | 102 |
| 89 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | 103 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( |
| 90 const base::FilePath& path) { | 104 const base::FilePath& path) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const GURL& favicon_url, | 145 const GURL& favicon_url, |
| 132 const GURL& thumbnail_url) | 146 const GURL& thumbnail_url) |
| 133 : title(title), | 147 : title(title), |
| 134 url(url), | 148 url(url), |
| 135 favicon_url(favicon_url), | 149 favicon_url(favicon_url), |
| 136 thumbnail_url(thumbnail_url) {} | 150 thumbnail_url(thumbnail_url) {} |
| 137 | 151 |
| 138 PopularSites::Site::~Site() {} | 152 PopularSites::Site::~Site() {} |
| 139 | 153 |
| 140 PopularSites::PopularSites(Profile* profile, | 154 PopularSites::PopularSites(Profile* profile, |
| 141 const std::string& filename, | 155 const std::string& override_country, |
| 156 const std::string& override_version, |
| 157 const std::string& override_filename, |
| 142 const FinishedCallback& callback) | 158 const FinishedCallback& callback) |
| 143 : callback_(callback), weak_ptr_factory_(this) { | 159 : callback_(callback), weak_ptr_factory_(this) { |
| 144 base::FilePath path = GetPopularSitesPath(); | 160 base::FilePath path = GetPopularSitesPath(); |
| 145 // Re-download the file once on every Chrome startup, but use the cached | 161 // Re-download the file once on every Chrome startup, but use the cached |
| 146 // local file afterwards. | 162 // local file afterwards. |
| 147 static bool overwrite = true; | 163 static bool overwrite = true; |
| 148 downloader_.reset(new FileDownloader( | 164 downloader_.reset(new FileDownloader( |
| 149 GetPopularSitesURL(profile, filename), path, overwrite, | 165 GetPopularSitesURL( |
| 150 profile->GetRequestContext(), | 166 profile, override_country, override_version, override_filename), |
| 167 path, overwrite, profile->GetRequestContext(), |
| 151 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | 168 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| 152 overwrite = false; | 169 overwrite = false; |
| 153 } | 170 } |
| 154 | 171 |
| 155 PopularSites::~PopularSites() {} | 172 PopularSites::~PopularSites() {} |
| 156 | 173 |
| 157 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | 174 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| 158 if (success) { | 175 if (success) { |
| 159 base::PostTaskAndReplyWithResult( | 176 base::PostTaskAndReplyWithResult( |
| 160 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 177 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 171 downloader_.reset(); | 188 downloader_.reset(); |
| 172 } | 189 } |
| 173 | 190 |
| 174 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 191 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 175 if (sites) | 192 if (sites) |
| 176 sites_.swap(*sites); | 193 sites_.swap(*sites); |
| 177 else | 194 else |
| 178 sites_.clear(); | 195 sites_.clear(); |
| 179 callback_.Run(!!sites); | 196 callback_.Run(!!sites); |
| 180 } | 197 } |
| OLD | NEW |