| 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" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/net/file_downloader.h" | 16 #include "chrome/browser/net/file_downloader.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "components/google/core/browser/google_util.h" | 20 #include "components/google/core/browser/google_util.h" |
| 20 #include "components/search_engines/search_engine_type.h" | 21 #include "components/search_engines/search_engine_type.h" |
| 21 #include "components/search_engines/template_url_prepopulate_data.h" | 22 #include "components/search_engines/template_url_prepopulate_data.h" |
| 22 #include "components/search_engines/template_url_service.h" | 23 #include "components/search_engines/template_url_service.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 | 25 |
| 25 using content::BrowserThread; | 26 using content::BrowserThread; |
| 26 | 27 |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; | 30 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; |
| 30 const char kPopularSitesFilenameFormat[] = "suggested_sites_%s_2.json"; | 31 const char kPopularSitesServerFilenameFormat[] = "suggested_sites_%s_2.json"; |
| 31 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; | 32 const char kPopularSitesDefaultCountryCode[] = "DEFAULT"; |
| 33 const char kPopularSitesLocalFilename[] = "suggested_sites.json"; |
| 32 | 34 |
| 33 | 35 |
| 34 // Find out the country code of the user by using the Google country code if | 36 // Find out the country code of the user by using the Google country code if |
| 35 // Google is the default search engine set. Fallback to a default if we can't | 37 // Google is the default search engine set. Fallback to a default if we can't |
| 36 // make an educated guess. | 38 // make an educated guess. |
| 37 std::string GetCountryCode(Profile* profile) { | 39 std::string GetCountryCode(Profile* profile) { |
| 38 DCHECK(profile); | 40 DCHECK(profile); |
| 39 | 41 |
| 40 const TemplateURLService* template_url_service = | 42 const TemplateURLService* template_url_service = |
| 41 TemplateURLServiceFactory::GetForProfile(profile); | 43 TemplateURLServiceFactory::GetForProfile(profile); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 57 | 59 |
| 58 GURL search_url = default_provider->GenerateSearchURL( | 60 GURL search_url = default_provider->GenerateSearchURL( |
| 59 template_url_service->search_terms_data()); | 61 template_url_service->search_terms_data()); |
| 60 | 62 |
| 61 std::string country_code = | 63 std::string country_code = |
| 62 base::ToUpperASCII(google_util::GetGoogleCountryCode(search_url)); | 64 base::ToUpperASCII(google_util::GetGoogleCountryCode(search_url)); |
| 63 | 65 |
| 64 return country_code; | 66 return country_code; |
| 65 } | 67 } |
| 66 | 68 |
| 67 std::string GetPopularSitesFilename(Profile* profile, | 69 std::string GetPopularSitesServerFilename(Profile* profile, |
| 68 const std::string& filename) { | 70 const std::string& filename) { |
| 69 if (!filename.empty()) | 71 if (!filename.empty()) |
| 70 return filename; | 72 return filename; |
| 71 | 73 |
| 72 return base::StringPrintf(kPopularSitesFilenameFormat, | 74 return base::StringPrintf(kPopularSitesServerFilenameFormat, |
| 73 GetCountryCode(profile).c_str()); | 75 GetCountryCode(profile).c_str()); |
| 74 } | 76 } |
| 75 | 77 |
| 76 base::FilePath GetPopularSitesPath(Profile* profile, | |
| 77 const std::string& filename) { | |
| 78 base::FilePath dir; | |
| 79 PathService::Get(chrome::DIR_USER_DATA, &dir); | |
| 80 return dir.AppendASCII(GetPopularSitesFilename(profile, filename)); | |
| 81 } | |
| 82 | |
| 83 GURL GetPopularSitesURL(Profile* profile, const std::string& filename) { | 78 GURL GetPopularSitesURL(Profile* profile, const std::string& filename) { |
| 84 return GURL(base::StringPrintf(kPopularSitesURLFormat, | 79 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 85 GetPopularSitesFilename(profile, filename).c_str())); | 80 GetPopularSitesServerFilename(profile, filename).c_str())); |
| 81 } |
| 82 |
| 83 base::FilePath GetPopularSitesPath() { |
| 84 base::FilePath dir; |
| 85 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 86 return dir.AppendASCII(kPopularSitesLocalFilename); |
| 86 } | 87 } |
| 87 | 88 |
| 88 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | 89 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( |
| 89 const base::FilePath& path) { | 90 const base::FilePath& path) { |
| 90 std::string json; | 91 std::string json; |
| 91 if (!base::ReadFileToString(path, &json)) { | 92 if (!base::ReadFileToString(path, &json)) { |
| 92 DLOG(WARNING) << "Failed reading file"; | 93 DLOG(WARNING) << "Failed reading file"; |
| 93 return nullptr; | 94 return nullptr; |
| 94 } | 95 } |
| 95 | 96 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const GURL& thumbnail_url) | 132 const GURL& thumbnail_url) |
| 132 : title(title), | 133 : title(title), |
| 133 url(url), | 134 url(url), |
| 134 favicon_url(favicon_url), | 135 favicon_url(favicon_url), |
| 135 thumbnail_url(thumbnail_url) {} | 136 thumbnail_url(thumbnail_url) {} |
| 136 | 137 |
| 137 PopularSites::Site::~Site() {} | 138 PopularSites::Site::~Site() {} |
| 138 | 139 |
| 139 PopularSites::PopularSites(Profile* profile, | 140 PopularSites::PopularSites(Profile* profile, |
| 140 const std::string& filename, | 141 const std::string& filename, |
| 141 net::URLRequestContextGetter* request_context, | |
| 142 const FinishedCallback& callback) | 142 const FinishedCallback& callback) |
| 143 : callback_(callback), weak_ptr_factory_(this) { | 143 : callback_(callback), weak_ptr_factory_(this) { |
| 144 base::FilePath path = GetPopularSitesPath(profile, filename); | 144 base::FilePath path = GetPopularSitesPath(); |
| 145 // Re-download the file once on every Chrome startup, but use the cached | 145 // Re-download the file once on every Chrome startup, but use the cached |
| 146 // local file afterwards. | 146 // local file afterwards. |
| 147 static bool overwrite = true; | 147 static bool overwrite = true; |
| 148 downloader_.reset(new FileDownloader( | 148 downloader_.reset(new FileDownloader( |
| 149 GetPopularSitesURL(profile, filename), path, overwrite, request_context, | 149 GetPopularSitesURL(profile, filename), path, overwrite, |
| 150 profile->GetRequestContext(), |
| 150 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | 151 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| 151 overwrite = false; | 152 overwrite = false; |
| 152 } | 153 } |
| 153 | 154 |
| 154 PopularSites::~PopularSites() {} | 155 PopularSites::~PopularSites() {} |
| 155 | 156 |
| 156 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | 157 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| 157 if (success) { | 158 if (success) { |
| 158 base::PostTaskAndReplyWithResult( | 159 base::PostTaskAndReplyWithResult( |
| 159 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 160 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 170 downloader_.reset(); | 171 downloader_.reset(); |
| 171 } | 172 } |
| 172 | 173 |
| 173 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 174 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 174 if (sites) | 175 if (sites) |
| 175 sites_.swap(*sites); | 176 sites_.swap(*sites); |
| 176 else | 177 else |
| 177 sites_.clear(); | 178 sites_.clear(); |
| 178 callback_.Run(!!sites); | 179 callback_.Run(!!sites); |
| 179 } | 180 } |
| OLD | NEW |