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