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

Side by Side Diff: chrome/browser/policy/url_blacklist_manager.h

Issue 7716003: WIP: URL blacklisting by policy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reviewed Created 9 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 | Annotate | Revision Log
OLDNEW
(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 // can be invalid URLs e.g. "google.com".
65 // Returns false if the URL couldn't be parsed.
66 // The optional username and password are ignored.
67 // |port| is 0 if none is explicitly defined.
68 // |path| does not include query parameters.
69 static bool FilterToComponents(const std::string& filter,
70 std::string* scheme,
71 std::string* host,
72 uint16* port,
73 std::string* path);
74 private:
75 void AddFilter(const std::string& filter, bool block);
76
77 struct PathFilter {
78 explicit PathFilter(const std::string& path, uint16 port, bool match)
79 : path_prefix(path),
80 port(port),
81 blocked_schemes(0),
82 allowed_schemes(0),
83 match_subdomains(match) {}
84
85 std::string path_prefix;
86 uint16 port;
87 uint8 blocked_schemes;
88 uint8 allowed_schemes;
89 bool match_subdomains;
90 };
91
92 typedef std::vector<PathFilter> PathFilterList;
93 typedef base::hash_map<std::string, PathFilterList> HostFilterTable;
94
95 HostFilterTable host_filters_;
96
97 DISALLOW_COPY_AND_ASSIGN(Blacklist);
98 };
99
100 // Tracks the blacklist policies for a given profile, and updates it on changes.
101 //
102 // This class interacts with both the UI thread, where notifications of pref
103 // changes are received from, and the IO thread, which owns it (in the
104 // ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate).
105 //
106 // It must be constructed on the UI thread first, to set up
107 // |ui_method_factory_| and |initialize_on_ui_task_|. The task is used to
108 // resume initialization on the UI thread, after initialization on the IO thread
109 // is complete.
110 //
111 // InitializeOnIOThread sets up |io_weak_ptr_factory_| and |weak_ptr_|. The
112 // |weak_ptr_| is valid on the IO thread, and is used to post updates from UI.
113 // InitializeOnUIThread then posts |initialize_on_ui_task_|, which resumes
114 // initialization on the UI thread. At this point, update tasks can pass
115 // |weak_ptr_| around, and use it on IO to check if the instance is stil there
116 // before finalizing the update.
117 //
118 // The task will invoke InitializeOnUIThread, which sets up preference
119 // listeners. These can only start updating once |weak_ptr_| is set.
120 //
121 // Destruction must also pass through both threads. ShutdownOnUIThread must be
122 // called first, and will invalidate any pending updates (and even a pending
123 // initialize_on_ui_task_, if it hasn't executed yet). After that, no more
124 // calls will be made from the UI thread. It is then safe to call the dtor
125 // on the IO thread. Any updates will flight have a weak ptr to self, which
126 // will be invalidated and the update is discarded.
127 class URLBlacklistManager : public NotificationObserver {
128 public:
129 // Must be constructed on the UI thread.
130 explicit URLBlacklistManager(Profile* profile);
131 virtual ~URLBlacklistManager();
132
133 // Must be called on the IO thread.
134 void InitializeOnIOThread();
135
136 // Must be called on the UI thread, before destruction.
137 void ShutdownOnUIThread();
138
139 // Returns true if |url| is blocked by the current blacklist. Must be called
140 // from the IO thread.
141 bool IsURLBlocked(const GURL& url) const;
142
143 // Replaces the current blacklist. Must be called on the IO thread.
144 void SetBlacklist(Blacklist* blacklist);
145
146 // Registers the preferences related to blacklisting in the given PrefService.
147 static void RegisterPrefs(PrefService* pref_service);
148
149 protected:
150 // These are used to delay updating the blacklist while the preferences are
151 // changing, and execute only one update per simultaneous prefs changes.
152 void ScheduleUpdate();
153 virtual void PostUpdateTask(Task* task); // Virtual for testing.
154 virtual void Update(); // Virtual for testing.
155
156 private:
157 virtual void Observe(int type,
158 const NotificationSource& source,
159 const NotificationDetails& details) OVERRIDE;
160
161 // Completes initialization on the UI thread.
162 void InitializeOnUIThread();
163
164 // Used to post update tasks to the UI thread.
165 ScopedRunnableMethodFactory<URLBlacklistManager> ui_method_factory_;
166
167 // Used to get |weak_ptr_| to self on the IO thread.
168 base::WeakPtrFactory<URLBlacklistManager> io_weak_ptr_factory_;
169
170 // Holds a weak reference to self on the IO thread. This is used to enable
171 // the UI thread to posts tasks to IO that reference |this|.
172 base::WeakPtr<URLBlacklistManager> weak_ptr_;
173
174 // A task that is created on the UI thread but posted from the IO thread to
175 // resume initialization on the UI thread.
176 Task* initialize_on_ui_task_;
177
178 // Used to track the policies and update the blacklist on changes.
179 PrefChangeRegistrar pref_change_registrar_;
180 PrefService* pref_service_; // Weak.
181
182 // The current blacklist. Lives in the IO thread.
183 scoped_ptr<Blacklist> blacklist_;
184
185 DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager);
186 };
187
188 } // namespace policy
189
190 #endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_
OLDNEW
« no previous file with comments | « chrome/browser/net/chrome_network_delegate.cc ('k') | chrome/browser/policy/url_blacklist_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698