| 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 <array> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
| 14 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 15 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "components/favicon/core/favicon_service.h" |
| 16 #include "components/ntp_tiles/most_visited_sites.h" | 19 #include "components/ntp_tiles/most_visited_sites.h" |
| 17 #include "components/ntp_tiles/pref_names.h" | 20 #include "components/ntp_tiles/pref_names.h" |
| 18 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client.
h" | 21 #include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client.
h" |
| 19 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 20 #include "components/url_formatter/url_fixer.h" | 23 #include "components/url_formatter/url_fixer.h" |
| 21 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 22 | 25 |
| 23 namespace ntp_tiles { | 26 namespace ntp_tiles { |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 30 using FaviconResultMap = std::map<std::pair<GURL, favicon_base::IconType>, |
| 31 favicon_base::FaviconRawBitmapResult>; |
| 32 |
| 33 struct IconTypeAndName { |
| 34 favicon_base::IconType type_enum; |
| 35 const char* type_name; |
| 36 }; |
| 37 |
| 38 constexpr std::array<IconTypeAndName, 4> kIconTypesAndNames{{ |
| 39 {favicon_base::FAVICON, "FAVICON"}, |
| 40 {favicon_base::TOUCH_ICON, "TOUCH_ICON"}, |
| 41 {favicon_base::TOUCH_PRECOMPOSED_ICON, "TOUCH_PRECOMPOSED_ICON"}, |
| 42 {favicon_base::WEB_MANIFEST_ICON, "WEB_MANIFEST_ICON"}, |
| 43 }}; |
| 44 |
| 27 std::string FormatJson(const base::Value& value) { | 45 std::string FormatJson(const base::Value& value) { |
| 28 std::string pretty_printed; | 46 std::string pretty_printed; |
| 29 bool ok = base::JSONWriter::WriteWithOptions( | 47 bool ok = base::JSONWriter::WriteWithOptions( |
| 30 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed); | 48 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed); |
| 31 DCHECK(ok); | 49 DCHECK(ok); |
| 32 return pretty_printed; | 50 return pretty_printed; |
| 33 } | 51 } |
| 34 | 52 |
| 35 } // namespace | 53 } // namespace |
| 36 | 54 |
| 37 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() | 55 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler( |
| 38 : client_(nullptr), site_count_(8), weak_ptr_factory_(this) {} | 56 favicon::FaviconService* favicon_service) |
| 57 : favicon_service_(favicon_service), |
| 58 client_(nullptr), |
| 59 site_count_(8), |
| 60 weak_ptr_factory_(this) {} |
| 39 | 61 |
| 40 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; | 62 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; |
| 41 | 63 |
| 42 void NTPTilesInternalsMessageHandler::RegisterMessages( | 64 void NTPTilesInternalsMessageHandler::RegisterMessages( |
| 43 NTPTilesInternalsMessageHandlerClient* client) { | 65 NTPTilesInternalsMessageHandlerClient* client) { |
| 44 client_ = client; | 66 client_ = client; |
| 45 | 67 |
| 46 client_->RegisterMessageCallback( | 68 client_->RegisterMessageCallback( |
| 47 "registerForEvents", | 69 "registerForEvents", |
| 48 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, | 70 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 66 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( | 88 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( |
| 67 const base::ListValue* args) { | 89 const base::ListValue* args) { |
| 68 if (!client_->SupportsNTPTiles()) { | 90 if (!client_->SupportsNTPTiles()) { |
| 69 base::DictionaryValue disabled; | 91 base::DictionaryValue disabled; |
| 70 disabled.SetBoolean("topSites", false); | 92 disabled.SetBoolean("topSites", false); |
| 71 disabled.SetBoolean("suggestionsService", false); | 93 disabled.SetBoolean("suggestionsService", false); |
| 72 disabled.SetBoolean("popular", false); | 94 disabled.SetBoolean("popular", false); |
| 73 disabled.SetBoolean("whitelist", false); | 95 disabled.SetBoolean("whitelist", false); |
| 74 client_->CallJavascriptFunction( | 96 client_->CallJavascriptFunction( |
| 75 "chrome.ntp_tiles_internals.receiveSourceInfo", disabled); | 97 "chrome.ntp_tiles_internals.receiveSourceInfo", disabled); |
| 76 SendTiles(NTPTilesVector()); | 98 SendTiles(NTPTilesVector(), FaviconResultMap()); |
| 77 return; | 99 return; |
| 78 } | 100 } |
| 79 DCHECK(args->empty()); | 101 DCHECK(args->empty()); |
| 80 | 102 |
| 81 suggestions_status_.clear(); | 103 suggestions_status_.clear(); |
| 82 popular_sites_json_.clear(); | 104 popular_sites_json_.clear(); |
| 83 most_visited_sites_ = client_->MakeMostVisitedSites(); | 105 most_visited_sites_ = client_->MakeMostVisitedSites(); |
| 84 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | 106 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); |
| 85 SendSourceInfo(); | 107 SendSourceInfo(); |
| 86 } | 108 } |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 | 231 |
| 210 value.SetString("popular.json", popular_sites_json_); | 232 value.SetString("popular.json", popular_sites_json_); |
| 211 } else { | 233 } else { |
| 212 value.SetBoolean("popular", false); | 234 value.SetBoolean("popular", false); |
| 213 } | 235 } |
| 214 | 236 |
| 215 client_->CallJavascriptFunction( | 237 client_->CallJavascriptFunction( |
| 216 "chrome.ntp_tiles_internals.receiveSourceInfo", value); | 238 "chrome.ntp_tiles_internals.receiveSourceInfo", value); |
| 217 } | 239 } |
| 218 | 240 |
| 219 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { | 241 void NTPTilesInternalsMessageHandler::SendTiles( |
| 242 const NTPTilesVector& tiles, |
| 243 const FaviconResultMap& result_map) { |
| 220 auto sites_list = base::MakeUnique<base::ListValue>(); | 244 auto sites_list = base::MakeUnique<base::ListValue>(); |
| 221 for (const NTPTile& tile : tiles) { | 245 for (const NTPTile& tile : tiles) { |
| 222 auto entry = base::MakeUnique<base::DictionaryValue>(); | 246 auto entry = base::MakeUnique<base::DictionaryValue>(); |
| 223 entry->SetString("title", tile.title); | 247 entry->SetString("title", tile.title); |
| 224 entry->SetString("url", tile.url.spec()); | 248 entry->SetString("url", tile.url.spec()); |
| 225 entry->SetInteger("source", static_cast<int>(tile.source)); | 249 entry->SetInteger("source", static_cast<int>(tile.source)); |
| 226 entry->SetString("whitelistIconPath", | 250 entry->SetString("whitelistIconPath", |
| 227 tile.whitelist_icon_path.LossyDisplayName()); | 251 tile.whitelist_icon_path.LossyDisplayName()); |
| 252 |
| 253 auto icon_list = base::MakeUnique<base::ListValue>(); |
| 254 for (const auto& entry : kIconTypesAndNames) { |
| 255 FaviconResultMap::const_iterator it = result_map.find( |
| 256 FaviconResultMap::key_type(tile.url, entry.type_enum)); |
| 257 |
| 258 if (it != result_map.end()) { |
| 259 auto icon = base::MakeUnique<base::DictionaryValue>(); |
| 260 icon->SetString("url", it->second.icon_url.spec()); |
| 261 icon->SetString("type", entry.type_name); |
| 262 icon_list->Append(std::move(icon)); |
| 263 } |
| 264 } |
| 265 entry->Set("icons", std::move(icon_list)); |
| 266 |
| 228 sites_list->Append(std::move(entry)); | 267 sites_list->Append(std::move(entry)); |
| 229 } | 268 } |
| 230 | 269 |
| 231 base::DictionaryValue result; | 270 base::DictionaryValue result; |
| 232 result.Set("sites", std::move(sites_list)); | 271 result.Set("sites", std::move(sites_list)); |
| 233 client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites", | 272 client_->CallJavascriptFunction("chrome.ntp_tiles_internals.receiveSites", |
| 234 result); | 273 result); |
| 235 } | 274 } |
| 236 | 275 |
| 237 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( | 276 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( |
| 238 const NTPTilesVector& tiles) { | 277 const NTPTilesVector& tiles) { |
| 239 SendTiles(tiles); | 278 cancelable_task_tracker_.TryCancelAll(); |
| 279 |
| 280 if (tiles.empty()) { |
| 281 SendTiles(tiles, FaviconResultMap()); |
| 282 return; |
| 283 } |
| 284 |
| 285 auto on_lookup_done = base::BindRepeating( |
| 286 &NTPTilesInternalsMessageHandler::OnFaviconLookupDone, |
| 287 // Unretained(this) is safe because of |cancelable_task_tracker_|. |
| 288 base::Unretained(this), tiles, base::Owned(new FaviconResultMap()), |
| 289 base::Owned(new size_t(tiles.size() * kIconTypesAndNames.size()))); |
| 290 |
| 291 for (const NTPTile& tile : tiles) { |
| 292 for (const auto& entry : kIconTypesAndNames) { |
| 293 favicon_service_->GetLargestRawFaviconForPageURL( |
| 294 tile.url, std::vector<int>(1U, entry.type_enum), |
| 295 /*minimum_size_in_pixels=*/0, base::Bind(on_lookup_done, tile.url), |
| 296 &cancelable_task_tracker_); |
| 297 } |
| 298 } |
| 240 } | 299 } |
| 241 | 300 |
| 242 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( | 301 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( |
| 243 const GURL& site_url) {} | 302 const GURL& site_url) {} |
| 244 | 303 |
| 304 void NTPTilesInternalsMessageHandler::OnFaviconLookupDone( |
| 305 const NTPTilesVector& tiles, |
| 306 FaviconResultMap* result_map, |
| 307 size_t* num_pending_lookups, |
| 308 const GURL& page_url, |
| 309 const favicon_base::FaviconRawBitmapResult& result) { |
| 310 DCHECK_NE(0u, *num_pending_lookups); |
| 311 |
| 312 result_map->emplace( |
| 313 std::pair<GURL, favicon_base::IconType>(page_url, result.icon_type), |
| 314 result); |
| 315 |
| 316 --*num_pending_lookups; |
| 317 if (*num_pending_lookups == 0) |
| 318 SendTiles(tiles, *result_map); |
| 319 } |
| 320 |
| 245 } // namespace ntp_tiles | 321 } // namespace ntp_tiles |
| OLD | NEW |