| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 thumbnail_url(thumbnail_url) {} | 135 thumbnail_url(thumbnail_url) {} |
| 136 | 136 |
| 137 PopularSites::Site::~Site() {} | 137 PopularSites::Site::~Site() {} |
| 138 | 138 |
| 139 PopularSites::PopularSites(Profile* profile, | 139 PopularSites::PopularSites(Profile* profile, |
| 140 const std::string& filename, | 140 const std::string& filename, |
| 141 net::URLRequestContextGetter* request_context, | 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(profile, filename); |
| 145 // Re-download the file once on every Chrome startup, but use the cached |
| 146 // local file afterwards. |
| 147 static bool overwrite = true; |
| 145 downloader_.reset(new FileDownloader( | 148 downloader_.reset(new FileDownloader( |
| 146 GetPopularSitesURL(profile, filename), path, request_context, | 149 GetPopularSitesURL(profile, filename), path, overwrite, request_context, |
| 147 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | 150 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| 151 overwrite = false; |
| 148 } | 152 } |
| 149 | 153 |
| 150 PopularSites::~PopularSites() {} | 154 PopularSites::~PopularSites() {} |
| 151 | 155 |
| 152 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | 156 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| 153 if (success) { | 157 if (success) { |
| 154 base::PostTaskAndReplyWithResult( | 158 base::PostTaskAndReplyWithResult( |
| 155 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 159 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 156 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 160 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| 157 FROM_HERE, | 161 FROM_HERE, |
| 158 base::Bind(&ReadAndParseJsonFile, path), | 162 base::Bind(&ReadAndParseJsonFile, path), |
| 159 base::Bind(&PopularSites::OnJsonParsed, | 163 base::Bind(&PopularSites::OnJsonParsed, |
| 160 weak_ptr_factory_.GetWeakPtr())); | 164 weak_ptr_factory_.GetWeakPtr())); |
| 161 } else { | 165 } else { |
| 162 DLOG(WARNING) << "Download failed"; | 166 DLOG(WARNING) << "Download failed"; |
| 163 callback_.Run(false); | 167 callback_.Run(false); |
| 164 } | 168 } |
| 165 | 169 |
| 166 downloader_.reset(); | 170 downloader_.reset(); |
| 167 } | 171 } |
| 168 | 172 |
| 169 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 173 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 170 if (sites) | 174 if (sites) |
| 171 sites_.swap(*sites); | 175 sites_.swap(*sites); |
| 172 else | 176 else |
| 173 sites_.clear(); | 177 sites_.clear(); |
| 174 callback_.Run(!!sites); | 178 callback_.Run(!!sites); |
| 175 } | 179 } |
| OLD | NEW |