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

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

Issue 2618703004: Use server signal to decide whether to notify (Closed)
Patch Set: 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 | « no previous file | 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/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h" 10 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 46
47 class ContentSuggestionsNotifierService::NotifyingObserver 47 class ContentSuggestionsNotifierService::NotifyingObserver
48 : public ContentSuggestionsService::Observer { 48 : public ContentSuggestionsService::Observer {
49 public: 49 public:
50 NotifyingObserver(ContentSuggestionsService* service, 50 NotifyingObserver(ContentSuggestionsService* service,
51 Profile* profile, 51 Profile* profile,
52 PrefService* prefs) 52 PrefService* prefs)
53 : service_(service), prefs_(prefs), weak_ptr_factory_(this) {} 53 : service_(service), prefs_(prefs), weak_ptr_factory_(this) {}
54 54
55 void OnNewSuggestions(Category category) override { 55 void OnNewSuggestions(Category category) override {
56 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) { 56 const ContentSuggestion* suggestion = GetSuggestionToNotifyAbout(category);
57 if (!suggestion) {
57 return; 58 return;
58 } 59 }
59 const auto& suggestions = service_->GetSuggestionsForCategory(category); 60 service_->FetchSuggestionImage(
60 if (!suggestions.empty()) { 61 suggestion->id(),
61 const auto& suggestion = suggestions[0]; 62 base::Bind(&NotifyingObserver::ImageFetched,
62 service_->FetchSuggestionImage( 63 weak_ptr_factory_.GetWeakPtr(), suggestion->id(),
63 suggestions[0].id(), 64 suggestion->url(), suggestion->title(),
64 base::Bind(&NotifyingObserver::ImageFetched, 65 suggestion->publisher_name()));
65 weak_ptr_factory_.GetWeakPtr(), suggestion.id(),
66 suggestion.url(), suggestion.title(),
67 suggestion.publisher_name()));
68 }
69 } 66 }
70 67
71 void OnCategoryStatusChanged(Category category, 68 void OnCategoryStatusChanged(Category category,
72 CategoryStatus new_status) override { 69 CategoryStatus new_status) override {
73 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) { 70 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) {
74 return; 71 return;
75 } 72 }
76 switch (new_status) { 73 switch (new_status) {
77 case CategoryStatus::AVAILABLE: 74 case CategoryStatus::AVAILABLE:
78 case CategoryStatus::AVAILABLE_LOADING: 75 case CategoryStatus::AVAILABLE_LOADING:
(...skipping 20 matching lines...) Expand all
99 96
100 void OnFullRefreshRequired() override { 97 void OnFullRefreshRequired() override {
101 ContentSuggestionsNotificationHelper::HideNotification(); 98 ContentSuggestionsNotificationHelper::HideNotification();
102 } 99 }
103 100
104 void ContentSuggestionsServiceShutdown() override { 101 void ContentSuggestionsServiceShutdown() override {
105 ContentSuggestionsNotificationHelper::HideNotification(); 102 ContentSuggestionsNotificationHelper::HideNotification();
106 } 103 }
107 104
108 private: 105 private:
106 const ContentSuggestion* GetSuggestionToNotifyAbout(Category category) {
107 const auto& suggestions = service_->GetSuggestionsForCategory(category);
108 if (false /* DO_NOT_SUBMIT: always_notify after crrev.com/2613863003 */) {
Bernhard Bauer 2017/01/05 17:31:17 So, if the feature is enabled, we will always noti
sfiera 2017/01/05 19:14:37 This condition will use AlwaysNotifyAboutContentSu
Bernhard Bauer 2017/01/06 13:26:59 Yes, but my point was, in the third case (if the a
sfiera 2017/01/06 15:39:52 Oh, I see. Yes, this is the intended behavior. The
109 if (category.IsKnownCategory(KnownCategories::ARTICLES) &&
110 suggestions.size()) {
Bernhard Bauer 2017/01/05 17:31:17 Maybe use !suggestions.empty()?
sfiera 2017/01/05 19:14:37 Done.
111 return &suggestions[0];
112 }
113 return nullptr;
114 }
115
116 for (const ContentSuggestion& suggestion : suggestions) {
117 if (suggestion.notification_extra()) {
118 return &suggestion;
119 }
120 }
121 return nullptr;
122 }
123
109 void ImageFetched(const ContentSuggestion::ID& id, 124 void ImageFetched(const ContentSuggestion::ID& id,
110 const GURL& url, 125 const GURL& url,
111 const base::string16& title, 126 const base::string16& title,
112 const base::string16& publisher, 127 const base::string16& publisher,
113 const gfx::Image& image) { 128 const gfx::Image& image) {
114 // check if suggestion is still valid. 129 // check if suggestion is still valid.
115 DVLOG(1) << "Fetched " << image.Size().width() << "x" 130 DVLOG(1) << "Fetched " << image.Size().width() << "x"
116 << image.Size().height() << " image for " << url.spec(); 131 << image.Size().height() << " image for " << url.spec();
117 prefs_->SetString(kNotificationIDWithinCategory, id.id_within_category()); 132 prefs_->SetString(kNotificationIDWithinCategory, id.id_within_category());
118 ContentSuggestionsNotificationHelper::SendNotification( 133 ContentSuggestionsNotificationHelper::SendNotification(
(...skipping 18 matching lines...) Expand all
137 suggestions->AddObserver(observer_.get()); 152 suggestions->AddObserver(observer_.get());
138 } 153 }
139 154
140 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() = 155 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() =
141 default; 156 default;
142 157
143 void ContentSuggestionsNotifierService::RegisterProfilePrefs( 158 void ContentSuggestionsNotifierService::RegisterProfilePrefs(
144 user_prefs::PrefRegistrySyncable* registry) { 159 user_prefs::PrefRegistrySyncable* registry) {
145 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string()); 160 registry->RegisterStringPref(kNotificationIDWithinCategory, std::string());
146 } 161 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698