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_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
6#define MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
7
8#include "base/macros.h"
9#include "mojo/embedder/platform_shared_buffer.h"
10#include "mojo/system/memory.h"
11#include "mojo/system/simple_dispatcher.h"
12#include "mojo/system/system_impl_export.h"
13
14namespace mojo {
15
16namespace embedder {
17class PlatformSupport;
18}
19
20namespace system {
21
22// TODO(vtl): We derive from SimpleDispatcher, even though we don't currently
23// have anything that's waitable. I want to add a "transferrable" wait flag
24// (which would entail overriding |GetHandleSignalsStateImplNoLock()|, etc.).
25class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher : public SimpleDispatcher {
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      UserPointer<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  static MojoResult Create(
44      embedder::PlatformSupport* platform_support,
45      const MojoCreateSharedBufferOptions& validated_options,
46      uint64_t num_bytes,
47      scoped_refptr<SharedBufferDispatcher>* result);
48
49  // |Dispatcher| public methods:
50  virtual Type GetType() const OVERRIDE;
51
52  // The "opposite" of |SerializeAndClose()|. (Typically this is called by
53  // |Dispatcher::Deserialize()|.)
54  static scoped_refptr<SharedBufferDispatcher> Deserialize(
55      Channel* channel,
56      const void* source,
57      size_t size,
58      embedder::PlatformHandleVector* platform_handles);
59
60 private:
61  explicit SharedBufferDispatcher(
62      scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer_);
63  virtual ~SharedBufferDispatcher();
64
65  // Validates and/or sets default options for
66  // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to
67  // a struct of at least |in_options->struct_size| bytes. |out_options| must
68  // point to a (current) |MojoDuplicateBufferHandleOptions| and will be
69  // entirely overwritten on success (it may be partly overwritten on failure).
70  static MojoResult ValidateDuplicateOptions(
71      UserPointer<const MojoDuplicateBufferHandleOptions> in_options,
72      MojoDuplicateBufferHandleOptions* out_options);
73
74  // |Dispatcher| protected methods:
75  virtual void CloseImplNoLock() OVERRIDE;
76  virtual scoped_refptr<Dispatcher>
77      CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE;
78  virtual MojoResult DuplicateBufferHandleImplNoLock(
79      UserPointer<const MojoDuplicateBufferHandleOptions> options,
80      scoped_refptr<Dispatcher>* new_dispatcher) OVERRIDE;
81  virtual MojoResult MapBufferImplNoLock(
82      uint64_t offset,
83      uint64_t num_bytes,
84      MojoMapBufferFlags flags,
85      scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) OVERRIDE;
86  virtual void StartSerializeImplNoLock(Channel* channel,
87                                        size_t* max_size,
88                                        size_t* max_platform_handles) OVERRIDE;
89  virtual bool EndSerializeAndCloseImplNoLock(
90      Channel* channel,
91      void* destination,
92      size_t* actual_size,
93      embedder::PlatformHandleVector* platform_handles) OVERRIDE;
94
95  scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer_;
96
97  DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher);
98};
99
100}  // namespace system
101}  // namespace mojo
102
103#endif  // MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
104