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