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