| 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 "chrome/browser/ui/webui/snippets_internals_message_handler.h" | 5 #include "chrome/browser/ui/webui/snippets_internals_message_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 66 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 67 | 67 |
| 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
| 69 | 69 |
| 70 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | 70 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
| 71 if (!dom_loaded_) return; | 71 if (!dom_loaded_) return; |
| 72 | 72 |
| 73 SendSnippets(); | 73 SendSnippets(); |
| 74 | 74 |
| 75 web_ui()->CallJavascriptFunction( | 75 web_ui()->CallJavascriptFunctionUnsafe( |
| 76 "chrome.SnippetsInternals.receiveJson", | 76 "chrome.SnippetsInternals.receiveJson", |
| 77 base::StringValue( | 77 base::StringValue( |
| 78 ntp_snippets_service_->snippets_fetcher()->last_json())); | 78 ntp_snippets_service_->snippets_fetcher()->last_json())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void SnippetsInternalsMessageHandler::NTPSnippetsServiceCleared() {} | 81 void SnippetsInternalsMessageHandler::NTPSnippetsServiceCleared() {} |
| 82 | 82 |
| 83 void SnippetsInternalsMessageHandler::RegisterMessages() { | 83 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 84 // additional initialization (web_ui() does not work from the constructor) | 84 // additional initialization (web_ui() does not work from the constructor) |
| 85 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 85 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 160 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void SnippetsInternalsMessageHandler::SendInitialData() { | 163 void SnippetsInternalsMessageHandler::SendInitialData() { |
| 164 SendHosts(); | 164 SendHosts(); |
| 165 | 165 |
| 166 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 166 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 167 chrome::android::kNTPSnippetsFeature)); | 167 chrome::android::kNTPSnippetsFeature)); |
| 168 | 168 |
| 169 web_ui()->CallJavascriptFunction( | 169 web_ui()->CallJavascriptFunctionUnsafe( |
| 170 "chrome.SnippetsInternals.setHostRestricted", | 170 "chrome.SnippetsInternals.setHostRestricted", |
| 171 base::FundamentalValue( | 171 base::FundamentalValue( |
| 172 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); | 172 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); |
| 173 | 173 |
| 174 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { | 174 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { |
| 175 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 175 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 176 SendString("switch-personalized", "Only personalized"); | 176 SendString("switch-personalized", "Only personalized"); |
| 177 break; | 177 break; |
| 178 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 178 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 179 SendString("switch-personalized", | 179 SendString("switch-personalized", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 191 void SnippetsInternalsMessageHandler::SendSnippets() { | 191 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 192 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 192 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 193 | 193 |
| 194 int index = 0; | 194 int index = 0; |
| 195 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 195 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 196 ntp_snippets_service_->snippets()) | 196 ntp_snippets_service_->snippets()) |
| 197 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); | 197 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); |
| 198 | 198 |
| 199 base::DictionaryValue result; | 199 base::DictionaryValue result; |
| 200 result.Set("list", std::move(snippets_list)); | 200 result.Set("list", std::move(snippets_list)); |
| 201 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets", | 201 web_ui()->CallJavascriptFunctionUnsafe( |
| 202 result); | 202 "chrome.SnippetsInternals.receiveSnippets", result); |
| 203 | 203 |
| 204 const std::string& status = | 204 const std::string& status = |
| 205 ntp_snippets_service_->snippets_fetcher()->last_status(); | 205 ntp_snippets_service_->snippets_fetcher()->last_status(); |
| 206 if (!status.empty()) | 206 if (!status.empty()) |
| 207 SendString("hosts-status", "Finished: " + status); | 207 SendString("hosts-status", "Finished: " + status); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { | 210 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { |
| 211 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 211 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 212 | 212 |
| 213 int index = 0; | 213 int index = 0; |
| 214 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) | 214 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) |
| 215 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | 215 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); |
| 216 | 216 |
| 217 base::DictionaryValue result; | 217 base::DictionaryValue result; |
| 218 result.Set("list", std::move(snippets_list)); | 218 result.Set("list", std::move(snippets_list)); |
| 219 web_ui()->CallJavascriptFunction( | 219 web_ui()->CallJavascriptFunctionUnsafe( |
| 220 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); | 220 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); |
| 221 } | 221 } |
| 222 | 222 |
| 223 void SnippetsInternalsMessageHandler::SendHosts() { | 223 void SnippetsInternalsMessageHandler::SendHosts() { |
| 224 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | 224 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); |
| 225 | 225 |
| 226 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); | 226 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); |
| 227 | 227 |
| 228 for (const std::string& host : hosts) { | 228 for (const std::string& host : hosts) { |
| 229 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 229 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 230 entry->SetString("url", host); | 230 entry->SetString("url", host); |
| 231 | 231 |
| 232 hosts_list->Append(std::move(entry)); | 232 hosts_list->Append(std::move(entry)); |
| 233 } | 233 } |
| 234 | 234 |
| 235 base::DictionaryValue result; | 235 base::DictionaryValue result; |
| 236 result.Set("list", std::move(hosts_list)); | 236 result.Set("list", std::move(hosts_list)); |
| 237 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", | 237 web_ui()->CallJavascriptFunctionUnsafe( |
| 238 result); | 238 "chrome.SnippetsInternals.receiveHosts", result); |
| 239 } | 239 } |
| 240 | 240 |
| 241 void SnippetsInternalsMessageHandler::SendJson(const std::string& json) { | 241 void SnippetsInternalsMessageHandler::SendJson(const std::string& json) { |
| 242 web_ui()->CallJavascriptFunction( | 242 web_ui()->CallJavascriptFunctionUnsafe( |
| 243 "chrome.SnippetsInternals.receiveJsonToDownload", | 243 "chrome.SnippetsInternals.receiveJsonToDownload", |
| 244 base::StringValue(json)); | 244 base::StringValue(json)); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 247 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 248 bool value) { | 248 bool value) { |
| 249 SendString(name, value ? "True" : "False"); | 249 SendString(name, value ? "True" : "False"); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 252 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 253 const std::string& value) { | 253 const std::string& value) { |
| 254 base::StringValue string_name(name); | 254 base::StringValue string_name(name); |
| 255 base::StringValue string_value(value); | 255 base::StringValue string_value(value); |
| 256 | 256 |
| 257 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 257 web_ui()->CallJavascriptFunctionUnsafe( |
| 258 string_name, string_value); | 258 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
| 259 } | 259 } |
| OLD | NEW |