| Index: components/ntp_tiles/webui/site_tiles_internals_message_handler.cc
|
| diff --git a/components/ntp_tiles/webui/site_tiles_internals_message_handler.cc b/components/ntp_tiles/webui/site_tiles_internals_message_handler.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..a094adc4f4a9f7f0c2fbe4bff62dea5a0844b49a
|
| --- /dev/null
|
| +++ b/components/ntp_tiles/webui/site_tiles_internals_message_handler.cc
|
| @@ -0,0 +1,205 @@
|
| +// 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/site_tiles_internals_message_handler.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/callback.h"
|
| +#include "base/files/file_util.h"
|
| +#include "base/logging.h"
|
| +#include "base/memory/ptr_util.h"
|
| +#include "base/task_runner_util.h"
|
| +#include "base/values.h"
|
| +#include "components/ntp_tiles/most_visited_sites.h"
|
| +#include "components/ntp_tiles/pref_names.h"
|
| +#include "components/ntp_tiles/webui/site_tiles_internals_message_handler_client.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 {
|
| +
|
| +SiteTilesInternalsMessageHandlerClient::
|
| + SiteTilesInternalsMessageHandlerClient() = default;
|
| +SiteTilesInternalsMessageHandlerClient::
|
| + ~SiteTilesInternalsMessageHandlerClient() = default;
|
| +
|
| +SiteTilesInternalsMessageHandler::SiteTilesInternalsMessageHandler(
|
| + SiteTilesInternalsMessageHandlerClient* web_ui)
|
| + : web_ui_(web_ui), site_count_(8), weak_ptr_factory_(this) {}
|
| +
|
| +SiteTilesInternalsMessageHandler::~SiteTilesInternalsMessageHandler() = default;
|
| +
|
| +void SiteTilesInternalsMessageHandler::RegisterMessages() {
|
| + web_ui_->RegisterMessageCallback(
|
| + "registerForEvents",
|
| + base::Bind(&SiteTilesInternalsMessageHandler::HandleRegisterForEvents,
|
| + base::Unretained(this)));
|
| +
|
| + web_ui_->RegisterMessageCallback(
|
| + "update", base::Bind(&SiteTilesInternalsMessageHandler::HandleUpdate,
|
| + base::Unretained(this)));
|
| +
|
| + web_ui_->RegisterMessageCallback(
|
| + "viewPopularSitesJson",
|
| + base::Bind(&SiteTilesInternalsMessageHandler::HandleViewPopularSitesJson,
|
| + base::Unretained(this)));
|
| +
|
| + web_ui_->RegisterMessageCallback(
|
| + "getFavicon",
|
| + base::Bind(&SiteTilesInternalsMessageHandler::HandleGetFavicon,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::HandleRegisterForEvents(
|
| + const base::ListValue* args) {
|
| + DCHECK(args->empty());
|
| +
|
| + SendSourceInfo();
|
| +
|
| + most_visited_sites_ = web_ui_->MakeMostVisitedSites();
|
| + most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::HandleUpdate(
|
| + const base::ListValue* args) {
|
| + const base::DictionaryValue* dict;
|
| + DCHECK_EQ(1u, args->GetSize());
|
| + DCHECK(args->GetDictionary(0, &dict));
|
| +
|
| + PrefService* prefs = web_ui_->GetPrefs();
|
| +
|
| + if (web_ui_->IsSourceEnabled(ntp_tiles::NTPTileSource::POPULAR)) {
|
| + std::string url;
|
| + dict->GetString("popular.overrideURL", &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;
|
| + dict->GetString("popular.overrideCountry", &country);
|
| + if (country.empty())
|
| + prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry);
|
| + else
|
| + prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country);
|
| +
|
| + std::string version;
|
| + dict->GetString("popular.overrideVersion", &version);
|
| + if (version.empty())
|
| + prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion);
|
| + else
|
| + prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version);
|
| + }
|
| +
|
| + // Recreate to pick up new values.
|
| + most_visited_sites_ = web_ui_->MakeMostVisitedSites();
|
| + most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
|
| + SendSourceInfo();
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::HandleViewPopularSitesJson(
|
| + const base::ListValue* args) {
|
| + DCHECK_EQ(0u, args->GetSize());
|
| +
|
| + const base::FilePath& path = web_ui_->MakePopularSites()->local_path();
|
| + base::PostTaskAndReplyWithResult(
|
| + web_ui_->GetBlockingPool()
|
| + ->GetTaskRunnerWithShutdownBehavior(
|
| + base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
|
| + .get(),
|
| + FROM_HERE, base::Bind(&ReadFileToString, path),
|
| + base::Bind(&SiteTilesInternalsMessageHandler::SendPopularSitesJson,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::HandleGetFavicon(
|
| + const base::ListValue* args) {
|
| + DCHECK_EQ(1u, args->GetSize());
|
| + // TODO(sfiera): implement
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::SendSourceInfo() {
|
| + PrefService* prefs = web_ui_->GetPrefs();
|
| + base::DictionaryValue value;
|
| +
|
| + value.SetBoolean("topSites",
|
| + web_ui_->IsSourceEnabled(NTPTileSource::TOP_SITES));
|
| + value.SetBoolean(
|
| + "suggestionsService",
|
| + web_ui_->IsSourceEnabled(NTPTileSource::SUGGESTIONS_SERVICE));
|
| + value.SetBoolean("whitelist",
|
| + web_ui_->IsSourceEnabled(NTPTileSource::WHITELIST));
|
| +
|
| + if (web_ui_->IsSourceEnabled(NTPTileSource::POPULAR)) {
|
| + auto popular_sites = web_ui_->MakePopularSites();
|
| + value.SetString("popular.url", popular_sites->GetURLToUse().spec());
|
| + value.SetString("popular.country", popular_sites->GetCountryToUse());
|
| + value.SetString("popular.version", popular_sites->GetVersionToUse());
|
| +
|
| + value.SetString(
|
| + "popular.overrideURL",
|
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL));
|
| + value.SetString(
|
| + "popular.overrideCountry",
|
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry));
|
| + value.SetString(
|
| + "popular.overrideVersion",
|
| + prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion));
|
| + } else {
|
| + value.SetBoolean("popular", false);
|
| + }
|
| +
|
| + web_ui_->CallJavascriptFunction(
|
| + "chrome.site_tiles_internals.receiveSourceInfo", value);
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) {
|
| + auto sites_list = base::MakeUnique<base::ListValue>();
|
| + for (const NTPTile& tile : tiles) {
|
| + auto entry = base::MakeUnique<base::DictionaryValue>();
|
| + entry->SetString("title", tile.title);
|
| + entry->SetString("url", tile.url.spec());
|
| + entry->SetInteger("source", static_cast<int>(tile.source));
|
| + if (tile.source == NTPTileSource::WHITELIST) {
|
| + entry->SetString("whitelistIconPath",
|
| + tile.whitelist_icon_path.AsUTF8Unsafe());
|
| + }
|
| + sites_list->Append(std::move(entry));
|
| + }
|
| +
|
| + base::DictionaryValue result;
|
| + result.Set("sites", std::move(sites_list));
|
| + web_ui_->CallJavascriptFunction("chrome.site_tiles_internals.receiveSites",
|
| + result);
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::SendPopularSitesJson(
|
| + const std::string& json) {
|
| + web_ui_->CallJavascriptFunction(
|
| + "chrome.site_tiles_internals.receivePopularSitesJson",
|
| + base::StringValue(json));
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::OnMostVisitedURLsAvailable(
|
| + const NTPTilesVector& tiles) {
|
| + SendTiles(tiles);
|
| +}
|
| +
|
| +void SiteTilesInternalsMessageHandler::OnIconMadeAvailable(
|
| + const GURL& site_url) {}
|
| +
|
| +} // namespace ntp_tiles
|
|
|