blob: 945aa073eeac38f18ada48a608fa73ca4597df3c [file] [log] [blame]
jame76324e2015-10-03 06:01:28 +09001// 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 MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_
6#define MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_
7
Avi Drissman20b0cb02015-12-22 03:14:57 +09008#include <stddef.h>
9#include <stdint.h>
10
rockotc0195992016-01-27 04:23:21 +090011#include "base/macros.h"
jame76324e2015-10-03 06:01:28 +090012#include "base/memory/ref_counted.h"
rockotc0195992016-01-27 04:23:21 +090013#include "base/memory/scoped_ptr.h"
14#include "base/synchronization/lock.h"
15#include "mojo/edk/embedder/platform_handle_vector.h"
16#include "mojo/edk/embedder/platform_shared_buffer.h"
17#include "mojo/edk/embedder/scoped_platform_handle.h"
jame76324e2015-10-03 06:01:28 +090018#include "mojo/edk/system/awakable_list.h"
19#include "mojo/edk/system/dispatcher.h"
rockotc0195992016-01-27 04:23:21 +090020#include "mojo/edk/system/ports/port_ref.h"
jame76324e2015-10-03 06:01:28 +090021#include "mojo/edk/system/system_impl_export.h"
jame76324e2015-10-03 06:01:28 +090022
23namespace mojo {
24namespace edk {
25
rockotc0195992016-01-27 04:23:21 +090026struct DataPipeControlMessage;
27class NodeController;
28
29// This is the Dispatcher implementation for the consumer handle for data
30// pipes created by the Mojo primitive MojoCreateDataPipe(). This class is
jame76324e2015-10-03 06:01:28 +090031// thread-safe.
32class MOJO_SYSTEM_IMPL_EXPORT DataPipeConsumerDispatcher final
rockotc0195992016-01-27 04:23:21 +090033 : public Dispatcher {
jame76324e2015-10-03 06:01:28 +090034 public:
rockotc0195992016-01-27 04:23:21 +090035 DataPipeConsumerDispatcher(
36 NodeController* node_controller,
37 const ports::PortRef& control_port,
38 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer,
39 const MojoCreateDataPipeOptions& options,
40 bool initialized,
41 uint64_t pipe_id);
jame76324e2015-10-03 06:01:28 +090042
rockotc0195992016-01-27 04:23:21 +090043 // Dispatcher:
jame76324e2015-10-03 06:01:28 +090044 Type GetType() const override;
rockotc0195992016-01-27 04:23:21 +090045 MojoResult Close() override;
rockot4b472712016-03-02 12:46:37 +090046 MojoResult Watch(MojoHandleSignals signals,
47 const Watcher::WatchCallback& callback,
48 uintptr_t context) override;
49 MojoResult CancelWatch(uintptr_t context) override;
rockotc0195992016-01-27 04:23:21 +090050 MojoResult ReadData(void* elements,
51 uint32_t* num_bytes,
52 MojoReadDataFlags flags) override;
53 MojoResult BeginReadData(const void** buffer,
54 uint32_t* buffer_num_bytes,
55 MojoReadDataFlags flags) override;
56 MojoResult EndReadData(uint32_t num_bytes_read) override;
57 HandleSignalsState GetHandleSignalsState() const override;
58 MojoResult AddAwakable(Awakable* awakable,
59 MojoHandleSignals signals,
60 uintptr_t context,
61 HandleSignalsState* signals_state) override;
62 void RemoveAwakable(Awakable* awakable,
63 HandleSignalsState* signals_state) override;
64 void StartSerialize(uint32_t* num_bytes,
65 uint32_t* num_ports,
66 uint32_t* num_handles) override;
67 bool EndSerialize(void* destination,
68 ports::PortName* ports,
69 PlatformHandle* handles) override;
70 bool BeginTransit() override;
71 void CompleteTransitAndClose() override;
72 void CancelTransit() override;
jame76324e2015-10-03 06:01:28 +090073
jame76324e2015-10-03 06:01:28 +090074 static scoped_refptr<DataPipeConsumerDispatcher>
rockotc0195992016-01-27 04:23:21 +090075 Deserialize(const void* data,
76 size_t num_bytes,
77 const ports::PortName* ports,
78 size_t num_ports,
79 PlatformHandle* handles,
80 size_t num_handles);
jame76324e2015-10-03 06:01:28 +090081
82 private:
rockotc0195992016-01-27 04:23:21 +090083 class PortObserverThunk;
84 friend class PortObserverThunk;
85
jame76324e2015-10-03 06:01:28 +090086 ~DataPipeConsumerDispatcher() override;
87
rockotc0195992016-01-27 04:23:21 +090088 void InitializeNoLock();
89 MojoResult CloseNoLock();
90 HandleSignalsState GetHandleSignalsStateNoLock() const;
91 void NotifyRead(uint32_t num_bytes);
92 void OnPortStatusChanged();
93 void UpdateSignalsStateNoLock();
jame76324e2015-10-03 06:01:28 +090094
rockotc0195992016-01-27 04:23:21 +090095 const MojoCreateDataPipeOptions options_;
96 NodeController* const node_controller_;
97 const ports::PortRef control_port_;
98 const uint64_t pipe_id_;
jame76324e2015-10-03 06:01:28 +090099
rockotc0195992016-01-27 04:23:21 +0900100 // Guards access to the fields below.
101 mutable base::Lock lock_;
jame76324e2015-10-03 06:01:28 +0900102
jame76324e2015-10-03 06:01:28 +0900103 AwakableList awakable_list_;
104
rockotc0195992016-01-27 04:23:21 +0900105 scoped_refptr<PlatformSharedBuffer> shared_ring_buffer_;
106 scoped_ptr<PlatformSharedBufferMapping> ring_buffer_mapping_;
107 ScopedPlatformHandle buffer_handle_for_transit_;
jame76324e2015-10-03 06:01:28 +0900108
rockotc0195992016-01-27 04:23:21 +0900109 bool in_two_phase_read_ = false;
110 uint32_t two_phase_max_bytes_read_ = 0;
jame76324e2015-10-03 06:01:28 +0900111
rockotc0195992016-01-27 04:23:21 +0900112 bool in_transit_ = false;
113 bool is_closed_ = false;
114 bool peer_closed_ = false;
115 bool transferred_ = false;
jame76324e2015-10-03 06:01:28 +0900116
rockotc0195992016-01-27 04:23:21 +0900117 uint32_t read_offset_ = 0;
118 uint32_t bytes_available_ = 0;
jame76324e2015-10-03 06:01:28 +0900119
rockotc0195992016-01-27 04:23:21 +0900120 DISALLOW_COPY_AND_ASSIGN(DataPipeConsumerDispatcher);
jame76324e2015-10-03 06:01:28 +0900121};
122
123} // namespace edk
124} // namespace mojo
125
126#endif // MOJO_EDK_SYSTEM_DATA_PIPE_CONSUMER_DISPATCHER_H_