OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 CONTENT_COMMON_MESSAGE_PORT_H_ |
| 6 #define CONTENT_COMMON_MESSAGE_PORT_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/callback.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/strings/string16.h" |
| 14 #include "content/common/content_export.h" |
| 15 #include "mojo/public/cpp/system/message_pipe.h" |
| 16 |
| 17 namespace content { |
| 18 |
| 19 // MessagePort corresponds to a HTML MessagePort. It is a thin wrapper around a |
| 20 // Mojo MessagePipeHandle and provides methods for reading and writing messages. |
| 21 // |
| 22 // A MessagePort is only actively listening for incoming messages once |
| 23 // SetCallback has been called with a valid callback. If ClearCallback is |
| 24 // called (or if SetCallback is called with a null callback), then the |
| 25 // MessagePort will stop listening for incoming messages. The callback runs on |
| 26 // an unspecified background thread. |
| 27 // |
| 28 // Upon destruction, if the MessagePort is listening for incoming messages, |
| 29 // then the destructor will first synchronize with the background thread, |
| 30 // waiting for it to finish any in-process callback before closing the |
| 31 // underlying MessagePipeHandle. This synchronization ensures that any code |
| 32 // running in the callback can be sure to not worry about the MessagePort |
| 33 // becoming invalid during callback execution. |
| 34 // |
| 35 // MessagePort methods may be used from any thread; however, care must be taken |
| 36 // when using ReleaseHandle, ReleaseHandles or when destroying a MessagePort |
| 37 // instance. The MessagePort class does not synchronize those methods with |
| 38 // methods like PostMessage, GetMessage and SetCallback that use the underlying |
| 39 // MessagePipeHandle. |
| 40 // |
| 41 // TODO(darin): Make this class move-only once no longer used with Chrome IPC. |
| 42 // |
| 43 class CONTENT_EXPORT MessagePort { |
| 44 public: |
| 45 ~MessagePort(); |
| 46 MessagePort(); |
| 47 |
| 48 // Shallow copy, resulting in multiple references to the same port. |
| 49 MessagePort(const MessagePort& other); |
| 50 MessagePort& operator=(const MessagePort& other); |
| 51 |
| 52 explicit MessagePort(mojo::ScopedMessagePipeHandle handle); |
| 53 |
| 54 const mojo::ScopedMessagePipeHandle& GetHandle() const; |
| 55 mojo::ScopedMessagePipeHandle ReleaseHandle() const; |
| 56 |
| 57 static std::vector<mojo::ScopedMessagePipeHandle> ReleaseHandles( |
| 58 const std::vector<MessagePort>& ports); |
| 59 |
| 60 // Sends an encoded message (along with ports to transfer) to this port's |
| 61 // peer. |
| 62 void PostMessage(const base::string16& encoded_message, |
| 63 std::vector<MessagePort> ports); |
| 64 |
| 65 // Get the next available encoded message if any. Returns true if a message |
| 66 // was read. |
| 67 bool GetMessage(base::string16* encoded_message, |
| 68 std::vector<MessagePort>* ports); |
| 69 |
| 70 // This callback will be invoked on a background thread when messages are |
| 71 // available to be read via GetMessage. |
| 72 void SetCallback(const base::Closure& callback); |
| 73 |
| 74 // Clears any callback specified by a prior call to SetCallback. |
| 75 void ClearCallback(); |
| 76 |
| 77 private: |
| 78 class State : public base::RefCountedThreadSafe<State> { |
| 79 public: |
| 80 State(); |
| 81 State(mojo::ScopedMessagePipeHandle handle); |
| 82 |
| 83 void AddWatch(); |
| 84 void CancelWatch(); |
| 85 static void OnHandleReady(uintptr_t context, |
| 86 MojoResult result, |
| 87 MojoHandleSignalsState signals_state, |
| 88 MojoWatchNotificationFlags flags); |
| 89 |
| 90 mojo::ScopedMessagePipeHandle handle_; |
| 91 base::Closure callback_; |
| 92 |
| 93 private: |
| 94 friend class base::RefCountedThreadSafe<State>; |
| 95 ~State(); |
| 96 }; |
| 97 mutable scoped_refptr<State> state_; |
| 98 }; |
| 99 |
| 100 } // namespace content |
| 101 |
| 102 #endif // CONTENT_COMMON_MESSAGE_PORT_H_ |
OLD | NEW |