Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1630)

Unified Diff: content/common/message_port.h

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Eliminate unnecessary PostTask Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/common/message_port.h
diff --git a/content/common/message_port.h b/content/common/message_port.h
new file mode 100644
index 0000000000000000000000000000000000000000..3e6290a1db456f98c9126d5db8b92c1ec0c1864b
--- /dev/null
+++ b/content/common/message_port.h
@@ -0,0 +1,83 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_COMMON_MESSAGE_PORT_H_
+#define CONTENT_COMMON_MESSAGE_PORT_H_
+
+#include <vector>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/ref_counted.h"
+#include "base/strings/string16.h"
+#include "content/common/content_export.h"
+#include "mojo/public/cpp/system/message_pipe.h"
+
+namespace content {
+
+// MessagePort corresponds to a HTML MessagePort. It is a thin wrapper around a
+// Mojo MessagePipeHandle and provides methods for reading and writing messages.
+//
+// TODO(darin): Make this class move-only once no longer used with Chrome IPC.
+//
+class CONTENT_EXPORT MessagePort {
+ public:
+ ~MessagePort();
+ MessagePort();
+
+ // Shallow copy, resulting in multiple references to the same port.
+ MessagePort(const MessagePort& other);
+ MessagePort& operator=(const MessagePort& other);
+
+ explicit MessagePort(mojo::ScopedMessagePipeHandle handle);
+
+ const mojo::ScopedMessagePipeHandle& GetHandle() const;
+ mojo::ScopedMessagePipeHandle ReleaseHandle() const;
+
+ static std::vector<mojo::ScopedMessagePipeHandle> ReleaseHandles(
+ const std::vector<MessagePort>& ports);
+
+ // Sends an encoded message (along with ports to transfer) to this port's
+ // peer.
+ void PostMessage(const base::string16& encoded_message,
+ std::vector<MessagePort> ports);
+
+ // Get the next available encoded message if any. Returns true if a message
+ // was read.
+ bool GetMessage(base::string16* encoded_message,
+ std::vector<MessagePort>* ports);
+
+ // This callback will be invoked on a background thread when messages are
+ // available to be read via GetMessage.
+ void SetCallback(const base::Closure& callback);
+
+ // Clears any callback specified by a prior call to SetCallback.
+ void ClearCallback();
+
+ private:
+ class State : public base::RefCountedThreadSafe<State> {
+ public:
+ State();
+ State(mojo::ScopedMessagePipeHandle handle);
+
+ void AddWatch();
+ void CancelWatch();
+ static void OnHandleReady(uintptr_t context,
+ MojoResult result,
+ MojoHandleSignalsState signals_state,
+ MojoWatchNotificationFlags flags);
+
+ mojo::ScopedMessagePipeHandle handle_;
+ base::Closure callback_;
+
+ private:
+ friend class base::RefCountedThreadSafe<State>;
+ ~State();
+ };
+ mutable scoped_refptr<State> state_;
+};
+
+} // namespace content
+
+#endif // CONTENT_COMMON_MESSAGE_PORT_H_

Powered by Google App Engine
This is Rietveld 408576698