| 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 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 ntp_snippets_service_(nullptr) {} | 64 ntp_snippets_service_(nullptr) {} |
| 65 | 65 |
| 66 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 66 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 67 | 67 |
| 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | 68 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} |
| 69 | 69 |
| 70 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | 70 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { |
| 71 if (!dom_loaded_) return; | 71 if (!dom_loaded_) return; |
| 72 | 72 |
| 73 SendSnippets(); | 73 SendSnippets(); |
| 74 SendDiscardedSnippets(); | |
| 75 | 74 |
| 76 web_ui()->CallJavascriptFunction( | 75 web_ui()->CallJavascriptFunction( |
| 77 "chrome.SnippetsInternals.receiveJson", | 76 "chrome.SnippetsInternals.receiveJson", |
| 78 base::StringValue(ntp_snippets_service_->last_json())); | 77 base::StringValue( |
| 78 ntp_snippets_service_->snippets_fetcher()->last_json())); |
| 79 } | 79 } |
| 80 | 80 |
| 81 void SnippetsInternalsMessageHandler::NTPSnippetsServiceCleared() {} | 81 void SnippetsInternalsMessageHandler::NTPSnippetsServiceCleared() {} |
| 82 | 82 |
| 83 void SnippetsInternalsMessageHandler::RegisterMessages() { | 83 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 84 // additional initialization (web_ui() does not work from the constructor) | 84 // additional initialization (web_ui() does not work from the constructor) |
| 85 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 85 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| 86 GetForProfile(Profile::FromWebUI(web_ui())); | 86 GetForProfile(Profile::FromWebUI(web_ui())); |
| 87 observer_.Add(ntp_snippets_service_); | 87 observer_.Add(ntp_snippets_service_); |
| 88 | 88 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 160 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 161 } | 161 } |
| 162 | 162 |
| 163 void SnippetsInternalsMessageHandler::SendInitialData() { | 163 void SnippetsInternalsMessageHandler::SendInitialData() { |
| 164 SendHosts(); | 164 SendHosts(); |
| 165 | 165 |
| 166 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 166 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 167 chrome::android::kNTPSnippetsFeature)); | 167 chrome::android::kNTPSnippetsFeature)); |
| 168 | 168 |
| 169 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch( | 169 web_ui()->CallJavascriptFunction( |
| 170 ntp_snippets::switches::kDontRestrict); | 170 "chrome.SnippetsInternals.setHostRestricted", |
| 171 SendBoolean("switch-restrict-to-hosts", restricted); | 171 base::FundamentalValue( |
| 172 const std::string help(restricted ? "(specify at least one host)" : | 172 ntp_snippets_service_->snippets_fetcher()->UsesHostRestrictions())); |
| 173 "(unrestricted if no host is given)"); | 173 |
| 174 SendString("hosts-help", help); | 174 switch (ntp_snippets_service_->snippets_fetcher()->personalization()) { |
| 175 case ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal: |
| 176 SendString("switch-personalized", "Only personalized"); |
| 177 break; |
| 178 case ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth: |
| 179 SendString("switch-personalized", |
| 180 "Both personalized and non-personalized"); |
| 181 break; |
| 182 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 183 SendString("switch-personalized", "Only non-personalized"); |
| 184 break; |
| 185 } |
| 175 | 186 |
| 176 SendSnippets(); | 187 SendSnippets(); |
| 177 SendDiscardedSnippets(); | 188 SendDiscardedSnippets(); |
| 178 } | 189 } |
| 179 | 190 |
| 180 void SnippetsInternalsMessageHandler::SendSnippets() { | 191 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 181 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 192 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 182 | 193 |
| 183 int index = 0; | 194 int index = 0; |
| 184 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 195 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| 185 ntp_snippets_service_->snippets()) | 196 ntp_snippets_service_->snippets()) |
| 186 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); | 197 snippets_list->Append(PrepareSnippet(*snippet, index++, false)); |
| 187 | 198 |
| 188 base::DictionaryValue result; | 199 base::DictionaryValue result; |
| 189 result.Set("list", std::move(snippets_list)); | 200 result.Set("list", std::move(snippets_list)); |
| 190 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets", | 201 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveSnippets", |
| 191 result); | 202 result); |
| 192 | 203 |
| 193 const std::string& status = ntp_snippets_service_->last_status(); | 204 const std::string& status = |
| 205 ntp_snippets_service_->snippets_fetcher()->last_status(); |
| 194 if (!status.empty()) | 206 if (!status.empty()) |
| 195 SendString("hosts-status", "Finished: " + status); | 207 SendString("hosts-status", "Finished: " + status); |
| 196 } | 208 } |
| 197 | 209 |
| 198 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { | 210 void SnippetsInternalsMessageHandler::SendDiscardedSnippets() { |
| 199 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 211 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 200 | 212 |
| 201 int index = 0; | 213 int index = 0; |
| 202 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) | 214 for (const auto& snippet : ntp_snippets_service_->discarded_snippets()) |
| 203 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); | 215 snippets_list->Append(PrepareSnippet(*snippet, index++, true)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 } | 250 } |
| 239 | 251 |
| 240 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 252 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 241 const std::string& value) { | 253 const std::string& value) { |
| 242 base::StringValue string_name(name); | 254 base::StringValue string_name(name); |
| 243 base::StringValue string_value(value); | 255 base::StringValue string_value(value); |
| 244 | 256 |
| 245 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 257 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", |
| 246 string_name, string_value); | 258 string_name, string_value); |
| 247 } | 259 } |
| OLD | NEW |