| 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 64 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 65 | 65 |
| 66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | 66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
| 67 | 67 |
| 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
| 69 if (!dom_loaded_) return; | 69 if (!dom_loaded_) return; |
| 70 | 70 |
| 71 SendSnippets(); | 71 SendSnippets(); |
| 72 SendDiscardedSnippets(); | 72 SendDiscardedSnippets(); |
| 73 |
| 74 web_ui()->CallJavascriptFunction( |
| 75 "chrome.SnippetsInternals.receiveJson", |
| 76 base::StringValue(ntp_snippets_service_->last_json())); |
| 73 } | 77 } |
| 74 | 78 |
| 75 void SnippetsInternalsMessageHandler::RegisterMessages() { | 79 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 76 // additional initialization (web_ui() does not work from the constructor) | 80 // additional initialization (web_ui() does not work from the constructor) |
| 77 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 81 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| 78 GetForProfile(Profile::FromWebUI(web_ui())); | 82 GetForProfile(Profile::FromWebUI(web_ui())); |
| 79 observer_.Add(ntp_snippets_service_); | 83 observer_.Add(ntp_snippets_service_); |
| 80 | 84 |
| 81 web_ui()->RegisterMessageCallback( | 85 web_ui()->RegisterMessageCallback( |
| 82 "loaded", | 86 "loaded", |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 122 |
| 119 void SnippetsInternalsMessageHandler::HandleDump(const base::ListValue* args) { | 123 void SnippetsInternalsMessageHandler::HandleDump(const base::ListValue* args) { |
| 120 DCHECK_EQ(0u, args->GetSize()); | 124 DCHECK_EQ(0u, args->GetSize()); |
| 121 | 125 |
| 122 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 126 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); |
| 123 | 127 |
| 124 std::string json; | 128 std::string json; |
| 125 base::JSONWriter::Write( | 129 base::JSONWriter::Write( |
| 126 *pref_service->GetList(ntp_snippets::prefs::kSnippets), &json); | 130 *pref_service->GetList(ntp_snippets::prefs::kSnippets), &json); |
| 127 | 131 |
| 128 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveJson", | 132 SendJson(json); |
| 129 base::StringValue(json)); | |
| 130 } | 133 } |
| 131 | 134 |
| 132 void SnippetsInternalsMessageHandler::HandleClearDiscarded( | 135 void SnippetsInternalsMessageHandler::HandleClearDiscarded( |
| 133 const base::ListValue* args) { | 136 const base::ListValue* args) { |
| 134 DCHECK_EQ(0u, args->GetSize()); | 137 DCHECK_EQ(0u, args->GetSize()); |
| 135 | 138 |
| 136 ntp_snippets_service_->ClearDiscardedSnippets(); | 139 ntp_snippets_service_->ClearDiscardedSnippets(); |
| 137 SendDiscardedSnippets(); | 140 SendDiscardedSnippets(); |
| 138 } | 141 } |
| 139 | 142 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 | 214 |
| 212 hosts_list->Append(std::move(entry)); | 215 hosts_list->Append(std::move(entry)); |
| 213 } | 216 } |
| 214 | 217 |
| 215 base::DictionaryValue result; | 218 base::DictionaryValue result; |
| 216 result.Set("list", std::move(hosts_list)); | 219 result.Set("list", std::move(hosts_list)); |
| 217 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", | 220 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", |
| 218 result); | 221 result); |
| 219 } | 222 } |
| 220 | 223 |
| 224 void SnippetsInternalsMessageHandler::SendJson(const std::string& json) { |
| 225 web_ui()->CallJavascriptFunction( |
| 226 "chrome.SnippetsInternals.receiveJsonToDownload", |
| 227 base::StringValue(json)); |
| 228 } |
| 229 |
| 221 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 230 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 222 bool value) { | 231 bool value) { |
| 223 SendString(name, value ? "True" : "False"); | 232 SendString(name, value ? "True" : "False"); |
| 224 } | 233 } |
| 225 | 234 |
| 226 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 235 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 227 const std::string& value) { | 236 const std::string& value) { |
| 228 base::StringValue string_name(name); | 237 base::StringValue string_name(name); |
| 229 base::StringValue string_value(value); | 238 base::StringValue string_value(value); |
| 230 | 239 |
| 231 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 240 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", |
| 232 string_name, string_value); | 241 string_name, string_value); |
| 233 } | 242 } |
| OLD | NEW |