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