• 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_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
6 #define MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
7 
8 #include "base/macros.h"
9 #include "mojo/system/raw_shared_buffer.h"
10 #include "mojo/system/simple_dispatcher.h"
11 #include "mojo/system/system_impl_export.h"
12 
13 namespace mojo {
14 namespace system {
15 
16 // TODO(vtl): We derive from SimpleDispatcher, even though we don't currently
17 // have anything that's waitable. I want to add a "transferrable" wait flag.
18 class MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher : public SimpleDispatcher {
19  public:
20   // The default options to use for |MojoCreateSharedBuffer()|. (Real uses
21   // should obtain this via |ValidateCreateOptions()| with a null |in_options|;
22   // this is exposed directly for testing convenience.)
23   static const MojoCreateSharedBufferOptions kDefaultCreateOptions;
24 
25   // Validates and/or sets default options for |MojoCreateSharedBufferOptions|.
26   // If non-null, |in_options| must point to a struct of at least
27   // |in_options->struct_size| bytes. |out_options| must point to a (current)
28   // |MojoCreateSharedBufferOptions| and will be entirely overwritten on success
29   // (it may be partly overwritten on failure).
30   static MojoResult ValidateCreateOptions(
31       const MojoCreateSharedBufferOptions* in_options,
32       MojoCreateSharedBufferOptions* out_options);
33 
34   // Static factory method: |validated_options| must be validated (obviously).
35   // On failure, |*result| will be left as-is.
36   static MojoResult Create(
37       const MojoCreateSharedBufferOptions& validated_options,
38       uint64_t num_bytes,
39       scoped_refptr<SharedBufferDispatcher>* result);
40 
41   // |Dispatcher| public methods:
42   virtual Type GetType() const OVERRIDE;
43 
44   // The "opposite" of |SerializeAndClose()|. (Typically this is called by
45   // |Dispatcher::Deserialize()|.)
46   static scoped_refptr<SharedBufferDispatcher> Deserialize(
47       Channel* channel,
48       const void* source,
49       size_t size,
50       embedder::PlatformHandleVector* platform_handles);
51 
52  private:
53   explicit SharedBufferDispatcher(
54       scoped_refptr<RawSharedBuffer> shared_buffer_);
55   virtual ~SharedBufferDispatcher();
56 
57   // Validates and/or sets default options for
58   // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to
59   // a struct of at least |in_options->struct_size| bytes. |out_options| must
60   // point to a (current) |MojoDuplicateBufferHandleOptions| and will be
61   // entirely overwritten on success (it may be partly overwritten on failure).
62   static MojoResult ValidateDuplicateOptions(
63       const MojoDuplicateBufferHandleOptions* in_options,
64       MojoDuplicateBufferHandleOptions* out_options);
65 
66   // |Dispatcher| protected methods:
67   virtual void CloseImplNoLock() OVERRIDE;
68   virtual scoped_refptr<Dispatcher>
69       CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE;
70   virtual MojoResult DuplicateBufferHandleImplNoLock(
71       const MojoDuplicateBufferHandleOptions* options,
72       scoped_refptr<Dispatcher>* new_dispatcher) OVERRIDE;
73   virtual MojoResult MapBufferImplNoLock(
74       uint64_t offset,
75       uint64_t num_bytes,
76       MojoMapBufferFlags flags,
77       scoped_ptr<RawSharedBufferMapping>* mapping) OVERRIDE;
78   virtual void StartSerializeImplNoLock(Channel* channel,
79                                         size_t* max_size,
80                                         size_t* max_platform_handles) OVERRIDE;
81   virtual bool EndSerializeAndCloseImplNoLock(
82       Channel* channel,
83       void* destination,
84       size_t* actual_size,
85       embedder::PlatformHandleVector* platform_handles) OVERRIDE;
86 
87   // |SimpleDispatcher| method:
88   virtual HandleSignalsState GetHandleSignalsStateNoLock() const OVERRIDE;
89 
90   scoped_refptr<RawSharedBuffer> shared_buffer_;
91 
92   DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher);
93 };
94 
95 }  // namespace system
96 }  // namespace mojo
97 
98 #endif  // MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
99