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_EMBEDDER_CHANNEL_INIT_H_ 6 #define MOJO_EMBEDDER_CHANNEL_INIT_H_ 7 8 #include "base/files/file.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/weak_ptr.h" 11 #include "mojo/public/cpp/system/core.h" 12 #include "mojo/system/system_impl_export.h" 13 14 namespace base { 15 class MessageLoopProxy; 16 class TaskRunner; 17 } 18 19 namespace mojo { 20 namespace embedder { 21 struct ChannelInfo; 22 } 23 24 namespace embedder { 25 26 // |ChannelInit| handles creation (and destruction) of the Mojo channel. It is 27 // not thread-safe, but may be used on any single thread (with a |MessageLoop|). 28 class MOJO_SYSTEM_IMPL_EXPORT ChannelInit { 29 public: 30 ChannelInit(); 31 ~ChannelInit(); 32 33 // Initializes the channel. This takes ownership of |file|. Returns the 34 // primordial MessagePipe for the channel. 35 mojo::ScopedMessagePipeHandle Init( 36 base::PlatformFile file, 37 scoped_refptr<base::TaskRunner> io_thread_task_runner); 38 39 // Notifies the channel that we (hence it) will soon be destroyed. 40 void WillDestroySoon(); 41 42 private: 43 // Invoked on the thread on which this object lives once the channel has been 44 // established. (This is a static method that takes a weak pointer to self, 45 // since we want to destroy the channel even if we're destroyed.) 46 static void OnCreatedChannel(base::WeakPtr<ChannelInit> self, 47 scoped_refptr<base::TaskRunner> io_thread, 48 ChannelInfo* channel); 49 50 scoped_refptr<base::TaskRunner> io_thread_task_runner_; 51 52 // If non-null the channel has been established. 53 ChannelInfo* channel_info_; 54 55 base::WeakPtrFactory<ChannelInit> weak_factory_; 56 57 DISALLOW_COPY_AND_ASSIGN(ChannelInit); 58 }; 59 60 } // namespace embedder 61 } // namespace mojo 62 63 #endif // MOJO_EMBEDDER_CHANNEL_INIT_H_ 64