• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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_SHARED_BUFFER_DISPATCHER_H_
6 #define MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <utility>
12 
13 #include "base/macros.h"
14 #include "mojo/edk/embedder/platform_handle_vector.h"
15 #include "mojo/edk/embedder/platform_shared_buffer.h"
16 #include "mojo/edk/embedder/scoped_platform_handle.h"
17 #include "mojo/edk/system/dispatcher.h"
18 #include "mojo/edk/system/system_impl_export.h"
19 
20 namespace mojo {
21 
22 namespace edk {
23 class NodeController;
24 class PlatformSupport;
25 
26 class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher final : public Dispatcher {
27  public:
28   // The default options to use for |MojoCreateSharedBuffer()|. (Real uses
29   // should obtain this via |ValidateCreateOptions()| with a null |in_options|;
30   // this is exposed directly for testing convenience.)
31   static const MojoCreateSharedBufferOptions kDefaultCreateOptions;
32 
33   // Validates and/or sets default options for |MojoCreateSharedBufferOptions|.
34   // If non-null, |in_options| must point to a struct of at least
35   // |in_options->struct_size| bytes. |out_options| must point to a (current)
36   // |MojoCreateSharedBufferOptions| and will be entirely overwritten on success
37   // (it may be partly overwritten on failure).
38   static MojoResult ValidateCreateOptions(
39       const MojoCreateSharedBufferOptions* in_options,
40       MojoCreateSharedBufferOptions* out_options);
41 
42   // Static factory method: |validated_options| must be validated (obviously).
43   // On failure, |*result| will be left as-is.
44   // TODO(vtl): This should probably be made to return a scoped_refptr and have
45   // a MojoResult out parameter instead.
46   static MojoResult Create(
47       const MojoCreateSharedBufferOptions& validated_options,
48       NodeController* node_controller,
49       uint64_t num_bytes,
50       scoped_refptr<SharedBufferDispatcher>* result);
51 
52   // Create a |SharedBufferDispatcher| from |shared_buffer|.
53   static MojoResult CreateFromPlatformSharedBuffer(
54       const scoped_refptr<PlatformSharedBuffer>& shared_buffer,
55       scoped_refptr<SharedBufferDispatcher>* result);
56 
57   // The "opposite" of SerializeAndClose(). Called by Dispatcher::Deserialize().
58   static scoped_refptr<SharedBufferDispatcher> Deserialize(
59       const void* bytes,
60       size_t num_bytes,
61       const ports::PortName* ports,
62       size_t num_ports,
63       PlatformHandle* platform_handles,
64       size_t num_platform_handles);
65 
66   // Passes the underlying platform shared buffer. This dispatcher must be
67   // closed after calling this function.
68   scoped_refptr<PlatformSharedBuffer> PassPlatformSharedBuffer();
69 
70   // Dispatcher:
71   Type GetType() const override;
72   MojoResult Close() override;
73   MojoResult DuplicateBufferHandle(
74       const MojoDuplicateBufferHandleOptions* options,
75       scoped_refptr<Dispatcher>* new_dispatcher) override;
76   MojoResult MapBuffer(
77       uint64_t offset,
78       uint64_t num_bytes,
79       MojoMapBufferFlags flags,
80       std::unique_ptr<PlatformSharedBufferMapping>* mapping) override;
81   void StartSerialize(uint32_t* num_bytes,
82                       uint32_t* num_ports,
83                       uint32_t* num_platform_handles) override;
84   bool EndSerialize(void* destination,
85                     ports::PortName* ports,
86                     PlatformHandle* handles) override;
87   bool BeginTransit() override;
88   void CompleteTransitAndClose() override;
89   void CancelTransit() override;
90 
91  private:
CreateInternal(scoped_refptr<PlatformSharedBuffer> shared_buffer)92   static scoped_refptr<SharedBufferDispatcher> CreateInternal(
93       scoped_refptr<PlatformSharedBuffer> shared_buffer) {
94     return make_scoped_refptr(
95         new SharedBufferDispatcher(std::move(shared_buffer)));
96   }
97 
98   explicit SharedBufferDispatcher(
99       scoped_refptr<PlatformSharedBuffer> shared_buffer);
100   ~SharedBufferDispatcher() override;
101 
102   // Validates and/or sets default options for
103   // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to
104   // a struct of at least |in_options->struct_size| bytes. |out_options| must
105   // point to a (current) |MojoDuplicateBufferHandleOptions| and will be
106   // entirely overwritten on success (it may be partly overwritten on failure).
107   static MojoResult ValidateDuplicateOptions(
108       const MojoDuplicateBufferHandleOptions* in_options,
109       MojoDuplicateBufferHandleOptions* out_options);
110 
111   // Guards access to |shared_buffer_|.
112   base::Lock lock_;
113 
114   bool in_transit_ = false;
115 
116   // We keep a copy of the buffer's platform handle during transit so we can
117   // close it if something goes wrong.
118   ScopedPlatformHandle handle_for_transit_;
119 
120   scoped_refptr<PlatformSharedBuffer> shared_buffer_;
121 
122   DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher);
123 };
124 
125 }  // namespace edk
126 }  // namespace mojo
127 
128 #endif  // MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
129