Home | History | Annotate | Download | only in plugin
      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_PLUGIN_PLUGIN_CHANNEL_H_
      6 #define CONTENT_PLUGIN_PLUGIN_CHANNEL_H_
      7 
      8 #include <vector>
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_handle.h"
     11 #include "base/process/process.h"
     12 #include "build/build_config.h"
     13 #include "content/child/npapi/np_channel_base.h"
     14 #include "content/child/scoped_child_process_reference.h"
     15 #include "content/plugin/webplugin_delegate_stub.h"
     16 
     17 namespace base {
     18 class WaitableEvent;
     19 }
     20 
     21 namespace content {
     22 
     23 // Encapsulates an IPC channel between the plugin process and one renderer
     24 // process.  On the renderer side there's a corresponding PluginChannelHost.
     25 class PluginChannel : public NPChannelBase {
     26  public:
     27   // Get a new PluginChannel object for the current process to talk to the
     28   // given renderer process. The renderer ID is an opaque unique ID generated
     29   // by the browser.
     30   static PluginChannel* GetPluginChannel(
     31       int renderer_id, base::MessageLoopProxy* ipc_message_loop);
     32 
     33   // Send a message to all renderers that the process is going to shutdown.
     34   static void NotifyRenderersOfPendingShutdown();
     35 
     36   // IPC::Listener:
     37   virtual bool Send(IPC::Message* msg) OVERRIDE;
     38   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     39   virtual void OnChannelError() OVERRIDE;
     40 
     41   int renderer_id() { return renderer_id_; }
     42 
     43   virtual int GenerateRouteID() OVERRIDE;
     44 
     45   // Returns the event that's set when a call to the renderer causes a modal
     46   // dialog to come up.
     47   virtual base::WaitableEvent* GetModalDialogEvent(int render_view_id) OVERRIDE;
     48 
     49   bool in_send() { return in_send_ != 0; }
     50 
     51   bool incognito() { return incognito_; }
     52   void set_incognito(bool value) { incognito_ = value; }
     53 
     54 #if defined(OS_POSIX)
     55   int TakeRendererFileDescriptor() {
     56     return channel_->TakeClientFileDescriptor();
     57   }
     58 #endif
     59 
     60  protected:
     61   virtual ~PluginChannel();
     62 
     63   // NPChannelBase::
     64   virtual void CleanUp() OVERRIDE;
     65   virtual bool Init(base::MessageLoopProxy* ipc_message_loop,
     66                     bool create_pipe_now,
     67                     base::WaitableEvent* shutdown_event) OVERRIDE;
     68 
     69  private:
     70   class MessageFilter;
     71 
     72   // Called on the plugin thread
     73   PluginChannel();
     74 
     75   virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
     76 
     77   static NPChannelBase* ClassFactory() { return new PluginChannel(); }
     78 
     79   void OnCreateInstance(const std::string& mime_type, int* instance_id);
     80   void OnDestroyInstance(int instance_id, IPC::Message* reply_msg);
     81   void OnGenerateRouteID(int* route_id);
     82   void OnClearSiteData(const std::string& site,
     83                        uint64 flags,
     84                        uint64 max_age);
     85   void OnDidAbortLoading(int render_view_id);
     86 
     87   std::vector<scoped_refptr<WebPluginDelegateStub> > plugin_stubs_;
     88 
     89   ScopedChildProcessReference process_ref_;
     90 
     91   // The id of the renderer who is on the other side of the channel.
     92   int renderer_id_;
     93 
     94   int in_send_;  // Tracks if we're in a Send call.
     95   bool log_messages_;  // True if we should log sent and received messages.
     96   bool incognito_; // True if the renderer is in incognito mode.
     97   scoped_refptr<MessageFilter> filter_;  // Handles the modal dialog events.
     98 
     99   // Dummy NPP value used in the plugin process to represent entities other
    100   // that other plugin instances for the purpose of object ownership tracking.
    101   scoped_ptr<struct _NPP> npp_;
    102 
    103   DISALLOW_COPY_AND_ASSIGN(PluginChannel);
    104 };
    105 
    106 }  // namespace content
    107 
    108 #endif  // CONTENT_PLUGIN_PLUGIN_CHANNEL_H_
    109