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

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

Issue 2606753002: Post notification after fetching articles (Closed)
Patch Set: override Created 3 years, 12 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/android/ntp/content_suggestions_notifier_service.h"
6
7 #include <algorithm>
8
9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/android/ntp/content_suggestions_notification_helper.h"
11 #include "chrome/browser/notifications/notification.h"
12 #include "chrome/browser/notifications/notification_handler.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "components/ntp_snippets/content_suggestions_service.h"
15 #include "components/pref_registry/pref_registry_syncable.h"
16 #include "components/prefs/pref_service.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/gfx/image/image_skia_operations.h"
20
21 using ntp_snippets::Category;
22 using ntp_snippets::CategoryStatus;
23 using ntp_snippets::ContentSuggestion;
24 using ntp_snippets::ContentSuggestionsNotificationHelper;
25 using ntp_snippets::ContentSuggestionsService;
26 using ntp_snippets::KnownCategories;
27
28 namespace {
29
30 const char kNotificationIDWithinCategory[] =
31 "ContentSuggestionsNotificationIDWithinCategory";
32
33 gfx::Image CropSquare(const gfx::Image& image) {
34 if (image.IsEmpty()) {
35 return image;
36 }
37 const gfx::ImageSkia* skimage = image.ToImageSkia();
38 gfx::Rect bounds{{0, 0}, skimage->size()};
39 int size = std::min(bounds.width(), bounds.height());
40 bounds.ClampToCenteredSize({size, size});
41 return gfx::Image(gfx::ImageSkiaOperations::CreateTiledImage(
42 *skimage, bounds.x(), bounds.y(), bounds.width(), bounds.height()));
43 }
44
45 } // namespace
46
47 class ContentSuggestionsNotifierService::NotifyingObserver
48 : public ContentSuggestionsService::Observer {
49 public:
50 NotifyingObserver(ContentSuggestionsService* service,
51 Profile* profile,
52 PrefService* prefs)
53 : service_(service), prefs_(prefs), weak_ptr_factory_(this) {}
54
55 void OnNewSuggestions(Category category) override {
56 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) {
57 return;
58 }
59 const auto& suggestions = service_->GetSuggestionsForCategory(category);
60 if (!suggestions.empty()) {
61 const auto& suggestion = suggestions[0];
62 service_->FetchSuggestionImage(
63 suggestions[0].id(),
64 base::Bind(&NotifyingObserver::ImageFetched,
65 weak_ptr_factory_.GetWeakPtr(), suggestion.id(),
66 suggestion.url(), suggestion.title(),
67 suggestion.publisher_name()));
68 }
69 }
70
71 void OnCategoryStatusChanged(Category category,
72 CategoryStatus new_status) override {
73 if (!category.IsKnownCategory(KnownCategories::ARTICLES)) {
74 return;
75 }
76 switch (new_status) {
77 case CategoryStatus::AVAILABLE:
78 case CategoryStatus::AVAILABLE_LOADING:
79 break; // nothing to do
80 case CategoryStatus::INITIALIZING:
81 case CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED:
82 case CategoryStatus::CATEGORY_EXPLICITLY_DISABLED:
83 case CategoryStatus::LOADING_ERROR:
84 case CategoryStatus::NOT_PROVIDED:
85 case CategoryStatus::SIGNED_OUT:
86 ContentSuggestionsNotificationHelper::HideNotification();
87 break;
88 }
89 }
90
91 void OnSuggestionInvalidated(
92 const ContentSuggestion::ID& suggestion_id) override {
93 if (suggestion_id.category().IsKnownCategory(KnownCategories::ARTICLES) &&
94 (suggestion_id.id_within_category() ==
95 prefs_->GetString(kNotificationIDWithinCategory))) {
96 ContentSuggestionsNotificationHelper::HideNotification();
97 }
98 }
99
100 void OnFullRefreshRequired() override {
101 ContentSuggestionsNotificationHelper::HideNotification();
102 }
103
104 void ContentSuggestionsServiceShutdown() override {
105 ContentSuggestionsNotificationHelper::HideNotification();
106 }
107
108 private:
109 void ImageFetched(const ContentSuggestion::ID& id,
110 const GURL& url,
111 const base::string16& title,
112 const base::string16& publisher,
113 const gfx::Image& image) {
114 // check if suggestion is still valid.
115 DVLOG(1) << "Fetched " << image.Size().width() << "x"
116 << image.Size().height() << " image for " << url.spec();
117 prefs_->SetString(kNotificationIDWithinCategory, id.id_within_category());
118 ContentSuggestionsNotificationHelper::SendNotification(
119 url, title, publisher, CropSquare(image));
120 }
121
122 ContentSuggestionsService* const service_;
123 PrefService* const prefs_;
124
125 base::WeakPtrFactory<NotifyingObserver> weak_ptr_factory_;
126
127 DISALLOW_COPY_AND_ASSIGN(NotifyingObserver);
128 };
129
130 ContentSuggestionsNotifierService::ContentSuggestionsNotifierService(
131 Profile* profile,
132 ContentSuggestionsService* suggestions,
133 PrefService* prefs)
134 : observer_(base::MakeUnique<NotifyingObserver>(suggestions,
135 profile,
136 profile->GetPrefs())) {
137 suggestions->AddObserver(observer_.get());
138 }
139
140 ContentSuggestionsNotifierService::~ContentSuggestionsNotifierService() =
141 default;
142
143 void ContentSuggestionsNotifierService::RegisterProfilePrefs(
144 user_prefs::PrefRegistrySyncable* registry) {
145 registry->RegisterStringPref(kNotificationIDWithinCategory, "");
Bernhard Bauer 2017/01/03 17:38:59 Nit: use an empty std::string() constructor to sav
sfiera 2017/01/03 23:13:55 Done.
146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698