| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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 MOJO_EDK_PLATFORM_PLATFORM_HANDLE_WATCHER_H_ |
| 6 #define MOJO_EDK_PLATFORM_PLATFORM_HANDLE_WATCHER_H_ |
| 7 |
| 8 #include <functional> |
| 9 #include <memory> |
| 10 |
| 11 #include "mojo/edk/platform/platform_handle.h" |
| 12 #include "mojo/public/cpp/system/macros.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace platform { |
| 16 |
| 17 // Interface for things that can watch |PlatformHandle|s to be readable/writable |
| 18 // without blocking. Typically, these are tied to a (particular) |MessageLoop|: |
| 19 // handles will only be watched while the loop is running and the lifetime of |
| 20 // the object is typically tied to the |MessageLoop|'s lifetime. |
| 21 class PlatformHandleWatcher { |
| 22 public: |
| 23 // Abstract "token" class returned by the |Watch()| (below). This object |
| 24 // should be destroyed to cancel watching. |
| 25 // |
| 26 // Note: In theory, libevent (e.g.) can be used without a heap allocation on |
| 27 // each "watch". However, in practice, including libevent's event.h pollutes |
| 28 // the global namespace with |struct event|, which is very undesirable, so |
| 29 // this is hidden via an indirection anyway. However, when using Chromium's |
| 30 // |base::MessagePumpLibevent|, this leads to an extra indirection and yet |
| 31 // another heap allocation. |
| 32 class WatchToken { |
| 33 public: |
| 34 virtual ~WatchToken() {} |
| 35 |
| 36 protected: |
| 37 WatchToken() {} |
| 38 |
| 39 private: |
| 40 MOJO_DISALLOW_COPY_AND_ASSIGN(WatchToken); |
| 41 }; |
| 42 |
| 43 ~PlatformHandleWatcher() {} |
| 44 |
| 45 // Watches |platform_handle| to be readable and/or writable (without blocking) |
| 46 // as indicated by the presence of |read_callback| and/or |write_callback| (at |
| 47 // least one of which must have a valid target), respectively, at which point |
| 48 // the respective callback will be called by the message loop. If |persistent| |
| 49 // is true, the message loop will continue watching and calling the |
| 50 // callback(s) as appropriate. |
| 51 virtual std::unique_ptr<WatchToken> Watch( |
| 52 PlatformHandle platform_handle, |
| 53 bool persistent, |
| 54 std::function<void()>&& read_callback, |
| 55 std::function<void()>&& write_callback) = 0; |
| 56 |
| 57 protected: |
| 58 PlatformHandleWatcher() {} |
| 59 |
| 60 private: |
| 61 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformHandleWatcher); |
| 62 }; |
| 63 |
| 64 } // namespace platform |
| 65 } // namespace mojo |
| 66 |
| 67 #endif // MOJO_EDK_PLATFORM_PLATFORM_HANDLE_WATCHER_H_ |
| OLD | NEW |