| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 | 61 |
| 62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 63 | 63 |
| 64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | 64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
| 65 | 65 |
| 66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | 66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
| 67 if (!dom_loaded_) return; | 67 if (!dom_loaded_) return; |
| 68 | 68 |
| 69 SendSnippets(); | 69 SendSnippets(); |
| 70 | 70 |
| 71 web_ui()->CallJavascriptFunction( | 71 web_ui()->CallJavascriptFunctionUnsafe( |
| 72 "chrome.SnippetsInternals.receiveJson", | 72 "chrome.SnippetsInternals.receiveJson", |
| 73 base::StringValue( | 73 base::StringValue( |
| 74 ntp_snippets_service_->snippets_fetcher()->last_json())); | 74 ntp_snippets_service_->snippets_fetcher()->last_json())); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabled() {} | 77 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabled() {} |
| 78 | 78 |
| 79 void SnippetsInternalsMessageHandler::RegisterMessages() { | 79 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 80 // additional initialization (web_ui() does not work from the constructor) | 80 // additional initialization (web_ui() does not work from the constructor) |
| 81 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 81 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 | 139 |
| 140 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 140 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void SnippetsInternalsMessageHandler::SendInitialData() { | 143 void SnippetsInternalsMessageHandler::SendInitialData() { |
| 144 SendHosts(); | 144 SendHosts(); |
| 145 | 145 |
| 146 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 146 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 147 chrome::android::kNTPSnippetsFeature)); | 147 chrome::android::kNTPSnippetsFeature)); |
| 148 | 148 |
| 149 web_ui()->CallJavascriptFunction( | 149 web_ui()->CallJavascriptFunctionUnsafe( |
| 150 "chrome.SnippetsInternals.setHostRestricted", | 150 "chrome.SnippetsInternals.setHostRestricted", |
| 151 base::FundamentalValue( | 151 base::FundamentalValue( |
| 152 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); | 152 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); |
| 153 | 153 |
| 154 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { | 154 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { |
| 155 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 155 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 156 SendString("switch-personalized", "Only personalized"); | 156 SendString("switch-personalized", "Only personalized"); |
| 157 break; | 157 break; |
| 158 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 158 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 159 SendString("switch-personalized", | 159 SendString("switch-personalized", |
| (...skipping 11 matching lines...) Expand all Loading... |
| 171 void SnippetsInternalsMessageHandler::SendSnippets() { | 171 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 172 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 172 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 173 | 173 |
| 174 int index = 0; | 174 int index = 0; |
| 175 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 175 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 176 ntp_snippets_service_->snippets()) | 176 ntp_snippets_service_->snippets()) |
| 177 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); | 177 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); |
| 178 | 178 |
| 179 base::DictionaryValue result; | 179 base::DictionaryValue result; |
| 180 result.Set("list", std::move(snippets_list)); | 180 result.Set("list", std::move(snippets_list)); |
| 181 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets", | 181 web_ui()->CallJavascriptFunctionUnsafe( |
| 182 result); | 182 "chrome.SnippetsInternals.receiveSnippets", result); |
| 183 | 183 |
| 184 const std::string& status = | 184 const std::string& status = |
| 185 ntp_snippets_service_->snippets_fetcher()->last_status(); | 185 ntp_snippets_service_->snippets_fetcher()->last_status(); |
| 186 if (!status.empty()) | 186 if (!status.empty()) |
| 187 SendString("hosts-status", "Finished: " + status); | 187 SendString("hosts-status", "Finished: " + status); |
| 188 } | 188 } |
| 189 | 189 |
| 190 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { | 190 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { |
| 191 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 191 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 192 | 192 |
| 193 int index = 0; | 193 int index = 0; |
| 194 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) | 194 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) |
| 195 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | 195 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); |
| 196 | 196 |
| 197 base::DictionaryValue result; | 197 base::DictionaryValue result; |
| 198 result.Set("list", std::move(snippets_list)); | 198 result.Set("list", std::move(snippets_list)); |
| 199 web_ui()->CallJavascriptFunction( | 199 web_ui()->CallJavascriptFunctionUnsafe( |
| 200 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); | 200 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void SnippetsInternalsMessageHandler::SendHosts() { | 203 void SnippetsInternalsMessageHandler::SendHosts() { |
| 204 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | 204 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); |
| 205 | 205 |
| 206 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); | 206 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); |
| 207 | 207 |
| 208 for (const std::string& host : hosts) { | 208 for (const std::string& host : hosts) { |
| 209 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 209 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 210 entry->SetString("url", host); | 210 entry->SetString("url", host); |
| 211 | 211 |
| 212 hosts_list->Append(std::move(entry)); | 212 hosts_list->Append(std::move(entry)); |
| 213 } | 213 } |
| 214 | 214 |
| 215 base::DictionaryValue result; | 215 base::DictionaryValue result; |
| 216 result.Set("list", std::move(hosts_list)); | 216 result.Set("list", std::move(hosts_list)); |
| 217 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", | 217 web_ui()->CallJavascriptFunctionUnsafe( |
| 218 result); | 218 "chrome.SnippetsInternals.receiveHosts", result); |
| 219 } | 219 } |
| 220 | 220 |
| 221 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 221 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 222 bool value) { | 222 bool value) { |
| 223 SendString(name, value ? "True" : "False"); | 223 SendString(name, value ? "True" : "False"); |
| 224 } | 224 } |
| 225 | 225 |
| 226 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 226 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 227 const std::string& value) { | 227 const std::string& value) { |
| 228 base::StringValue string_name(name); | 228 base::StringValue string_name(name); |
| 229 base::StringValue string_value(value); | 229 base::StringValue string_value(value); |
| 230 | 230 |
| 231 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 231 web_ui()->CallJavascriptFunctionUnsafe( |
| 232 string_name, string_value); | 232 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
| 233 } | 233 } |
| OLD | NEW |