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 #include "mojo/edk/system/shared_buffer_dispatcher.h" | 5 #include "mojo/edk/system/shared_buffer_dispatcher.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "mojo/edk/embedder/platform_support.h" | 11 #include "mojo/edk/embedder/platform_support.h" |
12 #include "mojo/edk/system/channel.h" | 12 #include "mojo/edk/system/channel.h" |
13 #include "mojo/edk/system/constants.h" | 13 #include "mojo/edk/system/configuration.h" |
14 #include "mojo/edk/system/memory.h" | 14 #include "mojo/edk/system/memory.h" |
15 #include "mojo/edk/system/options_validation.h" | 15 #include "mojo/edk/system/options_validation.h" |
16 #include "mojo/public/c/system/macros.h" | 16 #include "mojo/public/c/system/macros.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace system { | 19 namespace system { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 struct SerializedSharedBufferDispatcher { | 23 struct SerializedSharedBufferDispatcher { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 } | 62 } |
63 | 63 |
64 // static | 64 // static |
65 MojoResult SharedBufferDispatcher::Create( | 65 MojoResult SharedBufferDispatcher::Create( |
66 embedder::PlatformSupport* platform_support, | 66 embedder::PlatformSupport* platform_support, |
67 const MojoCreateSharedBufferOptions& /*validated_options*/, | 67 const MojoCreateSharedBufferOptions& /*validated_options*/, |
68 uint64_t num_bytes, | 68 uint64_t num_bytes, |
69 scoped_refptr<SharedBufferDispatcher>* result) { | 69 scoped_refptr<SharedBufferDispatcher>* result) { |
70 if (!num_bytes) | 70 if (!num_bytes) |
71 return MOJO_RESULT_INVALID_ARGUMENT; | 71 return MOJO_RESULT_INVALID_ARGUMENT; |
72 if (num_bytes > kMaxSharedMemoryNumBytes) | 72 if (num_bytes > GetConfiguration().max_shared_memory_num_bytes) |
73 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 73 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
74 | 74 |
75 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 75 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
76 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); | 76 platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes))); |
77 if (!shared_buffer.get()) | 77 if (!shared_buffer.get()) |
78 return MOJO_RESULT_RESOURCE_EXHAUSTED; | 78 return MOJO_RESULT_RESOURCE_EXHAUSTED; |
79 | 79 |
80 *result = new SharedBufferDispatcher(shared_buffer); | 80 *result = new SharedBufferDispatcher(shared_buffer); |
81 return MOJO_RESULT_OK; | 81 return MOJO_RESULT_OK; |
82 } | 82 } |
83 | 83 |
84 Dispatcher::Type SharedBufferDispatcher::GetType() const { | 84 Dispatcher::Type SharedBufferDispatcher::GetType() const { |
85 return kTypeSharedBuffer; | 85 return kTypeSharedBuffer; |
86 } | 86 } |
87 | 87 |
88 // static | 88 // static |
89 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( | 89 scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize( |
90 Channel* channel, | 90 Channel* channel, |
91 const void* source, | 91 const void* source, |
92 size_t size, | 92 size_t size, |
93 embedder::PlatformHandleVector* platform_handles) { | 93 embedder::PlatformHandleVector* platform_handles) { |
94 DCHECK(channel); | 94 DCHECK(channel); |
95 | 95 |
96 if (size != sizeof(SerializedSharedBufferDispatcher)) { | 96 if (size != sizeof(SerializedSharedBufferDispatcher)) { |
97 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; | 97 LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)"; |
98 return scoped_refptr<SharedBufferDispatcher>(); | 98 return nullptr; |
99 } | 99 } |
100 | 100 |
101 const SerializedSharedBufferDispatcher* serialization = | 101 const SerializedSharedBufferDispatcher* serialization = |
102 static_cast<const SerializedSharedBufferDispatcher*>(source); | 102 static_cast<const SerializedSharedBufferDispatcher*>(source); |
103 size_t num_bytes = serialization->num_bytes; | 103 size_t num_bytes = serialization->num_bytes; |
104 size_t platform_handle_index = serialization->platform_handle_index; | 104 size_t platform_handle_index = serialization->platform_handle_index; |
105 | 105 |
106 if (!num_bytes) { | 106 if (!num_bytes) { |
107 LOG(ERROR) | 107 LOG(ERROR) |
108 << "Invalid serialized shared buffer dispatcher (invalid num_bytes)"; | 108 << "Invalid serialized shared buffer dispatcher (invalid num_bytes)"; |
109 return scoped_refptr<SharedBufferDispatcher>(); | 109 return nullptr; |
110 } | 110 } |
111 | 111 |
112 if (!platform_handles || platform_handle_index >= platform_handles->size()) { | 112 if (!platform_handles || platform_handle_index >= platform_handles->size()) { |
113 LOG(ERROR) | 113 LOG(ERROR) |
114 << "Invalid serialized shared buffer dispatcher (missing handles)"; | 114 << "Invalid serialized shared buffer dispatcher (missing handles)"; |
115 return scoped_refptr<SharedBufferDispatcher>(); | 115 return nullptr; |
116 } | 116 } |
117 | 117 |
118 // Starts off invalid, which is what we want. | 118 // Starts off invalid, which is what we want. |
119 embedder::PlatformHandle platform_handle; | 119 embedder::PlatformHandle platform_handle; |
120 // We take ownership of the handle, so we have to invalidate the one in | 120 // We take ownership of the handle, so we have to invalidate the one in |
121 // |platform_handles|. | 121 // |platform_handles|. |
122 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); | 122 std::swap(platform_handle, (*platform_handles)[platform_handle_index]); |
123 | 123 |
124 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be | 124 // Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be |
125 // closed even if creation fails. | 125 // closed even if creation fails. |
126 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( | 126 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer( |
127 channel->platform_support()->CreateSharedBufferFromHandle( | 127 channel->platform_support()->CreateSharedBufferFromHandle( |
128 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); | 128 num_bytes, embedder::ScopedPlatformHandle(platform_handle))); |
129 if (!shared_buffer.get()) { | 129 if (!shared_buffer.get()) { |
130 LOG(ERROR) | 130 LOG(ERROR) |
131 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; | 131 << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)"; |
132 return scoped_refptr<SharedBufferDispatcher>(); | 132 return nullptr; |
133 } | 133 } |
134 | 134 |
135 return scoped_refptr<SharedBufferDispatcher>( | 135 return scoped_refptr<SharedBufferDispatcher>( |
136 new SharedBufferDispatcher(shared_buffer)); | 136 new SharedBufferDispatcher(shared_buffer)); |
137 } | 137 } |
138 | 138 |
139 SharedBufferDispatcher::SharedBufferDispatcher( | 139 SharedBufferDispatcher::SharedBufferDispatcher( |
140 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer) | 140 scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer) |
141 : shared_buffer_(shared_buffer) { | 141 : shared_buffer_(shared_buffer) { |
142 DCHECK(shared_buffer_.get()); | 142 DCHECK(shared_buffer_.get()); |
(...skipping 13 matching lines...) Expand all Loading... |
156 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}; | 156 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE}; |
157 | 157 |
158 *out_options = kDefaultOptions; | 158 *out_options = kDefaultOptions; |
159 if (in_options.IsNull()) | 159 if (in_options.IsNull()) |
160 return MOJO_RESULT_OK; | 160 return MOJO_RESULT_OK; |
161 | 161 |
162 UserOptionsReader<MojoDuplicateBufferHandleOptions> reader(in_options); | 162 UserOptionsReader<MojoDuplicateBufferHandleOptions> reader(in_options); |
163 if (!reader.is_valid()) | 163 if (!reader.is_valid()) |
164 return MOJO_RESULT_INVALID_ARGUMENT; | 164 return MOJO_RESULT_INVALID_ARGUMENT; |
165 | 165 |
166 if (!OPTIONS_STRUCT_HAS_MEMBER( | 166 if (!OPTIONS_STRUCT_HAS_MEMBER(MojoDuplicateBufferHandleOptions, flags, |
167 MojoDuplicateBufferHandleOptions, flags, reader)) | 167 reader)) |
168 return MOJO_RESULT_OK; | 168 return MOJO_RESULT_OK; |
169 if ((reader.options().flags & ~kKnownFlags)) | 169 if ((reader.options().flags & ~kKnownFlags)) |
170 return MOJO_RESULT_UNIMPLEMENTED; | 170 return MOJO_RESULT_UNIMPLEMENTED; |
171 out_options->flags = reader.options().flags; | 171 out_options->flags = reader.options().flags; |
172 | 172 |
173 // Checks for fields beyond |flags|: | 173 // Checks for fields beyond |flags|: |
174 | 174 |
175 // (Nothing here yet.) | 175 // (Nothing here yet.) |
176 | 176 |
177 return MOJO_RESULT_OK; | 177 return MOJO_RESULT_OK; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 platform_handles->push_back(platform_handle.release()); | 267 platform_handles->push_back(platform_handle.release()); |
268 *actual_size = sizeof(SerializedSharedBufferDispatcher); | 268 *actual_size = sizeof(SerializedSharedBufferDispatcher); |
269 | 269 |
270 shared_buffer_ = nullptr; | 270 shared_buffer_ = nullptr; |
271 | 271 |
272 return true; | 272 return true; |
273 } | 273 } |
274 | 274 |
275 } // namespace system | 275 } // namespace system |
276 } // namespace mojo | 276 } // namespace mojo |
OLD | NEW |