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/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 } |
| 84 return std::string(); |
| 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 return std::string(); |
| 113 } |
| 114 |
55 } // namespace | 115 } // namespace |
56 | 116 |
57 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() | 117 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() |
58 : observer_(this), | 118 : ntp_snippets_service_observer_(this), |
| 119 content_suggestions_service_observer_(this), |
59 dom_loaded_(false), | 120 dom_loaded_(false), |
60 ntp_snippets_service_(nullptr) {} | 121 ntp_snippets_service_(nullptr), |
| 122 content_suggestions_service_(nullptr) {} |
61 | 123 |
62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 124 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
63 | 125 |
64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | 126 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
65 | 127 |
66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | 128 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
67 if (!dom_loaded_) return; | 129 if (!dom_loaded_) return; |
68 | 130 |
69 SendSnippets(); | 131 SendSnippets(); |
70 | 132 |
71 web_ui()->CallJavascriptFunctionUnsafe( | 133 web_ui()->CallJavascriptFunctionUnsafe( |
72 "chrome.SnippetsInternals.receiveJson", | 134 "chrome.SnippetsInternals.receiveJson", |
73 base::StringValue( | 135 base::StringValue( |
74 ntp_snippets_service_->snippets_fetcher()->last_json())); | 136 ntp_snippets_service_->snippets_fetcher()->last_json())); |
75 } | 137 } |
76 | 138 |
77 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( | 139 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( |
78 ntp_snippets::DisabledReason disabled_reason) {} | 140 ntp_snippets::DisabledReason disabled_reason) {} |
79 | 141 |
| 142 void SnippetsInternalsMessageHandler::OnNewSuggestions() { |
| 143 if (!dom_loaded_) |
| 144 return; |
| 145 SendContentSuggestions(); |
| 146 } |
| 147 |
| 148 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( |
| 149 ContentSuggestionsCategory category, |
| 150 ContentSuggestionsCategoryStatus new_status) { |
| 151 if (!dom_loaded_) |
| 152 return; |
| 153 SendContentSuggestions(); |
| 154 } |
| 155 |
| 156 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {} |
| 157 |
80 void SnippetsInternalsMessageHandler::RegisterMessages() { | 158 void SnippetsInternalsMessageHandler::RegisterMessages() { |
81 // additional initialization (web_ui() does not work from the constructor) | 159 // additional initialization (web_ui() does not work from the constructor) |
82 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 160 Profile* profile = Profile::FromWebUI(web_ui()); |
83 GetForProfile(Profile::FromWebUI(web_ui())); | 161 |
84 observer_.Add(ntp_snippets_service_); | 162 ntp_snippets_service_ = |
| 163 NTPSnippetsServiceFactory::GetInstance()->GetForProfile(profile); |
| 164 ntp_snippets_service_observer_.Add(ntp_snippets_service_); |
| 165 |
| 166 content_suggestions_service_ = |
| 167 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); |
| 168 content_suggestions_service_observer_.Add(content_suggestions_service_); |
85 | 169 |
86 web_ui()->RegisterMessageCallback( | 170 web_ui()->RegisterMessageCallback( |
87 "loaded", | 171 "loaded", |
88 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, | 172 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, |
89 base::Unretained(this))); | 173 base::Unretained(this))); |
90 | 174 |
91 web_ui()->RegisterMessageCallback( | 175 web_ui()->RegisterMessageCallback( |
92 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, | 176 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, |
93 base::Unretained(this))); | 177 base::Unretained(this))); |
94 | 178 |
95 web_ui()->RegisterMessageCallback( | 179 web_ui()->RegisterMessageCallback( |
96 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, | 180 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, |
97 base::Unretained(this))); | 181 base::Unretained(this))); |
98 | 182 |
99 web_ui()->RegisterMessageCallback( | 183 web_ui()->RegisterMessageCallback( |
100 "clearDiscarded", | 184 "clearDiscarded", |
101 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, | 185 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, |
102 base::Unretained(this))); | 186 base::Unretained(this))); |
| 187 |
| 188 web_ui()->RegisterMessageCallback( |
| 189 "clearCachedSuggestions", |
| 190 base::Bind(&SnippetsInternalsMessageHandler::HandleClearCachedSuggestions, |
| 191 base::Unretained(this))); |
| 192 |
| 193 web_ui()->RegisterMessageCallback( |
| 194 "clearDiscardedSuggestions", |
| 195 base::Bind( |
| 196 &SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions, |
| 197 base::Unretained(this))); |
103 } | 198 } |
104 | 199 |
105 void SnippetsInternalsMessageHandler::HandleLoaded( | 200 void SnippetsInternalsMessageHandler::HandleLoaded( |
106 const base::ListValue* args) { | 201 const base::ListValue* args) { |
107 DCHECK_EQ(0u, args->GetSize()); | 202 DCHECK_EQ(0u, args->GetSize()); |
108 | 203 |
109 dom_loaded_ = true; | 204 dom_loaded_ = true; |
110 | 205 |
111 SendInitialData(); | 206 SendInitialData(); |
112 } | 207 } |
(...skipping 21 matching lines...) Expand all Loading... |
134 std::string hosts_string; | 229 std::string hosts_string; |
135 args->GetString(0, &hosts_string); | 230 args->GetString(0, &hosts_string); |
136 | 231 |
137 std::vector<std::string> hosts_vector = base::SplitString( | 232 std::vector<std::string> hosts_vector = base::SplitString( |
138 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 233 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
139 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); | 234 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); |
140 | 235 |
141 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 236 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
142 } | 237 } |
143 | 238 |
| 239 void SnippetsInternalsMessageHandler::HandleClearCachedSuggestions( |
| 240 const base::ListValue* args) { |
| 241 DCHECK_EQ(0u, args->GetSize()); |
| 242 |
| 243 content_suggestions_service_->ClearCachedSuggestionsForDebugging(); |
| 244 } |
| 245 |
| 246 void SnippetsInternalsMessageHandler::HandleClearDiscardedSuggestions( |
| 247 const base::ListValue* args) { |
| 248 DCHECK_EQ(0u, args->GetSize()); |
| 249 |
| 250 content_suggestions_service_->ClearDiscardedSuggestionsForDebugging(); |
| 251 } |
| 252 |
144 void SnippetsInternalsMessageHandler::SendInitialData() { | 253 void SnippetsInternalsMessageHandler::SendInitialData() { |
145 SendHosts(); | 254 SendHosts(); |
146 | 255 |
147 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 256 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
148 chrome::android::kNTPSnippetsFeature)); | 257 chrome::android::kNTPSnippetsFeature)); |
149 | 258 |
150 web_ui()->CallJavascriptFunctionUnsafe( | 259 web_ui()->CallJavascriptFunctionUnsafe( |
151 "chrome.SnippetsInternals.setHostRestricted", | 260 "chrome.SnippetsInternals.setHostRestricted", |
152 base::FundamentalValue( | 261 base::FundamentalValue( |
153 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); | 262 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); |
154 | 263 |
155 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { | 264 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { |
156 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: | 265 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
157 SendString("switch-personalized", "Only personalized"); | 266 SendString("switch-personalized", "Only personalized"); |
158 break; | 267 break; |
159 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: | 268 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
160 SendString("switch-personalized", | 269 SendString("switch-personalized", |
161 "Both personalized and non-personalized"); | 270 "Both personalized and non-personalized"); |
162 break; | 271 break; |
163 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 272 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
164 SendString("switch-personalized", "Only non-personalized"); | 273 SendString("switch-personalized", "Only non-personalized"); |
165 break; | 274 break; |
166 } | 275 } |
167 | 276 |
168 SendString("switch-fetch-url", | 277 SendString("switch-fetch-url", |
169 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); | 278 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); |
170 | 279 |
171 SendSnippets(); | 280 SendSnippets(); |
172 SendDiscardedSnippets(); | 281 SendDiscardedSnippets(); |
| 282 SendContentSuggestions(); |
173 } | 283 } |
174 | 284 |
175 void SnippetsInternalsMessageHandler::SendSnippets() { | 285 void SnippetsInternalsMessageHandler::SendSnippets() { |
176 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 286 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
177 | 287 |
178 int index = 0; | 288 int index = 0; |
179 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 289 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
180 ntp_snippets_service_->snippets()) | 290 ntp_snippets_service_->snippets()) |
181 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); | 291 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); |
182 | 292 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 325 |
216 hosts_list->Append(std::move(entry)); | 326 hosts_list->Append(std::move(entry)); |
217 } | 327 } |
218 | 328 |
219 base::DictionaryValue result; | 329 base::DictionaryValue result; |
220 result.Set("list", std::move(hosts_list)); | 330 result.Set("list", std::move(hosts_list)); |
221 web_ui()->CallJavascriptFunctionUnsafe( | 331 web_ui()->CallJavascriptFunctionUnsafe( |
222 "chrome.SnippetsInternals.receiveHosts", result); | 332 "chrome.SnippetsInternals.receiveHosts", result); |
223 } | 333 } |
224 | 334 |
| 335 void SnippetsInternalsMessageHandler::SendContentSuggestions() { |
| 336 std::unique_ptr<base::ListValue> categories_list(new base::ListValue); |
| 337 |
| 338 int index = 0; |
| 339 for (ContentSuggestionsCategory category : |
| 340 content_suggestions_service_->GetCategories()) { |
| 341 ContentSuggestionsCategoryStatus status = |
| 342 content_suggestions_service_->GetCategoryStatus(category); |
| 343 const std::vector<ContentSuggestion>& suggestions = |
| 344 content_suggestions_service_->GetSuggestionsForCategory(category); |
| 345 |
| 346 std::unique_ptr<base::ListValue> suggestions_list(new base::ListValue); |
| 347 for (const ContentSuggestion& suggestion : suggestions) { |
| 348 suggestions_list->Append(PrepareSuggestion(suggestion, index++)); |
| 349 } |
| 350 |
| 351 std::unique_ptr<base::DictionaryValue> category_entry( |
| 352 new base::DictionaryValue); |
| 353 category_entry->SetString("name", MapCategoryName(category)); |
| 354 category_entry->SetString("status", MapCategoryStatus(status)); |
| 355 category_entry->Set("suggestions", std::move(suggestions_list)); |
| 356 categories_list->Append(std::move(category_entry)); |
| 357 } |
| 358 |
| 359 base::DictionaryValue result; |
| 360 result.Set("list", std::move(categories_list)); |
| 361 web_ui()->CallJavascriptFunctionUnsafe( |
| 362 "chrome.SnippetsInternals.receiveContentSuggestions", result); |
| 363 } |
| 364 |
225 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | 365 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
226 bool value) { | 366 bool value) { |
227 SendString(name, value ? "True" : "False"); | 367 SendString(name, value ? "True" : "False"); |
228 } | 368 } |
229 | 369 |
230 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 370 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
231 const std::string& value) { | 371 const std::string& value) { |
232 base::StringValue string_name(name); | 372 base::StringValue string_name(name); |
233 base::StringValue string_value(value); | 373 base::StringValue string_value(value); |
234 | 374 |
235 web_ui()->CallJavascriptFunctionUnsafe( | 375 web_ui()->CallJavascriptFunctionUnsafe( |
236 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 376 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
237 } | 377 } |
OLD | NEW |