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() {} | |
| 58 | |
| 59 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() { | |
| 60 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 61 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 62 Profile::FromWebUI(web_ui())); | |
| 63 | |
| 64 ntp_snippets_service->RemoveObserver(this); | |
|
Marc Treib
2016/04/14 15:43:44
You can use base/scoped_observer.h for this kind o
jkrcal
2016/04/14 16:41:01
Done.
| |
| 65 } | |
| 66 | |
| 67 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | |
| 68 | |
| 69 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | |
| 70 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 71 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 72 Profile::FromWebUI(web_ui())); | |
| 73 | |
| 74 SendSnippets(ntp_snippets_service); | |
| 75 SendDiscardedSnippets(ntp_snippets_service); | |
| 76 } | |
| 77 | |
| 78 void SnippetsInternalsMessageHandler::RegisterMessages() { | |
| 79 web_ui()->RegisterMessageCallback( | |
| 80 "loaded", | |
| 81 base::Bind(&SnippetsInternalsMessageHandler::HandleLoaded, | |
| 82 base::Unretained(this))); | |
| 83 | |
| 84 web_ui()->RegisterMessageCallback( | |
| 85 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, | |
| 86 base::Unretained(this))); | |
| 87 | |
| 88 web_ui()->RegisterMessageCallback( | |
| 89 "download", base::Bind(&SnippetsInternalsMessageHandler::HandleDownload, | |
| 90 base::Unretained(this))); | |
| 91 | |
| 92 web_ui()->RegisterMessageCallback( | |
| 93 "clearDiscarded", | |
| 94 base::Bind(&SnippetsInternalsMessageHandler::HandleClearDiscarded, | |
| 95 base::Unretained(this))); | |
| 96 } | |
| 97 | |
| 98 void SnippetsInternalsMessageHandler::HandleLoaded( | |
| 99 const base::ListValue* args) { | |
| 100 DCHECK(args->empty()); | |
|
Bernhard Bauer
2016/04/14 16:50:24
Try to be consistent about whether to use DCHECK(.
jkrcal
2016/04/15 14:11:50
Done.
| |
| 101 | |
| 102 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 103 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 104 Profile::FromWebUI(web_ui())); | |
| 105 | |
| 106 ntp_snippets_service->AddObserver(this); | |
| 107 ntp_snippets_service->FetchSnippets(); | |
| 108 | |
| 109 SendInitialData(ntp_snippets_service); | |
| 110 } | |
| 111 | |
| 112 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { | |
| 113 DCHECK_EQ(0u, args->GetSize()); | |
| 114 | |
| 115 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 116 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 117 Profile::FromWebUI(web_ui())); | |
| 118 | |
| 119 ntp_snippets_service->ClearSnippets(); | |
| 120 } | |
| 121 | |
| 122 void SnippetsInternalsMessageHandler::HandleClearDiscarded( | |
| 123 const base::ListValue* args) { | |
| 124 DCHECK_EQ(0u, args->GetSize()); | |
| 125 | |
| 126 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 127 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 128 Profile::FromWebUI(web_ui())); | |
| 129 | |
| 130 ntp_snippets_service->ClearDiscardedSnippets(); | |
| 131 SendDiscardedSnippets(ntp_snippets_service); | |
| 132 } | |
| 133 | |
| 134 void SnippetsInternalsMessageHandler::HandleDownload( | |
| 135 const base::ListValue* args) { | |
| 136 DCHECK_EQ(1u, args->GetSize()); | |
| 137 | |
| 138 std::string hosts_string; | |
| 139 args->GetString(0, &hosts_string); | |
| 140 | |
| 141 std::vector<std::string> hosts_vector = base::SplitString( | |
| 142 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 143 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); | |
| 144 | |
| 145 ntp_snippets::NTPSnippetsService* ntp_snippets_service = | |
| 146 NTPSnippetsServiceFactory::GetInstance()->GetForProfile( | |
| 147 Profile::FromWebUI(web_ui())); | |
| 148 | |
| 149 ntp_snippets_service->FetchSnippetsFromHosts(hosts); | |
| 150 } | |
| 151 | |
| 152 void SnippetsInternalsMessageHandler::SendInitialData( | |
| 153 ntp_snippets::NTPSnippetsService* service){ | |
| 154 SendHosts(service); | |
| 155 | |
| 156 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | |
| 157 chrome::android::kNTPSnippetsFeature)); | |
| 158 | |
| 159 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 160 ntp_snippets::switches::kDontRestrict); | |
| 161 SendBoolean("switch-restrict-to-hosts", restricted); | |
| 162 const std::string help(restricted ? "cannot be empty" | |
| 163 : "unrestricted if empty"); | |
| 164 SendString("hosts-help", help); | |
| 165 } | |
| 166 | |
| 167 void SnippetsInternalsMessageHandler::SendSnippets( | |
| 168 ntp_snippets::NTPSnippetsService* service) { | |
| 169 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | |
| 170 | |
| 171 int index = 0; | |
| 172 for (auto& snippet : *service) | |
| 173 snippets_list->Append(PrepareSnippet(snippet, index++, false)); | |
| 174 | |
| 175 base::DictionaryValue result; | |
| 176 result.Set("list", std::move(snippets_list)); | |
| 177 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveSnippets", | |
| 178 result); | |
| 179 } | |
| 180 | |
| 181 void SnippetsInternalsMessageHandler::SendDiscardedSnippets( | |
| 182 ntp_snippets::NTPSnippetsService* service) { | |
| 183 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | |
| 184 | |
| 185 int index = 0; | |
| 186 for (auto& snippet : service->discarded_snippets()) | |
| 187 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | |
| 188 | |
| 189 base::DictionaryValue result; | |
| 190 result.Set("list", std::move(snippets_list)); | |
| 191 web_ui()->CallJavascriptFunction( | |
| 192 "chrome.snippets_internals.receiveDiscardedSnippets", result); | |
| 193 } | |
| 194 | |
| 195 void SnippetsInternalsMessageHandler::SendHosts( | |
| 196 ntp_snippets::NTPSnippetsService* service) { | |
| 197 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); | |
| 198 | |
| 199 std::set<std::string> hosts = service->GetSuggestionsHosts(); | |
| 200 | |
| 201 for (const std::string& host : hosts) { | |
| 202 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); | |
| 203 entry->SetString("url", host); | |
| 204 | |
| 205 hosts_list->Append(std::move(entry)); | |
| 206 } | |
| 207 | |
| 208 base::DictionaryValue result; | |
| 209 result.Set("list", std::move(hosts_list)); | |
| 210 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveHosts", | |
| 211 result); | |
| 212 } | |
| 213 | |
| 214 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, | |
| 215 bool value) { | |
| 216 SendString(name, value ? "True" : "False"); | |
| 217 } | |
| 218 | |
| 219 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | |
| 220 const std::string& value) { | |
| 221 base::StringValue string_name(name); | |
| 222 base::StringValue string_value(value); | |
| 223 | |
| 224 web_ui()->CallJavascriptFunction("chrome.snippets_internals.receiveProperty", | |
| 225 string_name, string_value); | |
| 226 } | |
| OLD | NEW |