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