Home | History | Annotate | Download | only in child
      1 // Copyright (c) 2012 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 CONTENT_CHILD_CHILD_THREAD_H_
      6 #define CONTENT_CHILD_CHILD_THREAD_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/shared_memory.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/power_monitor/power_monitor.h"
     15 #include "base/tracked_objects.h"
     16 #include "content/child/mojo/mojo_application.h"
     17 #include "content/common/content_export.h"
     18 #include "content/common/message_router.h"
     19 #include "ipc/ipc_message.h"  // For IPC_MESSAGE_LOG_ENABLED.
     20 
     21 namespace base {
     22 class MessageLoop;
     23 
     24 namespace debug {
     25 class TraceMemoryController;
     26 }  // namespace debug
     27 }  // namespace base
     28 
     29 namespace IPC {
     30 class SyncChannel;
     31 class SyncMessageFilter;
     32 }  // namespace IPC
     33 
     34 namespace blink {
     35 class WebFrame;
     36 }  // namespace blink
     37 
     38 namespace content {
     39 class ChildHistogramMessageFilter;
     40 class ChildResourceMessageFilter;
     41 class ChildSharedBitmapManager;
     42 class FileSystemDispatcher;
     43 class ServiceWorkerMessageFilter;
     44 class QuotaDispatcher;
     45 class QuotaMessageFilter;
     46 class ResourceDispatcher;
     47 class SocketStreamDispatcher;
     48 class ThreadSafeSender;
     49 class WebSocketDispatcher;
     50 struct RequestInfo;
     51 
     52 // The main thread of a child process derives from this class.
     53 class CONTENT_EXPORT ChildThread : public IPC::Listener, public IPC::Sender {
     54  public:
     55   struct CONTENT_EXPORT Options {
     56     Options();
     57     explicit Options(bool mojo);
     58     Options(std::string name, bool mojo)
     59         : channel_name(name), use_mojo_channel(mojo) {}
     60 
     61     std::string channel_name;
     62     bool use_mojo_channel;
     63   };
     64 
     65   // Creates the thread.
     66   ChildThread();
     67   // Used for single-process mode and for in process gpu mode.
     68   explicit ChildThread(const Options& options);
     69   // ChildProcess::main_thread() is reset after Shutdown(), and before the
     70   // destructor, so any subsystem that relies on ChildProcess::main_thread()
     71   // must be terminated before Shutdown returns. In particular, if a subsystem
     72   // has a thread that post tasks to ChildProcess::main_thread(), that thread
     73   // should be joined in Shutdown().
     74   virtual ~ChildThread();
     75   virtual void Shutdown();
     76 
     77   // IPC::Sender implementation:
     78   virtual bool Send(IPC::Message* msg) OVERRIDE;
     79 
     80   IPC::SyncChannel* channel() { return channel_.get(); }
     81 
     82   MessageRouter* GetRouter();
     83 
     84   // Allocates a block of shared memory of the given size and
     85   // maps in into the address space. Returns NULL of failure.
     86   // Note: On posix, this requires a sync IPC to the browser process,
     87   // but on windows the child process directly allocates the block.
     88   base::SharedMemory* AllocateSharedMemory(size_t buf_size);
     89 
     90   // A static variant that can be called on background threads provided
     91   // the |sender| passed in is safe to use on background threads.
     92   static base::SharedMemory* AllocateSharedMemory(size_t buf_size,
     93                                                   IPC::Sender* sender);
     94 
     95   ChildSharedBitmapManager* shared_bitmap_manager() const {
     96     return shared_bitmap_manager_.get();
     97   }
     98 
     99   ResourceDispatcher* resource_dispatcher() const {
    100     return resource_dispatcher_.get();
    101   }
    102 
    103   SocketStreamDispatcher* socket_stream_dispatcher() const {
    104     return socket_stream_dispatcher_.get();
    105   }
    106 
    107   WebSocketDispatcher* websocket_dispatcher() const {
    108     return websocket_dispatcher_.get();
    109   }
    110 
    111   FileSystemDispatcher* file_system_dispatcher() const {
    112     return file_system_dispatcher_.get();
    113   }
    114 
    115   QuotaDispatcher* quota_dispatcher() const {
    116     return quota_dispatcher_.get();
    117   }
    118 
    119   IPC::SyncMessageFilter* sync_message_filter() const {
    120     return sync_message_filter_.get();
    121   }
    122 
    123   // The getter should only be called on the main thread, however the
    124   // IPC::Sender it returns may be safely called on any thread including
    125   // the main thread.
    126   ThreadSafeSender* thread_safe_sender() const {
    127     return thread_safe_sender_.get();
    128   }
    129 
    130   ChildHistogramMessageFilter* child_histogram_message_filter() const {
    131     return histogram_message_filter_.get();
    132   }
    133 
    134   ServiceWorkerMessageFilter* service_worker_message_filter() const {
    135     return service_worker_message_filter_.get();
    136   }
    137 
    138   QuotaMessageFilter* quota_message_filter() const {
    139     return quota_message_filter_.get();
    140   }
    141 
    142   base::MessageLoop* message_loop() const { return message_loop_; }
    143 
    144   // Returns the one child thread. Can only be called on the main thread.
    145   static ChildThread* current();
    146 
    147 #if defined(OS_ANDROID)
    148   // Called on Android's service thread to shutdown the main thread of this
    149   // process.
    150   static void ShutdownThread();
    151 #endif
    152 
    153   ServiceRegistry* service_registry() const {
    154     return mojo_application_->service_registry();
    155   }
    156 
    157  protected:
    158   friend class ChildProcess;
    159 
    160   // Called when the process refcount is 0.
    161   void OnProcessFinalRelease();
    162 
    163   virtual bool OnControlMessageReceived(const IPC::Message& msg);
    164 
    165   void set_on_channel_error_called(bool on_channel_error_called) {
    166     on_channel_error_called_ = on_channel_error_called;
    167   }
    168 
    169   // IPC::Listener implementation:
    170   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
    171   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
    172   virtual void OnChannelError() OVERRIDE;
    173 
    174  private:
    175   class ChildThreadMessageRouter : public MessageRouter {
    176    public:
    177     // |sender| must outlive this object.
    178     explicit ChildThreadMessageRouter(IPC::Sender* sender);
    179     virtual bool Send(IPC::Message* msg) OVERRIDE;
    180 
    181    private:
    182     IPC::Sender* const sender_;
    183   };
    184 
    185   void Init(const Options& options);
    186   scoped_ptr<IPC::SyncChannel> CreateChannel(bool use_mojo_channel);
    187 
    188   // IPC message handlers.
    189   void OnShutdown();
    190   void OnSetProfilerStatus(tracked_objects::ThreadData::Status status);
    191   void OnGetChildProfilerData(int sequence_number);
    192   void OnDumpHandles();
    193   void OnProcessBackgrounded(bool background);
    194 #ifdef IPC_MESSAGE_LOG_ENABLED
    195   void OnSetIPCLoggingEnabled(bool enable);
    196 #endif
    197 #if defined(USE_TCMALLOC)
    198   void OnGetTcmallocStats();
    199 #endif
    200 
    201   void EnsureConnected();
    202 
    203   scoped_ptr<MojoApplication> mojo_application_;
    204 
    205   std::string channel_name_;
    206   scoped_ptr<IPC::SyncChannel> channel_;
    207 
    208   // Allows threads other than the main thread to send sync messages.
    209   scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
    210 
    211   scoped_refptr<ThreadSafeSender> thread_safe_sender_;
    212 
    213   // Implements message routing functionality to the consumers of ChildThread.
    214   ChildThreadMessageRouter router_;
    215 
    216   // Handles resource loads for this process.
    217   scoped_ptr<ResourceDispatcher> resource_dispatcher_;
    218 
    219   // Handles SocketStream for this process.
    220   scoped_ptr<SocketStreamDispatcher> socket_stream_dispatcher_;
    221 
    222   scoped_ptr<WebSocketDispatcher> websocket_dispatcher_;
    223 
    224   // The OnChannelError() callback was invoked - the channel is dead, don't
    225   // attempt to communicate.
    226   bool on_channel_error_called_;
    227 
    228   base::MessageLoop* message_loop_;
    229 
    230   scoped_ptr<FileSystemDispatcher> file_system_dispatcher_;
    231 
    232   scoped_ptr<QuotaDispatcher> quota_dispatcher_;
    233 
    234   scoped_refptr<ChildHistogramMessageFilter> histogram_message_filter_;
    235 
    236   scoped_refptr<ChildResourceMessageFilter> resource_message_filter_;
    237 
    238   scoped_refptr<ServiceWorkerMessageFilter> service_worker_message_filter_;
    239 
    240   scoped_refptr<QuotaMessageFilter> quota_message_filter_;
    241 
    242   scoped_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
    243 
    244   // Observes the trace event system. When tracing is enabled, optionally
    245   // starts profiling the tcmalloc heap.
    246   scoped_ptr<base::debug::TraceMemoryController> trace_memory_controller_;
    247 
    248   scoped_ptr<base::PowerMonitor> power_monitor_;
    249 
    250   bool in_browser_process_;
    251 
    252   base::WeakPtrFactory<ChildThread> channel_connected_factory_;
    253 
    254   DISALLOW_COPY_AND_ASSIGN(ChildThread);
    255 };
    256 
    257 }  // namespace content
    258 
    259 #endif  // CONTENT_CHILD_CHILD_THREAD_H_
    260