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_PREVIEWS_PREVIEWS_BLACK_LIST_H_ | |
6 #define COMPONENTS_PREVIEWS_PREVIEWS_BLACK_LIST_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <memory> | |
11 #include <queue> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/callback.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/weak_ptr.h" | |
18 #include "base/threading/thread_checker.h" | |
19 #include "components/previews/core/previews_opt_out_store.h" | |
20 | |
21 class GURL; | |
22 | |
23 namespace base { | |
24 class Clock; | |
25 class Time; | |
tbansal1
2016/09/19 21:26:08
nit: rm forward declare Time.
RyanSturm
2016/09/19 22:37:45
Done.
| |
26 } | |
27 | |
28 namespace previews { | |
29 class PreviewsBlackListItem; | |
30 | |
31 // Manages the state of black listed domains for the previews experiment. Loads | |
32 // the stored black list from |opt_out_store| and manages an in memory black | |
33 // list on the IO thread. Updates to the black list are stored in memory and | |
34 // pushed to the store. Asynchronous modifications are stored in a queue and | |
35 // executed in order. Reading from the black list is always synchronous, and if | |
36 // the black list is not currently loaded (e.g., at startup, after clearing | |
37 // browsing history), domains are reported as black listed. The list stores no | |
38 // more than previews::params::MaxInMemoryHostsInBlackList hosts in-memory, | |
39 // which defaults to 100. | |
40 class PreviewsBlackList { | |
41 public: | |
42 // |opt_out_store| is the backing store to retrieve and store black list | |
43 // information, and can be null. When |opt_out_store| is null, the in-memory | |
44 // map will be immeadiately loaded to empty. If |opt_out_store| is non-null, | |
45 // it will be used to load the in-memory map asynchronously. | |
46 explicit PreviewsBlackList(std::unique_ptr<PreviewsOptOutStore> opt_out_store, | |
tbansal1
2016/09/19 21:26:08
nit: explicit not needed.
RyanSturm
2016/09/19 22:37:45
Done.
| |
47 std::unique_ptr<base::Clock> clock); | |
48 ~PreviewsBlackList(); | |
49 | |
50 // Asynchronously adds a new navigation to to the in-memory black list and | |
51 // backing store. |opt_out| is whether the user opted out of the preview or | |
52 // navigated away from the page without opting out. |type| is only passed to | |
53 // the backing store. If the in memory map has reached the max number of hosts | |
54 // allowed, and |url| is a new host, a host will be evicted based on recency | |
55 // of the hosts most recent opt out. | |
56 void AddPreviewNavigation(const GURL& url, bool opt_out, PreviewsType type); | |
57 | |
58 // Synchronously determines if |host_name| should be allowed to show previews. | |
59 // If the black list has loaded yet, this will always return false. |type| is | |
60 // not used to make this decision. | |
61 bool IsLoadedAndAllowed(const GURL& url, PreviewsType type); | |
62 | |
63 private: | |
64 typedef base::Closure QueueClosure; | |
65 | |
66 // Synchronous version of AddPreviewNavigation method. | |
67 void AddPreviewNavigationSync(const GURL& host_name, | |
68 bool opt_out, | |
69 PreviewsType type); | |
70 | |
71 // Returns the PreviewsBlackListItem representing |host_name|. If there is no | |
72 // item for |host_name|, one will be created iff |create_if_needed| is true. | |
73 PreviewsBlackListItem* GetBlackListItem(const std::string& host_name, | |
74 bool create_if_needed); | |
75 | |
76 // Callback passed to the backing store when loading black list information. | |
77 // Moves the returned map into the in-memory black list and runs any | |
78 // outstanding tasks. | |
79 void LoadBlackListDone(std::unique_ptr<BlackListItemMap> black_list_item_map); | |
80 | |
81 // Called while waiting for the black list to be loaded from the backing | |
82 // store. | |
83 // Enqueues a task to run when when loading black list information has | |
84 // completed. Maintains the order that tasks were called in. | |
85 void QueuePendingTask(QueueClosure callback); | |
86 | |
87 // Evicts entries from the in-memory black list based on recency of a hosts | |
88 // most recent opt out time. | |
89 void EvictOldestOptOut(); | |
90 | |
91 // Map maintaining the in-memory black list. | |
92 std::unique_ptr<BlackListItemMap> black_list_item_map_; | |
93 | |
94 // Whether the black list is done being loaded from the backing store. | |
95 bool loaded_; | |
96 | |
97 // The backing store of the black list information. | |
98 std::unique_ptr<PreviewsOptOutStore> opt_out_store_; | |
99 | |
100 // Callbacks to be run after loading information from the backing store has | |
101 // completed. | |
102 std::queue<QueueClosure> pending_callbacks_; | |
103 | |
104 std::unique_ptr<base::Clock> clock_; | |
105 | |
106 base::ThreadChecker thread_checker_; | |
107 | |
108 base::WeakPtrFactory<PreviewsBlackList> weak_factory_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(PreviewsBlackList); | |
111 }; | |
112 | |
113 } // namespace previews | |
114 | |
115 #endif // COMPONENTS_PREVIEWS_PREVIEWS_BLACK_LIST_ITEM_H_ | |
OLD | NEW |