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/ntp_tiles_internals_message_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/callback.h" | |
| 9 #include "base/files/file_util.h" | |
|
Marc Treib
2016/12/12 10:51:22
Not needed I think
sfiera
2016/12/12 16:08:32
Done.
| |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/task_runner_util.h" | |
|
Marc Treib
2016/12/12 10:51:22
Not needed I think
sfiera
2016/12/12 16:08:32
Done.
| |
| 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/ntp_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 NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() = | |
| 24 default; | |
| 25 NTPTilesInternalsMessageHandlerClient:: | |
| 26 ~NTPTilesInternalsMessageHandlerClient() = default; | |
|
Marc Treib
2016/12/12 10:51:22
These should go into their own .cc file. (Or in th
sfiera
2016/12/12 16:08:32
Done.
| |
| 27 | |
| 28 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() | |
| 29 : site_count_(8) {} | |
|
Marc Treib
2016/12/12 10:51:22
client_(nullptr)
sfiera
2016/12/12 16:08:32
Done.
| |
| 30 | |
| 31 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; | |
| 32 | |
| 33 void NTPTilesInternalsMessageHandler::RegisterMessages( | |
| 34 NTPTilesInternalsMessageHandlerClient* client) { | |
| 35 client_ = client; | |
| 36 | |
| 37 client_->RegisterMessageCallback( | |
| 38 "registerForEvents", | |
| 39 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, | |
| 40 base::Unretained(this))); | |
| 41 | |
| 42 client_->RegisterMessageCallback( | |
| 43 "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate, | |
| 44 base::Unretained(this))); | |
| 45 } | |
| 46 | |
| 47 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( | |
| 48 const base::ListValue* args) { | |
| 49 DCHECK(args->empty()); | |
| 50 | |
| 51 SendSourceInfo(); | |
| 52 | |
| 53 most_visited_sites_ = client_->MakeMostVisitedSites(); | |
| 54 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | |
| 55 } | |
| 56 | |
| 57 void NTPTilesInternalsMessageHandler::HandleUpdate( | |
| 58 const base::ListValue* args) { | |
| 59 const base::DictionaryValue* dict = nullptr; | |
| 60 DCHECK_EQ(1u, args->GetSize()); | |
| 61 args->GetDictionary(0, &dict); | |
| 62 DCHECK(dict); | |
| 63 | |
| 64 PrefService* prefs = client_->GetPrefs(); | |
| 65 | |
| 66 if (client_->IsSourceEnabled(ntp_tiles::NTPTileSource::POPULAR)) { | |
| 67 std::string url; | |
| 68 dict->GetString("popular.overrideURL", &url); | |
| 69 if (url.empty()) { | |
| 70 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | |
| 71 } else { | |
| 72 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | |
| 73 url_formatter::FixupURL(url, std::string()).spec()); | |
| 74 } | |
| 75 | |
| 76 std::string country; | |
| 77 dict->GetString("popular.overrideCountry", &country); | |
| 78 if (country.empty()) { | |
| 79 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideCountry); | |
| 80 } else { | |
| 81 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideCountry, country); | |
| 82 } | |
| 83 | |
| 84 std::string version; | |
| 85 dict->GetString("popular.overrideVersion", &version); | |
| 86 if (version.empty()) { | |
| 87 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideVersion); | |
| 88 } else { | |
| 89 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideVersion, version); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 // Recreate to pick up new values. | |
| 94 // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as | |
| 95 // that will pick up changes to the Popular Sites overrides. | |
| 96 most_visited_sites_ = client_->MakeMostVisitedSites(); | |
| 97 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | |
| 98 SendSourceInfo(); | |
| 99 } | |
| 100 | |
| 101 void NTPTilesInternalsMessageHandler::SendSourceInfo() { | |
| 102 PrefService* prefs = client_->GetPrefs(); | |
| 103 base::DictionaryValue value; | |
| 104 | |
| 105 value.SetBoolean("topSites", | |
| 106 client_->IsSourceEnabled(NTPTileSource::TOP_SITES)); | |
| 107 value.SetBoolean( | |
| 108 "suggestionsService", | |
| 109 client_->IsSourceEnabled(NTPTileSource::SUGGESTIONS_SERVICE)); | |
| 110 value.SetBoolean("whitelist", | |
| 111 client_->IsSourceEnabled(NTPTileSource::WHITELIST)); | |
| 112 | |
| 113 if (client_->IsSourceEnabled(NTPTileSource::POPULAR)) { | |
| 114 auto popular_sites = client_->MakePopularSites(); | |
| 115 value.SetString("popular.url", popular_sites->GetURLToUse().spec()); | |
| 116 value.SetString("popular.country", popular_sites->GetCountryToUse()); | |
| 117 value.SetString("popular.version", popular_sites->GetVersionToUse()); | |
| 118 | |
| 119 value.SetString( | |
| 120 "popular.overrideURL", | |
| 121 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | |
| 122 value.SetString( | |
| 123 "popular.overrideCountry", | |
| 124 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); | |
| 125 value.SetString( | |
| 126 "popular.overrideVersion", | |
| 127 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); | |
| 128 } else { | |
| 129 value.SetBoolean("popular", false); | |
| 130 } | |
| 131 | |
| 132 client_->CallJavascriptFunction( | |
| 133 "chrome.ntp_tiles_internals.receiveSourceInfo", value); | |
| 134 } | |
| 135 | |
| 136 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { | |
| 137 auto sites_list = base::MakeUnique<base::ListValue>(); | |
| 138 for (const NTPTile& tile : tiles) { | |
| 139 auto entry = base::MakeUnique<base::DictionaryValue>(); | |
| 140 entry->SetString("title", tile.title); | |
| 141 entry->SetString("url", tile.url.spec()); | |
| 142 entry->SetInteger("source", static_cast<int>(tile.source)); | |
| 143 entry->SetString("whitelistIconPath", | |
| 144 tile.whitelist_icon_path.LossyDisplayName()); | |
| 145 sites_list->Append(std::move(entry)); | |
| 146 } | |
| 147 | |
| 148 base::DictionaryValue result; | |
| 149 result.Set("sites", std::move(sites_list)); | |
| 150 client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites", | |
| 151 result); | |
| 152 } | |
| 153 | |
| 154 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( | |
| 155 const NTPTilesVector& tiles) { | |
| 156 SendTiles(tiles); | |
| 157 } | |
| 158 | |
| 159 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( | |
| 160 const GURL& site_url) {} | |
| 161 | |
| 162 } // namespace ntp_tiles | |
| OLD | NEW |