OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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_COMMON_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ | |
6 #define CHROME_COMMON_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ | |
7 | |
8 // NetworkChangeObserverProxy is a class that listens to a | |
9 // NetworkChangeNotifier on one thread (the source thread, which is | |
10 // usually the Chrome IO thread) and emits events to a target observer | |
11 // on another thread (the target thread). | |
12 // | |
13 // How to use: | |
14 // | |
15 // In the target thread, create the observer proxy: | |
16 // | |
17 // NetworkChangeNotifierThread* source_thread = ...; | |
18 // NetworkChangeObserverProxy* proxy = | |
19 // new NetworkChangeObserverProxy(source_thread, | |
20 // MessageLoop::current()); | |
21 // | |
22 // Both source_thread and its owned NetworkChangeNotifier must be | |
23 // guaranteed to outlive the target (current) thread. | |
24 // | |
25 // Then, attach the target observer: | |
26 // | |
27 // proxy->Attach(target_observer); | |
28 // | |
29 // target_observer will then begin to receive events on the target | |
30 // thread. | |
31 // | |
32 // If you call Attach(), you *must* call Detach() before releasing: | |
33 // | |
34 // proxy->Detach(); | |
35 // proxy->Release(); // omit if proxy is a scoped_refptr | |
36 // proxy = NULL; | |
37 // | |
38 // The proxy may be destroyed on either the source or the target | |
39 // thread (depending on which one ends up holding the last reference). | |
40 | |
41 #include "base/basictypes.h" | |
42 #include "base/message_loop.h" | |
43 #include "base/ref_counted.h" | |
44 #include "net/base/network_change_notifier.h" | |
45 | |
46 namespace chrome_common_net { | |
47 | |
48 class NetworkChangeNotifierThread; | |
49 | |
50 // TODO(akalin): Remove use of private inheritance. | |
51 class NetworkChangeObserverProxy | |
52 : public base::RefCountedThreadSafe<NetworkChangeObserverProxy>, | |
53 private net::NetworkChangeNotifier::Observer { | |
54 public: | |
55 // All public methods (including the constructor) must be called on | |
56 // the target thread. | |
57 | |
58 // Does not take ownership of any arguments. | |
59 NetworkChangeObserverProxy( | |
60 const NetworkChangeNotifierThread* source_thread, | |
61 MessageLoop* target_message_loop); | |
62 | |
63 // After this method is called, |target_observer| will start | |
64 // receiving events on the target thread. Once called, Detach() | |
65 // must be called before releasing and you cannot call Attach() | |
66 // again until you have done so. Does not take ownership of | |
67 // |target_observer|. | |
68 void Attach(net::NetworkChangeNotifier::Observer* target_observer); | |
69 | |
70 // After this method is called, the target observer will stop | |
71 // receiving events. You can call Attach() again after this method | |
72 // is called. | |
73 void Detach(); | |
74 | |
75 protected: | |
76 // May be called on either the source or the target thread. Marked | |
77 // protected instead of private so that this class can be subclassed | |
78 // for unit tests. | |
79 virtual ~NetworkChangeObserverProxy(); | |
80 | |
81 private: | |
82 friend class base::RefCountedThreadSafe<NetworkChangeObserverProxy>; | |
83 | |
84 // Adds ourselves as an observer of | |
85 // |source_network_change_notifier_|. Must be called on the source | |
86 // thread. | |
87 void Observe(); | |
88 | |
89 // Removes ourselves as an observer of | |
90 // |source_network_change_notifier_|. Must be called on the source | |
91 // thread. | |
92 void Unobserve(); | |
93 | |
94 // net::NetworkChangeNotifier::Observer implementation. | |
95 // | |
96 // Called on the source thread. Posts | |
97 // TargetObserverOnIPAddressChanged() on the target thread. | |
98 virtual void OnIPAddressChanged(); | |
99 | |
100 // Called on the target thread. Invokes OnIPAddressChanged() on | |
101 // |target_observer_|. | |
102 void TargetObserverOnIPAddressChanged(); | |
103 | |
104 const NetworkChangeNotifierThread* source_thread_; | |
105 MessageLoop* const target_message_loop_; | |
106 // |target_observer_| is used only by the target thread. | |
107 net::NetworkChangeNotifier::Observer* target_observer_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(NetworkChangeObserverProxy); | |
110 }; | |
111 | |
112 } // namespace chrome_common_net | |
113 | |
114 #endif // CHROME_COMMON_NET_NETWORK_CHANGE_OBSERVER_PROXY_H_ | |
OLD | NEW |