Skip to content

Commit

Permalink
linux plugins: eliminate GtkWidget* from plugin process
Browse files Browse the repository at this point in the history
GtkWidget* (and its wrapper, gfx::NativeView) only work within a
single process.  Plugins already work with a lower-level type that
works across processes -- an X Window id.  The parent of a plugin
is an HWND on windows, but it's an X Window id on X.  So we
introduce a new typedef wrapping that and push it through all the
places in the code that needs it.

Since we no longer have a GtkSocket in the WebPluginDelegateImpl,
we need to do a bit more work in the WebViewDelegate (aka the
browser process in the multiproc world).

We also need the plugin host (the browser) to track the
GtkSockets that are hosting the plugin, as well as destroy them
when necessary.  To do this we require the plugin owner to
grab the plug-removed signal rather than putting its handler in
GtkPluginContainer; this is the way it worked before I'd refactored
into GtkPluginContainer so it shouldn't be so bad.

This change still only works in test_shell, but the refactoring
should translate to the multiprocess case pretty easily.

Review URL: http://codereview.chromium.org/146009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@19320 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
evan@chromium.org committed Jun 26, 2009
1 parent 5f1b0d6 commit 3b33faa
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 87 deletions.
13 changes: 13 additions & 0 deletions base/gfx/native_widget_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ static inline NativeViewId IdFromNativeView(NativeView view) {
NativeViewId IdFromNativeView(NativeView view);
#endif // defined(OS_LINUX)


// PluginWindowHandle is an abstraction wrapping "the types of windows
// used by NPAPI plugins". On Windows it's an HWND, on X it's an X
// window id.
#if defined(OS_WIN)
typedef HWND PluginWindowHandle;
#elif defined(OS_LINUX)
typedef unsigned long PluginWindowHandle;
#else
// On OS X we don't have windowed plugins.
typedef void* PluginWindowHandle;
#endif

} // namespace gfx

#endif // BASE_GFX_NATIVE_WIDGET_TYPES_H_
19 changes: 19 additions & 0 deletions chrome/common/ipc_message_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ struct ParamTraits<long> {
}
};

// unsigned long is used for serializing X window ids.
template <>
struct ParamTraits<unsigned long> {
typedef unsigned long param_type;
static void Write(Message* m, const param_type& p) {
m->WriteLong(p);
}
static bool Read(const Message* m, void** iter, param_type* r) {
long read_output;
if (!m->ReadLong(iter, &read_output))
return false;
*r = static_cast<unsigned long>(read_output);
return true;
}
static void Log(const param_type& p, std::wstring* l) {
l->append(StringPrintf(L"%ul", p));
}
};

