Chromium Code Reviews| 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(ntp_snippets_service_->last_json())); |
| 79 } | 78 } |
| 80 | 79 |
| 81 void SnippetsInternalsMessageHandler::RegisterMessages() { | 80 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 82 // additional initialization (web_ui() does not work from the constructor) | 81 // additional initialization (web_ui() does not work from the constructor) |
| 83 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> | 82 ntp_snippets_service_ = NTPSnippetsServiceFactory::GetInstance()-> |
| 84 GetForProfile(Profile::FromWebUI(web_ui())); | 83 GetForProfile(Profile::FromWebUI(web_ui())); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 | 156 |
| 158 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); | 157 ntp_snippets_service_->FetchSnippetsFromHosts(hosts); |
| 159 } | 158 } |
| 160 | 159 |
| 161 void SnippetsInternalsMessageHandler::SendInitialData() { | 160 void SnippetsInternalsMessageHandler::SendInitialData() { |
| 162 SendHosts(); | 161 SendHosts(); |
| 163 | 162 |
| 164 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( | 163 SendBoolean("flag-snippets", base::FeatureList::IsEnabled( |
| 165 chrome::android::kNTPSnippetsFeature)); | 164 chrome::android::kNTPSnippetsFeature)); |
| 166 | 165 |
| 167 bool restricted = !base::CommandLine::ForCurrentProcess()->HasSwitch( | 166 bool host_restricted = ntp_snippets_service_->UseHostRestriction(); |
| 168 ntp_snippets::switches::kDontRestrict); | 167 SendBoolean("switch-restrict-to-hosts", host_restricted); |
| 169 SendBoolean("switch-restrict-to-hosts", restricted); | 168 if (!host_restricted) { |
| 170 const std::string help(restricted ? "(specify at least one host)" : | 169 web_ui()->CallJavascriptFunction( |
| 171 "(unrestricted if no host is given)"); | 170 "chrome.SnippetsInternals.hideHostRestricts"); |
|
Bernhard Bauer
2016/05/18 10:08:03
If you add a dedicated method here anyway, you cou
jkrcal
2016/05/19 07:30:28
Done.
| |
| 172 SendString("hosts-help", help); | 171 } |
| 172 | |
| 173 ntp_snippets::NTPSnippetsFetcher::Personalization personal = | |
| 174 ntp_snippets_service_->personalization(); | |
| 175 if (personal == ntp_snippets::NTPSnippetsFetcher::Personalization::kPersonal) | |
| 176 SendString("switch-personalized", "Only personalized"); | |
|
Bernhard Bauer
2016/05/18 10:08:03
Use a switch statement so the compiler can check y
jkrcal
2016/05/19 07:30:28
Done.
| |
| 177 else if (personal == ntp_snippets::NTPSnippetsFetcher::Personalization::kBoth) | |
| 178 SendString("switch-personalized", "Both personalized and non-personalized"); | |
| 179 else | |
| 180 SendString("switch-personalized", "Only non-personalized"); | |
| 173 | 181 |
| 174 SendSnippets(); | 182 SendSnippets(); |
| 175 SendDiscardedSnippets(); | 183 SendDiscardedSnippets(); |
| 176 } | 184 } |
| 177 | 185 |
| 178 void SnippetsInternalsMessageHandler::SendSnippets() { | 186 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 179 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 187 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 180 | 188 |
| 181 int index = 0; | 189 int index = 0; |
| 182 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : | 190 for (const std::unique_ptr<ntp_snippets::NTPSnippet>& snippet : |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 } | 244 } |
| 237 | 245 |
| 238 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 246 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 239 const std::string& value) { | 247 const std::string& value) { |
| 240 base::StringValue string_name(name); | 248 base::StringValue string_name(name); |
| 241 base::StringValue string_value(value); | 249 base::StringValue string_value(value); |
| 242 | 250 |
| 243 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", | 251 web_ui()->CallJavascriptFunction("chrome.SnippetsInternals.receiveProperty", |
| 244 string_name, string_value); | 252 string_name, string_value); |
| 245 } | 253 } |
| OLD | NEW |