Commit 43cb7378 authored by vitaliii's avatar vitaliii Committed by Commit Bot

[NTP::Push] Disable snippets internals button when not possible to push.

This CL deactivates "Push dummy suggestion in 10 seconds" button in
snippets internals if either
1) listener is nullptr (e.g. feature is disabled)
2) listener is not listening.

Previously pressing the button when (1) holds lead to a crash, (2) - nothing
being pushed without any signal.

Bug: 771537
Cq-Include-Trybots: master.tryserver.chromium.linux:closure_compilation
Change-Id: I788c9d16e277890db97dde2ed7cea166f32a96f7
Reviewed-on: https://chromium-review.googlesource.com/699999
Commit-Queue: vitaliii <vitaliii@chromium.org>
Reviewed-by: default avatarChris Pickel <sfiera@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506704}
parent 4e957c69
......@@ -170,6 +170,10 @@ cr.define('chrome.SnippetsInternals', function() {
lastRemoteSuggestionsBackgroundFetchTime);
}
function receiveWhetherSuggestionPushingPossible(possible) {
$('push-dummy-suggestion-10-seconds-button').disabled = !possible;
}
function downloadJson(json) {
// Redirect the browser to download data in |json| as a file "snippets.json"
// (Setting Content-Disposition: attachment via a data: URL is not possible;
......@@ -236,6 +240,8 @@ cr.define('chrome.SnippetsInternals', function() {
receiveRankerDebugData: receiveRankerDebugData,
receiveLastRemoteSuggestionsBackgroundFetchTime:
receiveLastRemoteSuggestionsBackgroundFetchTime,
receiveWhetherSuggestionPushingPossible:
receiveWhetherSuggestionPushingPossible,
receiveContextualSuggestions: receiveContextualSuggestions,
};
});
......
......@@ -172,6 +172,16 @@ std::string TimeToJSONTimeString(const base::Time time) {
exploded.millisecond);
}
ntp_snippets::BreakingNewsListener* GetBreakingNewsListener(
ntp_snippets::ContentSuggestionsService* service) {
DCHECK(service);
RemoteSuggestionsProvider* provider =
service->remote_suggestions_provider_for_debugging();
DCHECK(provider);
return static_cast<ntp_snippets::RemoteSuggestionsProviderImpl*>(provider)
->breaking_news_listener_for_debugging();
}
} // namespace
SnippetsInternalsMessageHandler::SnippetsInternalsMessageHandler(
......@@ -487,6 +497,7 @@ void SnippetsInternalsMessageHandler::SendAllContent() {
SendClassification();
SendRankerDebugData();
SendLastRemoteSuggestionsBackgroundFetchTime();
SendWhetherSuggestionPushingPossible();
if (remote_suggestions_provider_) {
const ntp_snippets::RemoteSuggestionsFetcher* fetcher =
......@@ -555,6 +566,15 @@ void SnippetsInternalsMessageHandler::
base::Value(base::TimeFormatShortDateAndTime(time)));
}
void SnippetsInternalsMessageHandler::SendWhetherSuggestionPushingPossible() {
ntp_snippets::BreakingNewsListener* listener =
GetBreakingNewsListener(content_suggestions_service_);
CallJavascriptFunction(
"chrome.SnippetsInternals."
"receiveWhetherSuggestionPushingPossible",
base::Value(listener != nullptr && listener->IsListening()));
}
void SnippetsInternalsMessageHandler::SendContentSuggestions() {
std::unique_ptr<base::ListValue> categories_list(new base::ListValue);
......@@ -657,13 +677,10 @@ void SnippetsInternalsMessageHandler::PushDummySuggestion() {
gcm::IncomingMessage message;
message.data["payload"] = json;
RemoteSuggestionsProvider* provider =
content_suggestions_service_->remote_suggestions_provider_for_debugging();
DCHECK(provider);
ntp_snippets::BreakingNewsListener* listener =
static_cast<ntp_snippets::RemoteSuggestionsProviderImpl*>(provider)
->breaking_news_listener_for_debugging();
GetBreakingNewsListener(content_suggestions_service_);
DCHECK(listener);
DCHECK(listener->IsListening());
static_cast<ntp_snippets::BreakingNewsGCMAppHandler*>(listener)->OnMessage(
"com.google.breakingnews.gcm", message);
}
......
......@@ -81,6 +81,7 @@ class SnippetsInternalsMessageHandler
void SendClassification();
void SendRankerDebugData();
void SendLastRemoteSuggestionsBackgroundFetchTime();
void SendWhetherSuggestionPushingPossible();
void SendContentSuggestions();
void SendBoolean(const std::string& name, bool value);
void SendString(const std::string& name, const std::string& value);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment