| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/i18n/time_formatting.h" | 15 #include "base/i18n/time_formatting.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/android/chrome_feature_list.h" | 21 #include "chrome/browser/android/chrome_feature_list.h" |
| 22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" | 22 #include "chrome/browser/ntp_snippets/content_suggestions_service_factory.h" |
| 23 #include "chrome/browser/ntp_snippets/ntp_snippets_service_factory.h" | |
| 24 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 25 #include "components/ntp_snippets/ntp_snippet.h" | 24 #include "components/ntp_snippets/ntp_snippet.h" |
| 26 #include "components/ntp_snippets/switches.h" | 25 #include "components/ntp_snippets/switches.h" |
| 27 #include "content/public/browser/web_ui.h" | 26 #include "content/public/browser/web_ui.h" |
| 28 | 27 |
| 29 using ntp_snippets::ContentSuggestion; | 28 using ntp_snippets::ContentSuggestion; |
| 30 using ntp_snippets::Category; | 29 using ntp_snippets::Category; |
| 31 using ntp_snippets::CategoryStatus; | 30 using ntp_snippets::CategoryStatus; |
| 32 using ntp_snippets::KnownCategories; | 31 using ntp_snippets::KnownCategories; |
| 33 | 32 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 return "SIGNED_OUT"; | 103 return "SIGNED_OUT"; |
| 105 case CategoryStatus::LOADING_ERROR: | 104 case CategoryStatus::LOADING_ERROR: |
| 106 return "LOADING_ERROR"; | 105 return "LOADING_ERROR"; |
| 107 } | 106 } |
| 108 return std::string(); | 107 return std::string(); |
| 109 } | 108 } |
| 110 | 109 |
| 111 } // namespace | 110 } // namespace |
| 112 | 111 |
| 113 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() | 112 SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler() |
| 114 : ntp_snippets_service_observer_(this), | 113 : content_suggestions_service_observer_(this), |
| 115 content_suggestions_service_observer_(this), | |
| 116 dom_loaded_(false), | 114 dom_loaded_(false), |
| 117 ntp_snippets_service_(nullptr), | 115 ntp_snippets_service_(nullptr), |
| 118 content_suggestions_service_(nullptr) {} | 116 content_suggestions_service_(nullptr) {} |
| 119 | 117 |
| 120 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} | 118 SnippetsInternalsMessageHandler::~SnippetsInternalsMessageHandler() {} |
| 121 | 119 |
| 122 void SnippetsInternalsMessageHandler::NTPSnippetsServiceShutdown() {} | |
| 123 | |
| 124 void SnippetsInternalsMessageHandler::NTPSnippetsServiceLoaded() { | |
| 125 if (!dom_loaded_) return; | |
| 126 | |
| 127 SendSnippets(); | |
| 128 | |
| 129 web_ui()->CallJavascriptFunctionUnsafe( | |
| 130 "chrome.SnippetsInternals.receiveJson", | |
| 131 base::StringValue( | |
| 132 ntp_snippets_service_->snippets_fetcher()->last_json())); | |
| 133 } | |
| 134 | |
| 135 void SnippetsInternalsMessageHandler::NTPSnippetsServiceDisabledReasonChanged( | |
| 136 ntp_snippets::DisabledReason disabled_reason) {} | |
| 137 | |
| 138 void SnippetsInternalsMessageHandler::OnNewSuggestions() { | 120 void SnippetsInternalsMessageHandler::OnNewSuggestions() { |
| 139 if (!dom_loaded_) | 121 if (!dom_loaded_) |
| 140 return; | 122 return; |
| 141 SendContentSuggestions(); | 123 SendContentSuggestions(); |
| 142 } | 124 } |
| 143 | 125 |
| 144 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( | 126 void SnippetsInternalsMessageHandler::OnCategoryStatusChanged( |
| 145 Category category, | 127 Category category, |
| 146 CategoryStatus new_status) { | 128 CategoryStatus new_status) { |
| 147 if (!dom_loaded_) | 129 if (!dom_loaded_) |
| 148 return; | 130 return; |
| 149 SendContentSuggestions(); | 131 SendContentSuggestions(); |
| 150 } | 132 } |
| 151 | 133 |
| 152 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {} | 134 void SnippetsInternalsMessageHandler::ContentSuggestionsServiceShutdown() {} |
| 153 | 135 |
| 154 void SnippetsInternalsMessageHandler::RegisterMessages() { | 136 void SnippetsInternalsMessageHandler::RegisterMessages() { |
| 155 // additional initialization (web_ui() does not work from the constructor) | 137 // additional initialization (web_ui() does not work from the constructor) |
| 156 Profile* profile = Profile::FromWebUI(web_ui()); | 138 Profile* profile = Profile::FromWebUI(web_ui()); |
| 157 | 139 |
| 158 ntp_snippets_service_ = | |
| 159 NTPSnippetsServiceFactory::GetInstance()->GetForProfile(profile); | |
| 160 ntp_snippets_service_observer_.Add(ntp_snippets_service_); | |
| 161 | |
| 162 content_suggestions_service_ = | 140 content_suggestions_service_ = |
| 163 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); | 141 ContentSuggestionsServiceFactory::GetInstance()->GetForProfile(profile); |
| 164 content_suggestions_service_observer_.Add(content_suggestions_service_); | 142 content_suggestions_service_observer_.Add(content_suggestions_service_); |
| 165 | 143 |
| 144 ntp_snippets_service_ = content_suggestions_service_->ntp_snippets_service(); |
| 145 |
| 166 web_ui()->RegisterMessageCallback( | 146 web_ui()->RegisterMessageCallback( |
| 167 "refreshContent", | 147 "refreshContent", |
| 168 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, | 148 base::Bind(&SnippetsInternalsMessageHandler::HandleRefreshContent, |
| 169 base::Unretained(this))); | 149 base::Unretained(this))); |
| 170 | 150 |
| 171 web_ui()->RegisterMessageCallback( | 151 web_ui()->RegisterMessageCallback( |
| 172 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, | 152 "clear", base::Bind(&SnippetsInternalsMessageHandler::HandleClear, |
| 173 base::Unretained(this))); | 153 base::Unretained(this))); |
| 174 | 154 |
| 175 web_ui()->RegisterMessageCallback( | 155 web_ui()->RegisterMessageCallback( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 SendString("switch-personalized", | 249 SendString("switch-personalized", |
| 270 "Both personalized and non-personalized"); | 250 "Both personalized and non-personalized"); |
| 271 break; | 251 break; |
| 272 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: | 252 case ntp_snippets::NTPSnippetsFetcher::Personalization::kNonPersonal: |
| 273 SendString("switch-personalized", "Only non-personalized"); | 253 SendString("switch-personalized", "Only non-personalized"); |
| 274 break; | 254 break; |
| 275 } | 255 } |
| 276 | 256 |
| 277 SendString("switch-fetch-url", | 257 SendString("switch-fetch-url", |
| 278 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); | 258 ntp_snippets_service_->snippets_fetcher()->fetch_url().spec()); |
| 259 web_ui()->CallJavascriptFunctionUnsafe( |
| 260 "chrome.SnippetsInternals.receiveJson", |
| 261 base::StringValue( |
| 262 ntp_snippets_service_->snippets_fetcher()->last_json())); |
| 279 | 263 |
| 280 SendSnippets(); | 264 SendSnippets(); |
| 281 SendDismissedSnippets(); | 265 SendDismissedSnippets(); |
| 282 SendContentSuggestions(); | 266 SendContentSuggestions(); |
| 283 } | 267 } |
| 284 | 268 |
| 285 void SnippetsInternalsMessageHandler::SendSnippets() { | 269 void SnippetsInternalsMessageHandler::SendSnippets() { |
| 286 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); | 270 std::unique_ptr<base::ListValue> snippets_list(new base::ListValue); |
| 287 | 271 |
| 288 int index = 0; | 272 int index = 0; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 } | 351 } |
| 368 | 352 |
| 369 void SnippetsInternalsMessageHandler::SendString(const std::string& name, | 353 void SnippetsInternalsMessageHandler::SendString(const std::string& name, |
| 370 const std::string& value) { | 354 const std::string& value) { |
| 371 base::StringValue string_name(name); | 355 base::StringValue string_name(name); |
| 372 base::StringValue string_value(value); | 356 base::StringValue string_value(value); |
| 373 | 357 |
| 374 web_ui()->CallJavascriptFunctionUnsafe( | 358 web_ui()->CallJavascriptFunctionUnsafe( |
| 375 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); | 359 "chrome.SnippetsInternals.receiveProperty", string_name, string_value); |
| 376 } | 360 } |
| OLD | NEW |