| 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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 url(url), | 148 url(url), |
| 149 favicon_url(favicon_url), | 149 favicon_url(favicon_url), |
| 150 thumbnail_url(thumbnail_url) {} | 150 thumbnail_url(thumbnail_url) {} |
| 151 | 151 |
| 152 PopularSites::Site::~Site() {} | 152 PopularSites::Site::~Site() {} |
| 153 | 153 |
| 154 PopularSites::PopularSites(Profile* profile, | 154 PopularSites::PopularSites(Profile* profile, |
| 155 const std::string& override_country, | 155 const std::string& override_country, |
| 156 const std::string& override_version, | 156 const std::string& override_version, |
| 157 const std::string& override_filename, | 157 const std::string& override_filename, |
| 158 bool force_download, |
| 158 const FinishedCallback& callback) | 159 const FinishedCallback& callback) |
| 159 : callback_(callback), weak_ptr_factory_(this) { | 160 : callback_(callback), weak_ptr_factory_(this) { |
| 160 base::FilePath path = GetPopularSitesPath(); | 161 base::FilePath path = GetPopularSitesPath(); |
| 161 // Re-download the file once on every Chrome startup, but use the cached | 162 // Re-download the file once on every Chrome startup, but use the cached |
| 162 // local file afterwards. | 163 // local file afterwards. |
| 163 static bool overwrite = true; | 164 static bool first_time = true; |
| 164 downloader_.reset(new FileDownloader( | 165 downloader_.reset(new FileDownloader( |
| 165 GetPopularSitesURL( | 166 GetPopularSitesURL( |
| 166 profile, override_country, override_version, override_filename), | 167 profile, override_country, override_version, override_filename), |
| 167 path, overwrite, profile->GetRequestContext(), | 168 path, first_time || force_download, profile->GetRequestContext(), |
| 168 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | 169 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| 169 overwrite = false; | 170 first_time = false; |
| 170 } | 171 } |
| 171 | 172 |
| 172 PopularSites::~PopularSites() {} | 173 PopularSites::~PopularSites() {} |
| 173 | 174 |
| 174 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | 175 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| 175 if (success) { | 176 if (success) { |
| 176 base::PostTaskAndReplyWithResult( | 177 base::PostTaskAndReplyWithResult( |
| 177 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 178 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 178 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 179 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| 179 FROM_HERE, | 180 FROM_HERE, |
| 180 base::Bind(&ReadAndParseJsonFile, path), | 181 base::Bind(&ReadAndParseJsonFile, path), |
| 181 base::Bind(&PopularSites::OnJsonParsed, | 182 base::Bind(&PopularSites::OnJsonParsed, |
| 182 weak_ptr_factory_.GetWeakPtr())); | 183 weak_ptr_factory_.GetWeakPtr())); |
| 183 } else { | 184 } else { |
| 184 DLOG(WARNING) << "Download failed"; | 185 DLOG(WARNING) << "Download failed"; |
| 185 callback_.Run(false); | 186 callback_.Run(false); |
| 186 } | 187 } |
| 187 | 188 |
| 188 downloader_.reset(); | 189 downloader_.reset(); |
| 189 } | 190 } |
| 190 | 191 |
| 191 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 192 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 192 if (sites) | 193 if (sites) |
| 193 sites_.swap(*sites); | 194 sites_.swap(*sites); |
| 194 else | 195 else |
| 195 sites_.clear(); | 196 sites_.clear(); |
| 196 callback_.Run(!!sites); | 197 callback_.Run(!!sites); |
| 197 } | 198 } |
| OLD | NEW |