| 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 std::string FormatJson(const base::Value& value) { |
| 28 std::string pretty_printed; |
| 29 bool ok = base::JSONWriter::WriteWithOptions( |
| 30 value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &pretty_printed); |
| 31 DCHECK(ok); |
| 32 return pretty_printed; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 21 NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() = | 37 NTPTilesInternalsMessageHandlerClient::NTPTilesInternalsMessageHandlerClient() = |
| 22 default; | 38 default; |
| 23 NTPTilesInternalsMessageHandlerClient:: | 39 NTPTilesInternalsMessageHandlerClient:: |
| 24 ~NTPTilesInternalsMessageHandlerClient() = default; | 40 ~NTPTilesInternalsMessageHandlerClient() = default; |
| 25 | 41 |
| 26 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() | 42 NTPTilesInternalsMessageHandler::NTPTilesInternalsMessageHandler() |
| 27 : client_(nullptr), site_count_(8) {} | 43 : client_(nullptr), site_count_(8), weak_ptr_factory_(this) {} |
| 28 | 44 |
| 29 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; | 45 NTPTilesInternalsMessageHandler::~NTPTilesInternalsMessageHandler() = default; |
| 30 | 46 |
| 31 void NTPTilesInternalsMessageHandler::RegisterMessages( | 47 void NTPTilesInternalsMessageHandler::RegisterMessages( |
| 32 NTPTilesInternalsMessageHandlerClient* client) { | 48 NTPTilesInternalsMessageHandlerClient* client) { |
| 33 client_ = client; | 49 client_ = client; |
| 34 | 50 |
| 35 client_->RegisterMessageCallback( | 51 client_->RegisterMessageCallback( |
| 36 "registerForEvents", | 52 "registerForEvents", |
| 37 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, | 53 base::Bind(&NTPTilesInternalsMessageHandler::HandleRegisterForEvents, |
| 38 base::Unretained(this))); | 54 base::Unretained(this))); |
| 39 | 55 |
| 40 client_->RegisterMessageCallback( | 56 client_->RegisterMessageCallback( |
| 41 "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate, | 57 "update", base::Bind(&NTPTilesInternalsMessageHandler::HandleUpdate, |
| 42 base::Unretained(this))); | 58 base::Unretained(this))); |
| 59 |
| 60 client_->RegisterMessageCallback( |
| 61 "fetchSuggestions", |
| 62 base::Bind(&NTPTilesInternalsMessageHandler::HandleFetchSuggestions, |
| 63 base::Unretained(this))); |
| 64 |
| 65 client_->RegisterMessageCallback( |
| 66 "viewPopularSitesJson", |
| 67 base::Bind(&NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson, |
| 68 base::Unretained(this))); |
| 43 } | 69 } |
| 44 | 70 |
| 45 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( | 71 void NTPTilesInternalsMessageHandler::HandleRegisterForEvents( |
| 46 const base::ListValue* args) { | 72 const base::ListValue* args) { |
| 47 if (!client_->SupportsNTPTiles()) { | 73 if (!client_->SupportsNTPTiles()) { |
| 48 return; | 74 return; |
| 49 } | 75 } |
| 50 DCHECK(args->empty()); | 76 DCHECK(args->empty()); |
| 51 | 77 |
| 52 SendSourceInfo(); | 78 suggestions_status_.clear(); |
| 53 | 79 popular_sites_json_.clear(); |
| 54 most_visited_sites_ = client_->MakeMostVisitedSites(); | 80 most_visited_sites_ = client_->MakeMostVisitedSites(); |
| 55 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | 81 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); |
| 82 SendSourceInfo(); |
| 56 } | 83 } |
| 57 | 84 |
| 58 void NTPTilesInternalsMessageHandler::HandleUpdate( | 85 void NTPTilesInternalsMessageHandler::HandleUpdate( |
| 59 const base::ListValue* args) { | 86 const base::ListValue* args) { |
| 60 if (!client_->SupportsNTPTiles()) { | 87 if (!client_->SupportsNTPTiles()) { |
| 61 return; | 88 return; |
| 62 } | 89 } |
| 63 const base::DictionaryValue* dict = nullptr; | 90 const base::DictionaryValue* dict = nullptr; |
| 64 DCHECK_EQ(1u, args->GetSize()); | 91 DCHECK_EQ(1u, args->GetSize()); |
| 65 args->GetDictionary(0, &dict); | 92 args->GetDictionary(0, &dict); |
| 66 DCHECK(dict); | 93 DCHECK(dict); |
| 67 | 94 |
| 68 PrefService* prefs = client_->GetPrefs(); | 95 PrefService* prefs = client_->GetPrefs(); |
| 69 | 96 |
| 70 if (client_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) { | 97 if (most_visited_sites_->DoesSourceExist(ntp_tiles::NTPTileSource::POPULAR)) { |
| 98 popular_sites_json_.clear(); |
| 99 |
| 71 std::string url; | 100 std::string url; |
| 72 dict->GetString("popular.overrideURL", &url); | 101 dict->GetString("popular.overrideURL", &url); |
| 73 if (url.empty()) { | 102 if (url.empty()) { |
| 74 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); | 103 prefs->ClearPref(ntp_tiles::prefs::kPopularSitesOverrideURL); |
| 75 } else { | 104 } else { |
| 76 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, | 105 prefs->SetString(ntp_tiles::prefs::kPopularSitesOverrideURL, |
| 77 url_formatter::FixupURL(url, std::string()).spec()); | 106 url_formatter::FixupURL(url, std::string()).spec()); |
| 78 } | 107 } |
| 79 | 108 |
| 80 std::string country; | 109 std::string country; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 95 } | 124 } |
| 96 | 125 |
| 97 // Recreate to pick up new values. | 126 // Recreate to pick up new values. |
| 98 // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as | 127 // TODO(sfiera): refresh MostVisitedSites without re-creating it, as soon as |
| 99 // that will pick up changes to the Popular Sites overrides. | 128 // that will pick up changes to the Popular Sites overrides. |
| 100 most_visited_sites_ = client_->MakeMostVisitedSites(); | 129 most_visited_sites_ = client_->MakeMostVisitedSites(); |
| 101 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); | 130 most_visited_sites_->SetMostVisitedURLsObserver(this, site_count_); |
| 102 SendSourceInfo(); | 131 SendSourceInfo(); |
| 103 } | 132 } |
| 104 | 133 |
| 134 void NTPTilesInternalsMessageHandler::HandleFetchSuggestions( |
| 135 const base::ListValue* args) { |
| 136 DCHECK_EQ(0u, args->GetSize()); |
| 137 if (!most_visited_sites_->DoesSourceExist( |
| 138 ntp_tiles::NTPTileSource::SUGGESTIONS_SERVICE)) { |
| 139 return; |
| 140 } |
| 141 |
| 142 if (most_visited_sites_->suggestions()->FetchSuggestionsData()) { |
| 143 suggestions_status_ = "fetching..."; |
| 144 } else { |
| 145 suggestions_status_ = "history sync is disabled, or not yet initialized"; |
| 146 } |
| 147 SendSourceInfo(); |
| 148 } |
| 149 |
| 150 void NTPTilesInternalsMessageHandler::HandleViewPopularSitesJson( |
| 151 const base::ListValue* args) { |
| 152 DCHECK_EQ(0u, args->GetSize()); |
| 153 if (!most_visited_sites_->DoesSourceExist( |
| 154 ntp_tiles::NTPTileSource::POPULAR)) { |
| 155 return; |
| 156 } |
| 157 |
| 158 popular_sites_json_ = FormatJson( |
| 159 *most_visited_sites_->popular_sites()->GetCachedJson()); |
| 160 SendSourceInfo(); |
| 161 } |
| 162 |
| 105 void NTPTilesInternalsMessageHandler::SendSourceInfo() { | 163 void NTPTilesInternalsMessageHandler::SendSourceInfo() { |
| 106 PrefService* prefs = client_->GetPrefs(); | 164 PrefService* prefs = client_->GetPrefs(); |
| 107 base::DictionaryValue value; | 165 base::DictionaryValue value; |
| 108 | 166 |
| 109 value.SetBoolean("topSites", | 167 value.SetBoolean("topSites", most_visited_sites_->DoesSourceExist( |
| 110 client_->DoesSourceExist(NTPTileSource::TOP_SITES)); | 168 NTPTileSource::TOP_SITES)); |
| 111 value.SetBoolean( | 169 value.SetBoolean("whitelist", most_visited_sites_->DoesSourceExist( |
| 112 "suggestionsService", | 170 NTPTileSource::WHITELIST)); |
| 113 client_->DoesSourceExist(NTPTileSource::SUGGESTIONS_SERVICE)); | |
| 114 value.SetBoolean("whitelist", | |
| 115 client_->DoesSourceExist(NTPTileSource::WHITELIST)); | |
| 116 | 171 |
| 117 if (client_->DoesSourceExist(NTPTileSource::POPULAR)) { | 172 if (most_visited_sites_->DoesSourceExist( |
| 118 auto popular_sites = client_->MakePopularSites(); | 173 NTPTileSource::SUGGESTIONS_SERVICE)) { |
| 174 value.SetString("suggestionsService.status", suggestions_status_); |
| 175 } else { |
| 176 value.SetBoolean("suggestionsService", false); |
| 177 } |
| 178 |
| 179 if (most_visited_sites_->DoesSourceExist(NTPTileSource::POPULAR)) { |
| 180 auto popular_sites = most_visited_sites_->popular_sites(); |
| 119 value.SetString("popular.url", popular_sites->GetURLToFetch().spec()); | 181 value.SetString("popular.url", popular_sites->GetURLToFetch().spec()); |
| 120 value.SetString("popular.country", popular_sites->GetCountryToFetch()); | 182 value.SetString("popular.country", popular_sites->GetCountryToFetch()); |
| 121 value.SetString("popular.version", popular_sites->GetVersionToFetch()); | 183 value.SetString("popular.version", popular_sites->GetVersionToFetch()); |
| 122 | 184 |
| 123 value.SetString( | 185 value.SetString( |
| 124 "popular.overrideURL", | 186 "popular.overrideURL", |
| 125 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); | 187 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideURL)); |
| 126 value.SetString( | 188 value.SetString( |
| 127 "popular.overrideCountry", | 189 "popular.overrideCountry", |
| 128 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); | 190 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideCountry)); |
| 129 value.SetString( | 191 value.SetString( |
| 130 "popular.overrideVersion", | 192 "popular.overrideVersion", |
| 131 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); | 193 prefs->GetString(ntp_tiles::prefs::kPopularSitesOverrideVersion)); |
| 194 |
| 195 value.SetString("popular.json", popular_sites_json_); |
| 132 } else { | 196 } else { |
| 133 value.SetBoolean("popular", false); | 197 value.SetBoolean("popular", false); |
| 134 } | 198 } |
| 135 | 199 |
| 136 client_->CallJavascriptFunction( | 200 client_->CallJavascriptFunction( |
| 137 "chrome.ntp_tiles_internals.receiveSourceInfo", value); | 201 "chrome.ntp_tiles_internals.receiveSourceInfo", value); |
| 138 } | 202 } |
| 139 | 203 |
| 140 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { | 204 void NTPTilesInternalsMessageHandler::SendTiles(const NTPTilesVector& tiles) { |
| 141 auto sites_list = base::MakeUnique<base::ListValue>(); | 205 auto sites_list = base::MakeUnique<base::ListValue>(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 157 | 221 |
| 158 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( | 222 void NTPTilesInternalsMessageHandler::OnMostVisitedURLsAvailable( |
| 159 const NTPTilesVector& tiles) { | 223 const NTPTilesVector& tiles) { |
| 160 SendTiles(tiles); | 224 SendTiles(tiles); |
| 161 } | 225 } |
| 162 | 226 |
| 163 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( | 227 void NTPTilesInternalsMessageHandler::OnIconMadeAvailable( |
| 164 const GURL& site_url) {} | 228 const GURL& site_url) {} |
| 165 | 229 |
| 166 } // namespace ntp_tiles | 230 } // namespace ntp_tiles |
| OLD | NEW |