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_CORE_HANDLE_TABLE_H_ 6 #define MOJO_CORE_HANDLE_TABLE_H_ 7 8 #include <stdint.h> 9 10 #include <vector> 11 12 #include "base/containers/hash_tables.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/macros.h" 15 #include "base/synchronization/lock.h" 16 // #include "base/trace_event/memory_dump_provider.h" 17 #include "mojo/core/dispatcher.h" 18 #include "mojo/core/system_impl_export.h" 19 #include "mojo/public/c/system/types.h" 20 21 namespace mojo { 22 namespace core { 23 24 class MOJO_SYSTEM_IMPL_EXPORT HandleTable { 25 public: 26 HandleTable(); 27 ~HandleTable(); 28 29 // HandleTable is thread-hostile. All access should be gated by GetLock(). 30 base::Lock& GetLock(); 31 32 MojoHandle AddDispatcher(scoped_refptr<Dispatcher> dispatcher); 33 34 // Inserts multiple dispatchers received from message transit, populating 35 // |handles| with their newly allocated handles. Returns |true| on success. 36 bool AddDispatchersFromTransit( 37 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers, 38 MojoHandle* handles); 39 40 scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle) const; 41 MojoResult GetAndRemoveDispatcher(MojoHandle, 42 scoped_refptr<Dispatcher>* dispatcher); 43 44 // Marks handles as busy and populates |dispatchers|. Returns MOJO_RESULT_BUSY 45 // if any of the handles are already in transit; MOJO_RESULT_INVALID_ARGUMENT 46 // if any of the handles are invalid; or MOJO_RESULT_OK if successful. 47 MojoResult BeginTransit( 48 const MojoHandle* handles, 49 size_t num_handles, 50 std::vector<Dispatcher::DispatcherInTransit>* dispatchers); 51 52 void CompleteTransitAndClose( 53 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers); 54 void CancelTransit( 55 const std::vector<Dispatcher::DispatcherInTransit>& dispatchers); 56 57 void GetActiveHandlesForTest(std::vector<MojoHandle>* handles); 58 59 private: 60 // FRIEND_TEST_ALL_PREFIXES(HandleTableTest, OnMemoryDump); 61 62 // MemoryDumpProvider implementation. 63 // bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, 64 // base::trace_event::ProcessMemoryDump* pmd) override; 65 66 struct Entry { 67 Entry(); 68 explicit Entry(scoped_refptr<Dispatcher> dispatcher); 69 Entry(const Entry& other); 70 ~Entry(); 71 72 scoped_refptr<Dispatcher> dispatcher; 73 bool busy = false; 74 }; 75 76 using HandleMap = base::hash_map<MojoHandle, Entry>; 77 78 HandleMap handles_; 79 base::Lock lock_; 80 81 uint32_t next_available_handle_ = 1; 82 83 DISALLOW_COPY_AND_ASSIGN(HandleTable); 84 }; 85 86 } // namespace core 87 } // namespace mojo 88 89 #endif // MOJO_CORE_HANDLE_TABLE_H_ 90