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

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

Issue 1883523002: chrome://snippets-internals page for debugging NTP snippets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fourth code review Created 4 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/snippets_internals_message_handler.h"
6
7 #include <memory>
8 #include <set>
9 #include <sstream>
10 #include <vector>
11
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/command_line.h"
15 #include "base/feature_list.h"
16 #include "base/i18n/time_formatting.h"
17 #include "base/logging.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_piece.h"
20 #include "base/strings/string_split.h"
21 #include "base/values.h"
22 #include "chrome/browser/android/chrome_feature_list.h"
23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "components/ntp_snippets/ntp_snippet.h"
26 #include "components/ntp_snippets/switches.h"
27 #include "content/public/browser/web_ui.h"
28
29 namespace {
30
31 std::unique_ptr<base::DictionaryValue> PrepareSnippet(
32 const ntp_snippets::NTPSnippet& snippet,
33 int index,
34 bool discarded) {
35 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
36 entry->SetString("title", snippet.title());
37 entry->SetString("siteTitle", snippet.site_title());
38 entry->SetString("snippet", snippet.snippet());
39 entry->SetString("published",
40 TimeFormatShortDateAndTime(snippet.publish_date()));
41 entry->SetString("expires",
42 TimeFormatShortDateAndTime(snippet.expiry_date()));
43 entry->SetString("url", snippet.url().spec());
44 entry->SetString("faviconUrl", snippet.favicon_url().spec());
45 entry->SetString("salientImageUrl", snippet.salient_image_url().spec());
46
47 if (discarded)
48 entry->SetString("id", "discarded-snippet-" + base::IntToString(index));
49 else
50 entry->SetString("id", "snippet-" + base::IntToString(index));
51
52 return entry;
53 }
54
55 } // namespace
56
57 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
58 : observer_(this),
59 dom_loaded_(false),
60 ntp_snippets_service_(NULL) {}
Marc Treib 2016/04/15 15:51:32 nit: nullptr please
jkrcal 2016/04/18 12:22:54 Done.
61
62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
63
64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {}
65
66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() {
67 if (dom_loaded_){
Marc Treib 2016/04/15 15:51:32 nit: space before brace Or, alternatively, if (dom
jkrcal 2016/04/18 12:22:54 Done.
68 SendSnippets();
69 SendDiscardedSnippets();
70 }
71 }
72
73 void SnippetsInternalsMessageHandler::RegisterMessages() {
74 web_ui()->RegisterMessageCallback(
75 "loaded",
76 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded,
77 base::Unretained(this)));
78
79 web_ui()->RegisterMessageCallback(
80 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear,
81 base::Unretained(this)));
82
83 web_ui()->RegisterMessageCallback(
84 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
85 base::Unretained(this)));
86
87 web_ui()->RegisterMessageCallback(
88 "clearDiscarded",
89 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded,
90 base::Unretained(this)));
91 }
92
93 void SnippetsInternalsMessageHandler::HandleLoaded(
94 const base::ListValue* args) {
95 DCHECK_EQ(0u, args->GetSize());
96
97 dom_loaded_ = true;
98
99 if (ntp_snippets_service_ == NULL) {
Marc Treib 2016/04/15 15:51:32 nit: if (!ntp_snippets_service_)
jkrcal 2016/04/18 12:22:54 Done.
100 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()->
101 GetForProfile(Profile::FromWebUI(web_ui()));
102 observer_.Add(ntp_snippets_service_);
103 }
104
105 SendInitialData();
106 }
107
108 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) {
109 DCHECK_EQ(0u, args->GetSize());
110
111 ntp_snippets_service_->ClearSnippets();
112 }
113
114 void SnippetsInternalsMessageHandler::HandleClearDiscarded(
115 const base::ListValue* args) {
116 DCHECK_EQ(0u, args->GetSize());
117
118 ntp_snippets_service_->ClearDiscardedSnippets();
119 SendDiscardedSnippets();
120 }
121
122 void SnippetsInternalsMessageHandler::HandleDownload(
123 const base::ListValue* args) {
124 DCHECK_EQ(1u, args->GetSize());
125
126 std::string hosts_string;
127 args->GetString(0, &hosts_string);
128
129 std::vector<std::string> hosts_vector = base::SplitString(
130 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
131 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
132
133 ntp_snippets_service_->FetchSnippetsFromHosts(hosts);
134 }
135
136 void SnippetsInternalsMessageHandler::SendInitialData() {
137 SendHosts();
138
139 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
140 chrome::android::kNTPSnippetsFeature));
141
142 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch(
143 ntp_snippets::switches::kDontRestrict);
144 SendBoolean("switch-restrict-to-hosts", restricted);
145 const std::string help(restricted ? "(specify at least one host)" :
146 "(unrestricted if no host is given)");
147 SendString("hosts-help", help);
148
149 SendSnippets();
150 SendDiscardedSnippets();
151 }
152
153 void SnippetsInternalsMessageHandler::SendSnippets() {
154 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
155
156 int index = 0;
157 for (auto& snippet : *ntp_snippets_service_)
158 snippets_list->Append(PrepareSnippet(snippet, index++, false));
159
160 base::DictionaryValue result;
161 result.Set("list", std::move(snippets_list));
162 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets",
163 result);
164 }
165
166 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() {
167 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
168
169 int index = 0;
170 for (auto& snippet : ntp_snippets_service_->discarded_snippets())
171 snippets_list->Append(PrepareSnippet(*snippet, index++, true));
172
173 base::DictionaryValue result;
174 result.Set("list", std::move(snippets_list));
175 web_ui()->CallJavascriptFunction(
176 "chrome.SnippetsInternals.receiveDiscardedSnippets", result);
177 }
178
179 void SnippetsInternalsMessageHandler::SendHosts() {
180 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue);
181
182 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts();
183
184 for (const std::string& host : hosts) {
185 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
186 entry->SetString("url", host);
187
188 hosts_list->Append(std::move(entry));
189 }
190
191 base::DictionaryValue result;
192 result.Set("list", std::move(hosts_list));
193 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts",
194 result);
195 }
196
197 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
198 bool value) {
199 SendString(name, value ? "True" : "False");
200 }
201
202 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
203 const std::string& value) {
204 base::StringValue string_name(name);
205 base::StringValue string_value(value);
206
207 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty",
208 string_name, string_value);
209 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/snippets_internals_message_handler.h ('k') | chrome/browser/ui/webui/snippets_internals_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698