Chromium Code Reviews| Index: components/ntp_tiles/webui/popular_sites_handler.cc |
| diff --git a/components/ntp_tiles/webui/popular_sites_handler.cc b/components/ntp_tiles/webui/popular_sites_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1b9a4d1772a4af6d88e432e83a6efe05601d8b09 |
| --- /dev/null |
| +++ b/components/ntp_tiles/webui/popular_sites_handler.cc |
| @@ -0,0 +1,171 @@ |
| +// Copyright 2016 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 "components/ntp_tiles/webui/popular_sites_handler.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/task_runner_util.h" |
| +#include "base/values.h" |
| +#include "components/ntp_tiles/popular_sites.h" |
| +#include "components/ntp_tiles/pref_names.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/url_formatter/url_fixer.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +std::string ReadFileToString(const base::FilePath& path) { |
| + std::string result; |
| + if (!base::ReadFileToString(path, &result)) |
| + result.clear(); |
| + return result; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace ntp_tiles { |
| + |
| +PopularSitesInternalsMessageHandlerImpl:: |
| + PopularSitesInternalsMessageHandlerImpl( |
| + PopularSitesInternalsMessageHandlerInterface* web_ui) |
| + : web_ui_(web_ui), weak_ptr_factory_(this) {} |
| + |
| +PopularSitesInternalsMessageHandlerImpl:: |
| + ~PopularSitesInternalsMessageHandlerImpl() = default; |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::RegisterMessages() { |
| + web_ui_->RegisterMessageCallback( |
| + "registerForEvents", |
| + base::Bind( |
| + &PopularSitesInternalsMessageHandlerImpl::HandleRegisterForEvents, |
| + base::Unretained(this))); |
| + |
| + web_ui_->RegisterMessageCallback( |
| + "update", |
| + base::Bind(&PopularSitesInternalsMessageHandlerImpl::HandleUpdate, |
| + base::Unretained(this))); |
| + |
| + web_ui_->RegisterMessageCallback( |
| + "viewJson", |
| + base::Bind(&PopularSitesInternalsMessageHandlerImpl::HandleViewJson, |
| + base::Unretained(this))); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::HandleRegisterForEvents( |
| + const base::ListValue* args) { |
| + DCHECK(args->empty()); |
| + |
| + SendOverrides(); |
| + |
| + popular_sites_ = web_ui_->MakePopularSites(); |
| + popular_sites_->StartFetch( |
| + false, |
| + base::Bind( |
| + &PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable, |
| + base::Unretained(this), false)); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::HandleUpdate( |
| + const base::ListValue* args) { |
| + DCHECK_EQ(3u, args->GetSize()); |
| + |
| + PrefService* prefs = web_ui_->GetPrefs(); |
| + |
| + std::string url; |
| + args->GetString(0, &url); |
| + if (url.empty()) |
| + prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| + else |
| + prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| + url_formatter::FixupURL(url, std::string()).spec()); |
| + |
| + std::string country; |
| + args->GetString(1, &country); |
| + if (country.empty()) |
| + prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| + else |
| + prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); |
| + |
| + std::string version; |
| + args->GetString(2, &version); |
| + if (version.empty()) |
| + prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| + else |
| + prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); |
| + |
| + popular_sites_ = web_ui_->MakePopularSites(); |
| + popular_sites_->StartFetch( |
| + true, |
| + base::Bind( |
| + &PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable, |
| + base::Unretained(this), true)); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::HandleViewJson( |
| + const base::ListValue* args) { |
| + DCHECK_EQ(0u, args->GetSize()); |
| + |
| + const base::FilePath& path = popular_sites_->local_path(); |
| + base::PostTaskAndReplyWithResult( |
| + web_ui_->GetBlockingPool() |
| + ->GetTaskRunnerWithShutdownBehavior( |
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
| + .get(), |
| + FROM_HERE, base::Bind(&ReadFileToString, path), |
| + base::Bind(&PopularSitesInternalsMessageHandlerImpl::SendJson, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::SendOverrides() { |
| + PrefService* prefs = web_ui_->GetPrefs(); |
| + std::string url = |
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| + std::string country = |
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry); |
| + std::string version = |
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion); |
| + web_ui_->CallJavascriptFunction( |
| + "chrome.popular_sites_internals.receiveOverrides", base::StringValue(url), |
| + base::StringValue(country), base::StringValue(version)); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::SendDownloadResult(bool success) { |
| + base::StringValue result(success ? "Success" : "Fail"); |
| + web_ui_->CallJavascriptFunction( |
| + "chrome.popular_sites_internals.receiveDownloadResult", result); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::SendSites() { |
| + std::unique_ptr<base::ListValue> sites_list(new base::ListValue); |
|
sdefresne
2016/10/28 13:34:02
Use base::MakeUnique<>
sfiera
2016/10/28 14:12:42
Done. (this is moved code, but might as well impro
|
| + for (const PopularSites::Site& site : popular_sites_->sites()) { |
| + std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
|
Bernhard Bauer
2016/10/28 13:23:07
Use base::MakeUnique<>?
sfiera
2016/10/28 14:12:42
Done.
|
| + entry->SetString("title", site.title); |
| + entry->SetString("url", site.url.spec()); |
| + sites_list->Append(std::move(entry)); |
| + } |
| + |
| + base::DictionaryValue result; |
| + result.Set("sites", std::move(sites_list)); |
| + result.SetString("url", popular_sites_->LastURL().spec()); |
| + web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveSites", |
| + result); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::SendJson( |
| + const std::string& json) { |
| + web_ui_->CallJavascriptFunction("chrome.popular_sites_internals.receiveJson", |
| + base::StringValue(json)); |
| +} |
| + |
| +void PopularSitesInternalsMessageHandlerImpl::OnPopularSitesAvailable( |
| + bool explicit_request, |
| + bool success) { |
| + if (explicit_request) |
| + SendDownloadResult(success); |
| + SendSites(); |
| +} |
| + |
| +} // namespace ntp_tiles |