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_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
| 6 #define CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/task.h" |
| 14 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 15 #include "content/common/notification_observer.h" |
| 16 #include "net/base/host_port_pair.h" |
| 17 #include "net/http/http_server_properties.h" |
| 18 #include "net/http/http_server_properties_impl.h" |
| 19 |
| 20 class NotificationDetails; |
| 21 class NotificationSource; |
| 22 class PrefService; |
| 23 |
| 24 namespace base { |
| 25 class ListValue; |
| 26 } |
| 27 |
| 28 namespace chrome_browser_net { |
| 29 |
| 30 //////////////////////////////////////////////////////////////////////////////// |
| 31 // HttpServerPropertiesManager |
| 32 |
| 33 // The manager for creating and updating an HttpServerProperties (for example it |
| 34 // tracks if a server supports SPDY or not). |
| 35 // |
| 36 // This class interacts with both the UI thread, where notifications of pref |
| 37 // changes are received from, and the IO thread, which owns it (in the |
| 38 // ProfileIOData) and it persists the changes from network stack that if a |
| 39 // server supports SPDY or not. |
| 40 // |
| 41 // It must be constructed on the UI thread, to set up |ui_method_factory_| and |
| 42 // the prefs listeners. |
| 43 // |
| 44 // ShutdownOnUIThread must be called from UI before destruction, to release |
| 45 // the prefs listeners on the UI thread. This is done from ProfileIOData. |
| 46 // |
| 47 // Update tasks from the UI thread can post safely to the IO thread, since the |
| 48 // destruction order of Profile and ProfileIOData guarantees that if this |
| 49 // exists in UI, then a potential destruction on IO will come after any task |
| 50 // posted to IO from that method on UI. This is used to go through IO before |
| 51 // the actual update starts, and grab a WeakPtr. |
| 52 class HttpServerPropertiesManager |
| 53 : public net::HttpServerProperties, |
| 54 public NotificationObserver { |
| 55 public: |
| 56 // Create an instance of the HttpServerPropertiesManager. The lifetime of the |
| 57 // PrefService objects must be longer than that of the |
| 58 // HttpServerPropertiesManager object. Must be constructed on the UI thread. |
| 59 explicit HttpServerPropertiesManager(PrefService* pref_service); |
| 60 virtual ~HttpServerPropertiesManager(); |
| 61 |
| 62 // Initialize |http_server_properties_impl_| and |io_method_factory_| on IO |
| 63 // thread. It also posts a task to UI thread to get SPDY Server preferences |
| 64 // from |pref_service_|. |
| 65 void InitializeOnIOThread(); |
| 66 |
| 67 // Prepare for shutdown. Must be called on the UI thread, before destruction. |
| 68 void ShutdownOnUIThread(); |
| 69 |
| 70 // Returns true if |server| supports SPDY. Should only be called from IO |
| 71 // thread. |
| 72 virtual bool SupportsSpdy(const net::HostPortPair& server) const OVERRIDE; |
| 73 |
| 74 // Add |server| as the SPDY server which supports SPDY protocol into the |
| 75 // persisitent store. Should only be called from IO thread. |
| 76 virtual void SetSupportsSpdy(const net::HostPortPair& server, |
| 77 bool support_spdy) OVERRIDE; |
| 78 |
| 79 // Deletes all data. |
| 80 virtual void DeleteAll() OVERRIDE; |
| 81 |
| 82 // Register |prefs| SPDY preferences. |
| 83 static void RegisterPrefs(PrefService* prefs); |
| 84 |
| 85 protected: |
| 86 // These are used to delay updating the spdy servers in |
| 87 // |http_server_properties_impl_| while the preferences are changing, and |
| 88 // execute only one update per simultaneous prefs changes. |
| 89 void ScheduleUpdateCacheOnUI(); |
| 90 |
| 91 // Update spdy servers (the cached data in |http_server_properties_impl_|) |
| 92 // with data from preferences. Virtual for testing. |
| 93 virtual void UpdateCacheFromPrefs(); |
| 94 |
| 95 // Starts the |spdy_servers| update on the IO thread. Protected for testing. |
| 96 void UpdateCacheFromPrefsOnIO(StringVector* spdy_servers, bool support_spdy); |
| 97 |
| 98 // These are used to delay updating the preferences when spdy servers_ are |
| 99 // changing, and execute only one update per simultaneous spdy server changes. |
| 100 void ScheduleUpdatePrefsOnIO(); |
| 101 |
| 102 // Update spdy servers in preferences with the cached data from |
| 103 // |http_server_properties_impl_|. Virtual for testing. |
| 104 virtual void UpdatePrefsFromCache(); // Virtual for testing. |
| 105 |
| 106 // Update |prefs::kSpdyServers| preferences with |spdy_server_list| on UI |
| 107 // thread. Protected for testing. |
| 108 void SetSpdyServersInPrefsOnUI(base::ListValue* spdy_server_list); |
| 109 |
| 110 private: |
| 111 // Post the tasks with the delay. These are overridden in tests to post the |
| 112 // task without the delay. |
| 113 virtual void PostUpdateTaskOnUI(Task* task); // Virtual for testing. |
| 114 virtual void PostUpdateTaskOnIO(Task* task); // Virtual for testing. |
| 115 |
| 116 // Callback for preference changes. |
| 117 virtual void Observe(int type, |
| 118 const NotificationSource& source, |
| 119 const NotificationDetails& details); |
| 120 |
| 121 // --------- |
| 122 // UI thread |
| 123 // --------- |
| 124 |
| 125 // Used to post update tasks to the UI thread. |
| 126 ScopedRunnableMethodFactory<HttpServerPropertiesManager> ui_method_factory_; |
| 127 |
| 128 // Used to get |weak_ptr_| to self on the UI thread. |
| 129 scoped_ptr<base::WeakPtrFactory<HttpServerPropertiesManager> > |
| 130 ui_weak_ptr_factory_; |
| 131 |
| 132 base::WeakPtr<HttpServerPropertiesManager> ui_weak_ptr_; |
| 133 |
| 134 // Used to track the spdy servers changes. |
| 135 PrefChangeRegistrar pref_change_registrar_; |
| 136 PrefService* pref_service_; // Weak. |
| 137 |
| 138 // --------- |
| 139 // IO thread |
| 140 // --------- |
| 141 |
| 142 // Used to post update tasks to the IO thread. |
| 143 scoped_ptr<ScopedRunnableMethodFactory<HttpServerPropertiesManager> > |
| 144 io_method_factory_; |
| 145 |
| 146 scoped_ptr<net::HttpServerPropertiesImpl> http_server_properties_impl_; |
| 147 |
| 148 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManager); |
| 149 }; |
| 150 |
| 151 } // namespace chrome_browser_net |
| 152 |
| 153 #endif // CHROME_BROWSER_NET_HTTP_SERVER_PROPERTIES_MANAGER_H_ |
OLD | NEW |