OLD | NEW |
---|---|
(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 "components/ntp_snippets/bookmarks/bookmark_suggestions_provider.h" | |
6 | |
7 #include <utility> | |
8 #include <vector> | |
9 | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "components/bookmarks/browser/bookmark_model.h" | |
12 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | |
13 #include "components/ntp_snippets/category_factory.h" | |
14 #include "components/ntp_snippets/content_suggestion.h" | |
15 | |
16 using bookmarks::BookmarkModel; | |
17 using bookmarks::BookmarkNode; | |
18 | |
19 namespace { | |
20 const int kMaxBookmarks = 10; | |
21 const int kMaxBookmarkAgeInDays = 42; | |
22 | |
23 base::Time GetThresholdTime() { | |
24 return base::Time::Now() - base::TimeDelta::FromDays(kMaxBookmarkAgeInDays); | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 namespace ntp_snippets { | |
30 | |
31 BookmarkSuggestionsProvider::BookmarkSuggestionsProvider( | |
32 ContentSuggestionsProvider::Observer* observer, | |
33 CategoryFactory* category_factory, | |
34 bookmarks::BookmarkModel* bookmark_model) | |
35 : ContentSuggestionsProvider(observer, category_factory), | |
36 category_status_(CategoryStatus::AVAILABLE_LOADING), | |
37 provided_category_( | |
38 category_factory->FromKnownCategory(KnownCategories::BOOKMARKS)), | |
39 bookmark_model_(bookmark_model), | |
40 fetch_requested_(false), | |
41 end_of_list_last_visit_date_(GetThresholdTime()) { | |
42 bookmark_model_->AddObserver(this); | |
43 FetchBookmarks(); | |
44 } | |
45 | |
46 BookmarkSuggestionsProvider::~BookmarkSuggestionsProvider() { | |
47 bookmark_model_->RemoveObserver(this); | |
48 } | |
49 | |
50 //////////////////////////////////////////////////////////////////////////////// | |
51 // Private methods | |
52 | |
53 std::vector<Category> BookmarkSuggestionsProvider::GetProvidedCategories() { | |
54 return std::vector<Category>({provided_category_}); | |
55 } | |
56 | |
57 CategoryStatus BookmarkSuggestionsProvider::GetCategoryStatus( | |
58 Category category) { | |
59 return category_status_; | |
60 } | |
61 | |
62 void BookmarkSuggestionsProvider::DismissSuggestion( | |
63 const std::string& suggestion_id) { | |
64 // TODO(jkrcal): Implement blacklisting bookmarks until they are next visited. | |
65 // Then also implement ClearDismissedSuggestionsForDebugging. | |
66 } | |
67 | |
68 void BookmarkSuggestionsProvider::FetchSuggestionImage( | |
69 const std::string& suggestion_id, | |
70 const ImageFetchedCallback& callback) { | |
71 callback.Run(suggestion_id, gfx::Image()); | |
Marc Treib
2016/08/04 13:57:14
Running the callback synchronously might be surpri
Philipp Keck
2016/08/04 14:43:42
So far, it has always worked fine. The ContentSugg
Marc Treib
2016/08/04 14:46:26
Acknowledged.
Bernhard Bauer
2016/08/04 18:10:55
+1 for asynchronously running the callback. Reentr
Philipp Keck
2016/08/05 08:00:21
Ok. CL coming up.
| |
72 } | |
73 | |
74 void BookmarkSuggestionsProvider::ClearCachedSuggestionsForDebugging() { | |
75 // Ignored. | |
76 } | |
77 | |
78 void BookmarkSuggestionsProvider::ClearDismissedSuggestionsForDebugging() { | |
79 // TODO(jkrcal): Implement when discarded suggestions are supported. | |
80 } | |
81 | |
82 void BookmarkSuggestionsProvider::BookmarkModelLoaded( | |
83 bookmarks::BookmarkModel* model, | |
84 bool ids_reassigned) { | |
85 DCHECK_EQ(bookmark_model_, model); | |
86 if (fetch_requested_) { | |
87 fetch_requested_ = false; | |
88 FetchBookmarksInternal(); | |
89 } | |
90 } | |
91 | |
92 void BookmarkSuggestionsProvider::OnWillChangeBookmarkMetaInfo( | |
93 BookmarkModel* model, | |
94 const BookmarkNode* node) { | |
95 // Store the last visit date of the node that is about to change. | |
96 node_to_change_last_visit_date_ = GetLastVisitDateForBookmark(node); | |
97 } | |
98 | |
99 void BookmarkSuggestionsProvider::BookmarkMetaInfoChanged( | |
100 BookmarkModel* model, | |
101 const BookmarkNode* node) { | |
102 base::Time time = GetLastVisitDateForBookmark(node); | |
103 if (time == node_to_change_last_visit_date_ || | |
104 time < end_of_list_last_visit_date_) | |
105 return; | |
106 | |
107 // Last visit date of a node has changed (and is relevant for the list), we | |
108 // should update the suggestions. | |
109 FetchBookmarks(); | |
Marc Treib
2016/08/04 13:57:14
We probably can't get here before the bookmark mod
Philipp Keck
2016/08/04 14:43:42
I don't know if the BookmarkModel guarantees that.
Marc Treib
2016/08/04 14:46:26
Alright. Carry on then I guess...
Philipp Keck
2016/08/05 08:00:21
Acknowledged.
| |
110 } | |
111 | |
112 void BookmarkSuggestionsProvider::FetchBookmarksInternal() { | |
113 DCHECK(bookmark_model_->loaded()); | |
114 | |
115 NotifyStatusChanged(CategoryStatus::AVAILABLE); | |
116 | |
117 std::vector<const BookmarkNode*> bookmarks = GetRecentlyVisitedBookmarks( | |
118 bookmark_model_, kMaxBookmarks, GetThresholdTime()); | |
119 | |
120 std::vector<ContentSuggestion> suggestions; | |
121 for (const BookmarkNode* bookmark : bookmarks) { | |
122 ContentSuggestion suggestion( | |
123 MakeUniqueID(provided_category_, bookmark->url().spec()), | |
124 bookmark->url()); | |
125 | |
126 suggestion.set_title(bookmark->GetTitle()); | |
127 suggestion.set_snippet_text(base::string16()); | |
128 suggestion.set_publish_date(GetLastVisitDateForBookmark(bookmark)); | |
129 suggestion.set_publisher_name(base::UTF8ToUTF16(bookmark->url().host())); | |
130 suggestions.emplace_back(std::move(suggestion)); | |
131 } | |
132 | |
133 if (suggestions.empty()) | |
134 end_of_list_last_visit_date_ = GetThresholdTime(); | |
135 else | |
136 end_of_list_last_visit_date_ = suggestions.back().publish_date(); | |
137 | |
138 observer()->OnNewSuggestions(this, provided_category_, | |
139 std::move(suggestions)); | |
140 } | |
141 | |
142 void BookmarkSuggestionsProvider::FetchBookmarks() { | |
143 if (bookmark_model_->loaded()) | |
144 FetchBookmarksInternal(); | |
145 else | |
146 fetch_requested_ = true; | |
147 } | |
148 | |
149 void BookmarkSuggestionsProvider::NotifyStatusChanged( | |
150 CategoryStatus new_status) { | |
151 if (category_status_ == new_status) | |
152 return; | |
153 category_status_ = new_status; | |
154 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | |
155 } | |
156 | |
157 } // namespace ntp_snippets | |
OLD | NEW |