| 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/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" |
| 11 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h" | 11 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h" |
| 12 #include "chrome/browser/notifications/notification.h" | 12 #include "chrome/browser/notifications/notification.h" |
| 13 #include "chrome/browser/notifications/notification_handler.h" | 13 #include "chrome/browser/notifications/notification_handler.h" |
| 14 #include "chrome/browser/ntp_snippets/ntp_snippets_features.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "components/ntp_snippets/content_suggestions_service.h" | 16 #include "components/ntp_snippets/content_suggestions_service.h" |
| 16 #include "components/pref_registry/pref_registry_syncable.h" | 17 #include "components/pref_registry/pref_registry_syncable.h" |
| 17 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
| 19 #include "components/variations/variations_associated_data.h" |
| 18 #include "ui/gfx/geometry/rect.h" | 20 #include "ui/gfx/geometry/rect.h" |
| 19 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
| 20 #include "ui/gfx/image/image_skia_operations.h" | 22 #include "ui/gfx/image/image_skia_operations.h" |
| 21 | 23 |
| 22 using ntp_snippets::Category; | 24 using ntp_snippets::Category; |
| 23 using ntp_snippets::CategoryStatus; | 25 using ntp_snippets::CategoryStatus; |
| 24 using ntp_snippets::ContentSuggestion; | 26 using ntp_snippets::ContentSuggestion; |
| 25 using ntp_snippets::ContentSuggestionsNotificationHelper; | 27 using ntp_snippets::ContentSuggestionsNotificationHelper; |
| 26 using ntp_snippets::ContentSuggestionsService; | 28 using ntp_snippets::ContentSuggestionsService; |
| 27 using ntp_snippets::KnownCategories; | 29 using ntp_snippets::KnownCategories; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 NotifyingObserver(ContentSuggestionsService* service, | 67 NotifyingObserver(ContentSuggestionsService* service, |
| 66 Profile* profile, | 68 Profile* profile, |
| 67 PrefService* prefs) | 69 PrefService* prefs) |
| 68 : service_(service), | 70 : service_(service), |
| 69 prefs_(prefs), | 71 prefs_(prefs), |
| 70 app_status_listener_(base::Bind(&NotifyingObserver::AppStatusChanged, | 72 app_status_listener_(base::Bind(&NotifyingObserver::AppStatusChanged, |
| 71 base::Unretained(this))), | 73 base::Unretained(this))), |
| 72 weak_ptr_factory_(this) {} | 74 weak_ptr_factory_(this) {} |
| 73 | 75 |
| 74 void OnNewSuggestions(Category category) override { | 76 void OnNewSuggestions(Category category) override { |
| 75 if (!ShouldNotifyInState(app_status_listener_.GetState()) || | 77 if (!ShouldNotifyInState(app_status_listener_.GetState())) { |
| 76 !category.IsKnownCategory(KnownCategories::ARTICLES)) { | |
| 77 return; | 78 return; |
| 78 } | 79 } |
| 79 const auto& suggestions = service_->GetSuggestionsForCategory(category); | 80 const ContentSuggestion* suggestion = GetSuggestionToNotifyAbout(category); |
| 80 if (!suggestions.empty()) { | 81 if (!suggestion) { |
| 81 const auto& suggestion = suggestions[0]; | 82 return; |
| 82 service_->FetchSuggestionImage( | |
| 83 suggestions[0].id(), | |
| 84 base::Bind(&NotifyingObserver::ImageFetched, | |
| 85 weak_ptr_factory_.GetWeakPtr(), suggestion.id(), | |
| 86 suggestion.url(), suggestion.title(), | |
| 87 suggestion.publisher_name())); | |
| 88 } | 83 } |
| 84 service_->FetchSuggestionImage( |
| 85 suggestion->id(), |
| 86 base::Bind(&NotifyingObserver::ImageFetched, |
| 87 weak_ptr_factory_.GetWeakPtr(), suggestion->id(), |
| 88 suggestion->url(), suggestion->title(), |
| 89 suggestion->publisher_name())); |
| 89 } | 90 } |
| 90 | 91 |
| 91 void OnCategoryStatusChanged(Category category, | 92 void OnCategoryStatusChanged(Category category, |
| 92 CategoryStatus new_status) override { | 93 CategoryStatus new_status) override { |
| 93 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) { | 94 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) { |
| 94 return; | 95 return; |
| 95 } | 96 } |
| 96 switch (new_status) { | 97 switch (new_status) { |
| 97 case CategoryStatus::AVAILABLE: | 98 case CategoryStatus::AVAILABLE: |
| 98 case CategoryStatus::AVAILABLE_LOADING: | 99 case CategoryStatus::AVAILABLE_LOADING: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 119 | 120 |
| 120 void OnFullRefreshRequired() override { | 121 void OnFullRefreshRequired() override { |
| 121 ContentSuggestionsNotificationHelper::HideNotification(); | 122 ContentSuggestionsNotificationHelper::HideNotification(); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void ContentSuggestionsServiceShutdown() override { | 125 void ContentSuggestionsServiceShutdown() override { |
| 125 ContentSuggestionsNotificationHelper::HideNotification(); | 126 ContentSuggestionsNotificationHelper::HideNotification(); |
| 126 } | 127 } |
| 127 | 128 |
| 128 private: | 129 private: |
| 130 const ContentSuggestion* GetSuggestionToNotifyAbout(Category category) { |
| 131 const auto& suggestions = service_->GetSuggestionsForCategory(category); |
| 132 // TODO(sfiera): replace with AlwaysNotifyAboutContentSuggestions(). |
| 133 if (variations::GetVariationParamByFeatureAsBool( |
| 134 kContentSuggestionsNotificationsFeature, |
| 135 kContentSuggestionsNotificationsAlwaysNotifyParam, false)) { |
| 136 if (category.IsKnownCategory(KnownCategories::ARTICLES) && |
| 137 !suggestions.empty()) { |
| 138 return &suggestions[0]; |
| 139 } |
| 140 return nullptr; |
| 141 } |
| 142 |
| 143 for (const ContentSuggestion& suggestion : suggestions) { |
| 144 if (suggestion.notification_extra()) { |
| 145 return &suggestion; |
| 146 } |
| 147 } |
| 148 return nullptr; |
| 149 } |
| 150 |
| 129 void AppStatusChanged(base::android::ApplicationState state) { | 151 void AppStatusChanged(base::android::ApplicationState state) { |
| 130 if (!ShouldNotifyInState(state)) { | 152 if (!ShouldNotifyInState(state)) { |
| 131 ContentSuggestionsNotificationHelper::HideNotification(); | 153 ContentSuggestionsNotificationHelper::HideNotification(); |
| 132 } | 154 } |
| 133 } | 155 } |
| 134 | 156 |
| 135 void ImageFetched(const ContentSuggestion::ID& id, | 157 void ImageFetched(const ContentSuggestion::ID& id, |
| 136 const GURL& url, | 158 const GURL& url, |
| 137 const base::string16& title, | 159 const base::string16& title, |
| 138 const base::string16& publisher, | 160 const base::string16& publisher, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 167 suggestions->AddObserver(observer_.get()); | 189 suggestions->AddObserver(observer_.get()); |
| 168 } | 190 } |
| 169 | 191 |
| 170 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() = | 192 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() = |
| 171 default; | 193 default; |
| 172 | 194 |
| 173 void ContentSuggestionsNotifierService::RegisterProfilePrefs( | 195 void ContentSuggestionsNotifierService::RegisterProfilePrefs( |
| 174 user_prefs::PrefRegistrySyncable* registry) { | 196 user_prefs::PrefRegistrySyncable* registry) { |
| 175 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string()); | 197 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string()); |
| 176 } | 198 } |
| OLD | NEW |