Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(381)

Unified Diff: components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc

Issue 2572133002: Add more features to ntp-tiles-internal (Closed)
Patch Set: address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc
diff --git a/components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc b/components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc
index d10a6c05fc90af73deeee19ac03b6fa07ea3ad16..0a0201cf75793ae5d9558a7c830fcbfdac1298e8 100644
--- a/components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc
+++ b/components/ntp_tiles/webui/ntp_tiles_internals_message_handler.cc
@@ -6,8 +6,12 @@
#include "base/bind.h"
#include "base/callback.h"
+#include "base/files/file_util.h"
+#include "base/json/json_reader.h"
+#include "base/json/json_writer.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"
@@ -18,13 +22,44 @@
namespace ntp_tiles {
+namespace {
+
+base::Optional<std::string> ReadFileToString(const base::FilePath& path) {
+ std::string result;
+ if (!base::ReadFileToString(path, &result))
+ return base::Optional<std::string>();
Marc Treib 2016/12/15 10:29:17 Could this just be "base::nullopt"?
sfiera 2017/02/02 17:16:44 Could've, but this went away with today's simpler
+ return std::move(result);
+}
+
+std::string FormatJson(base::Optional<std::string> content) {
+ if (!content) {
+ return "(null)";
+ }
+
+ // If we can, parse it and pretty-print it, since it's minified on disk.
+ // Read JSON in-process, as this is just a debugging page.
+ std::unique_ptr<base::Value> value = base::JSONReader::Read(*content);
+ if (value) {
+ std::string pretty_printed;
+ if (base::JSONWriter::WriteWithOptions(
+ *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed)) {
+ return pretty_printed;
+ }
+ }
+
+ // By default, use the value as-is.
+ return *content;
+}
+
+} // namespace
+
NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() =
default;
NTPTilesInternalsMessageHandlerClient::
~NTPTilesInternalsMessageHandlerClient() = default;
NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler()
- : client_(nullptr), site_count_(8) {}
+ : client_(nullptr), site_count_(8), weak_ptr_factory_(this) {}
NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default;
@@ -40,16 +75,27 @@ void NTPTilesInternalsMessageHandler::RegisterMessages(
client_->RegisterMessageCallback(
"update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate,
base::Unretained(this)));
+
+ client_->RegisterMessageCallback(
+ "fetchSuggestions",
+ base::Bind(&NTPTilesInternalsMessageHandler::HandleFetchSuggestions,
+ base::Unretained(this)));
+
+ client_->RegisterMessageCallback(
+ "viewPopularSitesJson",
+ base::Bind(&NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson,
+ base::Unretained(this)));
}
void NTPTilesInternalsMessageHandler::HandleRegisterForEvents(
const base::ListValue* args) {
DCHECK(args->empty());
- SendSourceInfo();
-
+ suggestions_status_.clear();
+ popular_sites_json_.clear();
most_visited_sites_ = client_->MakeMostVisitedSites();
most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
+ SendSourceInfo();
}
void NTPTilesInternalsMessageHandler::HandleUpdate(
@@ -61,7 +107,9 @@ void NTPTilesInternalsMessageHandler::HandleUpdate(
PrefService* prefs = client_->GetPrefs();
- if (client_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) {
+ if (most_visited_sites_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) {
+ popular_sites_json_.clear();
+
std::string url;
dict->GetString("popular.overrideURL", &url);
if (url.empty()) {
@@ -96,20 +144,60 @@ void NTPTilesInternalsMessageHandler::HandleUpdate(
SendSourceInfo();
}
+void NTPTilesInternalsMessageHandler::HandleFetchSuggestions(
+ const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+ if (!most_visited_sites_->DoesSourceExist(
+ ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE)) {
+ return;
+ }
+
+ if (most_visited_sites_->suggestions()->FetchSuggestionsData()) {
+ suggestions_status_ = "fetching...";
+ } else {
+ suggestions_status_ = "not synced";
+ }
+ SendSourceInfo();
+}
+
+void NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson(
+ const base::ListValue* args) {
+ DCHECK_EQ(0u, args->GetSize());
+ if (!most_visited_sites_->DoesSourceExist(
+ ntp_tiles::NTPTileSource::POPULAR)) {
+ return;
+ }
+
+ const base::FilePath& path =
+ most_visited_sites_->popular_sites()->local_path();
+ base::PostTaskAndReplyWithResult(
+ client_->GetBlockingPool()
+ ->GetTaskRunnerWithShutdownBehavior(
+ base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)
+ .get(),
+ FROM_HERE, base::Bind(&ReadFileToString, path),
+ base::Bind(&NTPTilesInternalsMessageHandler::PopularSitesJsonRead,
+ weak_ptr_factory_.GetWeakPtr()));
+}
+
void NTPTilesInternalsMessageHandler::SendSourceInfo() {
PrefService* prefs = client_->GetPrefs();
base::DictionaryValue value;
- value.SetBoolean("topSites",
- client_->DoesSourceExist(NTPTileSource::TOP_SITES));
- value.SetBoolean(
- "suggestionsService",
- client_->DoesSourceExist(NTPTileSource::SUGGESTIONS_SERVICE));
- value.SetBoolean("whitelist",
- client_->DoesSourceExist(NTPTileSource::WHITELIST));
+ value.SetBoolean("topSites", most_visited_sites_->DoesSourceExist(
+ NTPTileSource::TOP_SITES));
+ value.SetBoolean("whitelist", most_visited_sites_->DoesSourceExist(
+ NTPTileSource::WHITELIST));
- if (client_->DoesSourceExist(NTPTileSource::POPULAR)) {
- auto popular_sites = client_->MakePopularSites();
+ if (most_visited_sites_->DoesSourceExist(
+ NTPTileSource::SUGGESTIONS_SERVICE)) {
+ value.SetString("suggestionsService.status", suggestions_status_);
+ } else {
+ value.SetBoolean("suggestionsService", false);
+ }
+
+ if (most_visited_sites_->DoesSourceExist(NTPTileSource::POPULAR)) {
+ auto popular_sites = most_visited_sites_->popular_sites();
value.SetString("popular.url", popular_sites->GetURLToUse().spec());
value.SetString("popular.country", popular_sites->GetCountryToUse());
value.SetString("popular.version", popular_sites->GetVersionToUse());
@@ -123,6 +211,8 @@ void NTPTilesInternalsMessageHandler::SendSourceInfo() {
value.SetString(
"popular.overrideVersion",
prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion));
+
+ value.SetString("popular.json", popular_sites_json_);
} else {
value.SetBoolean("popular", false);
}
@@ -149,6 +239,12 @@ void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) {
result);
}
+void NTPTilesInternalsMessageHandler::PopularSitesJsonRead(
+ const base::Optional<std::string>& content) {
+ popular_sites_json_ = FormatJson(content);
+ SendSourceInfo();
+}
+
void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable(
const NTPTilesVector& tiles) {
SendTiles(tiles);

Powered by Google App Engine
This is Rietveld 408576698