Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/android/popular_sites.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/path_service.h" | |
| 12 #include "base/task_runner_util.h" | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/android/file_downloader.h" | |
| 15 #include "chrome/common/chrome_paths.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const char kPopularSitesFilename[] = "ntp-popular-sites.json"; | |
| 24 const char kPopularSitesURL[] = | |
| 25 "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
| |
| 26 | |
| 27 base::FilePath GetPopularSitesPath() { | |
| 28 base::FilePath dir; | |
| 29 PathService::Get(chrome::DIR_USER_DATA, &dir); | |
| 30 return dir.AppendASCII(kPopularSitesFilename); | |
| 31 } | |
| 32 | |
| 33 scoped_ptr<std::vector<PopularSites::Site>> ReadAndParseJsonFile( | |
| 34 const base::FilePath& path) { | |
| 35 scoped_ptr<std::vector<PopularSites::Site>> sites( | |
| 36 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.
| |
| 37 | |
| 38 std::string json; | |
| 39 if (!base::ReadFileToString(path, &json)) { | |
| 40 DLOG(WARNING) << "Failed reading file"; | |
| 41 return sites.Pass(); | |
| 42 } | |
| 43 | |
| 44 scoped_ptr<base::Value> value = | |
| 45 base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS); | |
| 46 base::ListValue* list; | |
| 47 if (!value || !value->GetAsList(&list)) { | |
| 48 DLOG(WARNING) << "Failed parsing json"; | |
| 49 return sites.Pass(); | |
| 50 } | |
| 51 | |
| 52 for (size_t i = 0; i < list->GetSize(); i++) { | |
| 53 base::DictionaryValue* item; | |
| 54 if (!list->GetDictionary(i, &item)) | |
| 55 continue; | |
| 56 std::string title; | |
| 57 std::string url; | |
| 58 if (!item->GetString("title", &title) || !item->GetString("url", &url)) | |
| 59 continue; | |
| 60 sites->push_back(PopularSites::Site(title, url)); | |
| 61 } | |
| 62 | |
| 63 return sites.Pass(); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 68 PopularSites::Site::Site(const std::string& title, const std::string& url) | |
| 69 : title(title), url(url) {} | |
| 70 | |
| 71 PopularSites::PopularSites(net::URLRequestContextGetter* request_context, | |
| 72 const FinishedCallback& callback) | |
| 73 : callback_(callback), weak_ptr_factory_(this) { | |
| 74 base::FilePath path = GetPopularSitesPath(); | |
| 75 downloader_.reset(new FileDownloader( | |
| 76 GURL(kPopularSitesURL), path, request_context, | |
| 77 base::Bind(&PopularSites::OnDownloadDone, base::Unretained(this), path))); | |
| 78 } | |
| 79 | |
| 80 PopularSites::~PopularSites() {} | |
| 81 | |
| 82 void PopularSites::OnDownloadDone(const base::FilePath& path, bool success) { | |
| 83 if (success) { | |
| 84 base::PostTaskAndReplyWithResult( | |
| 85 BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior( | |
| 86 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | |
| 87 FROM_HERE, | |
| 88 base::Bind(&ReadAndParseJsonFile, path), | |
| 89 base::Bind(&PopularSites::OnJsonParsed, | |
| 90 weak_ptr_factory_.GetWeakPtr())); | |
| 91 } else { | |
| 92 DLOG(WARNING) << "Download failed"; | |
| 93 callback_.Run(false); | |
| 94 } | |
| 95 | |
| 96 downloader_.reset(); | |
| 97 } | |
| 98 | |
| 99 void PopularSites::OnJsonParsed(scoped_ptr<std::vector<Site>> sites) { | |
| 100 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.
| |
| 101 callback_.Run(!!sites); | |
| 102 } | |
| OLD | NEW |