template <>
struct ParamTraits<size_t> {
typedef size_t param_type;
Expand Down
10 changes: 8 additions & 2 deletions chrome/plugin/webplugin_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,21 @@ bool WebPluginProxy::Send(IPC::Message* msg) {
return channel_->Send(msg);
}

void WebPluginProxy::SetWindow(gfx::NativeView window) {
void WebPluginProxy::SetWindow(gfx::PluginWindowHandle window) {
#if defined(OS_WIN)
Send(new PluginHostMsg_SetWindow(route_id_, gfx::IdFromNativeView(window)));
#else
NOTIMPLEMENTED();
#endif
}

void WebPluginProxy::WillDestroyWindow(gfx::NativeView window) {
void WebPluginProxy::WillDestroyWindow(gfx::PluginWindowHandle window) {
#if defined(OS_WIN)
PluginThread::current()->Send(
new PluginProcessHostMsg_PluginWindowDestroyed(
window, ::GetParent(window)));
#else
NOTIMPLEMENTED();
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions chrome/plugin/webplugin_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class WebPluginProxy : public WebPlugin {
~WebPluginProxy();

// WebPlugin overrides
void SetWindow(gfx::NativeView window);
void WillDestroyWindow(gfx::NativeView window);
void SetWindow(gfx::PluginWindowHandle window);
void WillDestroyWindow(gfx::PluginWindowHandle window);
#if defined(OS_WIN)
void SetWindowlessPumpEvent(HANDLE pump_messages_event);
// Returns true on success.
Expand Down
16 changes: 1 addition & 15 deletions webkit/glue/plugins/gtk_plugin_container.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ namespace {
// through GLib's object management.
class GtkPluginContainer : public GtkSocket {
public:
static GtkWidget* CreateNewWidget() {
GtkWidget* container = GTK_WIDGET(g_object_new(GetType(), NULL));
g_signal_connect(GTK_SOCKET(container), "plug-removed",
G_CALLBACK(OnPlugRemoved), NULL);
return container;
}

// Sets the requested size of the widget.
void set_size(int width, int height) {
width_ = width;
Expand All @@ -33,7 +26,6 @@ class GtkPluginContainer : public GtkSocket {
return G_TYPE_CHECK_INSTANCE_CAST(instance, GetType(), GtkPluginContainer);
}

private:
// Create and register our custom container type with GTK.
static GType GetType() {
static GType type = 0; // We only want to register our type once.
Expand Down Expand Up @@ -74,12 +66,6 @@ class GtkPluginContainer : public GtkSocket {
requisition->height = container->height_;
}

static gboolean OnPlugRemoved(GtkSocket* socket) {
// This is called when the other side of the socket goes away.
// We return TRUE to indicate that we don't want to destroy our side.
return TRUE;
}

int width_;
int height_;
DISALLOW_IMPLICIT_CONSTRUCTORS(GtkPluginContainer);
Expand All @@ -89,7 +75,7 @@ class GtkPluginContainer : public GtkSocket {

// Create a new instance of our GTK widget object.
GtkWidget* gtk_plugin_container_new() {
return GtkPluginContainer::CreateNewWidget();
return GTK_WIDGET(g_object_new(GtkPluginContainer::GetType(), NULL));
}

void gtk_plugin_container_set_size(GtkWidget *widget, int width, int height) {
Expand Down
2 changes: 1 addition & 1 deletion webkit/glue/plugins/plugin_host.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ NPError NPN_GetValue(NPP id, NPNVariable variable, void *value) {
{
#if defined(OS_WIN) || defined(OS_LINUX)
scoped_refptr<NPAPI::PluginInstance> plugin = FindInstance(id);
gfx::NativeView handle = plugin->window_handle();
gfx::PluginWindowHandle handle = plugin->window_handle();
*((void**)value) = (void*)handle;
rv = NPERR_NO_ERROR;
#else
Expand Down
8 changes: 5 additions & 3 deletions webkit/glue/plugins/plugin_instance.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ class PluginInstance : public base::RefCountedThreadSafe<PluginInstance> {
NPP npp() { return npp_; }

// Get/Set for the instance's window handle.
gfx::NativeView window_handle() { return window_handle_; }
void set_window_handle(gfx::NativeView value) { window_handle_ = value; }
gfx::PluginWindowHandle window_handle() const { return window_handle_; }
void set_window_handle(gfx::PluginWindowHandle value) {
window_handle_ = value;
}

// Get/Set whether this instance is in Windowless mode.
// Default is false.
Expand Down Expand Up @@ -223,7 +225,7 @@ class PluginInstance : public base::RefCountedThreadSafe<PluginInstance> {
scoped_refptr<PluginHost> host_;
NPPluginFuncs* npp_functions_;
std::vector<scoped_refptr<PluginStream> > open_streams_;
gfx::NativeView window_handle_;
gfx::PluginWindowHandle window_handle_;
bool windowless_;
bool transparent_;
WebPlugin* webplugin_;
Expand Down
4 changes: 2 additions & 2 deletions webkit/glue/plugins/webplugin_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ base::LazyInstance<iat_patch::IATPatchFunction> g_iat_patch_set_cursor(
WebPluginDelegate* WebPluginDelegate::Create(
const FilePath& filename,
const std::string& mime_type,
gfx::NativeView containing_view) {
gfx::PluginWindowHandle containing_view) {
scoped_refptr<NPAPI::PluginLib> plugin =
NPAPI::PluginLib::CreatePluginLib(filename);
if (plugin.get() == NULL)
Expand Down Expand Up @@ -140,7 +140,7 @@ LRESULT CALLBACK WebPluginDelegateImpl::HandleEventMessageFilterHook(
}

WebPluginDelegateImpl::WebPluginDelegateImpl(
gfx::NativeView containing_view,
gfx::PluginWindowHandle containing_view,
NPAPI::PluginInstance *instance)
: parent_(containing_view),
instance_(instance),
Expand Down
6 changes: 3 additions & 3 deletions webkit/glue/plugins/webplugin_delegate_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class WebPluginDelegateImpl : public WebPluginDelegate {
private:
friend class DeleteTask<WebPluginDelegateImpl>;

WebPluginDelegateImpl(gfx::NativeView containing_view,
WebPluginDelegateImpl(gfx::PluginWindowHandle containing_view,
NPAPI::PluginInstance *instance);
~WebPluginDelegateImpl();

Expand Down Expand Up @@ -154,7 +154,7 @@ class WebPluginDelegateImpl : public WebPluginDelegate {
void DestroyInstance();

// used for windowed plugins
gfx::NativeView windowed_handle_;
gfx::PluginWindowHandle windowed_handle_;
bool windowed_did_set_window_;
#if defined(OS_WIN)
gfx::Rect windowed_last_pos_;
Expand Down Expand Up @@ -190,7 +190,7 @@ class WebPluginDelegateImpl : public WebPluginDelegate {
void EnsurePixmapAtLeastSize(int width, int height);
#endif

gfx::NativeView parent_;
gfx::PluginWindowHandle parent_;
NPWindow window_;
#if defined(OS_MACOSX)
NP_CGContext cg_context_;
Expand Down
55 changes: 16 additions & 39 deletions webkit/glue/plugins/webplugin_delegate_impl_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ using WebKit::WebMouseEvent;
WebPluginDelegate* WebPluginDelegate::Create(
const FilePath& filename,
const std::string& mime_type,
gfx::NativeView containing_view) {
gfx::PluginWindowHandle containing_view) {
scoped_refptr<NPAPI::PluginLib> plugin =
NPAPI::PluginLib::CreatePluginLib(filename);
if (plugin.get() == NULL)
Expand All @@ -54,7 +54,7 @@ WebPluginDelegate* WebPluginDelegate::Create(
}

WebPluginDelegateImpl::WebPluginDelegateImpl(
gfx::NativeView containing_view,
gfx::PluginWindowHandle containing_view,
NPAPI::PluginInstance *instance)
:
windowed_handle_(0),
Expand Down Expand Up @@ -263,63 +263,40 @@ bool WebPluginDelegateImpl::WindowedCreatePlugin() {
return false;
}

windowed_handle_ = gtk_plugin_container_new();
gtk_container_add(GTK_CONTAINER(parent_), windowed_handle_);
gtk_widget_show(windowed_handle_);
gtk_widget_realize(windowed_handle_);

window_.window = GINT_TO_POINTER(
gtk_socket_get_id(GTK_SOCKET(windowed_handle_)));
window_.window = reinterpret_cast<void*>(parent_);
// The remainder of the code expects windowed_handle_ to exist for
// windowed mode, despite not actually ever reaching through
// windowed_handle_. So let's set it to the one window handle we
// actually have available.
windowed_handle_ = parent_;

if (!window_.ws_info)
window_.ws_info = new NPSetWindowCallbackStruct;
NPSetWindowCallbackStruct* extra =
static_cast<NPSetWindowCallbackStruct*>(window_.ws_info);
extra->display = GDK_WINDOW_XDISPLAY(windowed_handle_->window);
GdkVisual* visual = gdk_drawable_get_visual(windowed_handle_->window);
extra->visual = GDK_VISUAL_XVISUAL(visual);
extra->depth = visual->depth;
extra->colormap = GDK_COLORMAP_XCOLORMAP(gdk_drawable_get_colormap(windowed_handle_->window));
extra->display = GDK_DISPLAY();
extra->visual = DefaultVisual(GDK_DISPLAY(), 0);
extra->depth = DefaultDepth(GDK_DISPLAY(), 0);
extra->colormap = DefaultColormap(GDK_DISPLAY(), 0);

return true;
}

void WebPluginDelegateImpl::WindowedDestroyWindow() {
if (windowed_handle_ != NULL) {
gtk_widget_destroy(windowed_handle_);
windowed_handle_ = NULL;
}
// We have no window to destroy; see comment in WindowedCreatePlugin
// where windowed_handle_ is set.
windowed_handle_ = 0;
}

bool WebPluginDelegateImpl::WindowedReposition(
const gfx::Rect& window_rect,
const gfx::Rect& clip_rect) {
if (!windowed_handle_) {
NOTREACHED();
return false;
}

if (window_rect_ == window_rect && clip_rect_ == clip_rect)
if (window_rect == window_rect_ && clip_rect == clip_rect_)
return false;

// We only set the plugin's size here. Its position is moved elsewhere, which
// allows the window moves/scrolling/clipping to be synchronized with the page
// and other windows.
if (window_rect.size() != window_rect_.size()) {
gtk_plugin_container_set_size(windowed_handle_, window_rect.width(),
window_rect.height());
GtkAllocation allocation = { 0, 0,
window_rect.width(), window_rect.height() };
gtk_widget_size_allocate(windowed_handle_, &allocation);
}

window_rect_ = window_rect;
clip_rect_ = clip_rect;

// TODO(deanm): Is this really needed?
// Ensure that the entire window gets repainted.
gtk_widget_queue_draw(windowed_handle_);

return true;
}

Expand Down
8 changes: 5 additions & 3 deletions webkit/glue/webplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ struct NPObject;

// Describes the new location for a plugin window.
struct WebPluginGeometry {
gfx::NativeView window;
// On Windows, this is the plugin window in the plugin process.
// On X11, this is the browser process's hosting window (the GtkSocket).
gfx::PluginWindowHandle window;
gfx::Rect window_rect;
// Clip rect (include) and cutouts (excludes), relative to
// window_rect origin.
Expand Down Expand Up @@ -54,11 +56,11 @@ class WebPlugin {
// windowed (i.e. handle is not NULL) or windowless (handle is NULL). This
// tells the WebPlugin to send mouse/keyboard events to the plugin delegate,
// as well as the information about the HDC for paint operations.
virtual void SetWindow(gfx::NativeView window) = 0;
virtual void SetWindow(gfx::PluginWindowHandle window) = 0;

// Called by the plugin delegate to let it know that the window is being
// destroyed.
virtual void WillDestroyWindow(gfx::NativeView window) = 0;
virtual void WillDestroyWindow(gfx::PluginWindowHandle window) = 0;
#if defined(OS_WIN)
// The pump_messages_event is a event handle which is valid only for
// windowless plugins and is used in NPP_HandleEvent calls to pump messages
Expand Down
5 changes: 2 additions & 3 deletions webkit/glue/webplugin_delegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
#include <string>

#include "base/gfx/native_widget_types.h"
#include "build/build_config.h"
#include "third_party/npapi/bindings/npapi.h"

// TODO(port): put in OS_WIN check.
typedef struct HDC__* HDC;
struct NPObject;

class FilePath;
Expand Down Expand Up @@ -48,7 +47,7 @@ class WebPluginDelegate {

static WebPluginDelegate* Create(const FilePath& filename,
const std::string& mime_type,
gfx::NativeView containing_view);
gfx::PluginWindowHandle containing_view);

// Initializes the plugin implementation with the given (UTF8) arguments.
// Note that the lifetime of WebPlugin must be longer than this delegate.
Expand Down
2 changes: 1 addition & 1 deletion webkit/glue/webplugin_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ WebPluginImpl::WebPluginImpl(WebCore::HTMLPlugInElement* element,
WebPluginImpl::~WebPluginImpl() {
}

void WebPluginImpl::SetWindow(gfx::NativeView window) {
void WebPluginImpl::SetWindow(gfx::PluginWindowHandle window) {
if (window) {
DCHECK(!windowless_); // Make sure not called twice.
window_ = window;
Expand Down
6 changes: 3 additions & 3 deletions webkit/glue/webplugin_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ class WebPluginImpl : public WebPlugin,
int arg_count, char** arg_names, char** arg_values);

// WebPlugin implementation:
void SetWindow(gfx::NativeView window);
void WillDestroyWindow(gfx::NativeView window) { }
void SetWindow(gfx::PluginWindowHandle window);
void WillDestroyWindow(gfx::PluginWindowHandle window) { }
#if defined(OS_WIN)
void SetWindowlessPumpEvent(HANDLE pump_messages_event) { }
#endif
Expand Down Expand Up @@ -333,7 +333,7 @@ class WebPluginImpl : public WebPlugin,
std::vector<ClientInfo> clients_;

bool windowless_;
gfx::NativeView window_;
gfx::PluginWindowHandle window_;
WebCore::HTMLPlugInElement* element_;
WebFrameImpl* webframe_;

Expand Down
Loading

0 comments on commit 3b33faa

Please sign in to comment.