Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(459)

Side by Side Diff: chrome/browser/android/ntp/content_suggestions_notifier_service.cc

Issue 2616893003: Hide notifications after their deadline (Closed)
Patch Set: import annotation Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/android/ntp/content_suggestions_notification_helper.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/android/ntp/content_suggestions_notifier_service.h" 5 #include "chrome/browser/android/ntp/content_suggestions_notifier_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/android/application_status_listener.h" 9 #include "base/android/application_status_listener.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 weak_ptr_factory_(this) {} 74 weak_ptr_factory_(this) {}
75 75
76 void OnNewSuggestions(Category category) override { 76 void OnNewSuggestions(Category category) override {
77 if (!ShouldNotifyInState(app_status_listener_.GetState())) { 77 if (!ShouldNotifyInState(app_status_listener_.GetState())) {
78 return; 78 return;
79 } 79 }
80 const ContentSuggestion* suggestion = GetSuggestionToNotifyAbout(category); 80 const ContentSuggestion* suggestion = GetSuggestionToNotifyAbout(category);
81 if (!suggestion) { 81 if (!suggestion) {
82 return; 82 return;
83 } 83 }
84 base::Time timeout_at = suggestion->notification_extra()
85 ? suggestion->notification_extra()->deadline
86 : base::Time::Max();
84 service_->FetchSuggestionImage( 87 service_->FetchSuggestionImage(
85 suggestion->id(), 88 suggestion->id(),
86 base::Bind(&NotifyingObserver::ImageFetched, 89 base::Bind(&NotifyingObserver::ImageFetched,
87 weak_ptr_factory_.GetWeakPtr(), suggestion->id(), 90 weak_ptr_factory_.GetWeakPtr(), suggestion->id(),
88 suggestion->url(), suggestion->title(), 91 suggestion->url(), suggestion->title(),
89 suggestion->publisher_name())); 92 suggestion->publisher_name(), timeout_at));
90 } 93 }
91 94
92 void OnCategoryStatusChanged(Category category, 95 void OnCategoryStatusChanged(Category category,
93 CategoryStatus new_status) override { 96 CategoryStatus new_status) override {
94 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) { 97 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) {
95 return; 98 return;
96 } 99 }
97 switch (new_status) { 100 switch (new_status) {
98 case CategoryStatus::AVAILABLE: 101 case CategoryStatus::AVAILABLE:
99 case CategoryStatus::AVAILABLE_LOADING: 102 case CategoryStatus::AVAILABLE_LOADING:
100 break; // nothing to do 103 break; // nothing to do
101 case CategoryStatus::INITIALIZING: 104 case CategoryStatus::INITIALIZING:
102 case CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED: 105 case CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED:
103 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED: 106 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
104 case CategoryStatus::LOADING_ERROR: 107 case CategoryStatus::LOADING_ERROR:
105 case CategoryStatus::NOT_PROVIDED: 108 case CategoryStatus::NOT_PROVIDED:
106 case CategoryStatus::SIGNED_OUT: 109 case CategoryStatus::SIGNED_OUT:
107 ContentSuggestionsNotificationHelper::HideNotification(); 110 ContentSuggestionsNotificationHelper::HideAllNotifications();
108 break; 111 break;
109 } 112 }
110 } 113 }
111 114
112 void OnSuggestionInvalidated( 115 void OnSuggestionInvalidated(
113 const ContentSuggestion::ID& suggestion_id) override { 116 const ContentSuggestion::ID& suggestion_id) override {
114 if (suggestion_id.category().IsKnownCategory(KnownCategories::ARTICLES) && 117 if (suggestion_id.category().IsKnownCategory(KnownCategories::ARTICLES) &&
115 (suggestion_id.id_within_category() == 118 (suggestion_id.id_within_category() ==
116 prefs_->GetString(kNotificationIDWithinCategory))) { 119 prefs_->GetString(kNotificationIDWithinCategory))) {
117 ContentSuggestionsNotificationHelper::HideNotification(); 120 ContentSuggestionsNotificationHelper::HideAllNotifications();
118 } 121 }
119 } 122 }
120 123
121 void OnFullRefreshRequired() override { 124 void OnFullRefreshRequired() override {
122 ContentSuggestionsNotificationHelper::HideNotification(); 125 ContentSuggestionsNotificationHelper::HideAllNotifications();
123 } 126 }
124 127
125 void ContentSuggestionsServiceShutdown() override { 128 void ContentSuggestionsServiceShutdown() override {
126 ContentSuggestionsNotificationHelper::HideNotification(); 129 ContentSuggestionsNotificationHelper::HideAllNotifications();
127 } 130 }
128 131
129 private: 132 private:
130 const ContentSuggestion* GetSuggestionToNotifyAbout(Category category) { 133 const ContentSuggestion* GetSuggestionToNotifyAbout(Category category) {
131 const auto& suggestions = service_->GetSuggestionsForCategory(category); 134 const auto& suggestions = service_->GetSuggestionsForCategory(category);
132 // TODO(sfiera): replace with AlwaysNotifyAboutContentSuggestions(). 135 // TODO(sfiera): replace with AlwaysNotifyAboutContentSuggestions().
133 if (variations::GetVariationParamByFeatureAsBool( 136 if (variations::GetVariationParamByFeatureAsBool(
134 kContentSuggestionsNotificationsFeature, 137 kContentSuggestionsNotificationsFeature,
135 kContentSuggestionsNotificationsAlwaysNotifyParam, false)) { 138 kContentSuggestionsNotificationsAlwaysNotifyParam, false)) {
136 if (category.IsKnownCategory(KnownCategories::ARTICLES) && 139 if (category.IsKnownCategory(KnownCategories::ARTICLES) &&
137 !suggestions.empty()) { 140 !suggestions.empty()) {
138 return &suggestions[0]; 141 return &suggestions[0];
139 } 142 }
140 return nullptr; 143 return nullptr;
141 } 144 }
142 145
143 for (const ContentSuggestion& suggestion : suggestions) { 146 for (const ContentSuggestion& suggestion : suggestions) {
144 if (suggestion.notification_extra()) { 147 if (suggestion.notification_extra()) {
145 return &suggestion; 148 return &suggestion;
146 } 149 }
147 } 150 }
148 return nullptr; 151 return nullptr;
149 } 152 }
150 153
151 void AppStatusChanged(base::android::ApplicationState state) { 154 void AppStatusChanged(base::android::ApplicationState state) {
152 if (!ShouldNotifyInState(state)) { 155 if (!ShouldNotifyInState(state)) {
153 ContentSuggestionsNotificationHelper::HideNotification(); 156 ContentSuggestionsNotificationHelper::HideAllNotifications();
154 } 157 }
155 } 158 }
156 159
157 void ImageFetched(const ContentSuggestion::ID& id, 160 void ImageFetched(const ContentSuggestion::ID& id,
158 const GURL& url, 161 const GURL& url,
159 const base::string16& title, 162 const base::string16& title,
160 const base::string16& publisher, 163 const base::string16& publisher,
164 base::Time timeout_at,
161 const gfx::Image& image) { 165 const gfx::Image& image) {
162 if (!ShouldNotifyInState(app_status_listener_.GetState())) { 166 if (!ShouldNotifyInState(app_status_listener_.GetState())) {
163 return; // Became foreground while we were fetching the image; forget it. 167 return; // Became foreground while we were fetching the image; forget it.
164 } 168 }
165 // check if suggestion is still valid. 169 // check if suggestion is still valid.
166 DVLOG(1) << "Fetched " << image.Size().width() << "x" 170 DVLOG(1) << "Fetched " << image.Size().width() << "x"
167 << image.Size().height() << " image for " << url.spec(); 171 << image.Size().height() << " image for " << url.spec();
168 prefs_->SetString(kNotificationIDWithinCategory, id.id_within_category()); 172 prefs_->SetString(kNotificationIDWithinCategory, id.id_within_category());
169 ContentSuggestionsNotificationHelper::SendNotification( 173 ContentSuggestionsNotificationHelper::SendNotification(
170 url, title, publisher, CropSquare(image)); 174 url, title, publisher, CropSquare(image), timeout_at);
171 } 175 }
172 176
173 ContentSuggestionsService* const service_; 177 ContentSuggestionsService* const service_;
174 PrefService* const prefs_; 178 PrefService* const prefs_;
175 base::android::ApplicationStatusListener app_status_listener_; 179 base::android::ApplicationStatusListener app_status_listener_;
176 180
177 base::WeakPtrFactory<NotifyingObserver> weak_ptr_factory_; 181 base::WeakPtrFactory<NotifyingObserver> weak_ptr_factory_;
178 182
179 DISALLOW_COPY_AND_ASSIGN(NotifyingObserver); 183 DISALLOW_COPY_AND_ASSIGN(NotifyingObserver);
180 }; 184 };
181 185
182 ContentSuggestionsNotifierService::ContentSuggestionsNotifierService( 186 ContentSuggestionsNotifierService::ContentSuggestionsNotifierService(
183 Profile* profile, 187 Profile* profile,
184 ContentSuggestionsService* suggestions, 188 ContentSuggestionsService* suggestions,
185 PrefService* prefs) 189 PrefService* prefs)
186 : observer_(base::MakeUnique<NotifyingObserver>(suggestions, 190 : observer_(base::MakeUnique<NotifyingObserver>(suggestions,
187 profile, 191 profile,
188 profile->GetPrefs())) { 192 profile->GetPrefs())) {
189 suggestions->AddObserver(observer_.get()); 193 suggestions->AddObserver(observer_.get());
190 } 194 }
191 195
192 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() = 196 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() =
193 default; 197 default;
194 198
195 void ContentSuggestionsNotifierService::RegisterProfilePrefs( 199 void ContentSuggestionsNotifierService::RegisterProfilePrefs(
196 user_prefs::PrefRegistrySyncable* registry) { 200 user_prefs::PrefRegistrySyncable* registry) {
197 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string()); 201 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string());
198 } 202 }
OLDNEW
« no previous file with comments | « chrome/browser/android/ntp/content_suggestions_notification_helper.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698