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

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

Issue 2145563002: Add ContentSuggestionsService to SnippetsInternals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@articleprovider2
Patch Set: Bernhard's comments Created 4 years, 5 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
« no previous file with comments | « chrome/browser/ui/webui/snippets_internals_message_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/ptr_util.h"
17 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 19 #include "base/strings/string_split.h"
19 #include "base/values.h" 20 #include "base/values.h"
20 #include "chrome/browser/android/chrome_feature_list.h" 21 #include "chrome/browser/android/chrome_feature_list.h"
22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h"
21 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" 23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h"
22 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
23 #include "components/ntp_snippets/ntp_snippet.h" 25 #include "components/ntp_snippets/ntp_snippet.h"
24 #include "components/ntp_snippets/switches.h" 26 #include "components/ntp_snippets/switches.h"
25 #include "content/public/browser/web_ui.h" 27 #include "content/public/browser/web_ui.h"
26 28
29 using ntp_snippets::ContentSuggestion;
30 using ntp_snippets::ContentSuggestionsCategory;
31 using ntp_snippets::ContentSuggestionsCategoryStatus;
32
27 namespace { 33 namespace {
28 34
29 std::unique_ptr<base::DictionaryValue> PrepareSnippet( 35 std::unique_ptr<base::DictionaryValue> PrepareSnippet(
30 const ntp_snippets::NTPSnippet& snippet, 36 const ntp_snippets::NTPSnippet& snippet,
31 int index, 37 int index,
32 bool discarded) { 38 bool discarded) {
33 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); 39 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue);
34 entry->SetString("snippetId", snippet.id()); 40 entry->SetString("snippetId", snippet.id());
35 entry->SetString("title", snippet.title()); 41 entry->SetString("title", snippet.title());
36 entry->SetString("siteTitle", snippet.best_source().publisher_name); 42 entry->SetString("siteTitle", snippet.best_source().publisher_name);
37 entry->SetString("snippet", snippet.snippet()); 43 entry->SetString("snippet", snippet.snippet());
38 entry->SetString("published", 44 entry->SetString("published",
39 TimeFormatShortDateAndTime(snippet.publish_date())); 45 TimeFormatShortDateAndTime(snippet.publish_date()));
40 entry->SetString("expires", 46 entry->SetString("expires",
41 TimeFormatShortDateAndTime(snippet.expiry_date())); 47 TimeFormatShortDateAndTime(snippet.expiry_date()));
42 entry->SetString("url", snippet.best_source().url.spec()); 48 entry->SetString("url", snippet.best_source().url.spec());
43 entry->SetString("ampUrl", snippet.best_source().amp_url.spec()); 49 entry->SetString("ampUrl", snippet.best_source().amp_url.spec());
44 entry->SetString("salientImageUrl", snippet.salient_image_url().spec()); 50 entry->SetString("salientImageUrl", snippet.salient_image_url().spec());
45 entry->SetDouble("score", snippet.score()); 51 entry->SetDouble("score", snippet.score());
46 52
47 if (discarded) 53 if (discarded)
48 entry->SetString("id", "discarded-snippet-" + base::IntToString(index)); 54 entry->SetString("id", "discarded-snippet-" + base::IntToString(index));
49 else 55 else
50 entry->SetString("id", "snippet-" + base::IntToString(index)); 56 entry->SetString("id", "snippet-" + base::IntToString(index));
51 57
52 return entry; 58 return entry;
53 } 59 }
54 60
61 std::unique_ptr<base::DictionaryValue> PrepareSuggestion(
62 const ContentSuggestion& suggestion,
63 int index) {
64 auto entry = base::MakeUnique<base::DictionaryValue>();
65 entry->SetString("suggestionId", suggestion.id());
66 entry->SetString("url", suggestion.url().spec());
67 entry->SetString("ampUrl", suggestion.amp_url().spec());
68 entry->SetString("title", suggestion.title());
69 entry->SetString("snippetText", suggestion.snippet_text());
70 entry->SetString("publishDate",
71 TimeFormatShortDateAndTime(suggestion.publish_date()));
72 entry->SetString("publisherName", suggestion.publisher_name());
73 entry->SetString("id", "content-suggestion-" + base::IntToString(index));
74 return entry;
75 }
76
77 std::string MapCategoryName(ContentSuggestionsCategory category) {
78 switch (category) {
79 case ContentSuggestionsCategory::ARTICLES:
80 return "Articles";
81 case ContentSuggestionsCategory::COUNT:
82 NOTREACHED() << "Category::COUNT must not be used as a value";
83 return std::string();
84 }
85 }
86
87 std::string MapCategoryStatus(ContentSuggestionsCategoryStatus status) {
88 switch (status) {
89 case ContentSuggestionsCategoryStatus::INITIALIZING:
90 return "INITIALIZING";
91 case ContentSuggestionsCategoryStatus::AVAILABLE:
92 return "AVAILABLE";
93 case ContentSuggestionsCategoryStatus::AVAILABLE_LOADING:
94 return "AVAILABLE_LOADING";
95 case ContentSuggestionsCategoryStatus::NOT_PROVIDED:
96 return "NOT_PROVIDED";
97 case ContentSuggestionsCategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED:
98 return "ALL_SUGGESTIONS_EXPLICITLY_DISABLED";
99 case ContentSuggestionsCategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
100 return "CATEGORY_EXPLICITLY_DISABLED";
101 case ContentSuggestionsCategoryStatus::SIGNED_OUT:
102 return "SIGNED_OUT";
103 case ContentSuggestionsCategoryStatus::SYNC_DISABLED:
104 return "SYNC_DISABLED";
105 case ContentSuggestionsCategoryStatus::PASSPHRASE_ENCRYPTION_ENABLED:
106 return "PASSPHRASE_ENCRYPTION_ENABLED";
107 case ContentSuggestionsCategoryStatus::HISTORY_SYNC_DISABLED:
108 return "HISTORY_SYNC_DISABLED";
109 case ContentSuggestionsCategoryStatus::LOADING_ERROR:
110 return "LOADING_ERROR";
111 }
112 }
113
55 } // namespace 114 } // namespace
56 115
57 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() 116 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler()
58 : observer_(this), 117 : ntp_snippets_service_observer_(this),
118 content_suggestions_service_observer_(this),
59 dom_loaded_(false), 119 dom_loaded_(false),
60 ntp_snippets_service_(nullptr) {} 120 ntp_snippets_service_(nullptr),
121 content_suggestions_service_(nullptr) {}
61 122
62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} 123 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {}
63 124
64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} 125 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {}
65 126
66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { 127 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() {
67 if (!dom_loaded_) return; 128 if (!dom_loaded_) return;
68 129
69 SendSnippets(); 130 SendSnippets();
70 131
71 web_ui()->CallJavascriptFunctionUnsafe( 132 web_ui()->CallJavascriptFunctionUnsafe(
72 "chrome.SnippetsInternals.receiveJson", 133 "chrome.SnippetsInternals.receiveJson",
73 base::StringValue( 134 base::StringValue(
74 ntp_snippets_service_->snippets_fetcher()->last_json())); 135 ntp_snippets_service_->snippets_fetcher()->last_json()));
75 } 136 }
76 137
77 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( 138 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged(
78 ntp_snippets::DisabledReason disabled_reason) {} 139 ntp_snippets::DisabledReason disabled_reason) {}
79 140
141 void SnippetsInternalsMessageHandler::OnNewSuggestions() {
142 if (!dom_loaded_)
143 return;
144 SendContentSuggestions();
145 }
146
147 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged(
148 ContentSuggestionsCategory category,
149 ContentSuggestionsCategoryStatus new_status) {
150 if (!dom_loaded_)
151 return;
152 SendContentSuggestions();
153 }
154
155 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {}
156
80 void SnippetsInternalsMessageHandler::RegisterMessages() { 157 void SnippetsInternalsMessageHandler::RegisterMessages() {
81 // additional initialization (web_ui() does not work from the constructor) 158 // additional initialization (web_ui() does not work from the constructor)
82 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> 159 Profile* profile = Profile::FromWebUI(web_ui());
83 GetForProfile(Profile::FromWebUI(web_ui())); 160
84 observer_.Add(ntp_snippets_service_); 161 ntp_snippets_service_ =
162 NTPSnippetsServiceFactory::GetInstance()->GetForProfile(profile);
163 ntp_snippets_service_observer_.Add(ntp_snippets_service_);
164
165 content_suggestions_service_ =
166 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile);
167 content_suggestions_service_observer_.Add(content_suggestions_service_);
85 168
86 web_ui()->RegisterMessageCallback( 169 web_ui()->RegisterMessageCallback(
87 "loaded", 170 "loaded",
88 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, 171 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded,
89 base::Unretained(this))); 172 base::Unretained(this)));
90 173
91 web_ui()->RegisterMessageCallback( 174 web_ui()->RegisterMessageCallback(
92 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, 175 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear,
93 base::Unretained(this))); 176 base::Unretained(this)));
94 177
95 web_ui()->RegisterMessageCallback( 178 web_ui()->RegisterMessageCallback(
96 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, 179 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload,
97 base::Unretained(this))); 180 base::Unretained(this)));
98 181
99 web_ui()->RegisterMessageCallback( 182 web_ui()->RegisterMessageCallback(
100 "clearDiscarded", 183 "clearDiscarded",
101 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, 184 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded,
102 base::Unretained(this))); 185 base::Unretained(this)));
186
187 web_ui()->RegisterMessageCallback(
188 "clearCachedSuggestions",
189 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions,
190 base::Unretained(this)));
191
192 web_ui()->RegisterMessageCallback(
193 "clearDiscardedSuggestions",
194 base::Bind(
195 &SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions,
196 base::Unretained(this)));
103 } 197 }
104 198
105 void SnippetsInternalsMessageHandler::HandleLoaded( 199 void SnippetsInternalsMessageHandler::HandleLoaded(
106 const base::ListValue* args) { 200 const base::ListValue* args) {
107 DCHECK_EQ(0u, args->GetSize()); 201 DCHECK_EQ(0u, args->GetSize());
108 202
109 dom_loaded_ = true; 203 dom_loaded_ = true;
110 204
111 SendInitialData(); 205 SendInitialData();
112 } 206 }
(...skipping 21 matching lines...) Expand all
134 std::string hosts_string; 228 std::string hosts_string;
135 args->GetString(0, &hosts_string); 229 args->GetString(0, &hosts_string);
136 230
137 std::vector<std::string> hosts_vector = base::SplitString( 231 std::vector<std::string> hosts_vector = base::SplitString(
138 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); 232 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
139 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); 233 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end());
140 234
141 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); 235 ntp_snippets_service_->FetchSnippetsFromHosts(hosts);
142 } 236 }
143 237
238 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions(
239 const base::ListValue* args) {
240 DCHECK_EQ(0u, args->GetSize());
241
242 content_suggestions_service_->ClearCachedSuggestionsForDebugging();
243 }
244
245 void SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions(
246 const base::ListValue* args) {
247 DCHECK_EQ(0u, args->GetSize());
248
249 content_suggestions_service_->ClearDiscardedSuggestionsForDebugging();
250 }
251
144 void SnippetsInternalsMessageHandler::SendInitialData() { 252 void SnippetsInternalsMessageHandler::SendInitialData() {
145 SendHosts(); 253 SendHosts();
146 254
147 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( 255 SendBoolean("flag-snippets", base::FeatureList::IsEnabled(
148 chrome::android::kNTPSnippetsFeature)); 256 chrome::android::kNTPSnippetsFeature));
149 257
150 web_ui()->CallJavascriptFunctionUnsafe( 258 web_ui()->CallJavascriptFunctionUnsafe(
151 "chrome.SnippetsInternals.setHostRestricted", 259 "chrome.SnippetsInternals.setHostRestricted",
152 base::FundamentalValue( 260 base::FundamentalValue(
153 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); 261 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions()));
154 262
155 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { 263 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) {
156 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: 264 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal:
157 SendString("switch-personalized", "Only personalized"); 265 SendString("switch-personalized", "Only personalized");
158 break; 266 break;
159 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: 267 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth:
160 SendString("switch-personalized", 268 SendString("switch-personalized",
161 "Both personalized and non-personalized"); 269 "Both personalized and non-personalized");
162 break; 270 break;
163 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: 271 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal:
164 SendString("switch-personalized", "Only non-personalized"); 272 SendString("switch-personalized", "Only non-personalized");
165 break; 273 break;
166 } 274 }
167 275
168 SendString("switch-fetch-url", 276 SendString("switch-fetch-url",
169 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); 277 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec());
170 278
171 SendSnippets(); 279 SendSnippets();
172 SendDiscardedSnippets(); 280 SendDiscardedSnippets();
281 SendContentSuggestions();
173 } 282 }
174 283
175 void SnippetsInternalsMessageHandler::SendSnippets() { 284 void SnippetsInternalsMessageHandler::SendSnippets() {
176 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); 285 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue);
177 286
178 int index = 0; 287 int index = 0;
179 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : 288 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet :
180 ntp_snippets_service_->snippets()) 289 ntp_snippets_service_->snippets())
181 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); 290 snippets_list->Append(PrepareSnippet(*snippet, index++, false));
182 291
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 324
216 hosts_list->Append(std::move(entry)); 325 hosts_list->Append(std::move(entry));
217 } 326 }
218 327
219 base::DictionaryValue result; 328 base::DictionaryValue result;
220 result.Set("list", std::move(hosts_list)); 329 result.Set("list", std::move(hosts_list));
221 web_ui()->CallJavascriptFunctionUnsafe( 330 web_ui()->CallJavascriptFunctionUnsafe(
222 "chrome.SnippetsInternals.receiveHosts", result); 331 "chrome.SnippetsInternals.receiveHosts", result);
223 } 332 }
224 333
334 void SnippetsInternalsMessageHandler::SendContentSuggestions() {
335 std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
336
337 int index = 0;
338 for (ContentSuggestionsCategory category :
339 content_suggestions_service_->GetCategories()) {
340 ContentSuggestionsCategoryStatus status =
341 content_suggestions_service_->GetCategoryStatus(category);
342 const std::vector<ContentSuggestion>& suggestions =
343 content_suggestions_service_->GetSuggestionsForCategory(category);
344
345 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue);
346 for (const ContentSuggestion& suggestion : suggestions) {
347 suggestions_list->Append(PrepareSuggestion(suggestion, index++));
348 }
349
350 std::unique_ptr<base::DictionaryValue> category_entry(
351 new base::DictionaryValue);
352 category_entry->SetString("name", MapCategoryName(category));
353 category_entry->SetString("status", MapCategoryStatus(status));
354 category_entry->Set("suggestions", std::move(suggestions_list));
355 categories_list->Append(std::move(category_entry));
356 }
357
358 base::DictionaryValue result;
359 result.Set("list", std::move(categories_list));
360 web_ui()->CallJavascriptFunctionUnsafe(
361 "chrome.SnippetsInternals.receiveContentSuggestions", result);
362 }
363
225 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, 364 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name,
226 bool value) { 365 bool value) {
227 SendString(name, value ? "True" : "False"); 366 SendString(name, value ? "True" : "False");
228 } 367 }
229 368
230 void SnippetsInternalsMessageHandler::SendString(const std::string& name, 369 void SnippetsInternalsMessageHandler::SendString(const std::string& name,
231 const std::string& value) { 370 const std::string& value) {
232 base::StringValue string_name(name); 371 base::StringValue string_name(name);
233 base::StringValue string_value(value); 372 base::StringValue string_value(value);
234 373
235 web_ui()->CallJavascriptFunctionUnsafe( 374 web_ui()->CallJavascriptFunctionUnsafe(
236 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); 375 "chrome.SnippetsInternals.receiveProperty", string_name, string_value);
237 } 376 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/snippets_internals_message_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698