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

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

Issue 2557103004: Add chrome://site-tiles-internals/ (Closed)
Patch Set: Address review 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/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..66632d07a417871b1e78c3505eca963e23611673
--- /dev/null
+++ b/components/ntp_tiles/webui/site_tiles_internals_message_handler.cc
@@ -0,0 +1,167 @@
+// 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 ntp_tiles {
+
+SiteTilesInternalsMessageHandlerClient::
+ SiteTilesInternalsMessageHandlerClient() = default;
+SiteTilesInternalsMessageHandlerClient::
+ ~SiteTilesInternalsMessageHandlerClient() = default;
+
+SiteTilesInternalsMessageHandler::SiteTilesInternalsMessageHandler()
+ : site_count_(8), weak_ptr_factory_(this) {}
+
+SiteTilesInternalsMessageHandler::~SiteTilesInternalsMessageHandler() = default;
+
+void SiteTilesInternalsMessageHandler::RegisterMessages(
+ SiteTilesInternalsMessageHandlerClient* client) {
+ client_ = client;
+
+ client_->RegisterMessageCallback(
+ "registerForEvents",
+ base::Bind(&SiteTilesInternalsMessageHandler::HandleRegisterForEvents,
+ base::Unretained(this)));
+
+ client_->RegisterMessageCallback(
+ "update", base::Bind(&SiteTilesInternalsMessageHandler::HandleUpdate,
+ base::Unretained(this)));
+
+ client_->RegisterMessageCallback(
+ "getFavicon",
Marc Treib 2016/12/09 10:52:11 This doesn't seem to be used yet?
sfiera 2016/12/09 17:35:28 Removed. As you can see, this CL started out more
+ base::Bind(&SiteTilesInternalsMessageHandler::HandleGetFavicon,
+ base::Unretained(this)));
+}
+
+void SiteTilesInternalsMessageHandler::HandleRegisterForEvents(
+ const base::ListValue* args) {
+ DCHECK(args->empty());
+
+ SendSourceInfo();
+
+ most_visited_sites_ = client_->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));
Marc Treib 2016/12/09 10:52:11 This will break in release builds.
sfiera 2016/12/09 17:35:28 Done.
+
+ PrefService* prefs = client_->GetPrefs();
+
+ if (client_->IsSourceEnabled(ntp_tiles::NTPTileSource::POPULAR)) {
+ std::string url;
+ dict->GetString("popular.overrideURL", &url);
+ if (url.empty())
Marc Treib 2016/12/09 10:52:11 Braces :)
sfiera 2016/12/09 17:35:28 Done.
+ 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_ = client_->MakeMostVisitedSites();
Marc Treib 2016/12/09 10:52:11 After https://codereview.chromium.org/2532103002/,
sfiera 2016/12/09 17:35:28 Is it going to have the effect of recreating Popul
Marc Treib 2016/12/12 09:42:18 It doesn't do that yet, but it definitely should.
+ most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_);
+ SendSourceInfo();
+}
+
+void SiteTilesInternalsMessageHandler::HandleGetFavicon(
+ const base::ListValue* args) {
+ DCHECK_EQ(1u, args->GetSize());
+ // TODO(sfiera): implement
+}
+
+void SiteTilesInternalsMessageHandler::SendSourceInfo() {
+ PrefService* prefs = client_->GetPrefs();
+ base::DictionaryValue value;
+
+ value.SetBoolean("topSites",
+ client_->IsSourceEnabled(NTPTileSource::TOP_SITES));
+ value.SetBoolean(
+ "suggestionsService",
+ client_->IsSourceEnabled(NTPTileSource::SUGGESTIONS_SERVICE));
+ value.SetBoolean("whitelist",
+ client_->IsSourceEnabled(NTPTileSource::WHITELIST));
+
+ if (client_->IsSourceEnabled(NTPTileSource::POPULAR)) {
+ auto popular_sites = client_->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);
+ }
+
+ client_->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));
+ entry->SetString("whitelistIconPath",
+ tile.whitelist_icon_path.LossyDisplayName());
+ sites_list->Append(std::move(entry));
+ }
+
+ base::DictionaryValue result;
+ result.Set("sites", std::move(sites_list));
+ client_->CallJavascriptFunction("chrome.site_tiles_internals.receiveSites",
+ result);
+}
+
+void SiteTilesInternalsMessageHandler::OnMostVisitedURLsAvailable(
+ const NTPTilesVector& tiles) {
+ SendTiles(tiles);
+}
+
+void SiteTilesInternalsMessageHandler::OnIconMadeAvailable(
+ const GURL& site_url) {}
+
+} // namespace ntp_tiles

Powered by Google App Engine
This is Rietveld 408576698