OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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 TOOLS_ANDROID_FORWARDER2_FORWARDERS_MANAGER_H_ |
| 6 #define TOOLS_ANDROID_FORWARDER2_FORWARDERS_MANAGER_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/memory/scoped_vector.h" |
| 11 #include "tools/android/forwarder2/pipe_notifier.h" |
| 12 |
| 13 namespace base { |
| 14 |
| 15 class SingleThreadTaskRunner; |
| 16 |
| 17 } // namespace base |
| 18 |
| 19 namespace forwarder2 { |
| 20 |
| 21 class Forwarder; |
| 22 class Socket; |
| 23 |
| 24 // Creates and manages lifetime of a set of Forwarder instances. This class |
| 25 // ensures in particular that the forwarder instances are deleted on their |
| 26 // construction thread no matter which thread ~ForwardersManager() is called on. |
| 27 class ForwardersManager { |
| 28 public: |
| 29 // Can be called on any thread. |
| 30 ForwardersManager(); |
| 31 ~ForwardersManager(); |
| 32 |
| 33 // Note that the (unique) thread that this method is called on must outlive |
| 34 // the ForwardersManager instance. This is needed since the Forwarder |
| 35 // instances are deleted on the thread they were constructed on. |
| 36 void CreateAndStartNewForwarder(scoped_ptr<Socket> socket1, |
| 37 scoped_ptr<Socket> socket2); |
| 38 |
| 39 private: |
| 40 // Ref-counted thread-safe delegate that can (by definition) outlive its outer |
| 41 // ForwarderManager instance. This is needed since the forwarder instances are |
| 42 // destroyed on a thread other than the one ~ForwardersManager() is called on. |
| 43 // The forwarder instances can also call OnForwarderError() below at any time |
| 44 // on their construction thread (which is the same as their destruction |
| 45 // thread). This means that a WeakPtr to ForwardersManager can't be used |
| 46 // instead of ref-counting since checking the validity of the WeakPtr on a |
| 47 // thread other than the one ~ForwardersManager() is called on would be racy. |
| 48 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 49 public: |
| 50 Delegate(); |
| 51 |
| 52 void Clear(); |
| 53 |
| 54 void CreateAndStartNewForwarder(scoped_ptr<Socket> socket1, |
| 55 scoped_ptr<Socket> socket2); |
| 56 |
| 57 private: |
| 58 friend class base::RefCountedThreadSafe<Delegate>; |
| 59 |
| 60 virtual ~Delegate(); |
| 61 |
| 62 void OnForwarderError(scoped_ptr<Forwarder> forwarder); |
| 63 |
| 64 void ClearOnForwarderConstructorThread(); |
| 65 |
| 66 scoped_refptr<base::SingleThreadTaskRunner> forwarders_constructor_runner_; |
| 67 PipeNotifier deletion_notifier_; |
| 68 ScopedVector<Forwarder> forwarders_; |
| 69 }; |
| 70 |
| 71 const scoped_refptr<Delegate> delegate_; |
| 72 }; |
| 73 |
| 74 } // namespace forwarder2 |
| 75 |
| 76 #endif // TOOLS_ANDROID_FORWARDER2_FORWARDERS_MANAGER_H_ |
OLD | NEW |