Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: chrome/browser/ui/webui/snippets_internals_message_handler.cc

Issue 1927993002: Allow dumping raw json fetched from the server. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Some fixes Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 entry->SetString("id", "snippet-" + base::IntToString(index)); 52 entry->SetString("id", "snippet-" + base::IntToString(index));
53 53
54 return entry; 54 return entry;
55 } 55 }
56 56
57 } // namespace 57 } // namespace
58 58
59 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 59 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
60 : observer_(this), 60 : observer_(this),
61 dom_loaded_(false), 61 dom_loaded_(false),
62 dump_current_fetch_(false),
62 ntp_snippets_service_(nullptr) {} 63 ntp_snippets_service_(nullptr) {}
63 64
64 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 65 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
65 66
66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} 67 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {}
67 68
68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { 69 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() {
69 if (!dom_loaded_) return; 70 if (!dom_loaded_) return;
70 71
71 SendSnippets(); 72 SendSnippets();
72 SendDiscardedSnippets(); 73 SendDiscardedSnippets();
74
75 if (dump_current_fetch_) {
Bernhard Bauer 2016/04/29 12:02:22 If we have the last downloaded JSON anyway, we cou
jkrcal 2016/04/29 13:11:48 Done, with an extra download button. Did not forma
76 SendJson(ntp_snippets_service_->last_json());
77 dump_current_fetch_ = false;
78 }
73 } 79 }
74 80
75 void SnippetsInternalsMessageHandler::RegisterMessages() { 81 void SnippetsInternalsMessageHandler::RegisterMessages() {
76 // additional initialization (web_ui() does not work from the constructor) 82 // additional initialization (web_ui() does not work from the constructor)
77 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> 83 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()->
78 GetForProfile(Profile::FromWebUI(web_ui())); 84 GetForProfile(Profile::FromWebUI(web_ui()));
79 observer_.Add(ntp_snippets_service_); 85 observer_.Add(ntp_snippets_service_);
80 86
81 web_ui()->RegisterMessageCallback( 87 web_ui()->RegisterMessageCallback(
82 "loaded", 88 "loaded",
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 124
119 void SnippetsInternalsMessageHandler::HandleDump(const base::ListValue* args) { 125 void SnippetsInternalsMessageHandler::HandleDump(const base::ListValue* args) {
120 DCHECK_EQ(0u, args->GetSize()); 126 DCHECK_EQ(0u, args->GetSize());
121 127
122 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); 128 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
123 129
124 std::string json; 130 std::string json;
125 base::JSONWriter::Write( 131 base::JSONWriter::Write(
126 *pref_service->GetList(ntp_snippets::prefs::kSnippets), &json); 132 *pref_service->GetList(ntp_snippets::prefs::kSnippets), &json);
127 133
128 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveJson", 134 SendJson(json);
129 base::StringValue(json));
130 } 135 }
131 136
132 void SnippetsInternalsMessageHandler::HandleClearDiscarded( 137 void SnippetsInternalsMessageHandler::HandleClearDiscarded(
133 const base::ListValue* args) { 138 const base::ListValue* args) {
134 DCHECK_EQ(0u, args->GetSize()); 139 DCHECK_EQ(0u, args->GetSize());
135 140
136 ntp_snippets_service_->ClearDiscardedSnippets(); 141 ntp_snippets_service_->ClearDiscardedSnippets();
137 SendDiscardedSnippets(); 142 SendDiscardedSnippets();
138 } 143 }
139 144
140 void SnippetsInternalsMessageHandler::HandleDownload( 145 void SnippetsInternalsMessageHandler::HandleDownload(
141 const base::ListValue* args) { 146 const base::ListValue* args) {
142 DCHECK_EQ(1u, args->GetSize()); 147 DCHECK_EQ(2u, args->GetSize());
143 148
144 SendString("hosts-status", std::string()); 149 SendString("hosts-status", std::string());
145 150
146 std::string hosts_string; 151 std::string hosts_string;
147 args->GetString(0, &hosts_string); 152 args->GetString(0, &hosts_string);
153 args->GetBoolean(1, &dump_current_fetch_);
148 154
149 std::vector<std::string> hosts_vector = base::SplitString( 155 std::vector<std::string> hosts_vector = base::SplitString(
150 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 156 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
151 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 157 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
152 158
153 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); 159 ntp_snippets_service_->FetchSnippetsFromHosts(hosts);
154 } 160 }
155 161
156 void SnippetsInternalsMessageHandler::SendInitialData() { 162 void SnippetsInternalsMessageHandler::SendInitialData() {
157 SendHosts(); 163 SendHosts();
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 217
212 hosts_list->Append(std::move(entry)); 218 hosts_list->Append(std::move(entry));
213 } 219 }
214 220
215 base::DictionaryValue result; 221 base::DictionaryValue result;
216 result.Set("list", std::move(hosts_list)); 222 result.Set("list", std::move(hosts_list));
217 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", 223 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts",
218 result); 224 result);
219 } 225 }
220 226
227 void SnippetsInternalsMessageHandler::SendJson(const std::string& json) {
228 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveJson",
229 base::StringValue(json));
230 }
231
221 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 232 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
222 bool value) { 233 bool value) {
223 SendString(name, value ? "True" : "False"); 234 SendString(name, value ? "True" : "False");
224 } 235 }
225 236
226 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 237 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
227 const std::string& value) { 238 const std::string& value) {
228 base::StringValue string_name(name); 239 base::StringValue string_name(name);
229 base::StringValue string_value(value); 240 base::StringValue string_value(value);
230 241
231 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", 242 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty",
232 string_name, string_value); 243 string_name, string_value);
233 } 244 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698