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 #ifndef COMPONENTS_NTP_SNIPPETS_SESSIONS_FOREIGN_SESSIONS_SUGGESTIONS_PROVIDER_H _ | |
6 #define COMPONENTS_NTP_SNIPPETS_SESSIONS_FOREIGN_SESSIONS_SUGGESTIONS_PROVIDER_H _ | |
7 | |
8 #include <memory> | |
9 #include <set> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback_forward.h" | |
14 #include "base/macros.h" | |
15 #include "components/ntp_snippets/category.h" | |
16 #include "components/ntp_snippets/category_status.h" | |
17 #include "components/ntp_snippets/content_suggestions_provider.h" | |
18 #include "components/sessions/core/session_types.h" | |
19 #include "components/sync_sessions/synced_session.h" | |
20 | |
21 class PrefRegistrySimple; | |
22 class PrefService; | |
23 | |
24 namespace ntp_snippets { | |
25 | |
26 // Simple interface to get foreign tab data on demand and on change. | |
27 class ForeignSessionsProvider { | |
28 public: | |
29 virtual ~ForeignSessionsProvider() {} | |
30 virtual bool HasSessionsData() = 0; | |
31 virtual std::vector<const sync_sessions::SyncedSession*> | |
32 GetAllForeignSessions() = 0; | |
33 // Should only be called at most once. | |
34 virtual void SubscribeForForeignTabChange( | |
35 const base::Closure& change_callback) = 0; | |
36 }; | |
37 | |
38 // Provides content suggestions from foreign sessions. | |
39 class ForeignSessionsSuggestionsProvider : public ContentSuggestionsProvider { | |
40 public: | |
41 ForeignSessionsSuggestionsProvider( | |
42 ContentSuggestionsProvider::Observer* observer, | |
43 CategoryFactory* category_factory, | |
44 std::unique_ptr<ForeignSessionsProvider> foreign_sessions_provider, | |
45 PrefService* pref_service); | |
46 ~ForeignSessionsSuggestionsProvider() override; | |
47 | |
48 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | |
49 | |
50 private: | |
51 friend class ForeignSessionsSuggestionsProviderTest; | |
52 | |
53 // Collection of pointers to various sessions objects that contain a superset | |
54 // of the information needed to create a single suggestion. | |
55 struct SessionData { | |
Marc Treib
2016/09/19 09:24:09
optional: I think it's sufficient to forward-decla
skym
2016/09/19 18:54:48
Oooh, great idea, Done.
| |
56 const sync_sessions::SyncedSession* session; | |
57 const sessions::SessionTab* tab; | |
58 const sessions::SerializedNavigationEntry* navigation; | |
59 bool operator<(const SessionData& other) const { | |
60 // Note that SerializedNavigationEntry::timestamp() is never set to a | |
61 // value, so always use SessionTab::timestamp() instead. | |
62 // TODO(skym): It might be better if we sorted by recency of session, and | |
63 // only then by recency of the tab. Right now this causes a single | |
64 // device's tabs to be interleaved with another devices' tabs. | |
65 return tab->timestamp > other.tab->timestamp; | |
66 } | |
67 }; | |
68 | |
69 // ContentSuggestionsProvider implementation. | |
70 CategoryStatus GetCategoryStatus(Category category) override; | |
71 CategoryInfo GetCategoryInfo(Category category) override; | |
72 void DismissSuggestion(const std::string& suggestion_id) override; | |
73 void FetchSuggestionImage(const std::string& suggestion_id, | |
74 const ImageFetchedCallback& callback) override; | |
75 void ClearHistory( | |
76 base::Time begin, | |
77 base::Time end, | |
78 const base::Callback<bool(const GURL& url)>& filter) override; | |
79 void ClearCachedSuggestions(Category category) override; | |
80 void GetDismissedSuggestionsForDebugging( | |
81 Category category, | |
82 const DismissedSuggestionsCallback& callback) override; | |
83 void ClearDismissedSuggestionsForDebugging(Category category) override; | |
84 | |
85 void OnForeignTabChange(); | |
86 std::vector<ContentSuggestion> BuildSuggestions( | |
87 const std::vector<const sync_sessions::SyncedSession*>& foreign_sessions); | |
88 std::vector<SessionData> GetSuggestionCandidates( | |
89 const std::vector<const sync_sessions::SyncedSession*>& foreign_sessions); | |
90 ContentSuggestion BuildSuggestion(const SessionData& data); | |
91 | |
92 CategoryStatus category_status_; | |
93 const Category provided_category_; | |
94 std::unique_ptr<ForeignSessionsProvider> foreign_sessions_provider_; | |
95 PrefService* pref_service_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ForeignSessionsSuggestionsProvider); | |
98 }; | |
99 | |
100 } // namespace ntp_snippets | |
101 | |
102 #endif // COMPONENTS_NTP_SNIPPETS_SESSIONS_FOREIGN_SESSIONS_SUGGESTIONS_PROVIDE R_H_ | |
OLD | NEW |