Index: chrome/browser/policy/url_blacklist_manager.h |
diff --git a/chrome/browser/policy/url_blacklist_manager.h b/chrome/browser/policy/url_blacklist_manager.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0a7aaf1299804a963ae911d45541a7306339e3ac |
--- /dev/null |
+++ b/chrome/browser/policy/url_blacklist_manager.h |
@@ -0,0 +1,124 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
+#define CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |
+#pragma once |
+ |
+#include <string> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/compiler_specific.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/weak_ptr.h" |
+#include "base/task.h" |
+#include "chrome/browser/prefs/pref_change_registrar.h" |
+#include "content/common/notification_observer.h" |
+ |
+class GURL; |
+class NotificationDetails; |
+class NotificationSource; |
+class PrefService; |
+class Profile; |
+ |
+namespace base { |
+class ListValue; |
+} |
+ |
+namespace policy { |
+ |
+class Blacklist; |
+ |
+// Tracks the blacklist policies for a given profile, and updates it on changes. |
+// |
+// This class interacts with both the UI thread, where notifications of pref |
+// changes are received from, and the IO thread, which owns it (in the |
+// ProfileIOData) and checks for blacklisted URLs (from ChromeNetworkDelegate). |
+// |
+// It must be constructed on the UI thread first, to set up |
+// |ui_method_factory_| and |initialize_on_ui_task_|. The task is used to |
+// resume initialization on the UI thread, after initialization on the IO thread |
+// is complete. |
+// |
+// InitializeOnIOThread sets up |io_weak_ptr_factory_| and |weak_ptr_|. The |
+// |weak_ptr_| is valid on the IO thread, and is used to post updates from UI. |
+// InitializeOnUIThread then posts |initialize_on_ui_task_|, which resumes |
+// initialization on the UI thread. At this point, update tasks can pass |
+// |weak_ptr_| around, and use it on IO to check if the instance is stil there |
+// before finalizing the update. |
+// |
+// The task will invoke InitializeOnUIThread, which sets up preference |
+// listeners. These can only start updating once |weak_ptr_| is set. |
+// |
+// Destruction must also pass through both threads. ShutdownOnUIThread must be |
+// called first, and will invalidate any pending updates (and even a pending |
+// initialize_on_ui_task_, if it hasn't executed yet). After that, no more |
+// calls will be made from the UI thread. It is then safe to call the dtor |
+// on the IO thread. Any updates will flight have a weak ptr to self, which |
+// will be invalidated and the update is discarded. |
+class URLBlacklistManager : public NotificationObserver { |
+ public: |
+ // Must be constructed on the UI thread. |
+ explicit URLBlacklistManager(Profile* profile); |
willchan no longer on Chromium
2011/09/01 17:05:04
Please pass in PrefService instead. It's a bit lam
Joao da Silva
2011/09/02 14:31:02
Done.
|
+ virtual ~URLBlacklistManager(); |
+ |
+ // Must be called on the IO thread. |
+ void InitializeOnIOThread(); |
+ |
+ // Must be called on the UI thread, before destruction. |
+ void ShutdownOnUIThread(); |
+ |
+ // Returns true if |url| is blocked by the current blacklist. Must be called |
+ // from the IO thread. |
+ bool IsURLBlocked(const GURL& url) const; |
+ |
+ // Replaces the current blacklist. Must be called on the IO thread. |
+ void SetBlacklist(Blacklist* blacklist); |
+ |
+ // Registers the preferences related to blacklisting in the given PrefService. |
+ static void RegisterPrefs(PrefService* pref_service); |
+ |
+ protected: |
+ // These are used to delay updating the blacklist while the preferences are |
+ // changing, and execute only one update per simultaneous prefs changes. |
+ void ScheduleUpdate(); |
+ virtual void PostUpdateTask(Task* task); // Virtual for testing. |
willchan no longer on Chromium
2011/09/01 17:05:04
Can you move this to private? I don't think it eve
Joao da Silva
2011/09/02 14:31:02
The test just overrides this and posts the task, b
|
+ virtual void Update(); // Virtual for testing. |
+ |
+ private: |
+ virtual void Observe(int type, |
+ const NotificationSource& source, |
+ const NotificationDetails& details) OVERRIDE; |
+ |
+ // Completes initialization on the UI thread. |
+ void InitializeOnUIThread(); |
+ |
+ // Used to post update tasks to the UI thread. |
+ ScopedRunnableMethodFactory<URLBlacklistManager> ui_method_factory_; |
+ |
+ // Used to get |weak_ptr_| to self on the IO thread. |
willchan no longer on Chromium
2011/09/01 17:05:04
For clarity's sake, please organize these member v
Joao da Silva
2011/09/02 14:31:02
Great idea, done.
|
+ base::WeakPtrFactory<URLBlacklistManager> io_weak_ptr_factory_; |
+ |
+ // Holds a weak reference to self on the IO thread. This is used to enable |
+ // the UI thread to posts tasks to IO that reference |this|. |
+ base::WeakPtr<URLBlacklistManager> weak_ptr_; |
+ |
+ // A task that is created on the UI thread but posted from the IO thread to |
+ // resume initialization on the UI thread. |
+ Task* initialize_on_ui_task_; |
+ |
+ // Used to track the policies and update the blacklist on changes. |
+ PrefChangeRegistrar pref_change_registrar_; |
+ PrefService* pref_service_; // Weak. |
+ |
+ // The current blacklist. Lives in the IO thread. |
+ scoped_ptr<Blacklist> blacklist_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(URLBlacklistManager); |
+}; |
+ |
+} // namespace policy |
+ |
+#endif // CHROME_BROWSER_POLICY_URL_BLACKLIST_MANAGER_H_ |