| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 CHROME_PLUGIN_PLUGIN_CHANNEL_BASE_H_ | |
| 6 #define CHROME_PLUGIN_PLUGIN_CHANNEL_BASE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/hash_tables.h" | |
| 13 #include "base/message_loop.h" | |
| 14 #include "base/ref_counted.h" | |
| 15 #include "base/scoped_ptr.h" | |
| 16 #include "chrome/plugin/npobject_base.h" | |
| 17 #include "content/common/message_router.h" | |
| 18 #include "ipc/ipc_channel_handle.h" | |
| 19 #include "ipc/ipc_sync_channel.h" | |
| 20 #include "ui/gfx/native_widget_types.h" | |
| 21 | |
| 22 // Encapsulates an IPC channel between a renderer and a plugin process. | |
| 23 class PluginChannelBase : public IPC::Channel::Listener, | |
| 24 public IPC::Message::Sender, | |
| 25 public base::RefCountedThreadSafe<PluginChannelBase> { | |
| 26 public: | |
| 27 | |
| 28 // WebPlugin[Delegate] call these on construction and destruction to setup | |
| 29 // the routing and manage lifetime of this object. This is also called by | |
| 30 // NPObjectProxy and NPObjectStub. However the latter don't control the | |
| 31 // lifetime of this object (by passing true for npobject) because we don't | |
| 32 // want a leak of an NPObject in a plugin to keep the channel around longer | |
| 33 // than necessary. | |
| 34 void AddRoute(int route_id, IPC::Channel::Listener* listener, | |
| 35 NPObjectBase* npobject); | |
| 36 void RemoveRoute(int route_id); | |
| 37 | |
| 38 // IPC::Message::Sender implementation: | |
| 39 virtual bool Send(IPC::Message* msg); | |
| 40 | |
| 41 int peer_pid() { return peer_pid_; } | |
| 42 IPC::ChannelHandle channel_handle() const { return channel_handle_; } | |
| 43 | |
| 44 // Returns the number of open plugin channels in this process. | |
| 45 static int Count(); | |
| 46 | |
| 47 // Returns a new route id. | |
| 48 virtual int GenerateRouteID() = 0; | |
| 49 | |
| 50 // Returns whether the channel is valid or not. A channel is invalid | |
| 51 // if it is disconnected due to a channel error. | |
| 52 bool channel_valid() { | |
| 53 return channel_valid_; | |
| 54 } | |
| 55 | |
| 56 // Returns the most recent PluginChannelBase to have received a message | |
| 57 // in this process. | |
| 58 static PluginChannelBase* GetCurrentChannel(); | |
| 59 | |
| 60 static void CleanupChannels(); | |
| 61 | |
| 62 // Returns the NPObjectBase object for the route id passed in. | |
| 63 // Returns NULL on failure. | |
| 64 NPObjectBase* GetNPObjectListenerForRoute(int route_id); | |
| 65 | |
| 66 protected: | |
| 67 typedef PluginChannelBase* (*PluginChannelFactory)(); | |
| 68 | |
| 69 friend class base::RefCountedThreadSafe<PluginChannelBase>; | |
| 70 | |
| 71 virtual ~PluginChannelBase(); | |
| 72 | |
| 73 // Returns a PluginChannelBase derived object for the given channel name. | |
| 74 // If an existing channel exists returns that object, otherwise creates a | |
| 75 // new one. Even though on creation the object is refcounted, each caller | |
| 76 // must still ref count the returned value. When there are no more routes | |
| 77 // on the channel and its ref count is 0, the object deletes itself. | |
| 78 static PluginChannelBase* GetChannel( | |
| 79 const IPC::ChannelHandle& channel_handle, IPC::Channel::Mode mode, | |
| 80 PluginChannelFactory factory, MessageLoop* ipc_message_loop, | |
| 81 bool create_pipe_now); | |
| 82 | |
| 83 // Sends a message to all instances. | |
| 84 static void Broadcast(IPC::Message* message); | |
| 85 | |
| 86 // Called on the worker thread | |
| 87 PluginChannelBase(); | |
| 88 | |
| 89 virtual void CleanUp() { } | |
| 90 | |
| 91 // Implemented by derived classes to handle control messages | |
| 92 virtual bool OnControlMessageReceived(const IPC::Message& msg); | |
| 93 | |
| 94 // IPC::Channel::Listener implementation: | |
| 95 virtual bool OnMessageReceived(const IPC::Message& msg); | |
| 96 virtual void OnChannelConnected(int32 peer_pid); | |
| 97 virtual void OnChannelError(); | |
| 98 | |
| 99 void set_send_unblocking_only_during_unblock_dispatch() { | |
| 100 send_unblocking_only_during_unblock_dispatch_ = true; | |
| 101 } | |
| 102 | |
| 103 virtual bool Init(MessageLoop* ipc_message_loop, bool create_pipe_now); | |
| 104 | |
| 105 scoped_ptr<IPC::SyncChannel> channel_; | |
| 106 | |
| 107 private: | |
| 108 IPC::Channel::Mode mode_; | |
| 109 IPC::ChannelHandle channel_handle_; | |
| 110 int plugin_count_; | |
| 111 int peer_pid_; | |
| 112 | |
| 113 // true when in the middle of a RemoveRoute call | |
| 114 bool in_remove_route_; | |
| 115 | |
| 116 // Keep track of all the registered NPObjects proxies/stubs so that when the | |
| 117 // channel is closed we can inform them. | |
| 118 typedef base::hash_map<int, NPObjectBase*> ListenerMap; | |
| 119 ListenerMap npobject_listeners_; | |
| 120 | |
| 121 // Used to implement message routing functionality to WebPlugin[Delegate] | |
| 122 // objects | |
| 123 MessageRouter router_; | |
| 124 | |
| 125 // A channel is invalid if it is disconnected as a result of a channel | |
| 126 // error. This flag is used to indicate the same. | |
| 127 bool channel_valid_; | |
| 128 | |
| 129 // Track whether we're dispatching a message with the unblock flag; works like | |
| 130 // a refcount, 0 when we're not. | |
| 131 int in_unblock_dispatch_; | |
| 132 | |
| 133 // If true, sync messages will only be marked as unblocking if the channel is | |
| 134 // in the middle of dispatching an unblocking message. | |
| 135 // The plugin process wants to avoid setting the unblock flag on its sync | |
| 136 // messages unless necessary, since it can potentially introduce reentrancy | |
| 137 // into WebKit in ways that it doesn't expect (i.e. causing layout during | |
| 138 // paint). However to avoid deadlock, we must ensure that any message that's | |
| 139 // sent as a result of a sync call from the renderer must unblock the | |
| 140 // renderer. We additionally have to do this for async messages from the | |
| 141 // renderer that have the unblock flag set, since they could be followed by a | |
| 142 // sync message that won't get dispatched until the call to the renderer is | |
| 143 // complete. | |
| 144 bool send_unblocking_only_during_unblock_dispatch_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(PluginChannelBase); | |
| 147 }; | |
| 148 | |
| 149 #endif // CHROME_PLUGIN_PLUGIN_CHANNEL_BASE_H_ | |
| OLD | NEW |