| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_SYSTEM_DISPATCHER_H_ | 5 #ifndef MOJO_SYSTEM_DISPATCHER_H_ |
| 6 #define MOJO_SYSTEM_DISPATCHER_H_ | 6 #define MOJO_SYSTEM_DISPATCHER_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "mojo/embedder/platform_handle.h" |
| 17 #include "mojo/public/c/system/core.h" | 18 #include "mojo/public/c/system/core.h" |
| 18 #include "mojo/system/system_impl_export.h" | 19 #include "mojo/system/system_impl_export.h" |
| 19 | 20 |
| 20 namespace mojo { | 21 namespace mojo { |
| 21 namespace system { | 22 namespace system { |
| 22 | 23 |
| 23 class Channel; | 24 class Channel; |
| 24 class CoreImpl; | 25 class CoreImpl; |
| 25 class Dispatcher; | 26 class Dispatcher; |
| 26 class DispatcherTransport; | 27 class DispatcherTransport; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 // |CreateEquivalentDispatcherAndCloseImplNoLock()| return that wrapper (and | 156 // |CreateEquivalentDispatcherAndCloseImplNoLock()| return that wrapper (and |
| 156 // |MessageInTransit| only holding on to such wrappers). | 157 // |MessageInTransit| only holding on to such wrappers). |
| 157 class MessageInTransitAccess { | 158 class MessageInTransitAccess { |
| 158 private: | 159 private: |
| 159 friend class MessageInTransit; | 160 friend class MessageInTransit; |
| 160 | 161 |
| 161 // Serialization API. These functions may only be called on such | 162 // Serialization API. These functions may only be called on such |
| 162 // dispatchers. (|channel| is the |Channel| to which the dispatcher is to be | 163 // dispatchers. (|channel| is the |Channel| to which the dispatcher is to be |
| 163 // serialized.) See the |Dispatcher| methods of the same names for more | 164 // serialized.) See the |Dispatcher| methods of the same names for more |
| 164 // details. | 165 // details. |
| 165 // TODO(vtl): Consider replacing this API below with a proper two-phase one | 166 static void StartSerialize(Dispatcher* dispatcher, |
| 166 // ("StartSerialize()" and "EndSerializeAndClose()", with the lock possibly | 167 Channel* channel, |
| 167 // being held across their invocations). | 168 size_t* max_size, |
| 168 static size_t GetMaximumSerializedSize(const Dispatcher* dispatcher, | 169 size_t* max_platform_handles); |
| 169 const Channel* channel); | 170 static bool EndSerializeAndClose( |
| 170 static bool SerializeAndClose(Dispatcher* dispatcher, | 171 Dispatcher* dispatcher, |
| 171 Channel* channel, | 172 Channel* channel, |
| 172 void* destination, | 173 void* destination, |
| 173 size_t* actual_size); | 174 size_t* actual_size, |
| 175 std::vector<embedder::PlatformHandle>* platform_handles); |
| 174 | 176 |
| 175 // Deserialization API. | 177 // Deserialization API. |
| 176 static scoped_refptr<Dispatcher> Deserialize(Channel* channel, | 178 static scoped_refptr<Dispatcher> Deserialize(Channel* channel, |
| 177 int32_t type, | 179 int32_t type, |
| 178 const void* source, | 180 const void* source, |
| 179 size_t size); | 181 size_t size); |
| 180 }; | 182 }; |
| 181 | 183 |
| 182 protected: | 184 protected: |
| 183 friend class base::RefCountedThreadSafe<Dispatcher>; | 185 friend class base::RefCountedThreadSafe<Dispatcher>; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 scoped_ptr<RawSharedBufferMapping>* mapping); | 234 scoped_ptr<RawSharedBufferMapping>* mapping); |
| 233 virtual MojoResult AddWaiterImplNoLock(Waiter* waiter, | 235 virtual MojoResult AddWaiterImplNoLock(Waiter* waiter, |
| 234 MojoWaitFlags flags, | 236 MojoWaitFlags flags, |
| 235 MojoResult wake_result); | 237 MojoResult wake_result); |
| 236 virtual void RemoveWaiterImplNoLock(Waiter* waiter); | 238 virtual void RemoveWaiterImplNoLock(Waiter* waiter); |
| 237 | 239 |
| 238 // These implement the API used to serialize dispatchers to a |Channel| | 240 // These implement the API used to serialize dispatchers to a |Channel| |
| 239 // (described below). They will only be called on a dispatcher that's attached | 241 // (described below). They will only be called on a dispatcher that's attached |
| 240 // to and "owned" by a |MessageInTransit|. See the non-"impl" versions for | 242 // to and "owned" by a |MessageInTransit|. See the non-"impl" versions for |
| 241 // more information. | 243 // more information. |
| 244 // |
| 245 // Note: |StartSerializeImplNoLock()| is actually called with |lock_| NOT |
| 246 // held, since the dispatcher should only be accessible to the calling thread. |
| 247 // On Debug builds, |EndSerializeAndCloseImplNoLock()| is called with |lock_| |
| 248 // held, to satisfy any |lock_.AssertAcquired()| (e.g., in |CloseImplNoLock()| |
| 249 // -- and anything it calls); disentangling those assertions is |
| 250 // difficult/fragile, and would weaken our general checking of invariants. |
| 251 // |
| 242 // TODO(vtl): Consider making these pure virtual once most things support | 252 // TODO(vtl): Consider making these pure virtual once most things support |
| 243 // being passed over a message pipe. | 253 // being passed over a message pipe. |
| 244 virtual size_t GetMaximumSerializedSizeImplNoLock( | 254 virtual void StartSerializeImplNoLock(Channel* channel, |
| 245 const Channel* channel) const; | 255 size_t* max_size, |
| 246 virtual bool SerializeAndCloseImplNoLock(Channel* channel, | 256 size_t* max_platform_handles); |
| 247 void* destination, | 257 virtual bool EndSerializeAndCloseImplNoLock( |
| 248 size_t* actual_size); | 258 Channel* channel, |
| 259 void* destination, |
| 260 size_t* actual_size, |
| 261 std::vector<embedder::PlatformHandle>* platform_handles); |
| 249 | 262 |
| 250 // Available to subclasses. (Note: Returns a non-const reference, just like | 263 // Available to subclasses. (Note: Returns a non-const reference, just like |
| 251 // |base::AutoLock|'s constructor takes a non-const reference.) | 264 // |base::AutoLock|'s constructor takes a non-const reference.) |
| 252 base::Lock& lock() const { return lock_; } | 265 base::Lock& lock() const { return lock_; } |
| 253 | 266 |
| 254 private: | 267 private: |
| 255 friend class DispatcherTransport; | 268 friend class DispatcherTransport; |
| 256 | 269 |
| 257 // This should be overridden to return true if/when there's an ongoing | 270 // This should be overridden to return true if/when there's an ongoing |
| 258 // operation (e.g., two-phase read/writes on data pipes) that should prevent a | 271 // operation (e.g., two-phase read/writes on data pipes) that should prevent a |
| (...skipping 10 matching lines...) Expand all Loading... |
| 269 // dispatcher -- and close (i.e., disable) this dispatcher. I.e., this | 282 // dispatcher -- and close (i.e., disable) this dispatcher. I.e., this |
| 270 // dispatcher will look as though it was closed, but the resource it | 283 // dispatcher will look as though it was closed, but the resource it |
| 271 // represents will be assigned to the new dispatcher. This must be called | 284 // represents will be assigned to the new dispatcher. This must be called |
| 272 // under the dispatcher's lock. | 285 // under the dispatcher's lock. |
| 273 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseNoLock(); | 286 scoped_refptr<Dispatcher> CreateEquivalentDispatcherAndCloseNoLock(); |
| 274 | 287 |
| 275 // API to serialize dispatchers to a |Channel|, exposed to only | 288 // API to serialize dispatchers to a |Channel|, exposed to only |
| 276 // |MessageInTransit| (via |MessageInTransitAccess|). They may only be called | 289 // |MessageInTransit| (via |MessageInTransitAccess|). They may only be called |
| 277 // on a dispatcher attached to a |MessageInTransit| (and in particular not in | 290 // on a dispatcher attached to a |MessageInTransit| (and in particular not in |
| 278 // |CoreImpl|'s handle table). | 291 // |CoreImpl|'s handle table). |
| 279 // Gets the maximum amount of space that'll be needed to serialize this | 292 // |
| 280 // dispatcher to the given |Channel|. This amount must be no greater than | 293 // Starts the serialization. Returns (via the two "out" parameters) the |
| 281 // |MessageInTransit::kMaxSerializedDispatcherSize| (message_in_transit.h). | 294 // maximum amount of space that may be needed to serialize this dispatcher to |
| 282 // Returns zero to indicate that this dispatcher cannot be serialized (to the | 295 // the given |Channel| (no more than |
| 283 // given |Channel|). | 296 // |MessageInTransit::kMaxSerializedDispatcherSize|) and the maximum number of |
| 284 size_t GetMaximumSerializedSize(const Channel* channel) const; | 297 // |PlatformHandle|s that may need to be attached (no more than |
| 285 // Serializes this dispatcher to the given |Channel| by writing to | 298 // |MessageInTransit::kMaxSerializedDispatcherPlatformHandles|). If this |
| 286 // |destination| and then closes this dispatcher. It may write no more than | 299 // dispatcher cannot be serialized to the given |Channel|, |*max_size| and |
| 287 // was indicated by |GetMaximumSerializedSize()|. (WARNING: Beware of races, | 300 // |*max_platform_handles| should be set to zero. A call to this method will |
| 288 // e.g., if something can be mutated between the two calls!) Returns true on | 301 // ALWAYS be followed by a call to |EndSerializeAndClose()| (even if this |
| 289 // success, in which case |*actual_size| is set to the amount it actually | 302 // dispatcher cannot be serialized to the given |Channel|). |
| 290 // wrote to |destination|. On failure, |*actual_size| should not be modified; | 303 void StartSerialize(Channel* channel, |
| 291 // however, the dispatcher will still be closed. | 304 size_t* max_size, |
| 292 bool SerializeAndClose(Channel* channel, | 305 size_t* max_platform_handles); |
| 293 void* destination, | 306 // Completes the serialization of this dispatcher to the given |Channel| and |
| 294 size_t* actual_size); | 307 // closes it. (This call will always follow an earlier call to |
| 308 // |StartSerialize()|, with the same |Channel|.) This does so by writing to |
| 309 // |destination| and appending any |PlatformHandle|s needed to |
| 310 // |platform_handles| (which may be null if no platform handles were indicated |
| 311 // to be required to |StartSerialize()|). This may write no more than the |
| 312 // amount indicated by |StartSerialize()|. (WARNING: Beware of races, e.g., if |
| 313 // something can be mutated between the two calls!) Returns true on success, |
| 314 // in which case |*actual_size| is set to the amount it actually wrote to |
| 315 // |destination|. On failure, |*actual_size| should not be modified; however, |
| 316 // the dispatcher will still be closed. |
| 317 bool EndSerializeAndClose( |
| 318 Channel* channel, |
| 319 void* destination, |
| 320 size_t* actual_size, |
| 321 std::vector<embedder::PlatformHandle>* platform_handles); |
| 295 | 322 |
| 296 // This protects the following members as well as any state added by | 323 // This protects the following members as well as any state added by |
| 297 // subclasses. | 324 // subclasses. |
| 298 mutable base::Lock lock_; | 325 mutable base::Lock lock_; |
| 299 bool is_closed_; | 326 bool is_closed_; |
| 300 | 327 |
| 301 DISALLOW_COPY_AND_ASSIGN(Dispatcher); | 328 DISALLOW_COPY_AND_ASSIGN(Dispatcher); |
| 302 }; | 329 }; |
| 303 | 330 |
| 304 // Wrapper around a |Dispatcher| pointer, while it's being processed to be | 331 // Wrapper around a |Dispatcher| pointer, while it's being processed to be |
| (...skipping 28 matching lines...) Expand all Loading... |
| 333 | 360 |
| 334 Dispatcher* dispatcher_; | 361 Dispatcher* dispatcher_; |
| 335 | 362 |
| 336 // Copy and assign allowed. | 363 // Copy and assign allowed. |
| 337 }; | 364 }; |
| 338 | 365 |
| 339 } // namespace system | 366 } // namespace system |
| 340 } // namespace mojo | 367 } // namespace mojo |
| 341 | 368 |
| 342 #endif // MOJO_SYSTEM_DISPATCHER_H_ | 369 #endif // MOJO_SYSTEM_DISPATCHER_H_ |
| OLD | NEW |