| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 public: | 42 public: |
| 43 class NetworkStateObserver { | 43 class NetworkStateObserver { |
| 44 public: | 44 public: |
| 45 // Will be called on the thread of the context passed in addObserver. | 45 // Will be called on the thread of the context passed in addObserver. |
| 46 virtual void connectionTypeChange(blink::WebConnectionType) = 0; | 46 virtual void connectionTypeChange(blink::WebConnectionType) = 0; |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 NetworkStateNotifier() | 49 NetworkStateNotifier() |
| 50 : m_isOnLine(true) | 50 : m_isOnLine(true) |
| 51 , m_type(blink::ConnectionTypeOther) | 51 , m_type(blink::ConnectionTypeOther) |
| 52 , m_testUpdatesOnly(false) |
| 52 { | 53 { |
| 53 } | 54 } |
| 54 | 55 |
| 55 bool onLine() const | 56 bool onLine() const |
| 56 { | 57 { |
| 57 MutexLocker locker(m_mutex); | 58 MutexLocker locker(m_mutex); |
| 58 return m_isOnLine; | 59 return m_isOnLine; |
| 59 } | 60 } |
| 60 | 61 |
| 61 void setOnLine(bool); | 62 void setOnLine(bool); |
| 62 | 63 |
| 63 blink::WebConnectionType connectionType() const | 64 blink::WebConnectionType connectionType() const |
| 64 { | 65 { |
| 65 MutexLocker locker(m_mutex); | 66 MutexLocker locker(m_mutex); |
| 66 return m_type; | 67 return m_type; |
| 67 } | 68 } |
| 68 | 69 |
| 69 void setWebConnectionType(blink::WebConnectionType); | 70 void setWebConnectionType(blink::WebConnectionType); |
| 70 | 71 |
| 71 // Must be called on the context's thread. An added observer must be | 72 // Must be called on the context's thread. An added observer must be |
| 72 // removed before its ExecutionContext is deleted. | 73 // removed before its ExecutionContext is deleted. |
| 73 void addObserver(NetworkStateObserver*, ExecutionContext*); | 74 void addObserver(NetworkStateObserver*, ExecutionContext*); |
| 74 void removeObserver(NetworkStateObserver*, ExecutionContext*); | 75 void removeObserver(NetworkStateObserver*, ExecutionContext*); |
| 75 | 76 |
| 77 // The following functions are for testing purposes. |
| 78 |
| 79 // When true, setWebConnectionType calls are ignored and only setWebConnecti
onTypeForTest |
| 80 // can update the connection type. This is used for layout tests (see crbug.
com/377736). |
| 81 void setTestUpdatesOnly(bool); |
| 82 // Tests should call this as it will change the type regardless of the value
of m_testUpdatesOnly. |
| 83 void setWebConnectionTypeForTest(blink::WebConnectionType); |
| 84 |
| 76 private: | 85 private: |
| 77 struct ObserverList { | 86 struct ObserverList { |
| 78 ObserverList() | 87 ObserverList() |
| 79 : iterating(false) | 88 : iterating(false) |
| 80 { | 89 { |
| 81 } | 90 } |
| 82 bool iterating; | 91 bool iterating; |
| 83 Vector<NetworkStateObserver*> observers; | 92 Vector<NetworkStateObserver*> observers; |
| 84 Vector<size_t> zeroedObservers; // Indices in observers that are 0. | 93 Vector<size_t> zeroedObservers; // Indices in observers that are 0. |
| 85 }; | 94 }; |
| 86 | 95 |
| 96 void setWebConnectionTypeImpl(blink::WebConnectionType); |
| 97 |
| 87 typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; | 98 typedef HashMap<ExecutionContext*, OwnPtr<ObserverList> > ObserverListMap; |
| 88 | 99 |
| 89 void notifyObserversOnContext(ExecutionContext*, blink::WebConnectionType); | 100 void notifyObserversOnContext(ExecutionContext*, blink::WebConnectionType); |
| 90 | 101 |
| 91 ObserverList* lockAndFindObserverList(ExecutionContext*); | 102 ObserverList* lockAndFindObserverList(ExecutionContext*); |
| 92 | 103 |
| 93 // Removed observers are nulled out in the list in case the list is being | 104 // Removed observers are nulled out in the list in case the list is being |
| 94 // iterated over. Once done iterating, call this to clean up nulled | 105 // iterated over. Once done iterating, call this to clean up nulled |
| 95 // observers. | 106 // observers. |
| 96 void collectZeroedObservers(ObserverList*, ExecutionContext*); | 107 void collectZeroedObservers(ObserverList*, ExecutionContext*); |
| 97 | 108 |
| 98 mutable Mutex m_mutex; | 109 mutable Mutex m_mutex; |
| 99 bool m_isOnLine; | 110 bool m_isOnLine; |
| 100 blink::WebConnectionType m_type; | 111 blink::WebConnectionType m_type; |
| 101 ObserverListMap m_observers; | 112 ObserverListMap m_observers; |
| 113 bool m_testUpdatesOnly; |
| 102 }; | 114 }; |
| 103 | 115 |
| 104 NetworkStateNotifier& networkStateNotifier(); | 116 NetworkStateNotifier& networkStateNotifier(); |
| 105 | 117 |
| 106 } // namespace WebCore | 118 } // namespace WebCore |
| 107 | 119 |
| 108 #endif // NetworkStateNotifier_h | 120 #endif // NetworkStateNotifier_h |
| OLD | NEW |