1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// found in the LICENSE file.
4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "base/macros.h"
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/embedder/platform_shared_buffer.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "mojo/system/memory.h"
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "mojo/system/simple_dispatcher.h"
12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "mojo/system/system_impl_export.h"
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace mojo {
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace embedder {
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class PlatformSupport;
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace system {
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
22effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// TODO(vtl): We derive from SimpleDispatcher, even though we don't currently
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// have anything that's waitable. I want to add a "transferrable" wait flag
245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// (which would entail overriding |GetHandleSignalsStateImplNoLock()|, etc.).
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass MOJO_SYSTEM_IMPL_EXPORT SharedBufferDispatcher : public SimpleDispatcher {
26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch public:
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // The default options to use for |MojoCreateSharedBuffer()|. (Real uses
28f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // should obtain this via |ValidateCreateOptions()| with a null |in_options|;
29f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // this is exposed directly for testing convenience.)
30f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static const MojoCreateSharedBufferOptions kDefaultCreateOptions;
31f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Validates and/or sets default options for |MojoCreateSharedBufferOptions|.
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // If non-null, |in_options| must point to a struct of at least
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // |in_options->struct_size| bytes. |out_options| must point to a (current)
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // |MojoCreateSharedBufferOptions| and will be entirely overwritten on success
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // (it may be partly overwritten on failure).
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static MojoResult ValidateCreateOptions(
385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      UserPointer<const MojoCreateSharedBufferOptions> in_options,
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      MojoCreateSharedBufferOptions* out_options);
40effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
41effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Static factory method: |validated_options| must be validated (obviously).
42effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // On failure, |*result| will be left as-is.
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  static MojoResult Create(
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      embedder::PlatformSupport* platform_support,
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      const MojoCreateSharedBufferOptions& validated_options,
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      uint64_t num_bytes,
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      scoped_refptr<SharedBufferDispatcher>* result);
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |Dispatcher| public methods:
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual Type GetType() const OVERRIDE;
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The "opposite" of |SerializeAndClose()|. (Typically this is called by
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // |Dispatcher::Deserialize()|.)
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  static scoped_refptr<SharedBufferDispatcher> Deserialize(
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      Channel* channel,
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const void* source,
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      size_t size,
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      embedder::PlatformHandleVector* platform_handles);
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch private:
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  explicit SharedBufferDispatcher(
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer_);
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual ~SharedBufferDispatcher();
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
6546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Validates and/or sets default options for
6646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // |MojoDuplicateBufferHandleOptions|. If non-null, |in_options| must point to
6746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // a struct of at least |in_options->struct_size| bytes. |out_options| must
6846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // point to a (current) |MojoDuplicateBufferHandleOptions| and will be
6946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // entirely overwritten on success (it may be partly overwritten on failure).
7046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  static MojoResult ValidateDuplicateOptions(
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      UserPointer<const MojoDuplicateBufferHandleOptions> in_options,
7246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      MojoDuplicateBufferHandleOptions* out_options);
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // |Dispatcher| protected methods:
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual void CloseImplNoLock() OVERRIDE;
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual scoped_refptr<Dispatcher>
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      CreateEquivalentDispatcherAndCloseImplNoLock() OVERRIDE;
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual MojoResult DuplicateBufferHandleImplNoLock(
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      UserPointer<const MojoDuplicateBufferHandleOptions> options,
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      scoped_refptr<Dispatcher>* new_dispatcher) OVERRIDE;
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual MojoResult MapBufferImplNoLock(
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      uint64_t offset,
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      uint64_t num_bytes,
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      MojoMapBufferFlags flags,
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) OVERRIDE;
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void StartSerializeImplNoLock(Channel* channel,
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        size_t* max_size,
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                        size_t* max_platform_handles) OVERRIDE;
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool EndSerializeAndCloseImplNoLock(
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      Channel* channel,
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      void* destination,
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      size_t* actual_size,
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      embedder::PlatformHandleVector* platform_handles) OVERRIDE;
94effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer_;
96effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
97effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  DISALLOW_COPY_AND_ASSIGN(SharedBufferDispatcher);
98effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch};
99effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
100effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // namespace system
101effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}  // namespace mojo
102effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
103effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif  // MOJO_SYSTEM_SHARED_BUFFER_DISPATCHER_H_
104