OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | |
6 #define CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/hash_tables.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/task.h" | |
18 #include "chrome/browser/prefs/pref_change_registrar.h" | |
19 #include "content/common/notification_observer.h" | |
20 | |
21 class GURL; | |
22 class NotificationDetails; | |
23 class NotificationSource; | |
24 class PrefService; | |
25 class Profile; | |
26 | |
27 namespace base { | |
28 class ListValue; | |
29 } | |
30 | |
31 namespace policy { | |
32 | |
33 // Manages a table of blacklisted urls. Not a private implementation so that | |
34 // it can be tested. | |
35 class Blacklist { | |
36 public: | |
37 Blacklist(); | |
38 virtual ~Blacklist(); | |
39 | |
40 // URLs matching |filter| will be blocked. | |
41 void Block(const std::string& filter); | |
42 | |
43 // URLs matching |filter| will be allowed. If |filter| is both Blocked and | |
44 // Allowed, Allow takes precedence. | |
45 void Allow(const std::string& filter); | |
46 | |
47 // Returns true if the URL is blocked. | |
48 bool IsURLBlocked(const GURL& url) const; | |
49 | |
50 // A constant mapped to a scheme that can be filtered. | |
51 enum SchemeFlag { | |
52 SCHEME_HTTP = 1 << 0, | |
53 SCHEME_HTTPS = 1 << 1, | |
54 SCHEME_FTP = 1 << 2, | |
55 | |
56 SCHEME_ALL = (1 << 3) - 1, | |
57 }; | |
58 | |
59 // Returns true if |scheme| is a scheme that can be filtered. Returns true | |
60 // and sets |flag| to SCHEME_ALL if |scheme| is empty. | |
61 static bool SchemeToFlag(const std::string& scheme, SchemeFlag* flag); | |
62 | |
63 // Splits a URL filter into its components. A GURL isn't used because these | |
64 // aren't canonical URLs. Returns false if the URL couldn't be parsed. | |
Mattias Nissler (ping if slow)
2011/08/31 14:58:09
Actually, isn't the problem with GURL that it _doe
Joao da Silva
2011/09/01 12:47:36
Indeed it does. The problem is that GURL doesn't a
| |
65 // The optional username and password are ignored. | |
66 // |port| is 0 if none is explicitly defined. | |
67 // |path| does not include query parameters. | |
68 static bool FilterToComponents(const std::string& filter, | |
69 std::string* scheme, | |
70 std::string* host, | |
71 uint16* port, | |
72 std::string* path); | |
73 private: | |
74 void AddFilter(const std::string& filter, bool block); | |
75 | |
76 struct PathFilter { | |
77 explicit PathFilter(const std::string& path, uint16 port, bool match) | |
78 : path_prefix(path), | |
79 port(port), | |
80 blocked_schemes(0), | |
81 allowed_schemes(0), | |
82 match_subdomains(match) { } | |
Mattias Nissler (ping if slow)
2011/08/31 14:58:09
remove space after ) and between {}
Joao da Silva
2011/09/01 12:47:36
Done.
| |
83 | |
84 std::string path_prefix; | |
85 uint16 port; | |
86 uint8 blocked_schemes; | |
87 uint8 allowed_schemes; | |
88 bool match_subdomains; | |
89 }; | |
90 | |
91 typedef std::vector<PathFilter> PathFilterList; | |
92 typedef base::hash_map<std::string, PathFilterList> HostFilterTable; | |
93 | |
94 HostFilterTable host_filters_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(Blacklist); | |
97 }; | |
98 | |
99 // Tracks the blacklist policies for a given profile, and updates it on changes. | |
100 // | |
101 // This class interacts with both the UI thread, where notifications of pref | |
102 // changes are received from, and the IO thread, which owns it (in the | |
103 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). | |
104 // | |
105 // It must be constructed on the UI thread first, to set up | |
106 // |ui_method_factory_| and |initialize_on_ui_task_|. The task is used to | |
107 // resume initialization on the UI thread, after initialization on the IO thread | |
108 // is complete. | |
109 // | |
110 // InitializeOnIOThread sets up |io_weak_ptr_factory_| and |weak_ptr_|. These | |
111 // allow creating WeakPtrs to self on the UI thread, which will be passed | |
112 // around before being used on the IO thread, where they can be tested for | |
113 // validity and invalidated. InitializeOnIOThread then posts | |
114 // |initialize_on_ui_task_|, which resumes initialization on the UI thread. | |
115 // | |
116 // The task will invoke InitializeOnUIThread, which sets up preference | |
117 // listeners. These can only start updating once it's safe to get weak ptrs for | |
118 // the IO thread. | |
119 // | |
120 // Destruction must also pass through both threads. ShutdownOnUIThread must be | |
121 // called first, and will invalidate any pending updates (and even a pending | |
122 // initialize_on_ui_task_, if it hasn't executed yet). After that, no more | |
123 // calls will be made from the UI thread. It is then safe to call the dtor | |
124 // on the IO thread. Any updates will flight have a weak ptr to self, which | |
125 // will be invalidated and the update is discarded. | |
126 class URLBlacklistManager : public NotificationObserver { | |
127 public: | |
128 // Must be constructed on the UI thread. | |
129 explicit URLBlacklistManager(Profile* profile); | |
130 virtual ~URLBlacklistManager(); | |
131 | |
132 // Must be called on the IO thread. | |
133 void InitializeOnIOThread(); | |
134 | |
135 // Must be called on the UI thread, before destruction. | |
136 void ShutdownOnUIThread(); | |
137 | |
138 // Returns true if |url| is blocked by the current blacklist. Must be called | |
139 // from the IO thread. | |
140 bool IsURLBlocked(const GURL& url) const; | |
141 | |
142 // Replaces the current blacklist. Must be called on the IO thread. | |
143 void SetBlacklist(Blacklist* blacklist); | |
144 | |
145 // Registers the preferences related to blacklisting in the given PrefService. | |
146 static void RegisterPrefs(PrefService* pref_service); | |
147 | |
148 protected: | |
149 // These are used to delay updating the blacklist while the preferences are | |
150 // changing, and execute only one update per simultaneous prefs changes. | |
151 void ScheduleUpdate(); | |
152 virtual void PostUpdateTask(Task* task); // Virtual for testing. | |
153 virtual void Update(); // Virtual for testing. | |
154 | |
155 private: | |
156 virtual void Observe(int type, | |
157 const NotificationSource& source, | |
158 const NotificationDetails& details) OVERRIDE; | |
159 | |
160 // Completes initialization on the UI thread. | |
161 void InitializeOnUIThread(); | |
162 | |
163 // Used to post update tasks to the UI thread. | |
164 ScopedRunnableMethodFactory<URLBlacklistManager> ui_method_factory_; | |
165 | |
166 // Used to get weak ptrs to self that are valid on the IO thread. | |
167 base::WeakPtrFactory<URLBlacklistManager> io_weak_ptr_factory_; | |
168 | |
169 // Holds a weak reference to self on the IO thread. This is used to enable | |
170 // creation on the UI thread of weak refs that are used on the IO thread. | |
171 // Existence of this ref prevents reattachment of new weak ptrs to the UI | |
172 // thread. | |
173 base::WeakPtr<URLBlacklistManager> weak_ptr_; | |
174 | |
175 // A task that is created on the UI thread but posted from the IO thread to | |
176 // resume initialization on the UI thread. | |
177 Task* initialize_on_ui_task_; | |
178 | |
179 // Used to track the policies and update the blacklist on changes. | |
180 PrefChangeRegistrar pref_change_registrar_; | |
181 PrefService* pref_service_; // Weak. | |
182 | |
183 // The current blacklist. Lives in the IO thread. | |
184 scoped_ptr<Blacklist> blacklist_; | |
185 | |
186 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); | |
187 }; | |
188 | |
189 } // namespace policy | |
190 | |
191 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ | |
OLD | NEW |