Chromium Code Reviews| OLD | NEW |
|---|---|
| (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() : | |
|
Marc Treib
2016/04/15 09:00:30
style: the colon goes on the next line
jkrcal
2016/04/15 14:11:51
Done.
| |
| 58 observer_(this) {} | |
| 59 | |
| 60 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | |
| 61 | |
| 62 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | |
| 63 | |
| 64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | |
| 65 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 66 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 67 Profile::FromWebUI(web_ui())); | |
| 68 | |
| 69 SendSnippets(ntp_snippets_service); | |
| 70 SendDiscardedSnippets(ntp_snippets_service); | |
| 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(args->empty()); | |
| 96 | |
| 97 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 98 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 99 Profile::FromWebUI(web_ui())); | |
| 100 | |
| 101 if (!observer_.IsObserving(ntp_snippets_service)) { | |
|
Marc Treib
2016/04/15 09:00:30
nit: no braces
Also maybe add a comment that this
jkrcal
2016/04/15 14:11:51
Acknowledged.
(the code changed, I think it is se
| |
| 102 observer_.Add(ntp_snippets_service); | |
| 103 } | |
| 104 ntp_snippets_service->FetchSnippets(); | |
| 105 | |
| 106 SendInitialData(ntp_snippets_service); | |
| 107 } | |
| 108 | |
| 109 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { | |
| 110 DCHECK_EQ(0u, args->GetSize()); | |
| 111 | |
| 112 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 113 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 114 Profile::FromWebUI(web_ui())); | |
| 115 | |
| 116 ntp_snippets_service->ClearSnippets(); | |
| 117 } | |
| 118 | |
| 119 void SnippetsInternalsMessageHandler::HandleClearDiscarded( | |
| 120 const base::ListValue* args) { | |
| 121 DCHECK_EQ(0u, args->GetSize()); | |
| 122 | |
| 123 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 124 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 125 Profile::FromWebUI(web_ui())); | |
| 126 | |
| 127 ntp_snippets_service->ClearDiscardedSnippets(); | |
| 128 SendDiscardedSnippets(ntp_snippets_service); | |
| 129 } | |
| 130 | |
| 131 void SnippetsInternalsMessageHandler::HandleDownload( | |
| 132 const base::ListValue* args) { | |
| 133 DCHECK_EQ(1u, args->GetSize()); | |
| 134 | |
| 135 std::string hosts_string; | |
| 136 args->GetString(0, &hosts_string); | |
| 137 | |
| 138 std::vector<std::string> hosts_vector = base::SplitString( | |
| 139 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 140 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); | |
| 141 | |
| 142 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 143 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 144 Profile::FromWebUI(web_ui())); | |
| 145 | |
| 146 ntp_snippets_service->FetchSnippetsFromHosts(hosts); | |
| 147 } | |
| 148 | |
| 149 void SnippetsInternalsMessageHandler::SendInitialData( | |
| 150 ntp_snippets::NTPSnippetsService* service){ | |
| 151 SendHosts(service); | |
| 152 | |
| 153 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | |
| 154 chrome::android::kNTPSnippetsFeature)); | |
| 155 | |
| 156 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 157 ntp_snippets::switches::kDontRestrict); | |
| 158 SendBoolean("switch-restrict-to-hosts", restricted); | |
| 159 const std::string help(restricted ? "cannot be empty" | |
| 160 : "unrestricted if empty"); | |
| 161 SendString("hosts-help", help); | |
| 162 } | |
| 163 | |
| 164 void SnippetsInternalsMessageHandler::SendSnippets( | |
| 165 ntp_snippets::NTPSnippetsService* service) { | |
| 166 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | |
| 167 | |
| 168 int index = 0; | |
| 169 for (auto& snippet : *service) | |
| 170 snippets_list->Append(PrepareSnippet(snippet, index++, false)); | |
| 171 | |
| 172 base::DictionaryValue result; | |
| 173 result.Set("list", std::move(snippets_list)); | |
| 174 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveSnippets", | |
| 175 result); | |
| 176 } | |
| 177 | |
| 178 void SnippetsInternalsMessageHandler::SendDiscardedSnippets( | |
| 179 ntp_snippets::NTPSnippetsService* service) { | |
| 180 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | |
| 181 | |
| 182 int index = 0; | |
| 183 for (auto& snippet : service->discarded_snippets()) | |
| 184 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | |
| 185 | |
| 186 base::DictionaryValue result; | |
| 187 result.Set("list", std::move(snippets_list)); | |
| 188 web_ui()->CallJavascriptFunction( | |
| 189 "chrome.snippets_internals.receiveDiscardedSnippets", result); | |
| 190 } | |
| 191 | |
| 192 void SnippetsInternalsMessageHandler::SendHosts( | |
| 193 ntp_snippets::NTPSnippetsService* service) { | |
| 194 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | |
| 195 | |
| 196 std::set<std::string> hosts = service->GetSuggestionsHosts(); | |
| 197 | |
| 198 for (const std::string& host : hosts) { | |
| 199 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | |
| 200 entry->SetString("url", host); | |
| 201 | |
| 202 hosts_list->Append(std::move(entry)); | |
| 203 } | |
| 204 | |
| 205 base::DictionaryValue result; | |
| 206 result.Set("list", std::move(hosts_list)); | |
| 207 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveHosts", | |
| 208 result); | |
| 209 } | |
| 210 | |
| 211 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | |
| 212 bool value) { | |
| 213 SendString(name, value ? "True" : "False"); | |
| 214 } | |
| 215 | |
| 216 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | |
| 217 const std::string& value) { | |
| 218 base::StringValue string_name(name); | |
| 219 base::StringValue string_value(value); | |
| 220 | |
| 221 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveProperty", | |
| 222 string_name, string_value); | |
| 223 } | |
| OLD | NEW |