Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ |
| 6 #define MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ | 6 #define MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/macros.h" | |
| 14 #include "mojo/edk/embedder/platform_handle_vector.h" | |
| 13 #include "mojo/edk/embedder/platform_shared_buffer.h" | 15 #include "mojo/edk/embedder/platform_shared_buffer.h" |
| 14 #include "mojo/edk/system/simple_dispatcher.h" | 16 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 17 #include "mojo/edk/system/dispatcher.h" | |
| 15 #include "mojo/edk/system/system_impl_export.h" | 18 #include "mojo/edk/system/system_impl_export.h" |
| 16 #include "mojo/public/cpp/system/macros.h" | |
| 17 | 19 |
| 18 namespace mojo { | 20 namespace mojo { |
| 19 | 21 |
| 20 namespace edk { | 22 namespace edk { |
| 21 class PlatformSupport; | 23 class PlatformSupport; |
| 22 | 24 |
| 23 // TODO(vtl): We derive from SimpleDispatcher, even though we don't currently | 25 class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher final : public Dispatcher { |
| 24 // have anything that's waitable. I want to add a "transferrable" wait flag | |
| 25 // (which would entail overriding |GetHandleSignalsStateImplNoLock()|, etc.). | |
| 26 class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher final | |
| 27 : public SimpleDispatcher { | |
| 28 public: | 26 public: |
| 29 // The default options to use for |MojoCreateSharedBuffer()|. (Real uses | 27 // The default options to use for |MojoCreateSharedBuffer()|. (Real uses |
| 30 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; | 28 // should obtain this via |ValidateCreateOptions()| with a null |in_options|; |
| 31 // this is exposed directly for testing convenience.) | 29 // this is exposed directly for testing convenience.) |
| 32 static const MojoCreateSharedBufferOptions kDefaultCreateOptions; | 30 static const MojoCreateSharedBufferOptions kDefaultCreateOptions; |
| 33 | 31 |
| 34 // Validates and/or sets default options for |MojoCreateSharedBufferOptions|. | 32 // Validates and/or sets default options for |MojoCreateSharedBufferOptions|. |
| 35 // If non-null, |in_options| must point to a struct of at least | 33 // If non-null, |in_options| must point to a struct of at least |
| 36 // |in_options->struct_size| bytes. |out_options| must point to a (current) | 34 // |in_options->struct_size| bytes. |out_options| must point to a (current) |
| 37 // |MojoCreateSharedBufferOptions| and will be entirely overwritten on success | 35 // |MojoCreateSharedBufferOptions| and will be entirely overwritten on success |
| 38 // (it may be partly overwritten on failure). | 36 // (it may be partly overwritten on failure). |
| 39 static MojoResult ValidateCreateOptions( | 37 static MojoResult ValidateCreateOptions( |
| 40 const MojoCreateSharedBufferOptions* in_options, | 38 const MojoCreateSharedBufferOptions* in_options, |
| 41 MojoCreateSharedBufferOptions* out_options); | 39 MojoCreateSharedBufferOptions* out_options); |
| 42 | 40 |
| 43 // Static factory method: |validated_options| must be validated (obviously). | 41 // Static factory method: |validated_options| must be validated (obviously). |
| 44 // On failure, |*result| will be left as-is. | 42 // On failure, |*result| will be left as-is. |
| 45 // TODO(vtl): This should probably be made to return a scoped_refptr and have | 43 // TODO(vtl): This should probably be made to return a scoped_refptr and have |
| 46 // a MojoResult out parameter instead. | 44 // a MojoResult out parameter instead. |
| 47 static MojoResult Create( | 45 static MojoResult Create( |
| 48 PlatformSupport* platform_support, | 46 PlatformSupport* platform_support, |
| 49 const MojoCreateSharedBufferOptions& validated_options, | 47 const MojoCreateSharedBufferOptions& validated_options, |
| 50 uint64_t num_bytes, | 48 uint64_t num_bytes, |
| 51 scoped_refptr<SharedBufferDispatcher>* result); | 49 scoped_refptr<SharedBufferDispatcher>* result); |
| 52 | 50 |
| 53 // |Dispatcher| public methods: | 51 // The "opposite" of SerializeAndClose(). Called by Dispatcher::Deserialize(). |
| 52 static scoped_refptr<SharedBufferDispatcher> Deserialize( | |
| 53 const void* bytes, | |
| 54 size_t num_bytes, | |
| 55 const ports::PortName* ports, | |
| 56 size_t num_ports, | |
| 57 PlatformHandle* platform_handles, | |
| 58 size_t num_platform_handles); | |
| 59 | |
| 60 // Dispatcher: | |
| 54 Type GetType() const override; | 61 Type GetType() const override; |
| 55 | 62 MojoResult Close() override; |
| 56 // The "opposite" of |SerializeAndClose()|. (Typically this is called by | 63 MojoResult DuplicateBufferHandle( |
| 57 // |Dispatcher::Deserialize()|.) | 64 const MojoDuplicateBufferHandleOptions* options, |
| 58 static scoped_refptr<SharedBufferDispatcher> Deserialize( | 65 scoped_refptr<Dispatcher>* new_dispatcher) override; |
| 59 const void* source, | 66 MojoResult MapBuffer( |
| 60 size_t size, | 67 uint64_t offset, |
| 61 PlatformHandleVector* platform_handles); | 68 uint64_t num_bytes, |
| 69 MojoMapBufferFlags flags, | |
| 70 scoped_ptr<PlatformSharedBufferMapping>* mapping) override; | |
| 71 void StartSerialize(uint32_t* num_bytes, | |
| 72 uint32_t* num_ports, | |
| 73 uint32_t* num_platform_handles) override; | |
| 74 bool EndSerialize(void* destination, | |
| 75 ports::PortName* ports, | |
| 76 PlatformHandle* handles) override; | |
| 77 bool BeginTransit() override; | |
| 78 void CompleteTransitAndClose() override; | |
| 79 void CancelTransit() override; | |
| 62 | 80 |
| 63 private: | 81 private: |
| 64 static scoped_refptr<SharedBufferDispatcher> CreateInternal( | 82 static scoped_refptr<SharedBufferDispatcher> CreateInternal( |
| 65 scoped_refptr<PlatformSharedBuffer> shared_buffer) { | 83 scoped_refptr<PlatformSharedBuffer> shared_buffer) { |
| 66 return make_scoped_refptr( | 84 return make_scoped_refptr( |
| 67 new SharedBufferDispatcher(std::move(shared_buffer))); | 85 new SharedBufferDispatcher(std::move(shared_buffer))); |
| 68 } | 86 } |
| 69 | 87 |
| 70 explicit SharedBufferDispatcher( | 88 explicit SharedBufferDispatcher( |
| 71 scoped_refptr<PlatformSharedBuffer> shared_buffer); | 89 scoped_refptr<PlatformSharedBuffer> shared_buffer); |
| 72 ~SharedBufferDispatcher() override; | 90 ~SharedBufferDispatcher() override; |
| 73 | 91 |
| 74 // Validates and/or sets default options for | 92 // Validates and/or sets default options for |
| 75 // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to | 93 // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to |
| 76 // a struct of at least |in_options->struct_size| bytes. |out_options| must | 94 // a struct of at least |in_options->struct_size| bytes. |out_options| must |
| 77 // point to a (current) |MojoDuplicateBufferHandleOptions| and will be | 95 // point to a (current) |MojoDuplicateBufferHandleOptions| and will be |
| 78 // entirely overwritten on success (it may be partly overwritten on failure). | 96 // entirely overwritten on success (it may be partly overwritten on failure). |
| 79 static MojoResult ValidateDuplicateOptions( | 97 static MojoResult ValidateDuplicateOptions( |
| 80 const MojoDuplicateBufferHandleOptions* in_options, | 98 const MojoDuplicateBufferHandleOptions* in_options, |
| 81 MojoDuplicateBufferHandleOptions* out_options); | 99 MojoDuplicateBufferHandleOptions* out_options); |
| 82 | 100 |
| 83 // |Dispatcher| protected methods: | 101 // Guards access to |shared_buffer_|. |
| 84 void CloseImplNoLock() override; | 102 base::Lock lock_; |
| 85 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseImplNoLock() | 103 |
| 86 override; | 104 bool in_transit_ = false; |
| 87 MojoResult DuplicateBufferHandleImplNoLock( | 105 |
| 88 const MojoDuplicateBufferHandleOptions* options, | 106 // We keep a copy of the buffer's platform handle during transit so we can |
| 89 scoped_refptr<Dispatcher>* new_dispatcher) override; | 107 // close it if something goes wrong. |
| 90 MojoResult MapBufferImplNoLock( | 108 ScopedPlatformHandle handle_for_transit_; |
|
Anand Mistry (off Chromium)
2016/01/28 02:26:25
Instead of keeping track, why not just pass owners
| |
| 91 uint64_t offset, | |
| 92 uint64_t num_bytes, | |
| 93 MojoMapBufferFlags flags, | |
| 94 scoped_ptr<PlatformSharedBufferMapping>* mapping) override; | |
| 95 void StartSerializeImplNoLock(size_t* max_size, | |
| 96 size_t* max_platform_handles) override; | |
| 97 bool EndSerializeAndCloseImplNoLock( | |
| 98 void* destination, | |
| 99 size_t* actual_size, | |
| 100 PlatformHandleVector* platform_handles) override; | |
| 101 | 109 |
| 102 scoped_refptr<PlatformSharedBuffer> shared_buffer_; | 110 scoped_refptr<PlatformSharedBuffer> shared_buffer_; |
| 103 | 111 |
| 104 MOJO_DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher); | 112 DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher); |
| 105 }; | 113 }; |
| 106 | 114 |
| 107 } // namespace edk | 115 } // namespace edk |
| 108 } // namespace mojo | 116 } // namespace mojo |
| 109 | 117 |
| 110 #endif // MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ | 118 #endif // MOJO_EDK_SYSTEM_SHARED_BUFFER_DISPATCHER_H_ |
| OLD | NEW |