Chromium Code Reviews| Index: chrome/browser/android/popular_sites.cc |
| diff --git a/chrome/browser/android/popular_sites.cc b/chrome/browser/android/popular_sites.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4f24197d190c764a32220aab8a7a582873ae5da7 |
| --- /dev/null |
| +++ b/chrome/browser/android/popular_sites.cc |
| @@ -0,0 +1,102 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/popular_sites.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/json/json_reader.h" |
| +#include "base/path_service.h" |
| +#include "base/task_runner_util.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/android/file_downloader.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "url/gurl.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +const char kPopularSitesFilename[] = "ntp-popular-sites.json"; |
| +const char kPopularSitesURL[] = |
| + "https://www.gstatic.com/chrome/ntp/prototype0.json"; |
|
Bernhard Bauer
2015/08/03 17:04:51
How about giving this a name that mentions popular
Marc Treib
2015/08/04 08:16:41
popular_sites_IN.json? -> out-of-band discussion
|
| + |
| +base::FilePath GetPopularSitesPath() { |
| + base::FilePath dir; |
| + PathService::Get(chrome::DIR_USER_DATA, &dir); |
| + return dir.AppendASCII(kPopularSitesFilename); |
| +} |
| + |
| +scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( |
| + const base::FilePath& path) { |
| + scoped_ptr<std::vector<PopularSites::Site>> sites( |
| + new std::vector<PopularSites::Site>); |
|
Bernhard Bauer
2015/08/03 17:04:50
Allocate the new vector after you've verified that
Marc Treib
2015/08/04 08:16:41
Done.
|
| + |
| + std::string json; |
| + if (!base::ReadFileToString(path, &json)) { |
| + DLOG(WARNING) << "Failed reading file"; |
| + return sites.Pass(); |
| + } |
| + |
| + scoped_ptr<base::Value> value = |
| + base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); |
| + base::ListValue* list; |
| + if (!value || !value->GetAsList(&list)) { |
| + DLOG(WARNING) << "Failed parsing json"; |
| + return sites.Pass(); |
| + } |
| + |
| + for (size_t i = 0; i < list->GetSize(); i++) { |
| + base::DictionaryValue* item; |
| + if (!list->GetDictionary(i, &item)) |
| + continue; |
| + std::string title; |
| + std::string url; |
| + if (!item->GetString("title", &title) || !item->GetString("url", &url)) |
| + continue; |
| + sites->push_back(PopularSites::Site(title, url)); |
| + } |
| + |
| + return sites.Pass(); |
| +} |
| + |
| +} // namespace |
| + |
| +PopularSites::Site::Site(const std::string& title, const std::string& url) |
| + : title(title), url(url) {} |
| + |
| +PopularSites::PopularSites(net::URLRequestContextGetter* request_context, |
| + const FinishedCallback& callback) |
| + : callback_(callback), weak_ptr_factory_(this) { |
| + base::FilePath path = GetPopularSitesPath(); |
| + downloader_.reset(new FileDownloader( |
| + GURL(kPopularSitesURL), path, request_context, |
| + base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); |
| +} |
| + |
| +PopularSites::~PopularSites() {} |
| + |
| +void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { |
| + if (success) { |
| + base::PostTaskAndReplyWithResult( |
| + BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
| + FROM_HERE, |
| + base::Bind(&ReadAndParseJsonFile, path), |
| + base::Bind(&PopularSites::OnJsonParsed, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } else { |
| + DLOG(WARNING) << "Download failed"; |
| + callback_.Run(false); |
| + } |
| + |
| + downloader_.reset(); |
| +} |
| + |
| +void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { |
| + if (sites) sites_.swap(*sites); |
|
Bernhard Bauer
2015/08/03 17:04:50
That's Java style 😃 Chromium C++ style is to put t
Marc Treib
2015/08/04 08:16:41
Done.
|
| + callback_.Run(!!sites); |
| +} |