| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/i18n/time_formatting.h" | 15 #include "base/i18n/time_formatting.h" |
| 16 #include "base/json/json_writer.h" | |
| 17 #include "base/logging.h" | 16 #include "base/logging.h" |
| 18 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_piece.h" | |
| 20 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 21 #include "base/values.h" | 19 #include "base/values.h" |
| 22 #include "chrome/browser/android/chrome_feature_list.h" | 20 #include "chrome/browser/android/chrome_feature_list.h" |
| 23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | 21 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 25 #include "components/ntp_snippets/ntp_snippet.h" | 23 #include "components/ntp_snippets/ntp_snippet.h" |
| 26 #include "components/ntp_snippets/pref_names.h" | |
| 27 #include "components/ntp_snippets/switches.h" | 24 #include "components/ntp_snippets/switches.h" |
| 28 #include "components/prefs/pref_service.h" | |
| 29 #include "content/public/browser/web_ui.h" | 25 #include "content/public/browser/web_ui.h" |
| 30 | 26 |
| 31 namespace { | 27 namespace { |
| 32 | 28 |
| 33 std::unique_ptr<base::DictionaryValue> PrepareSnippet( | 29 std::unique_ptr<base::DictionaryValue> PrepareSnippet( |
| 34 const ntp_snippets::NTPSnippet& snippet, | 30 const ntp_snippets::NTPSnippet& snippet, |
| 35 int index, | 31 int index, |
| 36 bool discarded) { | 32 bool discarded) { |
| 37 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | 33 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 38 entry->SetString("snippetId", snippet.id()); | 34 entry->SetString("snippetId", snippet.id()); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 web_ui()->RegisterMessageCallback( | 85 web_ui()->RegisterMessageCallback( |
| 90 "loaded", | 86 "loaded", |
| 91 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, | 87 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, |
| 92 base::Unretained(this))); | 88 base::Unretained(this))); |
| 93 | 89 |
| 94 web_ui()->RegisterMessageCallback( | 90 web_ui()->RegisterMessageCallback( |
| 95 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, | 91 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, |
| 96 base::Unretained(this))); | 92 base::Unretained(this))); |
| 97 | 93 |
| 98 web_ui()->RegisterMessageCallback( | 94 web_ui()->RegisterMessageCallback( |
| 99 "dump", base::Bind(&SnippetsInternalsMessageHandler::HandleDump, | |
| 100 base::Unretained(this))); | |
| 101 | |
| 102 web_ui()->RegisterMessageCallback( | |
| 103 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, | 95 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, |
| 104 base::Unretained(this))); | 96 base::Unretained(this))); |
| 105 | 97 |
| 106 web_ui()->RegisterMessageCallback( | 98 web_ui()->RegisterMessageCallback( |
| 107 "clearDiscarded", | 99 "clearDiscarded", |
| 108 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, | 100 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, |
| 109 base::Unretained(this))); | 101 base::Unretained(this))); |
| 110 } | 102 } |
| 111 | 103 |
| 112 void SnippetsInternalsMessageHandler::HandleLoaded( | 104 void SnippetsInternalsMessageHandler::HandleLoaded( |
| 113 const base::ListValue* args) { | 105 const base::ListValue* args) { |
| 114 DCHECK_EQ(0u, args->GetSize()); | 106 DCHECK_EQ(0u, args->GetSize()); |
| 115 | 107 |
| 116 dom_loaded_ = true; | 108 dom_loaded_ = true; |
| 117 | 109 |
| 118 SendInitialData(); | 110 SendInitialData(); |
| 119 } | 111 } |
| 120 | 112 |
| 121 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { | 113 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { |
| 122 DCHECK_EQ(0u, args->GetSize()); | 114 DCHECK_EQ(0u, args->GetSize()); |
| 123 | 115 |
| 124 ntp_snippets_service_->ClearSnippets(); | 116 ntp_snippets_service_->ClearSnippets(); |
| 125 } | 117 } |
| 126 | 118 |
| 127 void SnippetsInternalsMessageHandler::HandleDump(const base::ListValue* args) { | |
| 128 DCHECK_EQ(0u, args->GetSize()); | |
| 129 | |
| 130 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | |
| 131 | |
| 132 std::string json; | |
| 133 base::JSONWriter::Write( | |
| 134 *pref_service->GetList(ntp_snippets::prefs::kSnippets), &json); | |
| 135 | |
| 136 SendJson(json); | |
| 137 } | |
| 138 | |
| 139 void SnippetsInternalsMessageHandler::HandleClearDiscarded( | 119 void SnippetsInternalsMessageHandler::HandleClearDiscarded( |
| 140 const base::ListValue* args) { | 120 const base::ListValue* args) { |
| 141 DCHECK_EQ(0u, args->GetSize()); | 121 DCHECK_EQ(0u, args->GetSize()); |
| 142 | 122 |
| 143 ntp_snippets_service_->ClearDiscardedSnippets(); | 123 ntp_snippets_service_->ClearDiscardedSnippets(); |
| 144 SendDiscardedSnippets(); | 124 SendDiscardedSnippets(); |
| 145 } | 125 } |
| 146 | 126 |
| 147 void SnippetsInternalsMessageHandler::HandleDownload( | 127 void SnippetsInternalsMessageHandler::HandleDownload( |
| 148 const base::ListValue* args) { | 128 const base::ListValue* args) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 | 211 |
| 232 hosts_list->Append(std::move(entry)); | 212 hosts_list->Append(std::move(entry)); |
| 233 } | 213 } |
| 234 | 214 |
| 235 base::DictionaryValue result; | 215 base::DictionaryValue result; |
| 236 result.Set("list", std::move(hosts_list)); | 216 result.Set("list", std::move(hosts_list)); |
| 237 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", | 217 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", |
| 238 result); | 218 result); |
| 239 } | 219 } |
| 240 | 220 |
| 241 void SnippetsInternalsMessageHandler::SendJson(const std::string& json) { | |
| 242 web_ui()->CallJavascriptFunction( | |
| 243 "chrome.SnippetsInternals.receiveJsonToDownload", | |
| 244 base::StringValue(json)); | |
| 245 } | |
| 246 | |
| 247 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 221 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 248 bool value) { | 222 bool value) { |
| 249 SendString(name, value ? "True" : "False"); | 223 SendString(name, value ? "True" : "False"); |
| 250 } | 224 } |
| 251 | 225 |
| 252 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 226 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 253 const std::string& value) { | 227 const std::string& value) { |
| 254 base::StringValue string_name(name); | 228 base::StringValue string_name(name); |
| 255 base::StringValue string_value(value); | 229 base::StringValue string_value(value); |
| 256 | 230 |
| 257 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 231 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", |
| 258 string_name, string_value); | 232 string_name, string_value); |
| 259 } | 233 } |
| OLD | NEW |