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

Side by Side Diff: components/previews/core/previews_black_list.h

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

Powered by Google App Engine
This is Rietveld 408576698