Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "components/ntp_tiles/webui/site_tiles_internals_message_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/task_runner_util.h" | |
| 13 #include "base/values.h" | |
| 14 #include "components/ntp_tiles/most_visited_sites.h" | |
| 15 #include "components/ntp_tiles/pref_names.h" | |
| 16 #include "components/ntp_tiles/webui/site_tiles_internals_message_handler_client .h" | |
| 17 #include "components/prefs/pref_service.h" | |
| 18 #include "components/url_formatter/url_fixer.h" | |
| 19 #include "url/gurl.h" | |
| 20 | |
| 21 namespace ntp_tiles { | |
| 22 | |
| 23 SiteTilesInternalsMessageHandlerClient:: | |
| 24 SiteTilesInternalsMessageHandlerClient() = default; | |
| 25 SiteTilesInternalsMessageHandlerClient:: | |
| 26 ~SiteTilesInternalsMessageHandlerClient() = default; | |
| 27 | |
| 28 SiteTilesInternalsMessageHandler::SiteTilesInternalsMessageHandler() | |
| 29 : site_count_(8), weak_ptr_factory_(this) {} | |
| 30 | |
| 31 SiteTilesInternalsMessageHandler::~SiteTilesInternalsMessageHandler() = default; | |
| 32 | |
| 33 void SiteTilesInternalsMessageHandler::RegisterMessages( | |
| 34 SiteTilesInternalsMessageHandlerClient* client) { | |
| 35 client_ = client; | |
| 36 | |
| 37 client_->RegisterMessageCallback( | |
| 38 "registerForEvents", | |
| 39 base::Bind(&SiteTilesInternalsMessageHandler::HandleRegisterForEvents, | |
| 40 base::Unretained(this))); | |
| 41 | |
| 42 client_->RegisterMessageCallback( | |
| 43 "update", base::Bind(&SiteTilesInternalsMessageHandler::HandleUpdate, | |
| 44 base::Unretained(this))); | |
| 45 | |
| 46 client_->RegisterMessageCallback( | |
| 47 "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
| |
| 48 base::Bind(&SiteTilesInternalsMessageHandler::HandleGetFavicon, | |
| 49 base::Unretained(this))); | |
| 50 } | |
| 51 | |
| 52 void SiteTilesInternalsMessageHandler::HandleRegisterForEvents( | |
| 53 const base::ListValue* args) { | |
| 54 DCHECK(args->empty()); | |
| 55 | |
| 56 SendSourceInfo(); | |
| 57 | |
| 58 most_visited_sites_ = client_->MakeMostVisitedSites(); | |
| 59 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | |
| 60 } | |
| 61 | |
| 62 void SiteTilesInternalsMessageHandler::HandleUpdate( | |
| 63 const base::ListValue* args) { | |
| 64 const base::DictionaryValue* dict; | |
| 65 DCHECK_EQ(1u, args->GetSize()); | |
| 66 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.
| |
| 67 | |
| 68 PrefService* prefs = client_->GetPrefs(); | |
| 69 | |
| 70 if (client_->IsSourceEnabled(ntp_tiles::NTPTileSource::POPULAR)) { | |
| 71 std::string url; | |
| 72 dict->GetString("popular.overrideURL", &url); | |
| 73 if (url.empty()) | |
|
Marc Treib
2016/12/09 10:52:11
Braces :)
sfiera
2016/12/09 17:35:28
Done.
| |
| 74 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | |
| 75 else | |
| 76 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | |
| 77 url_formatter::FixupURL(url, std::string()).spec()); | |
| 78 | |
| 79 std::string country; | |
| 80 dict->GetString("popular.overrideCountry", &country); | |
| 81 if (country.empty()) | |
| 82 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); | |
| 83 else | |
| 84 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); | |
| 85 | |
| 86 std::string version; | |
| 87 dict->GetString("popular.overrideVersion", &version); | |
| 88 if (version.empty()) | |
| 89 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | |
| 90 else | |
| 91 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | |
| 92 } | |
| 93 | |
| 94 // Recreate to pick up new values. | |
| 95 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.
| |
| 96 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | |
| 97 SendSourceInfo(); | |
| 98 } | |
| 99 | |
| 100 void SiteTilesInternalsMessageHandler::HandleGetFavicon( | |
| 101 const base::ListValue* args) { | |
| 102 DCHECK_EQ(1u, args->GetSize()); | |
| 103 // TODO(sfiera): implement | |
| 104 } | |
| 105 | |
| 106 void SiteTilesInternalsMessageHandler::SendSourceInfo() { | |
| 107 PrefService* prefs = client_->GetPrefs(); | |
| 108 base::DictionaryValue value; | |
| 109 | |
| 110 value.SetBoolean("topSites", | |
| 111 client_->IsSourceEnabled(NTPTileSource::TOP_SITES)); | |
| 112 value.SetBoolean( | |
| 113 "suggestionsService", | |
| 114 client_->IsSourceEnabled(NTPTileSource::SUGGESTIONS_SERVICE)); | |
| 115 value.SetBoolean("whitelist", | |
| 116 client_->IsSourceEnabled(NTPTileSource::WHITELIST)); | |
| 117 | |
| 118 if (client_->IsSourceEnabled(NTPTileSource::POPULAR)) { | |
| 119 auto popular_sites = client_->MakePopularSites(); | |
| 120 value.SetString("popular.url", popular_sites->GetURLToUse().spec()); | |
| 121 value.SetString("popular.country", popular_sites->GetCountryToUse()); | |
| 122 value.SetString("popular.version", popular_sites->GetVersionToUse()); | |
| 123 | |
| 124 value.SetString( | |
| 125 "popular.overrideURL", | |
| 126 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | |
| 127 value.SetString( | |
| 128 "popular.overrideCountry", | |
| 129 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); | |
| 130 value.SetString( | |
| 131 "popular.overrideVersion", | |
| 132 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); | |
| 133 } else { | |
| 134 value.SetBoolean("popular", false); | |
| 135 } | |
| 136 | |
| 137 client_->CallJavascriptFunction( | |
| 138 "chrome.site_tiles_internals.receiveSourceInfo", value); | |
| 139 } | |
| 140 | |
| 141 void SiteTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { | |
| 142 auto sites_list = base::MakeUnique<base::ListValue>(); | |
| 143 for (const NTPTile& tile : tiles) { | |
| 144 auto entry = base::MakeUnique<base::DictionaryValue>(); | |
| 145 entry->SetString("title", tile.title); | |
| 146 entry->SetString("url", tile.url.spec()); | |
| 147 entry->SetInteger("source", static_cast<int>(tile.source)); | |
| 148 entry->SetString("whitelistIconPath", | |
| 149 tile.whitelist_icon_path.LossyDisplayName()); | |
| 150 sites_list->Append(std::move(entry)); | |
| 151 } | |
| 152 | |
| 153 base::DictionaryValue result; | |
| 154 result.Set("sites", std::move(sites_list)); | |
| 155 client_->CallJavascriptFunction("chrome.site_tiles_internals.receiveSites", | |
| 156 result); | |
| 157 } | |
| 158 | |
| 159 void SiteTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( | |
| 160 const NTPTilesVector& tiles) { | |
| 161 SendTiles(tiles); | |
| 162 } | |
| 163 | |
| 164 void SiteTilesInternalsMessageHandler::OnIconMadeAvailable( | |
| 165 const GURL& site_url) {} | |
| 166 | |
| 167 } // namespace ntp_tiles | |
| OLD | NEW |