• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BROWSER_PLUGIN_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_PLUGIN_PROCESS_HOST_H_
7 
8 #include "build/build_config.h"
9 
10 #include <list>
11 #include <map>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #include "base/basictypes.h"
17 #include "base/compiler_specific.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/process/process_handle.h"
20 #include "content/common/content_export.h"
21 #include "content/public/browser/browser_child_process_host_delegate.h"
22 #include "content/public/browser/browser_child_process_host_iterator.h"
23 #include "content/public/common/process_type.h"
24 #include "content/public/common/resource_type.h"
25 #include "content/public/common/webplugininfo.h"
26 #include "ipc/ipc_channel_proxy.h"
27 #include "ui/gfx/native_widget_types.h"
28 
29 struct ResourceHostMsg_Request;
30 
31 namespace gfx {
32 class Rect;
33 }
34 
35 namespace IPC {
36 struct ChannelHandle;
37 }
38 
39 namespace net {
40 class URLRequestContext;
41 }
42 
43 namespace content {
44 class BrowserChildProcessHostImpl;
45 class ResourceContext;
46 
47 // Represents the browser side of the browser <--> plugin communication
48 // channel.  Different plugins run in their own process, but multiple instances
49 // of the same plugin run in the same process.  There will be one
50 // PluginProcessHost per plugin process, matched with a corresponding
51 // PluginProcess running in the plugin process.  The browser is responsible for
52 // starting the plugin process when a plugin is created that doesn't already
53 // have a process.  After that, most of the communication is directly between
54 // the renderer and plugin processes.
55 class CONTENT_EXPORT PluginProcessHost : public BrowserChildProcessHostDelegate,
56                                          public IPC::Sender {
57  public:
58   class Client {
59    public:
60     // Returns an opaque unique identifier for the process requesting
61     // the channel.
62     virtual int ID() = 0;
63     // Returns the resource context for the renderer requesting the channel.
64     virtual ResourceContext* GetResourceContext() = 0;
65     virtual bool OffTheRecord() = 0;
66     virtual void SetPluginInfo(const WebPluginInfo& info) = 0;
67     virtual void OnFoundPluginProcessHost(PluginProcessHost* host) = 0;
68     virtual void OnSentPluginChannelRequest() = 0;
69     // The client should delete itself when one of these methods is called.
70     virtual void OnChannelOpened(const IPC::ChannelHandle& handle) = 0;
71     virtual void OnError() = 0;
72 
73    protected:
~Client()74     virtual ~Client() {}
75   };
76 
77   PluginProcessHost();
78   virtual ~PluginProcessHost();
79 
80   // IPC::Sender implementation:
81   virtual bool Send(IPC::Message* message) OVERRIDE;
82 
83   // Initialize the new plugin process, returning true on success. This must
84   // be called before the object can be used.
85   bool Init(const WebPluginInfo& info);
86 
87   // Force the plugin process to shutdown (cleanly).
88   void ForceShutdown();
89 
90   virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
91   virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
92   virtual void OnChannelError() OVERRIDE;
93 
94   // Tells the plugin process to create a new channel for communication with a
95   // renderer.  When the plugin process responds with the channel name,
96   // OnChannelOpened in the client is called.
97   void OpenChannelToPlugin(Client* client);
98 
99   // This function is called to cancel pending requests to open new channels.
100   void CancelPendingRequest(Client* client);
101 
102   // This function is called to cancel sent requests to open new channels.
103   void CancelSentRequest(Client* client);
104 
105   // This function is called on the IO thread once we receive a reply from the
106   // modal HTML dialog (in the form of a JSON string). This function forwards
107   // that reply back to the plugin that requested the dialog.
108   void OnModalDialogResponse(const std::string& json_retval,
109                              IPC::Message* sync_result);
110 
111 #if defined(OS_MACOSX)
112   // This function is called on the IO thread when the browser becomes the
113   // active application.
114   void OnAppActivation();
115 #endif
116 
info()117   const WebPluginInfo& info() const { return info_; }
118 
119 #if defined(OS_WIN)
120   // Tracks plugin parent windows created on the browser UI thread.
121   void AddWindow(HWND window);
122 #endif
123 
124   // Given a pid of a plugin process, returns the plugin information in |info|
125   // if we know about that process. Otherwise returns false.
126   // This method can be called on any thread.
127   static bool GetWebPluginInfoFromPluginPid(base::ProcessId pid,
128                                             WebPluginInfo* info);
129 
130  private:
131   // Sends a message to the plugin process to request creation of a new channel
132   // for the given mime type.
133   void RequestPluginChannel(Client* client);
134 
135   // Message handlers.
136   void OnChannelCreated(const IPC::ChannelHandle& channel_handle);
137   void OnChannelDestroyed(int renderer_id);
138 
139 #if defined(OS_WIN)
140   void OnPluginWindowDestroyed(HWND window, HWND parent);
141 #endif
142 
143 #if defined(OS_MACOSX)
144   void OnPluginShowWindow(uint32 window_id, gfx::Rect window_rect,
145                           bool modal);
146   void OnPluginHideWindow(uint32 window_id, gfx::Rect window_rect);
147   void OnPluginSetCursorVisibility(bool visible);
148 #endif
149 
150   virtual bool CanShutdown() OVERRIDE;
151   virtual void OnProcessCrashed(int exit_code) OVERRIDE;
152 
153   void CancelRequests();
154 
155   // Callback for ResourceMessageFilter.
156   void GetContexts(const ResourceHostMsg_Request& request,
157                    ResourceContext** resource_context,
158                    net::URLRequestContext** request_context);
159 
160   // These are channel requests that we are waiting to send to the
161   // plugin process once the channel is opened.
162   std::vector<Client*> pending_requests_;
163 
164   // These are the channel requests that we have already sent to
165   // the plugin process, but haven't heard back about yet.
166   std::list<Client*> sent_requests_;
167 
168   // Information about the plugin.
169   WebPluginInfo info_;
170 
171   // The pid of the plugin process.
172   int pid_;
173 
174 #if defined(OS_WIN)
175   // Tracks plugin parent windows created on the UI thread.
176   std::set<HWND> plugin_parent_windows_set_;
177 #endif
178 #if defined(OS_MACOSX)
179   // Tracks plugin windows currently visible.
180   std::set<uint32> plugin_visible_windows_set_;
181   // Tracks full screen windows currently visible.
182   std::set<uint32> plugin_fullscreen_windows_set_;
183   // Tracks modal windows currently visible.
184   std::set<uint32> plugin_modal_windows_set_;
185   // Tracks the current visibility of the cursor.
186   bool plugin_cursor_visible_;
187 #endif
188 
189   // Map from render_process_id to its ResourceContext. Instead of storing the
190   // raw pointer, we store the struct below. This is needed because a renderer
191   // process can actually have multiple IPC channels to the same plugin process,
192   // depending on timing conditions with plugin instance creation and shutdown.
193   struct ResourceContextEntry {
194     ResourceContext* resource_context;
195     int ref_count;
196   };
197   typedef std::map<int, ResourceContextEntry> ResourceContextMap;
198   ResourceContextMap resource_context_map_;
199 
200   scoped_ptr<BrowserChildProcessHostImpl> process_;
201 
202   DISALLOW_COPY_AND_ASSIGN(PluginProcessHost);
203 };
204 
205 class PluginProcessHostIterator
206     : public BrowserChildProcessHostTypeIterator<PluginProcessHost> {
207  public:
PluginProcessHostIterator()208   PluginProcessHostIterator()
209       : BrowserChildProcessHostTypeIterator<PluginProcessHost>(
210           PROCESS_TYPE_PLUGIN) {}
211 };
212 
213 }  // namespace content
214 
215 #endif  // CONTENT_BROWSER_PLUGIN_PROCESS_HOST_H_
216