Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler.h" | 5 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/json/json_reader.h" | |
| 11 #include "base/json/json_writer.h" | |
| 9 #include "base/logging.h" | 12 #include "base/logging.h" |
| 10 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/task_runner_util.h" | |
| 11 #include "base/values.h" | 15 #include "base/values.h" |
| 12 #include "components/ntp_tiles/most_visited_sites.h" | 16 #include "components/ntp_tiles/most_visited_sites.h" |
| 13 #include "components/ntp_tiles/pref_names.h" | 17 #include "components/ntp_tiles/pref_names.h" |
| 14 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client. h" | 18 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client. h" |
| 15 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 16 #include "components/url_formatter/url_fixer.h" | 20 #include "components/url_formatter/url_fixer.h" |
| 17 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 18 | 22 |
| 19 namespace ntp_tiles { | 23 namespace ntp_tiles { |
| 20 | 24 |
| 25 namespace { | |
| 26 | |
| 27 base::Optional<std::string> ReadFileToString(const base::FilePath& path) { | |
| 28 std::string result; | |
| 29 if (!base::ReadFileToString(path, &result)) | |
| 30 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
| |
| 31 return std::move(result); | |
| 32 } | |
| 33 | |
| 34 std::string FormatJson(base::Optional<std::string> content) { | |
| 35 if (!content) { | |
| 36 return "(null)"; | |
| 37 } | |
| 38 | |
| 39 // If we can, parse it and pretty-print it, since it's minified on disk. | |
| 40 // Read JSON in-process, as this is just a debugging page. | |
| 41 std::unique_ptr<base::Value> value = base::JSONReader::Read(*content); | |
| 42 if (value) { | |
| 43 std::string pretty_printed; | |
| 44 if (base::JSONWriter::WriteWithOptions( | |
| 45 *value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed)) { | |
| 46 return pretty_printed; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 // By default, use the value as-is. | |
| 51 return *content; | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 21 NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() = | 56 NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() = |
| 22 default; | 57 default; |
| 23 NTPTilesInternalsMessageHandlerClient:: | 58 NTPTilesInternalsMessageHandlerClient:: |
| 24 ~NTPTilesInternalsMessageHandlerClient() = default; | 59 ~NTPTilesInternalsMessageHandlerClient() = default; |
| 25 | 60 |
| 26 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() | 61 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() |
| 27 : client_(nullptr), site_count_(8) {} | 62 : client_(nullptr), site_count_(8), weak_ptr_factory_(this) {} |
| 28 | 63 |
| 29 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; | 64 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; |
| 30 | 65 |
| 31 void NTPTilesInternalsMessageHandler::RegisterMessages( | 66 void NTPTilesInternalsMessageHandler::RegisterMessages( |
| 32 NTPTilesInternalsMessageHandlerClient* client) { | 67 NTPTilesInternalsMessageHandlerClient* client) { |
| 33 client_ = client; | 68 client_ = client; |
| 34 | 69 |
| 35 client_->RegisterMessageCallback( | 70 client_->RegisterMessageCallback( |
| 36 "registerForEvents", | 71 "registerForEvents", |
| 37 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, | 72 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, |
| 38 base::Unretained(this))); | 73 base::Unretained(this))); |
| 39 | 74 |
| 40 client_->RegisterMessageCallback( | 75 client_->RegisterMessageCallback( |
| 41 "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate, | 76 "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate, |
| 42 base::Unretained(this))); | 77 base::Unretained(this))); |
| 78 | |
| 79 client_->RegisterMessageCallback( | |
| 80 "fetchSuggestions", | |
| 81 base::Bind(&NTPTilesInternalsMessageHandler::HandleFetchSuggestions, | |
| 82 base::Unretained(this))); | |
| 83 | |
| 84 client_->RegisterMessageCallback( | |
| 85 "viewPopularSitesJson", | |
| 86 base::Bind(&NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson, | |
| 87 base::Unretained(this))); | |
| 43 } | 88 } |
| 44 | 89 |
| 45 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( | 90 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( |
| 46 const base::ListValue* args) { | 91 const base::ListValue* args) { |
| 47 DCHECK(args->empty()); | 92 DCHECK(args->empty()); |
| 48 | 93 |
| 49 SendSourceInfo(); | 94 suggestions_status_.clear(); |
| 50 | 95 popular_sites_json_.clear(); |
| 51 most_visited_sites_ = client_->MakeMostVisitedSites(); | 96 most_visited_sites_ = client_->MakeMostVisitedSites(); |
| 52 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | 97 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); |
| 98 SendSourceInfo(); | |
| 53 } | 99 } |
| 54 | 100 |
| 55 void NTPTilesInternalsMessageHandler::HandleUpdate( | 101 void NTPTilesInternalsMessageHandler::HandleUpdate( |
| 56 const base::ListValue* args) { | 102 const base::ListValue* args) { |
| 57 const base::DictionaryValue* dict = nullptr; | 103 const base::DictionaryValue* dict = nullptr; |
| 58 DCHECK_EQ(1u, args->GetSize()); | 104 DCHECK_EQ(1u, args->GetSize()); |
| 59 args->GetDictionary(0, &dict); | 105 args->GetDictionary(0, &dict); |
| 60 DCHECK(dict); | 106 DCHECK(dict); |
| 61 | 107 |
| 62 PrefService* prefs = client_->GetPrefs(); | 108 PrefService* prefs = client_->GetPrefs(); |
| 63 | 109 |
| 64 if (client_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) { | 110 if (most_visited_sites_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) { |
| 111 popular_sites_json_.clear(); | |
| 112 | |
| 65 std::string url; | 113 std::string url; |
| 66 dict->GetString("popular.overrideURL", &url); | 114 dict->GetString("popular.overrideURL", &url); |
| 67 if (url.empty()) { | 115 if (url.empty()) { |
| 68 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | 116 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| 69 } else { | 117 } else { |
| 70 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | 118 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 71 url_formatter::FixupURL(url, std::string()).spec()); | 119 url_formatter::FixupURL(url, std::string()).spec()); |
| 72 } | 120 } |
| 73 | 121 |
| 74 std::string country; | 122 std::string country; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 89 } | 137 } |
| 90 | 138 |
| 91 // Recreate to pick up new values. | 139 // Recreate to pick up new values. |
| 92 // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as | 140 // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as |
| 93 // that will pick up changes to the Popular Sites overrides. | 141 // that will pick up changes to the Popular Sites overrides. |
| 94 most_visited_sites_ = client_->MakeMostVisitedSites(); | 142 most_visited_sites_ = client_->MakeMostVisitedSites(); |
| 95 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | 143 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); |
| 96 SendSourceInfo(); | 144 SendSourceInfo(); |
| 97 } | 145 } |
| 98 | 146 |
| 147 void NTPTilesInternalsMessageHandler::HandleFetchSuggestions( | |
| 148 const base::ListValue* args) { | |
| 149 DCHECK_EQ(0u, args->GetSize()); | |
| 150 if (!most_visited_sites_->DoesSourceExist( | |
| 151 ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE)) { | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 if (most_visited_sites_->suggestions()->FetchSuggestionsData()) { | |
| 156 suggestions_status_ = "fetching..."; | |
| 157 } else { | |
| 158 suggestions_status_ = "not synced"; | |
| 159 } | |
| 160 SendSourceInfo(); | |
| 161 } | |
| 162 | |
| 163 void NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson( | |
| 164 const base::ListValue* args) { | |
| 165 DCHECK_EQ(0u, args->GetSize()); | |
| 166 if (!most_visited_sites_->DoesSourceExist( | |
| 167 ntp_tiles::NTPTileSource::POPULAR)) { | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 const base::FilePath& path = | |
| 172 most_visited_sites_->popular_sites()->local_path(); | |
| 173 base::PostTaskAndReplyWithResult( | |
| 174 client_->GetBlockingPool() | |
| 175 ->GetTaskRunnerWithShutdownBehavior( | |
| 176 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | |
| 177 .get(), | |
| 178 FROM_HERE, base::Bind(&ReadFileToString, path), | |
| 179 base::Bind(&NTPTilesInternalsMessageHandler::PopularSitesJsonRead, | |
| 180 weak_ptr_factory_.GetWeakPtr())); | |
| 181 } | |
| 182 | |
| 99 void NTPTilesInternalsMessageHandler::SendSourceInfo() { | 183 void NTPTilesInternalsMessageHandler::SendSourceInfo() { |
| 100 PrefService* prefs = client_->GetPrefs(); | 184 PrefService* prefs = client_->GetPrefs(); |
| 101 base::DictionaryValue value; | 185 base::DictionaryValue value; |
| 102 | 186 |
| 103 value.SetBoolean("topSites", | 187 value.SetBoolean("topSites", most_visited_sites_->DoesSourceExist( |
| 104 client_->DoesSourceExist(NTPTileSource::TOP_SITES)); | 188 NTPTileSource::TOP_SITES)); |
| 105 value.SetBoolean( | 189 value.SetBoolean("whitelist", most_visited_sites_->DoesSourceExist( |
| 106 "suggestionsService", | 190 NTPTileSource::WHITELIST)); |
| 107 client_->DoesSourceExist(NTPTileSource::SUGGESTIONS_SERVICE)); | |
| 108 value.SetBoolean("whitelist", | |
| 109 client_->DoesSourceExist(NTPTileSource::WHITELIST)); | |
| 110 | 191 |
| 111 if (client_->DoesSourceExist(NTPTileSource::POPULAR)) { | 192 if (most_visited_sites_->DoesSourceExist( |
| 112 auto popular_sites = client_->MakePopularSites(); | 193 NTPTileSource::SUGGESTIONS_SERVICE)) { |
| 194 value.SetString("suggestionsService.status", suggestions_status_); | |
| 195 } else { | |
| 196 value.SetBoolean("suggestionsService", false); | |
| 197 } | |
| 198 | |
| 199 if (most_visited_sites_->DoesSourceExist(NTPTileSource::POPULAR)) { | |
| 200 auto popular_sites = most_visited_sites_->popular_sites(); | |
| 113 value.SetString("popular.url", popular_sites->GetURLToUse().spec()); | 201 value.SetString("popular.url", popular_sites->GetURLToUse().spec()); |
| 114 value.SetString("popular.country", popular_sites->GetCountryToUse()); | 202 value.SetString("popular.country", popular_sites->GetCountryToUse()); |
| 115 value.SetString("popular.version", popular_sites->GetVersionToUse()); | 203 value.SetString("popular.version", popular_sites->GetVersionToUse()); |
| 116 | 204 |
| 117 value.SetString( | 205 value.SetString( |
| 118 "popular.overrideURL", | 206 "popular.overrideURL", |
| 119 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | 207 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 120 value.SetString( | 208 value.SetString( |
| 121 "popular.overrideCountry", | 209 "popular.overrideCountry", |
| 122 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); | 210 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); |
| 123 value.SetString( | 211 value.SetString( |
| 124 "popular.overrideVersion", | 212 "popular.overrideVersion", |
| 125 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); | 213 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); |
| 214 | |
| 215 value.SetString("popular.json", popular_sites_json_); | |
| 126 } else { | 216 } else { |
| 127 value.SetBoolean("popular", false); | 217 value.SetBoolean("popular", false); |
| 128 } | 218 } |
| 129 | 219 |
| 130 client_->CallJavascriptFunction( | 220 client_->CallJavascriptFunction( |
| 131 "chrome.ntp_tiles_internals.receiveSourceInfo", value); | 221 "chrome.ntp_tiles_internals.receiveSourceInfo", value); |
| 132 } | 222 } |
| 133 | 223 |
| 134 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { | 224 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { |
| 135 auto sites_list = base::MakeUnique<base::ListValue>(); | 225 auto sites_list = base::MakeUnique<base::ListValue>(); |
| 136 for (const NTPTile& tile : tiles) { | 226 for (const NTPTile& tile : tiles) { |
| 137 auto entry = base::MakeUnique<base::DictionaryValue>(); | 227 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 138 entry->SetString("title", tile.title); | 228 entry->SetString("title", tile.title); |
| 139 entry->SetString("url", tile.url.spec()); | 229 entry->SetString("url", tile.url.spec()); |
| 140 entry->SetInteger("source", static_cast<int>(tile.source)); | 230 entry->SetInteger("source", static_cast<int>(tile.source)); |
| 141 entry->SetString("whitelistIconPath", | 231 entry->SetString("whitelistIconPath", |
| 142 tile.whitelist_icon_path.LossyDisplayName()); | 232 tile.whitelist_icon_path.LossyDisplayName()); |
| 143 sites_list->Append(std::move(entry)); | 233 sites_list->Append(std::move(entry)); |
| 144 } | 234 } |
| 145 | 235 |
| 146 base::DictionaryValue result; | 236 base::DictionaryValue result; |
| 147 result.Set("sites", std::move(sites_list)); | 237 result.Set("sites", std::move(sites_list)); |
| 148 client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites", | 238 client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites", |
| 149 result); | 239 result); |
| 150 } | 240 } |
| 151 | 241 |
| 242 void NTPTilesInternalsMessageHandler::PopularSitesJsonRead( | |
| 243 const base::Optional<std::string>& content) { | |
| 244 popular_sites_json_ = FormatJson(content); | |
| 245 SendSourceInfo(); | |
| 246 } | |
| 247 | |
| 152 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( | 248 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( |
| 153 const NTPTilesVector& tiles) { | 249 const NTPTilesVector& tiles) { |
| 154 SendTiles(tiles); | 250 SendTiles(tiles); |
| 155 } | 251 } |
| 156 | 252 |
| 157 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( | 253 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( |
| 158 const GURL& site_url) {} | 254 const GURL& site_url) {} |
| 159 | 255 |
| 160 } // namespace ntp_tiles | 256 } // namespace ntp_tiles |
| OLD | NEW |