| 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 : observer_(this), |
| 59 dom_loaded_(false), |
| 60 ntp_snippets_service_(nullptr) {} |
| 61 |
| 62 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 63 |
| 64 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
| 65 |
| 66 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
| 67 if (!dom_loaded_) return; |
| 68 |
| 69 SendSnippets(); |
| 70 SendDiscardedSnippets(); |
| 71 } |
| 72 |
| 73 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 74 // additional initialization (web_ui() does not work from the constructor) |
| 75 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| 76 GetForProfile(Profile::FromWebUI(web_ui())); |
| 77 observer_.Add(ntp_snippets_service_); |
| 78 |
| 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_EQ(0u, args->GetSize()); |
| 101 |
| 102 dom_loaded_ = true; |
| 103 |
| 104 SendInitialData(); |
| 105 } |
| 106 |
| 107 void SnippetsInternalsMessageHandler::HandleClear(const base::ListValue* args) { |
| 108 DCHECK_EQ(0u, args->GetSize()); |
| 109 |
| 110 ntp_snippets_service_->ClearSnippets(); |
| 111 } |
| 112 |
| 113 void SnippetsInternalsMessageHandler::HandleClearDiscarded( |
| 114 const base::ListValue* args) { |
| 115 DCHECK_EQ(0u, args->GetSize()); |
| 116 |
| 117 ntp_snippets_service_->ClearDiscardedSnippets(); |
| 118 SendDiscardedSnippets(); |
| 119 } |
| 120 |
| 121 void SnippetsInternalsMessageHandler::HandleDownload( |
| 122 const base::ListValue* args) { |
| 123 DCHECK_EQ(1u, args->GetSize()); |
| 124 |
| 125 std::string hosts_string; |
| 126 args->GetString(0, &hosts_string); |
| 127 |
| 128 std::vector<std::string> hosts_vector = base::SplitString( |
| 129 hosts_string, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 130 std::set<std::string> hosts(hosts_vector.begin(), hosts_vector.end()); |
| 131 |
| 132 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 133 } |
| 134 |
| 135 void SnippetsInternalsMessageHandler::SendInitialData() { |
| 136 SendHosts(); |
| 137 |
| 138 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 139 chrome::android::kNTPSnippetsFeature)); |
| 140 |
| 141 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 142 ntp_snippets::switches::kDontRestrict); |
| 143 SendBoolean("switch-restrict-to-hosts", restricted); |
| 144 const std::string help(restricted ? "(specify at least one host)" : |
| 145 "(unrestricted if no host is given)"); |
| 146 SendString("hosts-help", help); |
| 147 |
| 148 SendSnippets(); |
| 149 SendDiscardedSnippets(); |
| 150 } |
| 151 |
| 152 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 153 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 154 |
| 155 int index = 0; |
| 156 for (auto& snippet : *ntp_snippets_service_) |
| 157 snippets_list->Append(PrepareSnippet(snippet, index++, false)); |
| 158 |
| 159 base::DictionaryValue result; |
| 160 result.Set("list", std::move(snippets_list)); |
| 161 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets", |
| 162 result); |
| 163 } |
| 164 |
| 165 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { |
| 166 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 167 |
| 168 int index = 0; |
| 169 for (auto& snippet : ntp_snippets_service_->discarded_snippets()) |
| 170 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); |
| 171 |
| 172 base::DictionaryValue result; |
| 173 result.Set("list", std::move(snippets_list)); |
| 174 web_ui()->CallJavascriptFunction( |
| 175 "chrome.SnippetsInternals.receiveDiscardedSnippets", result); |
| 176 } |
| 177 |
| 178 void SnippetsInternalsMessageHandler::SendHosts() { |
| 179 std::unique_ptr<base::ListValue> hosts_list(new base::ListValue); |
| 180 |
| 181 std::set<std::string> hosts = ntp_snippets_service_->GetSuggestionsHosts(); |
| 182 |
| 183 for (const std::string& host : hosts) { |
| 184 std::unique_ptr<base::DictionaryValue> entry(new base::DictionaryValue); |
| 185 entry->SetString("url", host); |
| 186 |
| 187 hosts_list->Append(std::move(entry)); |
| 188 } |
| 189 |
| 190 base::DictionaryValue result; |
| 191 result.Set("list", std::move(hosts_list)); |
| 192 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveHosts", |
| 193 result); |
| 194 } |
| 195 |
| 196 void SnippetsInternalsMessageHandler::SendBoolean(const std::string& name, |
| 197 bool value) { |
| 198 SendString(name, value ? "True" : "False"); |
| 199 } |
| 200 |
| 201 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 202 const std::string& value) { |
| 203 base::StringValue string_name(name); |
| 204 base::StringValue string_value(value); |
| 205 |
| 206 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", |
| 207 string_name, string_value); |
| 208 } |
| OLD | NEW |