| 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/stringprintf.h" |
| 12 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/browser/net/file_downloader.h" | 15 #include "chrome/browser/net/file_downloader.h" |
| 15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
| 16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 17 | 18 |
| 18 using content::BrowserThread; | 19 using content::BrowserThread; |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const char kPopularSitesFilename[] = "ntp-popular-sites.json"; | 23 const char kPopularSitesURLFormat[] = "https://www.gstatic.com/chrome/ntp/%s"; |
| 23 const char kPopularSitesURL[] = | 24 const char kPopularSitesDefaultFilename[] = "suggested_sites_IN_1.json"; |
| 24 "https://www.gstatic.com/chrome/ntp/suggested_sites_IN_1.json"; | |
| 25 | 25 |
| 26 base::FilePath GetPopularSitesPath() { | 26 std::string GetPopularSitesFilename(const std::string& filename) { |
| 27 return filename.empty() ? kPopularSitesDefaultFilename : filename.c_str(); |
| 28 } |
| 29 |
| 30 base::FilePath GetPopularSitesPath(const std::string& filename) { |
| 27 base::FilePath dir; | 31 base::FilePath dir; |
| 28 PathService::Get(chrome::DIR_USER_DATA, &dir); | 32 PathService::Get(chrome::DIR_USER_DATA, &dir); |
| 29 return dir.AppendASCII(kPopularSitesFilename); | 33 return dir.AppendASCII(GetPopularSitesFilename(filename)); |
| 34 } |
| 35 |
| 36 GURL GetPopularSitesURL(const std::string& filename) { |
| 37 return GURL(base::StringPrintf(kPopularSitesURLFormat, |
| 38 GetPopularSitesFilename(filename).c_str())); |
| 30 } | 39 } |
| 31 | 40 |
| 32 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | 41 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( |
| 33 const base::FilePath& path) { | 42 const base::FilePath& path) { |
| 34 std::string json; | 43 std::string json; |
| 35 if (!base::ReadFileToString(path, &json)) { | 44 if (!base::ReadFileToString(path, &json)) { |
| 36 DLOG(WARNING) << "Failed reading file"; | 45 DLOG(WARNING) << "Failed reading file"; |
| 37 return nullptr; | 46 return nullptr; |
| 38 } | 47 } |
| 39 | 48 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 63 return sites.Pass(); | 72 return sites.Pass(); |
| 64 } | 73 } |
| 65 | 74 |
| 66 } // namespace | 75 } // namespace |
| 67 | 76 |
| 68 PopularSites::Site::Site(const base::string16& title, | 77 PopularSites::Site::Site(const base::string16& title, |
| 69 const GURL& url, | 78 const GURL& url, |
| 70 const GURL& favicon_url) | 79 const GURL& favicon_url) |
| 71 : title(title), url(url), favicon_url(favicon_url) {} | 80 : title(title), url(url), favicon_url(favicon_url) {} |
| 72 | 81 |
| 73 PopularSites::PopularSites(net::URLRequestContextGetter* request_context, | 82 PopularSites::PopularSites(const std::string& filename, |
| 83 net::URLRequestContextGetter* request_context, |
| 74 const FinishedCallback& callback) | 84 const FinishedCallback& callback) |
| 75 : callback_(callback), weak_ptr_factory_(this) { | 85 : callback_(callback), weak_ptr_factory_(this) { |
| 76 base::FilePath path = GetPopularSitesPath(); | 86 base::FilePath path = GetPopularSitesPath(filename); |
| 77 downloader_.reset(new FileDownloader( | 87 downloader_.reset(new FileDownloader( |
| 78 GURL(kPopularSitesURL), path, request_context, | 88 GetPopularSitesURL(filename), path, request_context, |
| 79 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | 89 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| 80 } | 90 } |
| 81 | 91 |
| 82 PopularSites::~PopularSites() {} | 92 PopularSites::~PopularSites() {} |
| 83 | 93 |
| 84 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | 94 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| 85 if (success) { | 95 if (success) { |
| 86 base::PostTaskAndReplyWithResult( | 96 base::PostTaskAndReplyWithResult( |
| 87 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | 97 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| 88 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 98 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| 89 FROM_HERE, | 99 FROM_HERE, |
| 90 base::Bind(&ReadAndParseJsonFile, path), | 100 base::Bind(&ReadAndParseJsonFile, path), |
| 91 base::Bind(&PopularSites::OnJsonParsed, | 101 base::Bind(&PopularSites::OnJsonParsed, |
| 92 weak_ptr_factory_.GetWeakPtr())); | 102 weak_ptr_factory_.GetWeakPtr())); |
| 93 } else { | 103 } else { |
| 94 DLOG(WARNING) << "Download failed"; | 104 DLOG(WARNING) << "Download failed"; |
| 95 callback_.Run(false); | 105 callback_.Run(false); |
| 96 } | 106 } |
| 97 | 107 |
| 98 downloader_.reset(); | 108 downloader_.reset(); |
| 99 } | 109 } |
| 100 | 110 |
| 101 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | 111 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| 102 if (sites) | 112 if (sites) |
| 103 sites_.swap(*sites); | 113 sites_.swap(*sites); |
| 104 else | 114 else |
| 105 sites_.clear(); | 115 sites_.clear(); |
| 106 callback_.Run(!!sites); | 116 callback_.Run(!!sites); |
| 107 } | 117 } |
| OLD | NEW |